import { describe, expect, it } from "bun:test"; import * as fs from "node:fs/promises"; import * as os from "node:os"; import * as path from "node:path"; const repoRoot = path.resolve(import.meta.dir, "..", "..", ".."); const cliEntry = path.join(repoRoot, "packages", "coding-agent", "src", "cli.ts"); const workflowSkills = ["deep-interview", "ralplan", "ultragoal", "team"] as const; function sessionStateDir(cwd: string, sessionId: string): string { return path.join(cwd, ".skc", `_session-${encodeURIComponent(sessionId).replaceAll(".", "%2E")}`, "state"); } const initialPhases: Record<(typeof workflowSkills)[number], string> = { "deep-interview": "interviewing", ralplan: "planner", ultragoal: "goal-planning", team: "starting", }; async function withTempCwd(fn: (cwd: string) => Promise): Promise { const cwd = await fs.mkdtemp(path.join(os.tmpdir(), "skc-state-command-")); try { return await fn(cwd); } finally { await fs.rm(cwd, { recursive: true, force: true }); } } function runState(cwd: string, args: string[]) { return Bun.spawnSync(["bun", cliEntry, "state", ...args], { cwd, stderr: "pipe", stdout: "pipe", env: { ...process.env }, }); } describe("skc state workflow command", () => { it("writes readable canonical state and receipt for workflow skills through documented invocation", async () => { await withTempCwd(async cwd => { for (const skill of workflowSkills) { const result = runState(cwd, [ "write", "--session-id", `session-${skill}`, "--input", JSON.stringify({ skill, current_phase: initialPhases[skill], active: true, hud: { version: 1, chips: [{ label: "gate", value: "approval-required", priority: 5, severity: "warning" }], }, state: { blocked_reason: "execution approval missing" }, }), "--json", ]); expect(result.exitCode, result.stderr.toString()).toBe(0); const payload = JSON.parse(result.stdout.toString()) as { skill: string; status: string; }; expect(payload).toMatchObject({ skill, status: "fresh" }); const modeState = await Bun.file( path.join(sessionStateDir(cwd, `session-${skill}`), `${skill}-state.json`), ).json(); expect(modeState).toMatchObject({ skill, current_phase: initialPhases[skill], }); expect(modeState.blocked_reason ?? modeState.state?.blocked_reason).toBe("execution approval missing"); expect(modeState.receipt.command).toBe(`skc state ${skill} write`); const activeState = await Bun.file( path.join(sessionStateDir(cwd, `session-${skill}`), "skill-active-state.json"), ).json(); expect(activeState.active_skills[0]).toMatchObject({ skill, phase: initialPhases[skill], receipt: { owner: "skc-state-cli" }, }); } }); }, 30_000); it("rejects stale workflow phases without force", async () => { await withTempCwd(async cwd => { for (const { skill, phase } of [ { skill: "deep-interview", phase: "initializing" }, { skill: "ralplan", phase: "approval" }, ] as const) { const result = runState(cwd, [ "write", "--session-id", `session-${skill}-${phase}`, "--input", JSON.stringify({ skill, current_phase: phase, active: true }), "--json", ]); expect(result.exitCode).toBe(2); expect(result.stderr.toString()).toContain(`unknown ${skill} phase "${phase}"`); expect(result.stderr.toString()).toContain("use --force to bypass"); } }); }, 20_000); it("infers read skill from session context and prefers incoming phase transitions", async () => { await withTempCwd(async cwd => { const initial = runState(cwd, [ "write", "--session-id", "session-1", "--input", JSON.stringify({ skill: "deep-interview", current_phase: "interviewing", state: { current_phase: "ignored" }, }), "--json", ]); expect(initial.exitCode, initial.stderr.toString()).toBe(0); const transition = runState(cwd, [ "write", "--session-id", "session-1", "--input", JSON.stringify({ skill: "deep-interview", phase: "handoff", state: { current_phase: "stale" } }), "--json", ]); expect(transition.exitCode, transition.stderr.toString()).toBe(0); const modeState = await Bun.file( path.join(sessionStateDir(cwd, "session-1"), "deep-interview-state.json"), ).json(); expect(modeState.current_phase).toBe("handoff"); const activeState = await Bun.file( path.join(sessionStateDir(cwd, "session-1"), "skill-active-state.json"), ).json(); expect(activeState.active_skills[0]).toMatchObject({ skill: "deep-interview", phase: "handoff" }); const read = runState(cwd, ["read", "--session-id", "session-1", "--json"]); expect(read.exitCode, read.stderr.toString()).toBe(0); const readPayload = JSON.parse(read.stdout.toString()) as { skill: string; state: { current_phase: string } }; expect(readPayload.skill).toBe("deep-interview"); expect(readPayload.state.current_phase).toBe("handoff"); }); }, 20_000); it("syncs the active-state mirror with the lock-owned mode-state revision on each write", async () => { await withTempCwd(async cwd => { const dir = sessionStateDir(cwd, "session-rev"); const readActiveRev = async () => { const active = await Bun.file(path.join(dir, "skill-active-state.json")).json(); return active.active_skills[0].source_state_revision as number; }; const readModeRev = async () => { const mode = await Bun.file(path.join(dir, "deep-interview-state.json")).json(); return mode.state_revision as number; }; for (let i = 0; i < 3; i++) { const result = runState(cwd, [ "write", "--session-id", "session-rev", "--input", JSON.stringify({ skill: "deep-interview", current_phase: "interviewing", state: { round: i } }), "--json", ]); expect(result.exitCode, result.stderr.toString()).toBe(0); // The active mirror must carry the revision this write actually owns (lock-computed), // never a stale pre-write value, so later writes are not stale-skipped. expect(await readActiveRev()).toBe(await readModeRev()); } }); }, 20_000); });