const test = require('node:test'); const assert = require('node:assert/strict'); const filaments = require('../lib/filaments'); const spools = (...materials) => materials.map((m, i) => ({ id: i + 1, material: m, remaining_g: 500 })); test('a model is printable when every filament it names is on a spool', () => { const project = [{ type: 'PLA', colour: '#00AE42' }, { type: 'PETG', colour: '#000000' }]; assert.equal(filaments.isPrintable(project, spools('PLA', 'PETG', 'ABS')), true); assert.deepEqual(filaments.missingMaterials(project, spools('PLA', 'PETG')), []); }); test('what is missing is named, not just refused', () => { const project = [{ type: 'PLA' }, { type: 'PETG' }, { type: 'TPU' }]; assert.deepEqual(filaments.missingMaterials(project, spools('PLA')), ['PETG', 'TPU']); assert.equal(filaments.isPrintable(project, spools('PLA')), false); }); test('colour is ignored — a different colour still prints', () => { const project = [{ type: 'PLA', colour: '#FF0000' }]; assert.equal(filaments.isPrintable(project, [{ material: 'PLA', colour_hex: '#0000FF', remaining_g: 200 }]), true); }); test('case and spacing don’t matter, but a different material does', () => { assert.equal(filaments.normalise(' pla basic '), 'PLA BASIC'); // PLA-CF is carbon filled: a plain PLA spool is not a substitute. assert.deepEqual(filaments.missingMaterials([{ type: 'PLA-CF' }], spools('PLA')), ['PLA-CF']); assert.deepEqual(filaments.missingMaterials([{ type: 'pla' }], spools('PLA')), []); }); test('an empty spool doesn’t count, but an unweighed one does', () => { const project = [{ type: 'PLA' }]; assert.deepEqual(filaments.missingMaterials(project, [{ material: 'PLA', remaining_g: 0 }]), ['PLA']); assert.deepEqual(filaments.missingMaterials(project, [{ material: 'PLA', remaining_g: null }]), []); assert.deepEqual(filaments.missingMaterials(project, [{ material: 'PLA' }]), []); }); test('a model we know nothing about is not claimed to be printable', () => { assert.equal(filaments.isPrintable(null, spools('PLA')), false); assert.equal(filaments.isPrintable([], spools('PLA')), false); }); test('the same material twice is asked for once', () => { const project = [{ type: 'PLA', colour: '#FFF' }, { type: 'PLA', colour: '#000' }]; assert.deepEqual(filaments.missingMaterials(project, []), ['PLA']); });