feat(companion): add context warning stream helpers

This commit is contained in:
William Valentin
2026-02-16 19:12:02 -08:00
parent 43968f830a
commit 636f24016f
4 changed files with 65 additions and 1 deletions
+37
View File
@@ -224,6 +224,24 @@ describe('CompanionRuntimeClient', () => {
expect(typingHandler).toHaveBeenCalledWith({ active: true });
});
it('supports subscribeContextWarning helper', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const warningHandler = vi.fn();
client.subscribeContextWarning(warningHandler);
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 56,
event: 'context_warning',
data: { thresholdPct: 80, estimatedPct: 92 },
}),
);
expect(warningHandler).toHaveBeenCalledWith({ thresholdPct: 80, estimatedPct: 92 });
});
it('clears all event subscriptions', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
@@ -355,6 +373,25 @@ describe('CompanionRuntimeClient', () => {
await expect(awaited).resolves.toEqual({ active: true });
});
it('waitForContextWarning resolves on context_warning events', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
const awaited = client.waitForContextWarning<{ thresholdPct: number; estimatedPct: number }>({
timeoutMs: 2000,
});
(client as unknown as { handleMessage: (raw: string) => void }).handleMessage(
JSON.stringify({
id: 57,
event: 'context_warning',
data: { thresholdPct: 75, estimatedPct: 88 },
}),
);
await expect(awaited).resolves.toEqual({ thresholdPct: 75, estimatedPct: 88 });
});
it('connects and performs node registration + capability discovery', async () => {
if (!LISTEN_ALLOWED) {
return;