|
| 1 | +import { runStreamCase, StreamEvent } from "../lib/stream-harness" |
| 2 | + |
| 3 | +const FIRST_PROMPT = `What is 1+1? Reply with only "2".` |
| 4 | +const FOLLOWUP_PROMPT = `Different question now: what is 3+3? Reply with only "6".` |
| 5 | + |
| 6 | +function parseEventContent(text: string | undefined): string { |
| 7 | + return typeof text === "string" ? text : "" |
| 8 | +} |
| 9 | + |
| 10 | +function validateFollowupAnswer(text: string): void { |
| 11 | + const normalized = text.toLowerCase() |
| 12 | + const containsExpected = /\b6\b/.test(normalized) || normalized.includes("six") |
| 13 | + const containsOldAnswer = /\b1\+1\b/.test(normalized) || /\b2\b/.test(normalized) |
| 14 | + const containsQuestionReference = normalized.includes("3+3") |
| 15 | + |
| 16 | + if (!containsExpected) { |
| 17 | + throw new Error(`follow-up result did not answer the follow-up question; result="${text}"`) |
| 18 | + } |
| 19 | + |
| 20 | + if (!containsQuestionReference && containsOldAnswer && !containsExpected) { |
| 21 | + throw new Error(`follow-up result appears anchored to first question; result="${text}"`) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +async function main() { |
| 26 | + const startRequestId = `start-${Date.now()}` |
| 27 | + const followupRequestId = `message-${Date.now()}` |
| 28 | + const shutdownRequestId = `shutdown-${Date.now()}` |
| 29 | + |
| 30 | + let initSeen = false |
| 31 | + let sentFollowup = false |
| 32 | + let sentShutdown = false |
| 33 | + let firstResult = "" |
| 34 | + let followupResult = "" |
| 35 | + |
| 36 | + await runStreamCase({ |
| 37 | + onEvent(event: StreamEvent, context) { |
| 38 | + if (event.type === "system" && event.subtype === "init" && !initSeen) { |
| 39 | + initSeen = true |
| 40 | + context.sendCommand({ |
| 41 | + command: "start", |
| 42 | + requestId: startRequestId, |
| 43 | + prompt: FIRST_PROMPT, |
| 44 | + }) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + if (event.type === "control" && event.subtype === "error") { |
| 49 | + throw new Error( |
| 50 | + `received control error for requestId=${event.requestId ?? "unknown"} command=${event.command ?? "unknown"} code=${event.code ?? "unknown"} content=${event.content ?? ""}`, |
| 51 | + ) |
| 52 | + } |
| 53 | + |
| 54 | + if (event.type !== "result" || event.done !== true) { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if (event.requestId === startRequestId) { |
| 59 | + firstResult = parseEventContent(event.content) |
| 60 | + if (!/\b2\b/.test(firstResult)) { |
| 61 | + throw new Error(`first result did not answer first prompt; result="${firstResult}"`) |
| 62 | + } |
| 63 | + |
| 64 | + if (!sentFollowup) { |
| 65 | + context.sendCommand({ |
| 66 | + command: "message", |
| 67 | + requestId: followupRequestId, |
| 68 | + prompt: FOLLOWUP_PROMPT, |
| 69 | + }) |
| 70 | + sentFollowup = true |
| 71 | + } |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + if (event.requestId !== followupRequestId) { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + followupResult = parseEventContent(event.content) |
| 80 | + validateFollowupAnswer(followupResult) |
| 81 | + console.log(`[PASS] first result="${firstResult}"`) |
| 82 | + console.log(`[PASS] follow-up result="${followupResult}"`) |
| 83 | + |
| 84 | + if (!sentShutdown) { |
| 85 | + context.sendCommand({ |
| 86 | + command: "shutdown", |
| 87 | + requestId: shutdownRequestId, |
| 88 | + }) |
| 89 | + sentShutdown = true |
| 90 | + } |
| 91 | + }, |
| 92 | + onTimeoutMessage() { |
| 93 | + return `timed out waiting for completion (initSeen=${initSeen}, sentFollowup=${sentFollowup}, firstResult=${Boolean(firstResult)}, followupResult=${Boolean(followupResult)})` |
| 94 | + }, |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +main().catch((error) => { |
| 99 | + console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`) |
| 100 | + process.exit(1) |
| 101 | +}) |
0 commit comments