Files
AgentCompany/tests/integration/autonomous-chain/crash-recovery.spec.ts
T
ZacharyZhang-NY 0f1b7c0b33 test(integration): split autonomous-chain.spec.ts into per-scenario files
The original tests/integration/autonomous-chain.spec.ts was ~1400 lines
and 50 KB, covering 19 describe blocks across 10 orthogonal scenarios.
Physically extract each describe (or closely related group) into its
own file inside tests/integration/autonomous-chain/ so each file stays
under the project's 500-line ceiling and failures point at the
scenario that regressed.

Shared beforeEach / afterEach / createAgent / buildCtx helpers move to
autonomous-chain/shared-setup.ts, which exposes installSharedSetup() as
a factory that each spec calls at module load. The tests themselves
are moved byte-for-byte; only module-level references (`db`,
`companyId`, `workspaceId`, `tempDirs`) are rewritten to go through
the shared handle. No assertions, it() calls, or behavior are changed.

Split layout:
  role-detection.spec.ts        — Step 1: CEO role detection
  ceo-phased-prompts.spec.ts    — Step 2: phased prompts + approvals
  wakeup-queue.spec.ts          — Step 3: deferred wake queue
  approval-decisions.spec.ts    — Step 4: hire approval decisions
  task-propagation.spec.ts      — Step 5: task/comment wake propagation
  run-failure.spec.ts           — Step 6: run failure escalation
  crash-recovery.spec.ts        — Step 7: worker crash recovery
  wake-reason.spec.ts           — Step 8: wake reason injection
  e2e-flows.spec.ts             — full-chain + review-fix regressions
  automation-messages.spec.ts   — automation/messages/assignment/workflows

Test count stays at 363 passing; typecheck stays at 156 errors.
2026-04-16 23:34:54 -04:00

94 lines
2.9 KiB
TypeScript

/**
* Step 7: Worker crash recovery (database layer).
* Physically extracted from autonomous-chain.spec.ts without behavior changes.
*/
import { describe, expect, it } from "vitest";
import { AppDatabase } from "../../../src/main/database";
import { installSharedSetup } from "./shared-setup";
const shared = installSharedSetup();
const { createAgent } = shared;
describe("Step 7: Worker crash recovery (database layer)", () => {
it("interruptStaleRuns clears running and queued runs on init", async () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
companyId: shared.companyId,
title: "In-progress task",
description: "Running when crash happened",
assigneeAgentId: agentId,
workspaceId: shared.workspaceId,
priority: "medium",
status: "in_progress",
});
const run = shared.db.createRun({
companyId: shared.companyId,
taskId,
agentId,
workspaceId: shared.workspaceId,
connectorId: "codex_local",
});
shared.db.markRunStatus(run.id, "running", "Started");
// Verify it's running
let snapshot = shared.db.listSnapshot();
expect(snapshot.runs.find((r) => r.id === run.id)?.status).toBe("running");
// Simulate app restart — close and re-init
shared.db.close();
const profileDir = shared.tempDirs[shared.tempDirs.length - 1]!;
shared.db = new AppDatabase(profileDir);
shared.db.init(); // This calls interruptStaleRuns()
snapshot = shared.db.listSnapshot();
const recoveredRun = snapshot.runs.find((r) => r.id === run.id);
expect(recoveredRun?.status).toBe("interrupted");
});
it("finishRun can mark interrupted runs from crash recovery", () => {
const agentId = createAgent({ name: "Agent" });
const taskId = shared.db.saveTask({
companyId: shared.companyId,
title: "Crash recovery task",
description: "Test",
assigneeAgentId: agentId,
workspaceId: shared.workspaceId,
priority: "medium",
status: "in_progress",
});
const run = shared.db.createRun({
companyId: shared.companyId,
taskId,
agentId,
workspaceId: shared.workspaceId,
connectorId: "codex_local",
});
shared.db.markRunStatus(run.id, "running", "Started");
// Simulate crash recovery - mark as interrupted
shared.db.finishRun({
runId: run.id,
status: "interrupted",
summary: "Worker process crashed.",
errorMessage: "Worker process exited unexpectedly.",
exitCode: 1,
signal: null,
model: null,
sessionDisplayId: null,
costUsd: null,
unattributedCost: true,
});
const snapshot = shared.db.listSnapshot();
const recovered = snapshot.runs.find((r) => r.id === run.id);
expect(recovered?.status).toBe("interrupted");
expect(recovered?.errorMessage).toBe("Worker process exited unexpectedly.");
});
});