Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(chat): cover chat service and wire protocol

Steven Enamakel1951724bab46
diff --git a/app/src/services/__tests__/chatService.test.ts b/app/src/services/__tests__/chatService.test.ts
index 7c8b574520..9561c6c6b7 100644
--- a/app/src/services/__tests__/chatService.test.ts
+++ b/app/src/services/__tests__/chatService.test.ts
@@ -506,3 +506,95 @@ describe('chatService.chatCancel', () => {
});
});
});
+
+describe('chatService typed attachments', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ mockCallCoreRpc.mockResolvedValue(undefined);
+ });
+
+ const image = {
+ kind: 'image' as const,
+ media: { url: 'data:image/png;base64,AAA', contentType: 'image/png', fileName: 'a.png' },
+ };
+
+ it('chatSend forwards attachments as typed parts and leaves the text alone', async () => {
+ bindMockSocket(createMockSocket());
+
+ await chatSend({ threadId: 'thread-1', message: 'look', attachments: [image] });
+
+ const params = mockCallCoreRpc.mock.calls[0][0].params;
+ expect(params.message).toBe('look');
+ expect(params.attachments).toEqual([image]);
+ expect(JSON.stringify(params.message)).not.toMatch(/\[(IMAGE|FILE):/);
+ });
+
+ it('chatSend omits the attachments param when there are none', async () => {
+ bindMockSocket(createMockSocket());
+
+ await chatSend({ threadId: 'thread-1', message: 'hi', attachments: [] });
+
+ expect(mockCallCoreRpc.mock.calls[0][0].params.attachments).toBeUndefined();
+ });
+});
+
+describe('chatService wire decoding', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ // The core publishes the run mode in `message` and an approval's terminal
+ // outcome in `cancel_reason` (the tinychannels WebChannelEvent schema has no
+ // `mode` / `resolution` field), so consumers must get them lifted.
+ it('lifts run_mode_changed.message into mode', () => {
+ const socket = createMockSocket();
+ bindMockSocket(socket);
+ const onRunModeChanged = vi.fn();
+ subscribeChatEvents({ onRunModeChanged });
+
+ socket.emit('run_mode_changed', {
+ event: 'run_mode_changed',
+ client_id: '',
+ thread_id: 't1',
+ request_id: '',
+ message: 'plan',
+ });
+
+ expect(onRunModeChanged).toHaveBeenCalledTimes(1);
+ expect(onRunModeChanged.mock.calls[0][0]).toMatchObject({ thread_id: 't1', mode: 'plan' });
+ });
+
+ it('drops run_mode_changed with an unknown mode', () => {
+ const socket = createMockSocket();
+ bindMockSocket(socket);
+ const onRunModeChanged = vi.fn();
+ subscribeChatEvents({ onRunModeChanged });
+
+ socket.emit('run_mode_changed', { thread_id: 't1', request_id: '', message: 'turbo' });
+
+ expect(onRunModeChanged).not.toHaveBeenCalled();
+ });
+
+ it('lifts approval_decided.cancel_reason into resolution', () => {
+ const socket = createMockSocket();
+ bindMockSocket(socket);
+ const onApprovalDecided = vi.fn();
+ subscribeChatEvents({ onApprovalDecided });
+
+ socket.emit('approval_decided', {
+ event: 'approval_decided',
+ client_id: 'c1',
+ thread_id: 't1',
+ request_id: 'r1',
+ tool_name: 'shell',
+ message: 'deny',
+ cancel_reason: 'expired',
+ });
+
+ expect(onApprovalDecided).toHaveBeenCalledTimes(1);
+ expect(onApprovalDecided.mock.calls[0][0]).toMatchObject({
+ request_id: 'r1',
+ resolution: 'expired',
+ });
+ });
+});
diff --git a/app/src/services/chatWire.test.ts b/app/src/services/chatWire.test.ts
new file mode 100644
index 0000000000..b94e8e9462
--- /dev/null
+++ b/app/src/services/chatWire.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it } from 'vitest';
+
+import {
+ type ChatDoneEvent,
+ decodeApprovalDecided,
+ decodeRunModeChanged,
+ externalTransferProvider,
+ type TurnUsageWire,
+ type WireEvent,
+} from './chatWire';
+import type { WebChannelEvent } from './chatWire.generated';
+
+describe('chatWire', () => {
+ it('decodeRunModeChanged lifts the wire message into a mode', () => {
+ const wire: WireEvent<'run_mode_changed'> = { thread_id: 't', request_id: '', message: 'build' };
+ expect(decodeRunModeChanged(wire)).toEqual({ thread_id: 't', request_id: '', mode: 'build' });
+ });
+
+ it('decodeRunModeChanged rejects a mode the UI does not know', () => {
+ expect(decodeRunModeChanged({ thread_id: 't', request_id: '', message: 'x' })).toBeNull();
+ expect(decodeRunModeChanged({ thread_id: 't', request_id: '' })).toBeNull();
+ });
+
+ it('decodeApprovalDecided keeps the wire fields and adds resolution', () => {
+ const wire: WireEvent<'approval_decided'> = {
+ thread_id: 't',
+ request_id: 'r',
+ message: 'allow',
+ cancel_reason: 'cancelled',
+ };
+ expect(decodeApprovalDecided(wire)).toEqual({ ...wire, resolution: 'cancelled' });
+ expect(decodeApprovalDecided({ thread_id: 't', request_id: 'r' }).resolution).toBeUndefined();
+ });
+
+ it('externalTransferProvider reads the egress descriptor slug from args', () => {
+ expect(
+ externalTransferProvider({
+ thread_id: 't',
+ request_id: 'r',
+ args: { provider_slug: 'gmail', service: 'send' },
+ })
+ ).toBe('gmail');
+ expect(externalTransferProvider({ thread_id: 't', request_id: 'r' })).toBeUndefined();
+ expect(
+ externalTransferProvider({ thread_id: 't', request_id: 'r', args: { provider_slug: 4 } })
+ ).toBeUndefined();
+ });
+
+ it('derived event types accept a schema-shaped chat_done and usage payload', () => {
+ const usage: TurnUsageWire = {
+ input_tokens: 10,
+ output_tokens: 5,
+ cached_input_tokens: 2,
+ context_window: 1000,
+ cost_usd: null,
+ cost_source: 'unknown',
+ };
+ const done: ChatDoneEvent = { thread_id: 't', request_id: 'r', full_response: 'hi', usage };
+ // The generated union is the source of truth: a derived event, tagged,
+ // is assignable to it.
+ const wire: WebChannelEvent = { ...done, event: 'chat_done', client_id: 'c' };
+ expect(wire.event).toBe('chat_done');
+ expect(done.usage?.cost_source).toBe('unknown');
+ });
+});