|
| 1 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | + |
| 5 | +import { describe, expect, test } from "bun:test"; |
| 6 | + |
| 7 | +import { generateSessionId } from "../../src/session/index.js"; |
| 8 | +import type { RunState } from "../../src/session/state.js"; |
| 9 | + |
| 10 | +const FIXTURE = join(import.meta.dirname, "../fixtures/crash-run/simulate-exec-signal.ts"); |
| 11 | + |
| 12 | +async function readLine(stream: ReadableStream<Uint8Array>): Promise<string> { |
| 13 | + const reader = stream.getReader(); |
| 14 | + const decoder = new TextDecoder(); |
| 15 | + let buffer = ""; |
| 16 | + while (!buffer.includes("\n")) { |
| 17 | + const { value, done } = await reader.read(); |
| 18 | + if (done) break; |
| 19 | + buffer += decoder.decode(value, { stream: true }); |
| 20 | + } |
| 21 | + reader.releaseLock(); |
| 22 | + return buffer; |
| 23 | +} |
| 24 | + |
| 25 | +describe("integration — signaled exec process finalizes run.json", () => { |
| 26 | + test.each([ |
| 27 | + ["SIGINT", 130], |
| 28 | + ["SIGTERM", 143], |
| 29 | + ["SIGHUP", 129], |
| 30 | + ] as const)( |
| 31 | + "%s writes status: failed and exits with %i", |
| 32 | + async (signal, expectedExitCode) => { |
| 33 | + const cwd = mkdtempSync(join(tmpdir(), "corbits-exec-signal-cwd-")); |
| 34 | + const home = mkdtempSync(join(tmpdir(), "corbits-exec-signal-home-")); |
| 35 | + const sessionId = generateSessionId(); |
| 36 | + |
| 37 | + try { |
| 38 | + const proc = Bun.spawn(["bun", "run", FIXTURE], { |
| 39 | + cwd, |
| 40 | + env: { ...process.env, HOME: home, SIGNAL_TEST_SESSION_ID: sessionId }, |
| 41 | + stdout: "pipe", |
| 42 | + stderr: "pipe", |
| 43 | + }); |
| 44 | + |
| 45 | + const output = await readLine(proc.stdout); |
| 46 | + const [runDir] = output.split("\n"); |
| 47 | + if (runDir === undefined || runDir.length === 0) { |
| 48 | + const errText = await new Response(proc.stderr).text(); |
| 49 | + throw new Error( |
| 50 | + `fixture did not report a run directory: ${JSON.stringify(output)} stderr=${errText}`, |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + proc.kill(signal); |
| 55 | + const exitCode = await proc.exited; |
| 56 | + |
| 57 | + expect(exitCode).toBe(expectedExitCode); |
| 58 | + |
| 59 | + const runJsonPath = join(runDir, "run.json"); |
| 60 | + const raw = readFileSync(runJsonPath, "utf8"); |
| 61 | + const state = JSON.parse(raw) as RunState; |
| 62 | + |
| 63 | + expect(state.status).toBe("failed"); |
| 64 | + expect(state.status).not.toBe("running"); |
| 65 | + expect(state.finishedAt).toBeGreaterThan(0); |
| 66 | + expect(state.error).toBe(`terminated by ${signal}`); |
| 67 | + expect(state.task).toBe("headless exec signal task"); |
| 68 | + } finally { |
| 69 | + rmSync(cwd, { recursive: true, force: true }); |
| 70 | + rmSync(home, { recursive: true, force: true }); |
| 71 | + } |
| 72 | + }, |
| 73 | + 15_000, |
| 74 | + ); |
| 75 | +}); |
0 commit comments