const test = require('node:test'); const assert = require('node:assert/strict'); const printer = require('../lib/printer'); test('AMS trays are read into material, colour and how much is left', () => { const trays = printer.describeAms([{ id: '0', tray: [ { id: '0', tray_type: 'PLA', tray_color: 'FFFFFFFF', tray_info_idx: 'GFA00', tray_sub_brands: 'PLA Basic', remain: 82 }, { id: '1', tray_type: '', tray_color: '00000000' }, // empty slot { id: '2', tray_type: 'PETG', tray_color: '000000FF', remain: -1 }, // not a Bambu spool ] }]); assert.deepEqual(trays[0], { unit: 0, slot: 0, empty: false, material: 'PLA', colour: '#FFFFFF', profile: 'GFA00', brand: 'PLA Basic', remain: 82, }); assert.equal(trays[1].empty, true); assert.equal(trays[1].material, null); assert.equal(trays[1].colour, null, 'an empty slot has no colour, whatever the printer says'); assert.equal(trays[2].remain, null, '-1 means the printer can’t tell'); assert.equal(trays[2].colour, '#000000', 'the alpha byte is dropped'); }); test('a printer with no AMS reports nothing rather than failing', () => { assert.deepEqual(printer.describeAms(undefined), []); assert.deepEqual(printer.describeAms([{ id: '0' }]), []); }); test('the external spool holder is read the same way as a tray', () => { const ext = printer.describeTray({ id: '254', tray_type: 'PLA', tray_color: 'C12E1FFF', remain: 40 }); assert.equal(ext.unit, null); assert.equal(ext.material, 'PLA'); assert.equal(ext.colour, '#C12E1F'); assert.equal(ext.remain, 40); }); test('connection failures are explained in plain words', () => { assert.equal(printer.friendlyError(new Error('Connection refused: Not authorized')), 'The printer refused the access code.'); assert.equal(printer.friendlyError(new Error('connect ECONNREFUSED 192.0.2.10:8883')), 'Nothing answered on port 8883 at that address.'); assert.equal(printer.friendlyError(new Error('connect EHOSTUNREACH 192.0.2.10:8883')), 'Couldn’t reach the printer from the server.'); assert.equal(printer.friendlyError(null), 'The connection failed.'); });