| 1 | import assert from "node:assert/strict"; |
| 2 | import { once } from "node:events"; |
| 3 | import { createServer } from "node:http"; |
| 4 | import test from "node:test"; |
| 5 | import { |
| 6 | CodeWhaleRuntimeClient, |
| 7 | RuntimeApiError, |
| 8 | RuntimeCapabilityError, |
| 9 | createRuntimeClient, |
| 10 | } from "../index.js"; |
| 11 | |
| 12 | function jsonResponse(body, init = {}) { |
| 13 | return new Response(JSON.stringify(body), { |
| 14 | status: init.status ?? 200, |
| 15 | headers: { "content-type": "application/json", ...(init.headers ?? {}) }, |
| 16 | }); |
| 17 | } |
| 18 | |
| 19 | function fakeFetch(responseFactory) { |
| 20 | const calls = []; |
| 21 | const fetch = async (url, init) => { |
| 22 | calls.push({ url: url.toString(), init }); |
| 23 | return responseFactory(url, init, calls.length); |
| 24 | }; |
| 25 | fetch.calls = calls; |
| 26 | return fetch; |
| 27 | } |
| 28 | |
| 29 | test("createRuntimeClient returns CodeWhaleRuntimeClient instance", () => { |
| 30 | const fetch = fakeFetch(() => jsonResponse({})); |
| 31 | const client = createRuntimeClient({ fetch }); |
| 32 | |
| 33 | assert.ok(client instanceof CodeWhaleRuntimeClient); |
| 34 | assert.equal(client.baseUrl, "http://127.0.0.1:7878/"); |
| 35 | }); |
| 36 | |
| 37 | test("listFleetRuns calls the Runtime API with bearer auth", async () => { |
| 38 | const fetch = fakeFetch(() => |
| 39 | jsonResponse({ |
| 40 | status: { runs: 1, workers: {} }, |
| 41 | runs: [{ id: "run-1", name: "smoke", tasks: [], labels: {} }], |
| 42 | }), |
| 43 | ); |
| 44 | const client = createRuntimeClient({ |
| 45 | baseUrl: "http://127.0.0.1:7878", |
| 46 | token: "token-1", |
| 47 | fetch, |
| 48 | }); |
| 49 | |
| 50 | const response = await client.listFleetRuns(); |
| 51 | |
| 52 | assert.equal(response.runs[0].id, "run-1"); |
| 53 | assert.equal(fetch.calls[0].url, "http://127.0.0.1:7878/v1/fleet/runs"); |
| 54 | assert.equal(fetch.calls[0].init.method, "GET"); |
| 55 | assert.equal(fetch.calls[0].init.headers.get("authorization"), "Bearer token-1"); |
| 56 | assert.equal(fetch.calls[0].init.redirect, "error"); |
| 57 | }); |
| 58 | |
| 59 | test("fleet event paths cannot send runtime credentials outside the configured origin", async () => { |
| 60 | const fetch = fakeFetch(() => jsonResponse({ events: [] })); |
| 61 | const client = createRuntimeClient({ baseUrl: "http://127.0.0.1:7878/", token: "fixture-token", fetch }); |
| 62 | for (const path of [ |
| 63 | "https://example.invalid/events", "//example.invalid/events", "\\\\example.invalid/events", |
| 64 | "http://127.0.0.1:7879/events", "https://127.0.0.1:7878/events", |
| 65 | "data:application/json,%7B%7D", "file:///tmp/events", "http://user:password@127.0.0.1:7878/events", |
| 66 | ]) { |
| 67 | await assert.rejects(client.fleetEvents("run-1", { path }).next(), /configured HTTP\(S\) origin/); |
| 68 | } |
| 69 | assert.equal(fetch.calls.length, 0, "no rejected destination reaches fetch"); |
| 70 | }); |
| 71 | |
| 72 | test("fleet event paths preserve safe relative and same-origin URL semantics", async () => { |
| 73 | const fetch = fakeFetch(() => jsonResponse({ events: [] })); |
| 74 | const client = createRuntimeClient({ baseUrl: "http://127.0.0.1:7878/runtime/", token: "fixture-token", fetch }); |
| 75 | for (const path of ["events", "/events", "http://127.0.0.1:7878/events", "//127.0.0.1:7878/events"]) { |
| 76 | assert.deepEqual(await client.fleetEvents("run-1", { path, after: "cursor/a", limit: 5 }).next(), { value: undefined, done: true }); |
| 77 | } |
| 78 | assert.deepEqual(fetch.calls.map(({ url }) => new URL(url).pathname), ["/runtime/events", "/events", "/events", "/events"]); |
| 79 | for (const { url, init } of fetch.calls) { |
| 80 | assert.equal(new URL(url).searchParams.get("after"), "cursor/a"); |
| 81 | assert.equal(new URL(url).searchParams.get("limit"), "5"); |
| 82 | assert.equal(init.headers.get("authorization"), "Bearer fixture-token"); |
| 83 | assert.equal(init.redirect, "error"); |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | test("authenticated runtime GET and POST reject actual redirects before a second request", async (t) => { |
| 88 | const escaped = []; |
| 89 | const destination = createServer((req, res) => { |
| 90 | escaped.push({ url: req.url, authorization: req.headers.authorization }); |
| 91 | res.writeHead(200, { "content-type": "application/json" }).end("{}"); |
| 92 | }); |
| 93 | destination.listen(0, "127.0.0.1"); |
| 94 | await once(destination, "listening"); |
| 95 | t.after(() => new Promise(resolve => { destination.close(resolve); destination.closeAllConnections(); })); |
| 96 | const received = []; |
| 97 | const source = createServer((req, res) => { |
| 98 | received.push({ method: req.method, url: req.url, authorization: req.headers.authorization }); |
| 99 | if (req.url !== "/v1/fleet/runs") { |
| 100 | escaped.push({ url: req.url, authorization: req.headers.authorization }); |
| 101 | res.writeHead(200, { "content-type": "application/json" }).end("{}"); |
| 102 | return; |
| 103 | } |
| 104 | res.writeHead(307, { location: req.method === "GET" ? "/unexpected" : `http://127.0.0.1:${destination.address().port}/unexpected` }).end(); |
| 105 | }); |
| 106 | source.listen(0, "127.0.0.1"); |
| 107 | await once(source, "listening"); |
| 108 | t.after(() => new Promise(resolve => { source.close(resolve); source.closeAllConnections(); })); |
| 109 | const client = createRuntimeClient({ baseUrl: `http://127.0.0.1:${source.address().port}`, token: "fixture-token" }); |
| 110 | await assert.rejects(client.listFleetRuns(), TypeError); |
| 111 | await assert.rejects(client.createFleetRun({ name: "local-only" }), TypeError); |
| 112 | assert.deepEqual(received.map(({ method }) => method), ["GET", "POST"]); |
| 113 | assert.ok(received.every(({ authorization }) => authorization === "Bearer fixture-token")); |
| 114 | assert.deepEqual(escaped, [], "neither a same-origin nor an off-origin redirect is followed"); |
| 115 | }); |
| 116 | |
| 117 | test("worker and run actions use POST endpoints", async () => { |
| 118 | const fetch = fakeFetch((url) => |
| 119 | jsonResponse( |
| 120 | url.pathname.endsWith("/stop") |
| 121 | ? { |
| 122 | action: "stop", |
| 123 | run_id: "run-1", |
| 124 | stopped: 1, |
| 125 | status: { runs: 1, workers: {} }, |
| 126 | } |
| 127 | : { |
| 128 | action: url.pathname.endsWith("/restart") |
| 129 | ? "restart" |
| 130 | : url.pathname.endsWith("/stop") |
| 131 | ? "stop" |
| 132 | : "interrupt", |
| 133 | worker: { worker_id: "w1", artifacts: [] }, |
| 134 | }, |
| 135 | ), |
| 136 | ); |
| 137 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 138 | |
| 139 | await client.interruptWorker("w1"); |
| 140 | await client.stopWorker("w1"); |
| 141 | await client.restartWorker("w1"); |
| 142 | await client.startFleetRun("run-1"); |
| 143 | await client.stopFleetRun("run-1"); |
| 144 | |
| 145 | assert.deepEqual( |
| 146 | fetch.calls.map((call) => [new URL(call.url).pathname, call.init.method]), |
| 147 | [ |
| 148 | ["/v1/fleet/workers/w1/interrupt", "POST"], |
| 149 | ["/v1/fleet/workers/w1/stop", "POST"], |
| 150 | ["/v1/fleet/workers/w1/restart", "POST"], |
| 151 | ["/v1/fleet/runs/run-1/start", "POST"], |
| 152 | ["/v1/fleet/runs/run-1/stop", "POST"], |
| 153 | ], |
| 154 | ); |
| 155 | }); |
| 156 | |
| 157 | test("managed Fleet helpers send explicit launch metadata and reconnect cursors", async () => { |
| 158 | const fetch = fakeFetch((url) => |
| 159 | jsonResponse( |
| 160 | url.pathname.endsWith("/events/replay") |
| 161 | ? { run_id: "run-1", events: [], has_more: false, history_truncated: false } |
| 162 | : { execution: "awaiting_start", run: { id: "run-1" }, warnings: [] }, |
| 163 | ), |
| 164 | ); |
| 165 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 166 | const spec = { |
| 167 | target: "this_computer", |
| 168 | roles: [{ name: "reviewer" }], |
| 169 | workflow: { |
| 170 | id: "review", |
| 171 | kind: "parallel", |
| 172 | tasks: [ |
| 173 | { |
| 174 | id: "review", |
| 175 | name: "Review", |
| 176 | instructions: "Review.", |
| 177 | worker: { role: "reviewer" }, |
| 178 | budget: { max_steps: 0 }, |
| 179 | }, |
| 180 | ], |
| 181 | }, |
| 182 | }; |
| 183 | |
| 184 | await client.createFleetRun(spec); |
| 185 | await client.replayFleetEvents("run-1", { after: "fev1_cursor_worker", limit: 25 }); |
| 186 | |
| 187 | assert.deepEqual(JSON.parse(fetch.calls[0].init.body), spec); |
| 188 | assert.equal(JSON.parse(fetch.calls[0].init.body).workflow.tasks[0].budget.max_steps, 0); |
| 189 | const replayUrl = new URL(fetch.calls[1].url); |
| 190 | assert.equal(replayUrl.pathname, "/v1/fleet/runs/run-1/events/replay"); |
| 191 | assert.equal(replayUrl.searchParams.get("after"), "fev1_cursor_worker"); |
| 192 | assert.equal(replayUrl.searchParams.get("limit"), "25"); |
| 193 | }); |
| 194 | |
| 195 | test("unsupported fleet capabilities raise typed errors", async () => { |
| 196 | const fetch = fakeFetch(() => jsonResponse({ error: "not found" }, { status: 404 })); |
| 197 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 198 | |
| 199 | await assert.rejects( |
| 200 | () => client.createFleetRun({ name: "future" }), |
| 201 | (error) => |
| 202 | error instanceof RuntimeCapabilityError && |
| 203 | error.capability === "fleet_run_create" && |
| 204 | error.status === 404, |
| 205 | ); |
| 206 | |
| 207 | await assert.rejects( |
| 208 | async () => { |
| 209 | for await (const _event of client.fleetEvents("run-1")) { |
| 210 | throw new Error("unexpected event"); |
| 211 | } |
| 212 | }, |
| 213 | (error) => |
| 214 | error instanceof RuntimeCapabilityError && |
| 215 | error.capability === "fleet_event_stream" && |
| 216 | error.status === 404, |
| 217 | ); |
| 218 | }); |
| 219 | |
| 220 | test("fleetEvents replays JSON event fixtures regardless of media type casing", async (t) => { |
| 221 | const fixture = { |
| 222 | seq: 1, |
| 223 | run_id: "run-1", |
| 224 | worker_id: "w1", |
| 225 | task_id: "task-1", |
| 226 | timestamp: "2026-06-13T00:00:00Z", |
| 227 | label: "running", |
| 228 | payload: { state: "running" }, |
| 229 | }; |
| 230 | |
| 231 | for (const contentType of [ |
| 232 | "application/json", |
| 233 | "Application/JSON; charset=UTF-8", |
| 234 | "application/problem+json", |
| 235 | "Application/Vnd.CodeWhale+JSON ; charset=utf-8", |
| 236 | ]) { |
| 237 | await t.test(contentType, async () => { |
| 238 | const fetch = fakeFetch(() => |
| 239 | jsonResponse({ events: [fixture] }, { headers: { "content-type": contentType } }), |
| 240 | ); |
| 241 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 242 | const events = []; |
| 243 | for await (const event of client.fleetEvents("run-1")) { |
| 244 | events.push(event); |
| 245 | } |
| 246 | assert.deepEqual(events, [fixture]); |
| 247 | }); |
| 248 | } |
| 249 | }); |
| 250 | |
| 251 | test("fleetEvents parses text/event-stream frames", async () => { |
| 252 | const encoder = new TextEncoder(); |
| 253 | const body = new ReadableStream({ |
| 254 | start(controller) { |
| 255 | controller.enqueue( |
| 256 | encoder.encode( |
| 257 | 'id: fev1_heartbeat_worker\nevent: fleet.worker.heartbeat\ndata: {"cursor":"fev1_heartbeat_worker","event":"fleet.worker.heartbeat","run_id":"run-1","worker_id":"w1","task_id":"task-1","timestamp":"2026-06-13T00:00:01Z","worker_seq":2,"payload":{"state":"heartbeat","memory_mb":128}}\n\n', |
| 258 | ), |
| 259 | ); |
| 260 | controller.close(); |
| 261 | }, |
| 262 | }); |
| 263 | const fetch = fakeFetch( |
| 264 | () => |
| 265 | new Response(body, { |
| 266 | status: 200, |
| 267 | headers: { "content-type": "text/event-stream" }, |
| 268 | }), |
| 269 | ); |
| 270 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 271 | |
| 272 | const events = []; |
| 273 | for await (const event of client.fleetEvents("run-1", { after: "fev1_previous", limit: 10 })) { |
| 274 | events.push(event); |
| 275 | } |
| 276 | |
| 277 | assert.equal(events.length, 1); |
| 278 | assert.equal(events[0].payload.state, "heartbeat"); |
| 279 | assert.equal(events[0].payload.memory_mb, 128); |
| 280 | const eventUrl = new URL(fetch.calls[0].url); |
| 281 | assert.equal(eventUrl.searchParams.get("after"), "fev1_previous"); |
| 282 | assert.equal(eventUrl.searchParams.get("limit"), "10"); |
| 283 | assert.equal(fetch.calls[0].init.headers.get("accept"), "text/event-stream"); |
| 284 | }); |
| 285 | |
| 286 | test("fleetEvents uses the frame's final event name and last valid ID", async () => { |
| 287 | const frame = [ |
| 288 | "event: fleet.worker.heartbeat", |
| 289 | "id: fev1_previous", |
| 290 | "event: fleet.worker.exited", |
| 291 | "id: fev1_exit", |
| 292 | "id: fev1_invalid\u0000cursor", |
| 293 | 'data: {"worker_id":"w1"}', |
| 294 | "", |
| 295 | "", |
| 296 | ].join("\n"); |
| 297 | const fetch = fakeFetch(() => new Response(frame, { headers: { "content-type": "text/event-stream" } })); |
| 298 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 299 | const events = []; |
| 300 | for await (const event of client.fleetEvents("run-1")) { |
| 301 | events.push(event); |
| 302 | } |
| 303 | assert.deepEqual(events, [{ worker_id: "w1", event: "fleet.worker.exited", cursor: "fev1_exit" }]); |
| 304 | }); |
| 305 | |
| 306 | test("fleetEvents cancels an idle read when aborted", async (t) => { |
| 307 | const server = createServer((_request, response) => { |
| 308 | response.writeHead(200, { "content-type": "text/event-stream" }); |
| 309 | response.write('data: {"event":"fleet.worker.heartbeat"}\n\n'); |
| 310 | }); |
| 311 | server.listen(0, "127.0.0.1"); |
| 312 | await once(server, "listening"); |
| 313 | t.after(() => new Promise((resolve) => { |
| 314 | server.close(resolve); |
| 315 | server.closeAllConnections(); |
| 316 | })); |
| 317 | |
| 318 | const client = new CodeWhaleRuntimeClient({ baseUrl: `http://127.0.0.1:${server.address().port}` }); |
| 319 | const controller = new AbortController(); |
| 320 | const events = client.fleetEvents("run-1", { signal: controller.signal }); |
| 321 | assert.deepEqual(await events.next(), { value: { event: "fleet.worker.heartbeat" }, done: false }); |
| 322 | const pending = events.next(); |
| 323 | const deadline = new Promise((resolve) => { |
| 324 | const timer = setTimeout(resolve, 1000); |
| 325 | t.after(() => clearTimeout(timer)); |
| 326 | }); |
| 327 | controller.abort(); |
| 328 | await assert.rejects(Promise.race([pending, deadline]), { name: "AbortError" }); |
| 329 | }); |
| 330 | |
| 331 | test("fleetEvents preserves SSE control event names", async () => { |
| 332 | const encoder = new TextEncoder(); |
| 333 | const body = new ReadableStream({ |
| 334 | start(controller) { |
| 335 | controller.enqueue( |
| 336 | encoder.encode( |
| 337 | 'event: fleet.replay.cursor_unavailable\r\ndata: {"run_id":"run-1","reload_projection":true}\r\n\r\n', |
| 338 | ), |
| 339 | ); |
| 340 | controller.close(); |
| 341 | }, |
| 342 | }); |
| 343 | const client = new CodeWhaleRuntimeClient({ |
| 344 | fetch: fakeFetch( |
| 345 | () => |
| 346 | new Response(body, { |
| 347 | status: 200, |
| 348 | headers: { "content-type": "text/event-stream" }, |
| 349 | }), |
| 350 | ), |
| 351 | }); |
| 352 | |
| 353 | const events = []; |
| 354 | for await (const event of client.fleetEvents("run-1")) { |
| 355 | events.push(event); |
| 356 | } |
| 357 | |
| 358 | assert.deepEqual(events, [ |
| 359 | { |
| 360 | event: "fleet.replay.cursor_unavailable", |
| 361 | run_id: "run-1", |
| 362 | reload_projection: true, |
| 363 | }, |
| 364 | ]); |
| 365 | }); |
| 366 | |
| 367 | test("ordinary HTTP errors remain RuntimeApiError", async () => { |
| 368 | const fetch = fakeFetch(() => jsonResponse({ error: "bad" }, { status: 500 })); |
| 369 | const client = new CodeWhaleRuntimeClient({ fetch }); |
| 370 | |
| 371 | await assert.rejects( |
| 372 | () => client.getFleetRun("run-1"), |
| 373 | (error) => |
| 374 | error instanceof RuntimeApiError && |
| 375 | !(error instanceof RuntimeCapabilityError) && |
| 376 | error.status === 500, |
| 377 | ); |
| 378 | }); |
| 379 |