Settings → Printer takes the printer's address, serial number and access code (the code stored as a secret, like the other tokens). "Save & test connection" connects to the printer's own MQTT broker on port 8883, subscribes to its reports, asks once for a full report, and listens for a few seconds — then says what it heard: its state, and the filament in each AMS slot or on the external spool holder, with how much is left where the printer knows. Nothing sent to the printer changes what it is doing; the only thing published is the request for a full report. If the printer is in cloud mode and treats even that as a write, the test says it received nothing rather than failing silently. The access code never leaves the server. Adds mqtt as a runtime dependency — server.js now loads it at startup, so the lockfile has to travel with this change. Not verified against a real printer yet: the printer's ports answer on the network, but no connection has been made with its credentials. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
43 lines
2.1 KiB
JavaScript
43 lines
2.1 KiB
JavaScript
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.');
|
||
});
|