The brain: a long-running Claude Code session, confined with bwrap to its workspace, that hears the box through a channel and works the TV with the box's ir and look capabilities. - brain/channel.js: MCP on stdio to the brain (reply, say, ir_listen, ir_send, look, report_problem), and the ask capability, behind a gate, to the box - brain/ask.js: the conversations between them; report.js: an issue on one repository, by a token that can do nothing else - brain/brain.service: the session in tmux, in bwrap: a read-only system, an empty home but for its workspace, keys, and claude - brain/instructions.md: how to work the TV; it imports household.md, who's who (an example, with no real people) - brain/Makefile: make install, attach, status - checks: brain-js (unit tests), brain (the package, put together) Generated-by: Claude Opus 5.5
99 lines
4.3 KiB
JavaScript
99 lines
4.3 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_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('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();
|
|
});
|