Open History to see the last commit
1import { test, expect } from './fixtures'
2import type { APIRequestContext } from '@playwright/test'
3
4interface Started {
5 error?: string
6 runId: string
7 receipt?: { status: string }
8}
9
10interface Read {
11 error?: string
12 answered: boolean
13 transcript: Array<{ role: string; text: string }>
14 hostACalls: number
15 workerCalls: number
16}
17
18/** Start a run of `scenario`, and return its id. */
19async function start(
20 request: APIRequestContext,
21 scenario: 'submit' | 'schedule',
22) {
23 const res = await request.post('/api/harness-worker', { data: { scenario } })
24 expect(res.ok()).toBe(true)
25 const started: Started = await res.json()
26 expect(started.error ?? null).toBeNull()
27 return started
28}
29
30/** Read the thread of `runId` from host A until it has an answer. */
31async function readAnswer(request: APIRequestContext, runId: string) {
32 let read: Read | undefined
33 await expect
34 .poll(
35 async () => {
36 const res = await request.get(`/api/harness-worker?runId=${runId}`)
37 read = await res.json()
38 return read?.error ?? read?.answered
39 },
40 { timeout: 20_000 },
41 )
42 .toBe(true)
43 return read!
44}
45
46test.describe('harness: a worker runs the threads that no host runs', () => {
47 test('runs an input that another host submitted', async ({ request }) => {
48 const started = await start(request, 'submit')
49 // Host A stores the input and runs nothing.
50 expect(started.receipt?.status).toBe('queued')
51
52 const read = await readAnswer(request, started.runId)
53 expect(read.transcript).toEqual([
54 { role: 'user', text: '[harness-worker] submitted' },
55 { role: 'assistant', text: 'Done by the worker.' },
56 ])
57 expect(read.workerCalls).toBe(1)
58 expect(read.hostACalls).toBe(0)
59 })
60
61 test('runs a schedule at its time, after the host that made it stopped', async ({
62 request,
63 }) => {
64 const started = await start(request, 'schedule')
65
66 const read = await readAnswer(request, started.runId)
67 expect(read.transcript).toEqual([
68 { role: 'user', text: '[harness-worker] scheduled' },
69 { role: 'assistant', text: 'Done by the worker.' },
70 ])
71 expect(read.workerCalls).toBe(1)
72 expect(read.hostACalls).toBe(0)
73 })
74})