import { test } from 'node:test'; import assert from 'node:assert/strict'; import { encode, decode } from '../code.js'; test('decode(encode(x)) round-trips a full payload', () => { const payload = { v: 1, port: 9110, token: 'deadbeefcafe1234', label: 'prod-3', fp: 'AB:CD:EF:01:23:45:67:89', }; const code = encode(payload); assert.ok(code.startsWith('mhu1_')); assert.deepEqual(decode(code), payload); }); test('decode(encode(x)) round-trips a payload without fp (ws://)', () => { const payload = { v: 1, port: 9110, token: 'token-abc', label: 'localhost' }; const code = encode(payload); assert.deepEqual(decode(code), payload); }); test('decode rejects a string without the mhu1_ prefix', () => { assert.throws(() => decode('hello-world'), /missing "mhu1_" prefix/); }); test('decode rejects a wrong-version payload', () => { const bad = 'mhu1_' + Buffer.from( JSON.stringify({ v: 2, port: 9110, token: 'x', label: 'l' }), 'utf8', ).toString('base64url'); assert.throws(() => decode(bad), /unsupported version/); }); test('decode rejects garbage after the prefix', () => { assert.throws(() => decode('mhu1_!!!not-base64-or-json!!!'), /Invalid code/); }); test('decode rejects an empty payload', () => { assert.throws(() => decode('mhu1_'), /empty payload/); }); test('decode rejects a non-string input', () => { assert.throws(() => decode(null), /expected a string/); }); test('decode rejects a missing token', () => { const bad = 'mhu1_' + Buffer.from( JSON.stringify({ v: 1, port: 9110, label: 'l' }), 'utf8', ).toString('base64url'); assert.throws(() => decode(bad), /token/); }); test('decode rejects a non-number port', () => { const bad = 'mhu1_' + Buffer.from( JSON.stringify({ v: 1, port: 'nope', token: 'x', label: 'l' }), 'utf8', ).toString('base64url'); assert.throws(() => decode(bad), /port/); });