// Tests for the ir capability, with a fake Pico: no serial port. import { test } from 'node:test'; import assert from 'node:assert/strict'; import { Ir, parseHeard } from './ir.js'; test('listen sorts decoded codes from unparsed bursts', async () => { const calls = []; const ir = new Ir(async (args) => { calls.push(args); return 'ir-pico ready\ngot 20 DF 10 EF\r\nunparsed: 3 12 pulses\ngot 20 DF 40 BF\n'; }); assert.deepEqual(await ir.listen(10), { codes: ['20 DF 10 EF', '20 DF 40 BF'], unparsed: ['unparsed: 3 12 pulses'], }); assert.deepEqual(calls, [['listen', '10']]); }); test('listen takes a whole number of seconds, within reason', () => { const ir = new Ir(async () => ''); for (const bad of [0, 61, 1.5, '10', undefined]) assert.throws(() => ir.listen(bad), /seconds/); }); test('send normalizes the code and wants the Pico to confirm each press', async () => { const calls = []; const ir = new Ir(async (args) => { calls.push(args); const [, times, , ...octets] = args; return octets.includes('EF') ? `sent ${octets.join(' ')}\n`.repeat(Number(times)) : 'bad code\n'; }); assert.equal(await ir.send(' 20 df 10 ef '), 'sent 20 DF 10 EF'); assert.deepEqual(calls, [['repeat', '1', '400', '20', 'DF', '10', 'EF']]); await assert.rejects(ir.send('20 DF 10 EE'), /confirmed 0 of 1/); }); test('send presses a button several times, quickly enough for a menu', async () => { const calls = []; const ir = new Ir(async (args) => { calls.push(args); return 'sent 20 DF F4 0B\n'.repeat(args[1] === '5' ? 3 : Number(args[1])); }); assert.equal(await ir.send('20 DF F4 0B', 3, 300), 'sent 20 DF F4 0B 3 times, 300 ms apart'); assert.deepEqual(calls, [['repeat', '3', '300', '20', 'DF', 'F4', '0B']]); await assert.rejects(ir.send('20 DF F4 0B', 5), /confirmed 3 of 5/); for (const bad of [0, 21, 1.5, '3']) assert.throws(() => ir.send('20 DF F4 0B', bad), /times/); for (const bad of [50, 2001]) assert.throws(() => ir.send('20 DF F4 0B', 2, bad), /gapMs/); }); test('Samsung codes carry their name, there and back', async () => { const calls = []; const ir = new Ir(async (args) => { calls.push(args); return args[0] === 'listen' ? 'got samsung E0 E0 67 98\n' : `sent ${args.slice(3).join(' ')}\n`; }); assert.deepEqual(await ir.listen(5), { codes: ['samsung E0 E0 67 98'], unparsed: [] }); assert.equal(await ir.send('Samsung e0 e0 67 98'), 'sent samsung E0 E0 67 98'); assert.deepEqual(calls[1], ['repeat', '1', '400', 'samsung', 'E0', 'E0', '67', '98']); for (const bad of ['sony 01 02', 'samsung', '20 samsung DF']) assert.throws(() => ir.send(bad), /hex bytes/); }); test('other remotes come raw: a held button counts once, and goes back as heard', async () => { const sony = 'raw 2400 600 1200 600 600 600 1200'; const calls = []; const ir = new Ir(async (args) => { calls.push(args); return args[0] === 'listen' ? `got ${sony}\ngot raw 2380 610 1210 590 600 600 1190\ngot raw 2400 600 600 600 600 600 1200\nunparsed: Too short 3 pulses\n` : `sent ${args.slice(3).join(' ')}\n`; }); assert.deepEqual(await ir.listen(5), { codes: [sony, 'raw 2400 600 600 600 600 600 1200'], unparsed: ['unparsed: Too short 3 pulses'], }); assert.equal(await ir.send(`RAW ${sony.slice(4)}`), `sent ${sony}`); assert.deepEqual(calls[1], ['repeat', '1', '400', ...sony.split(' ')]); const long = `raw ${Array(201).fill('600').join(' ')}`; for (const bad of ['raw', 'raw 2400 600', 'raw 70000', 'raw 0600', 'raw 2400 6e2 600', long]) { assert.throws(() => ir.send(bad), /microseconds/); } }); test('send refuses what is not hex bytes', () => { const ir = new Ir(async () => ''); for (const bad of ['', 'power', '2 DF', '20DF10EF', '20; rm', 32]) assert.throws(() => ir.send(bad), /hex bytes/); }); test('one at a time: a send waits for a listen to finish', async () => { const log = []; const ir = new Ir(async ([verb, ...rest]) => { log.push(`start ${verb}`); await new Promise((r) => setTimeout(r, 10)); log.push(`end ${verb}`); return verb === 'repeat' ? `sent ${rest.slice(2).join(' ')}` : ''; }); await Promise.all([ir.listen(1), ir.send('20 DF 10 EF')]); assert.deepEqual(log, ['start listen', 'end listen', 'start repeat', 'end repeat']); }); test('parseHeard of nothing', () => { assert.deepEqual(parseHeard(''), { codes: [], unparsed: [] }); });