Files
piik/tests/support-bot.test.ts
T
AAA 76e5aa9043 fix(runtime): preserve resource ownership through cancellation
Retain native pair cleanup through shutdown and cancel microphone acquisition when its last voice consumer leaves. Start webpage read-response deadlines after transmission and retire failed media setup.

Share text-client shutdown completion, provide a small support-bot example, remove the unused input route and duplicate MF guard, and document safe Cargo cache handoff. Validated 413 focused Web tests, type checks, Windows capture compilation and six native lifecycle regressions.
2026-11-16 07:45:48 -04:00

52 lines
2.4 KiB
TypeScript

import { EventEmitter } from 'node:events';
import { expect, test, vi } from 'vitest';
import { attachSupportBot } from '../prototype/channel-browser/examples/support-bot';
import type { TextParticipant } from '../prototype/channel-browser/node/text-participant';
function fixture() {
const client = Object.assign(new EventEmitter(), { summary: () => ({ userId: 'bot' }),
chat: vi.fn(async () => ({ seq: 2 })), react: vi.fn(async () => ({})) });
const report = vi.fn();
const detach = attachSupportBot(client as unknown as TextParticipant, report);
const send = (text: string, userId = 'friend') => client.emit('live', { seq: 1, text, userId });
return { client, report, detach, send };
}
test('one live help message gets a quoted Markdown answer and one reaction; other events do not replay it', async () => {
const { client, report, detach, send } = fixture();
try {
send('!help', 'bot'); send('hello');
client.emit('confirmed', { seq: 1, text: '!help', userId: 'friend' });
expect(client.chat).not.toHaveBeenCalled();
send('!help');
await vi.waitFor(() => expect(client.react).toHaveBeenCalledExactlyOnceWith(1, 'wave'));
expect(client.chat).toHaveBeenCalledExactlyOnceWith(expect.stringContaining('```text\nVersion:'), { reply: { seq: 1, notify: true } });
expect(report).not.toHaveBeenCalled();
await detach(); send('!help');
expect(client.chat).toHaveBeenCalledTimes(1);
expect(client.listenerCount('live')).toBe(0);
} finally { await detach(); }
});
test('reaction rejection does not resend the confirmed reply', async () => {
const { client, report, detach, send } = fixture();
client.react.mockRejectedValueOnce(new Error('unsupported_reaction'));
try {
send('!help');
await vi.waitFor(() => expect(report).toHaveBeenCalledOnce());
expect(client.chat).toHaveBeenCalledTimes(1);
expect(client.react).toHaveBeenCalledTimes(1);
} finally { await detach(); }
});
test('detaching during a pending reply prevents a subsequent reaction', async () => {
const { client, detach, send } = fixture();
let complete!: (value: { seq: number }) => void;
client.chat.mockImplementationOnce(() => new Promise(resolve => { complete = resolve; }));
send('!help');
const drained = detach();
complete({ seq: 2 }); await drained;
expect(client.react).not.toHaveBeenCalled();
expect(client.listenerCount('live')).toBe(0);
});