Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

protocol.test.ts

73 lines2.7 KB

1/**
2 * Branch tests for {@link parseContainerRunRequest} — the in-container runner's
3 * only validation gate for the DO→container `POST /run` body. Pure, no Workers
4 * runtime. One happy path + one rejection per guarded field.
5 */
6import { describe, expect, it } from 'vitest'
7import { parseContainerRunRequest } from '../src/protocol'
8​
9/** A minimal well-formed request; tests clone + corrupt one field at a time. */
10function validBody(): Record<string, unknown> {
11 return {
12 runId: 'run-1',
13 threadId: 'thread-1',
14 model: 'sonnet',
15 toolExecUrl: 'https://example.com/tool-exec/run-1',
16 toolExecToken: 'tok',
17 messages: [{ role: 'user', content: 'hi' }],
18 harness: 'claude-code',
19 workspace: { source: { type: 'local', path: '.' } },
20 toolDescriptors: [{ name: 'getTodos' }],
21 }
23​
24describe('parseContainerRunRequest', () => {
25 it('parses a well-formed body', () => {
26 const parsed = parseContainerRunRequest(validBody())
27 expect(parsed.runId).toBe('run-1')
28 expect(parsed.harness).toBe('claude-code')
29 expect(parsed.messages).toHaveLength(1)
30 expect(parsed.toolDescriptors[0]?.name).toBe('getTodos')
31 })
32​
33 it('rejects a non-object body', () => {
34 expect(() => parseContainerRunRequest('nope')).toThrow(/JSON object/)
35 expect(() => parseContainerRunRequest(null)).toThrow(/JSON object/)
36 })
37​
38 it.each(['runId', 'threadId', 'model', 'toolExecUrl', 'toolExecToken'])(
39 'rejects a missing/empty %s',
40 (field) => {
41 const body = validBody()
42 body[field] = ''
43 expect(() => parseContainerRunRequest(body)).toThrow(new RegExp(field))
44 },
45 )
46​
47 it('rejects empty / non-array messages', () => {
48 const empty = { ...validBody(), messages: [] }
49 expect(() => parseContainerRunRequest(empty)).toThrow(/messages/)
50 const notArray = { ...validBody(), messages: 'x' }
51 expect(() => parseContainerRunRequest(notArray)).toThrow(/messages/)
52 })
53​
54 it('rejects a malformed message element', () => {
55 const body = { ...validBody(), messages: [{ role: 'user' }] } // no content
56 expect(() => parseContainerRunRequest(body)).toThrow(/messages/)
57 })
58​
59 it('rejects an unknown harness id', () => {
60 const body = { ...validBody(), harness: 'not-a-harness' }
61 expect(() => parseContainerRunRequest(body)).toThrow(/harness/)
62 })
63​
64 it('rejects a non-WorkspaceDefinition workspace', () => {
65 const body = { ...validBody(), workspace: { nope: true } }
66 expect(() => parseContainerRunRequest(body)).toThrow(/workspace/)
67 })
68​
69 it('rejects non-ToolDescriptor[] toolDescriptors', () => {
70 const body = { ...validBody(), toolDescriptors: [{ noName: true }] }
71 expect(() => parseContainerRunRequest(body)).toThrow(/toolDescriptors/)
72 })
73})