54 lines
3.3 KiB
JavaScript
54 lines
3.3 KiB
JavaScript
import assert from 'node:assert/strict';
|
||
import test from 'node:test';
|
||
import { readB2Usb } from '../src/b2/usb.mjs';
|
||
function fixture({ model = 231, silence = false, change = false, incomplete = false } = {}) {
|
||
let stream, closed = false, identities = 0; const writes = [];
|
||
const port = {
|
||
getInfo: () => ({ usbVendorId: 0x2e3c, usbProductId: 0x5740 }),
|
||
async open(options) { assert.equal(options.flowControl, 'none'); },
|
||
async close() { closed = true; },
|
||
readable: new ReadableStream({ start(controller) { stream = controller; } }),
|
||
writable: new WritableStream({ write(bytes) {
|
||
const command = new TextDecoder().decode(bytes); writes.push(command);
|
||
if (silence) return;
|
||
let response;
|
||
if (command === '>I 7\r\n') {
|
||
identities++;
|
||
response = `>DI=${model};0;6\r\n>DN=300\r\n>MN=${change && identities > 1 ? '4' : '3'}23456789012345\r\n>SID0=0\r\n>SID1=0\r\n`;
|
||
} else { assert.equal(command, '>D 15;0\r\n'); response = '>SF=synthetic-config\r\n'; }
|
||
if (incomplete) response = response.slice(0, -2);
|
||
for (let i = 0; i < response.length; i += 7) stream.enqueue(new TextEncoder().encode(response.slice(i, i + 7)));
|
||
} }),
|
||
};
|
||
return { serial: { async requestPort(options) { assert.deepEqual(options.filters, [{ usbVendorId: 0x2e3c, usbProductId: 0x5740 }]); return port; } }, writes, closed: () => closed, stream: () => stream };
|
||
}
|
||
test('read-only USB protocol handles fragmented lines and releases port', async () => {
|
||
// Successful reads must not depend on CI scheduling within 50–100 ms.
|
||
// The dedicated timeout test below supplies its own short deadline.
|
||
const f = fixture(); const result = await readB2Usb({ serial: f.serial });
|
||
assert.equal(result.settings, 'synthetic-config'); assert.equal(result.before.MN, result.after.MN);
|
||
assert.deepEqual(f.writes, ['>I 7\r\n', '>D 15;0\r\n', '>I 7\r\n']); assert.ok(f.closed());
|
||
});
|
||
test('wrong model stops before requesting configuration', async () => {
|
||
const f = fixture({ model: 999 });
|
||
await assert.rejects(readB2Usb({ serial: f.serial }), /не распознано/);
|
||
assert.deepEqual(f.writes, ['>I 7\r\n']); assert.ok(f.closed());
|
||
});
|
||
test('identity change cannot produce a registration snapshot', async () => {
|
||
const f = fixture({ change: true }); await assert.rejects(readB2Usb({ serial: f.serial }), /изменилось устройство/); assert.ok(f.closed());
|
||
});
|
||
test('incomplete response and silence time out and close the port', async () => {
|
||
for (const options of [{ silence: true }, { incomplete: true }]) {
|
||
const f = fixture(options); await assert.rejects(readB2Usb({ serial: f.serial, timeoutMs: 15 }), /не ответил/); assert.ok(f.closed());
|
||
}
|
||
});
|
||
test('closing the form aborts a pending read and releases USB', async () => {
|
||
const f = fixture({ silence: true }), controller = new AbortController();
|
||
const promise = readB2Usb({ serial: f.serial, signal: controller.signal });
|
||
setTimeout(() => controller.abort(), 10); await assert.rejects(promise, { name: 'AbortError' }); assert.ok(f.closed());
|
||
});
|
||
test('disconnect does not turn a partial snapshot into success', async () => {
|
||
const f = fixture({ silence: true }); const promise = readB2Usb({ serial: f.serial });
|
||
setTimeout(() => f.stream().close(), 10); await assert.rejects(promise, /USB отключён/); assert.ok(f.closed());
|
||
});
|