check / check (push) Waiting to run
whisper hears people better without the show behind them. The brain sets a mute code (and an unmute code, if not the same) with mute_while_listening on its ir capability, or turns it off; ir-server keeps the setting in its state directory. Mute is often a toggle, and the camera can't reliably tell whether the sound is off, so the box senses nothing: the setting is the brain's to turn on or off. The button service holds a connection to ir-server's conversation socket for each conversation: ir-server mutes when it opens and unmutes, with the same codes, when it closes. The socket's group is the button's alone, and the button still can't send any code. Generated-by: Claude Opus 5.5
137 lines
6.1 KiB
JavaScript
137 lines
6.1 KiB
JavaScript
// 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, Muting, 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: [] });
|
|
});
|
|
|
|
test('muting while listening: off until the brain sets it; a toggle sends one code twice', async () => {
|
|
const saves = [];
|
|
const sent = [];
|
|
const send = async (code) => { sent.push(code); return `sent ${code}`; };
|
|
const muting = new Muting(undefined, async (codes) => { saves.push(codes); });
|
|
const ir = new Ir(async () => '', muting);
|
|
assert.equal(await (await muting.start(send))(), 'muting is off');
|
|
assert.deepEqual(sent, []);
|
|
|
|
assert.match(await ir.muteWhileListening('raw 2390 620 590'), /again when it ends/);
|
|
assert.deepEqual(saves, [{ mute: 'raw 2390 620 590', unmute: 'raw 2390 620 590' }]);
|
|
const unmute = await muting.start(send);
|
|
assert.deepEqual(sent, ['raw 2390 620 590']);
|
|
await unmute();
|
|
assert.deepEqual(sent, ['raw 2390 620 590', 'raw 2390 620 590']);
|
|
});
|
|
|
|
test('muting with separate codes unmutes with the codes it muted with, even if the setting changes meanwhile', async () => {
|
|
const sent = [];
|
|
const send = async (code) => { sent.push(code); return `sent ${code}`; };
|
|
const muting = new Muting({ mute: 'samsung e0 e0 f0 0f', unmute: '20 DF 90 6F' }, async () => {});
|
|
const unmute = await muting.start(send);
|
|
assert.match(await muting.set(), /^off/);
|
|
await unmute();
|
|
assert.deepEqual(sent, ['samsung E0 E0 F0 0F', '20 DF 90 6F']);
|
|
assert.equal(muting.codes, undefined);
|
|
});
|
|
|
|
test('muting refuses a bad code, keeps the old setting, and a box without it says so', async () => {
|
|
const muting = new Muting({ mute: '20 DF 90 6F' }, async () => {});
|
|
await assert.rejects(async () => muting.set('mute please'), /code:/);
|
|
assert.deepEqual(muting.codes, { mute: '20 DF 90 6F', unmute: '20 DF 90 6F' });
|
|
assert.throws(() => new Ir(async () => '').muteWhileListening('20 DF 90 6F'), /no mute setting/);
|
|
});
|