|
| 1 | +import { runStreamCase, StreamEvent } from "../lib/stream-harness" |
| 2 | + |
| 3 | +const LONG_PROMPT = |
| 4 | + 'Run exactly this command and do not summarize until it finishes: sleep 20 && echo "done". After it finishes, reply with exactly "done".' |
| 5 | + |
| 6 | +async function main() { |
| 7 | + const startRequestId = `start-${Date.now()}` |
| 8 | + const messageRequestId = `message-${Date.now()}` |
| 9 | + const shutdownRequestId = `shutdown-${Date.now()}` |
| 10 | + const testImage = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB" |
| 11 | + |
| 12 | + let initSeen = false |
| 13 | + let startAccepted = false |
| 14 | + let messageAccepted = false |
| 15 | + let messageQueued = false |
| 16 | + let queueImageCountObserved = false |
| 17 | + let shutdownSent = false |
| 18 | + let shutdownAck = false |
| 19 | + let shutdownDone = false |
| 20 | + |
| 21 | + await runStreamCase({ |
| 22 | + timeoutMs: 180_000, |
| 23 | + onEvent(event: StreamEvent, context) { |
| 24 | + if (event.type === "system" && event.subtype === "init" && !initSeen) { |
| 25 | + initSeen = true |
| 26 | + context.sendCommand({ command: "start", requestId: startRequestId, prompt: LONG_PROMPT }) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + if ( |
| 31 | + event.type === "control" && |
| 32 | + event.subtype === "ack" && |
| 33 | + event.command === "start" && |
| 34 | + event.requestId === startRequestId && |
| 35 | + !startAccepted |
| 36 | + ) { |
| 37 | + startAccepted = true |
| 38 | + |
| 39 | + context.sendCommand({ |
| 40 | + command: "message", |
| 41 | + requestId: messageRequestId, |
| 42 | + prompt: "Respond with exactly IMAGE-QUEUED when this message is processed.", |
| 43 | + images: [testImage], |
| 44 | + }) |
| 45 | + |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + if ( |
| 50 | + event.type === "control" && |
| 51 | + event.subtype === "ack" && |
| 52 | + event.command === "message" && |
| 53 | + event.requestId === messageRequestId |
| 54 | + ) { |
| 55 | + messageAccepted = true |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + if ( |
| 60 | + event.type === "control" && |
| 61 | + event.subtype === "done" && |
| 62 | + event.command === "message" && |
| 63 | + event.requestId === messageRequestId && |
| 64 | + event.code === "queued" |
| 65 | + ) { |
| 66 | + messageQueued = true |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + if ( |
| 71 | + event.type === "queue" && |
| 72 | + (event.subtype === "snapshot" || event.subtype === "enqueued" || event.subtype === "updated") && |
| 73 | + Array.isArray(event.queue) && |
| 74 | + event.queue.some((item) => item?.imageCount === 1) |
| 75 | + ) { |
| 76 | + queueImageCountObserved = true |
| 77 | + |
| 78 | + if (!shutdownSent) { |
| 79 | + context.sendCommand({ command: "shutdown", requestId: shutdownRequestId }) |
| 80 | + shutdownSent = true |
| 81 | + } |
| 82 | + |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + if ( |
| 87 | + event.type === "control" && |
| 88 | + event.subtype === "ack" && |
| 89 | + event.command === "shutdown" && |
| 90 | + event.requestId === shutdownRequestId |
| 91 | + ) { |
| 92 | + shutdownAck = true |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + if ( |
| 97 | + event.type === "control" && |
| 98 | + event.subtype === "done" && |
| 99 | + event.command === "shutdown" && |
| 100 | + event.requestId === shutdownRequestId |
| 101 | + ) { |
| 102 | + shutdownDone = true |
| 103 | + } |
| 104 | + }, |
| 105 | + onTimeoutMessage() { |
| 106 | + return `timed out waiting for queue image metadata (initSeen=${initSeen}, startAccepted=${startAccepted}, messageAccepted=${messageAccepted}, messageQueued=${messageQueued}, queueImageCountObserved=${queueImageCountObserved}, shutdownSent=${shutdownSent}, shutdownAck=${shutdownAck}, shutdownDone=${shutdownDone})` |
| 107 | + }, |
| 108 | + }) |
| 109 | + |
| 110 | + if (!messageAccepted || !messageQueued || !queueImageCountObserved) { |
| 111 | + throw new Error( |
| 112 | + `expected queued message with image metadata (messageAccepted=${messageAccepted}, messageQueued=${messageQueued}, queueImageCountObserved=${queueImageCountObserved})`, |
| 113 | + ) |
| 114 | + } |
| 115 | + |
| 116 | + if (!shutdownAck || !shutdownDone) { |
| 117 | + throw new Error("shutdown control events were not fully observed") |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +main().catch((error) => { |
| 122 | + console.error(`[FAIL] ${error instanceof Error ? error.message : String(error)}`) |
| 123 | + process.exit(1) |
| 124 | +}) |
0 commit comments