|
| 1 | +/** |
| 2 | + * Chaos tests for `dot deploy` — verifies that the process-guard cleanup |
| 3 | + * + SIGINT handling work end-to-end. |
| 4 | + * |
| 5 | + * These run only in the nightly-chaos-sigint cell. PR runs skip them. |
| 6 | + * |
| 7 | + * Design notes |
| 8 | + * ------------ |
| 9 | + * • We spawn the CLI directly via execa (not the `dot()` helper) so we get a |
| 10 | + * child handle we can signal mid-flight. |
| 11 | + * • The sentinel `"▸ storage-and-dotns"` appears in headless-mode stdout |
| 12 | + * exactly once, from `logHeadlessEvent` in src/commands/deploy/index.ts |
| 13 | + * when the storage phase starts. It is specific to the headless code path |
| 14 | + * and does not rely on bulletin-deploy banner text (which is intercepted by |
| 15 | + * the log parser and never reaches stdout). |
| 16 | + * • If the sentinel never fires within 60 s (e.g. the deploy errors during |
| 17 | + * preflight), the test still sends SIGINT and asserts clean exit semantics. |
| 18 | + * The 5 s wall-clock assertion still exercises the process-guard — just |
| 19 | + * against a process that may already be winding down rather than one in |
| 20 | + * the middle of a chunk upload. |
| 21 | + */ |
| 22 | + |
| 23 | +import { describe, test, expect } from "vitest"; |
| 24 | +import { execa } from "execa"; |
| 25 | +import { resolve } from "node:path"; |
| 26 | +import { SIGNER, E2E_DOMAINS } from "./fixtures/accounts.js"; |
| 27 | +import { fixturePath } from "./fixtures/templates.js"; |
| 28 | + |
| 29 | +const REPO_ROOT = resolve(import.meta.dirname, "../.."); |
| 30 | +const CLI_ENTRY = resolve(REPO_ROOT, "src/index.ts"); |
| 31 | + |
| 32 | +/** How long to wait for the storage-phase sentinel before sending SIGINT anyway. */ |
| 33 | +const SENTINEL_WAIT_MS = 60_000; |
| 34 | + |
| 35 | +/** Expected maximum elapsed time from SIGINT to process exit. */ |
| 36 | +const MAX_EXIT_MS = 5_000; |
| 37 | + |
| 38 | +describe("dot deploy — chaos", () => { |
| 39 | + test("SIGINT mid-deploy exits with code 130 within 5s", { timeout: 120_000 }, async () => { |
| 40 | + const frontendOnly = fixturePath("frontend-only"); |
| 41 | + |
| 42 | + const child = execa( |
| 43 | + "bun", |
| 44 | + [ |
| 45 | + "run", |
| 46 | + CLI_ENTRY, |
| 47 | + "deploy", |
| 48 | + "--signer", |
| 49 | + "dev", |
| 50 | + "--domain", |
| 51 | + E2E_DOMAINS.chaos, |
| 52 | + "--buildDir", |
| 53 | + resolve(frontendOnly, "dist"), |
| 54 | + "--playground", |
| 55 | + "--suri", |
| 56 | + SIGNER.suri, |
| 57 | + "--dir", |
| 58 | + frontendOnly, |
| 59 | + ], |
| 60 | + { |
| 61 | + cwd: REPO_ROOT, |
| 62 | + env: { ...process.env, DOT_TAG: "e2e-chaos-sigint", DOT_TELEMETRY: "1" }, |
| 63 | + reject: false, |
| 64 | + }, |
| 65 | + ); |
| 66 | + |
| 67 | + // Accumulate output from both streams so the diagnostic tail is |
| 68 | + // available if the assertion fails. |
| 69 | + let buf = ""; |
| 70 | + child.stdout?.on("data", (chunk: Buffer) => { |
| 71 | + buf += chunk.toString(); |
| 72 | + }); |
| 73 | + child.stderr?.on("data", (chunk: Buffer) => { |
| 74 | + buf += chunk.toString(); |
| 75 | + }); |
| 76 | + |
| 77 | + // Wait for the storage phase to start OR for the process to exit early |
| 78 | + // (e.g. availability check fails, network error during preflight, etc.) |
| 79 | + // OR for the 60 s sentinel budget to expire. |
| 80 | + // |
| 81 | + // Sentinel: headless logHeadlessEvent writes `▸ storage-and-dotns…\n` |
| 82 | + // to stdout when the storage-and-dotns phase begins. |
| 83 | + const sentinel = "▸ storage-and-dotns"; |
| 84 | + await new Promise<void>((resolve) => { |
| 85 | + const timeout = setTimeout(() => resolve(), SENTINEL_WAIT_MS); |
| 86 | + |
| 87 | + const checkBuf = () => { |
| 88 | + if (buf.includes(sentinel)) { |
| 89 | + clearTimeout(timeout); |
| 90 | + resolve(); |
| 91 | + } |
| 92 | + }; |
| 93 | + |
| 94 | + child.stdout?.on("data", checkBuf); |
| 95 | + child.on("exit", () => { |
| 96 | + clearTimeout(timeout); |
| 97 | + resolve(); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + // Send SIGINT and measure how long the process takes to exit. |
| 102 | + const t0 = Date.now(); |
| 103 | + child.kill("SIGINT"); |
| 104 | + const result = await child; |
| 105 | + const elapsedMs = Date.now() - t0; |
| 106 | + |
| 107 | + // process-guard.ts calls runAllCleanupAndExit(130) on SIGINT. |
| 108 | + // execa reflects this as exitCode=130. If the handler doesn't get |
| 109 | + // to run (e.g. bun propagates the OS signal before our handler fires), |
| 110 | + // execa sets signal="SIGINT" instead. Both outcomes are acceptable: |
| 111 | + // what we're asserting is that the process is GONE cleanly within 5 s. |
| 112 | + const cleanExit = result.exitCode === 130 || result.signal === "SIGINT"; |
| 113 | + expect( |
| 114 | + cleanExit, |
| 115 | + `expected exit code 130 or signal SIGINT, got exitCode=${result.exitCode} signal=${result.signal}\nlast output:\n${buf.slice(-500)}`, |
| 116 | + ).toBe(true); |
| 117 | + |
| 118 | + expect( |
| 119 | + elapsedMs, |
| 120 | + `process took ${elapsedMs} ms to exit after SIGINT (limit ${MAX_EXIT_MS} ms)\nlast output:\n${buf.slice(-500)}`, |
| 121 | + ).toBeLessThan(MAX_EXIT_MS); |
| 122 | + }); |
| 123 | +}); |
0 commit comments