Files
dckc 5191ee9931 feat: the brain can change the box's speaker volume
"Speak up" and "too loud" now work: the reply tool takes louder, in
percentage points, and the button service nudges the speaker's softvol
before it speaks that reply, so the person hears the change at once.
The box keeps the level between 10% and 80% (body/mixer.py).

No new port or key: the volume changes only with a reply, in a
conversation someone is listening to.

Generated-by: Claude Opus 5.5
2026-12-03 16:44:54 -06:00

115 lines
5.2 KiB
JavaScript

// Tests for the ask capability's core: no MCP, no network, no real timers.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { Channel, Asker, MAX_LOUDER, MAX_QUESTION_CHARS } from './ask.js';
const harness = () => {
const sent = [], timers = [];
let n = 0;
const channel = new Channel({
notify: (content, meta) => sent.push({ content, meta }),
setTimeout: (ms, f) => timers.push(f),
newId: () => `c${++n}`,
}, 5000);
return { channel, sent, timers };
};
test('a question reaches the brain, and its reply settles the ask', async () => {
const { channel, sent } = harness();
const answer = new Asker(channel, 'box').ask('What day is it?');
assert.deepEqual(sent, [{ content: 'What day is it?', meta: { chat_id: 'c1', user: 'box' } }]);
assert.equal(channel.reply('c1', "It's Monday."), 'spoken');
assert.deepEqual(await answer, { text: "It's Monday.", end: false });
assert.equal(channel.waiting(), 0);
});
test('a conversation: its id goes to the brain, say speaks meanwhile, and a reply can end it', async () => {
const { channel, sent } = harness();
const asker = new Asker(channel, 'box');
const said = [];
const answer = asker.ask('Switch to the Media PC', 'k7', (t) => { said.push(t); });
assert.deepEqual(sent[0].meta, { chat_id: 'c1', user: 'box', conversation: 'k7' });
assert.match(await channel.say('c1', 'Let me try.'), /still yours/);
assert.deepEqual(said, ['Let me try.']);
assert.match(channel.reply('c1', 'Done. Goodbye!', true), /over/);
assert.deepEqual(await answer, { text: 'Done. Goodbye!', end: true });
assert.match(await channel.say('c1', 'too late'), /no one is waiting/);
});
test('say without a way to speak tells the brain to reply instead', async () => {
const { channel } = harness();
channel.ask('hi', 'box');
assert.match(await channel.say('c1', 'hm'), /use reply/);
});
test('the box ends a conversation; the brain hears it, with no question to answer', () => {
const { channel, sent } = harness();
new Asker(channel, 'box').end('k7');
assert.deepEqual(sent, [{
content: '(the conversation is over; no reply needed)',
meta: { user: 'box', conversation: 'k7', event: 'ended' },
}]);
assert.throws(() => channel.end('k 7; rm', 'box'), /conversation/);
});
test('a reply can turn the volume up or down; out of range, it speaks but leaves the volume', async () => {
const { channel } = harness();
const up = channel.ask('Speak up!', 'box');
assert.match(channel.reply('c1', 'Is this better?', false, 10), /louder by 10/);
assert.deepEqual(await up, { text: 'Is this better?', end: false, louder: 10 });
const down = channel.ask('Too loud.', 'box');
assert.match(channel.reply('c2', 'Sorry.', false, -5), /softer by 5/);
assert.deepEqual(await down, { text: 'Sorry.', end: false, louder: -5 });
const way = channel.ask('Way up!', 'box');
assert.match(channel.reply('c3', 'Okay.', false, MAX_LOUDER + 1), /volume is unchanged/);
assert.deepEqual(await way, { text: 'Okay.', end: false });
const odd = channel.ask('Louder?', 'box');
assert.match(channel.reply('c4', 'Hm.', false, 'max'), /volume is unchanged/);
assert.deepEqual(await odd, { text: 'Hm.', end: false });
});
test('replies go to the right question; a stale chat_id is reported, not thrown', async () => {
const { channel } = harness();
const a = channel.ask('one', 'box');
const b = channel.ask('two', 'box');
channel.reply('c2', 'second');
channel.reply('c1', 'first');
assert.deepEqual((await Promise.all([a, b])).map((x) => x.text), ['first', 'second']);
assert.match(channel.reply('c1', 'again'), /no one is waiting/);
});
test('no reply in time rejects the ask, and a late reply is reported', async () => {
const { channel, timers } = harness();
const answer = channel.ask('hello?', 'box');
timers[0]();
await assert.rejects(answer, /no reply in 5 s/);
assert.match(channel.reply('c1', 'sorry, late'), /no one is waiting/);
});
test('empty, overlong, or odd questions are refused before the brain sees them', () => {
const { channel, sent } = harness();
assert.throws(() => channel.ask(' ', 'box'), /needs text/);
assert.throws(() => channel.ask(7, 'box'), /needs text/);
assert.throws(() => channel.ask('x'.repeat(MAX_QUESTION_CHARS + 1), 'box'), /over/);
assert.throws(() => channel.ask('hi', 'box', { conversation: '' }), /conversation/);
assert.throws(() => channel.ask('hi', 'box', { onSay: 'speak' }), /onSay/);
assert.equal(sent.length, 0);
});
test('over Cap\'n Web: the onSay callback crosses the session, and the brain\'s say reaches the box', async () => {
const { newMessagePortRpcSession } = await import('capnweb');
const { channel } = harness();
const { port1, port2 } = new MessageChannel();
newMessagePortRpcSession(port1, new Asker(channel, 'box'));
const asker = newMessagePortRpcSession(port2);
const said = [];
const answer = asker.ask('Switch inputs', 'k1', (t) => { said.push(t); });
while (channel.waiting() === 0) await new Promise((r) => setTimeout(r, 1));
await channel.say('c1', 'Let me try.');
channel.reply('c1', 'Done.', false);
assert.deepEqual(await answer, { text: 'Done.', end: false });
assert.deepEqual(said, ['Let me try.']);
asker[Symbol.dispose]();
port1.close(); port2.close();
});