import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { describe, it } from "node:test";
import type { AsyncStatus, ProcessTerminal, WorkflowChildSummary } from "../../src/shared/types.ts";
import { readWorkflowTerminalProof } from "../../src/runs/background/workflow-terminal-proof.ts";
import { currentPidNamespaceScope } from "../../src/runs/background/pid-namespace.ts";

type Steps = NonNullable<AsyncStatus["steps"]>;

function summary(overrides: Partial<WorkflowChildSummary> = {}): WorkflowChildSummary {
	return {
		version: 1,
		parentToolCallId: "parent-tool-call",
		workflowRunId: "workflow-run",
		inventoryComplete: true,
		workflowState: "completed",
		children: [{ childId: "main", runId: "child-run", state: "completed" }],
		...overrides,
	};
}

const asyncChild = [{ agent: "worker", workflowKey: "main", runId: "child-run", async: true, status: "completed" }] as Steps;

function observedChild(): ProcessTerminal {
	return {
		version: 1,
		state: "observed",
		runId: "child-run",
		runnerProcessInstanceId: "runner-1",
		observedAt: 1_234,
		instances: [{ kind: "runner", processInstanceId: "runner-1", closeObservedAt: 1_234, exitCode: 0, signal: null }],
	};
}

function withFixture(run: (dirs: { root: string; asyncDir: string; writeChild: (status: object, proof?: ProcessTerminal) => void }) => void): void {
	const root = fs.mkdtempSync(path.join(os.tmpdir(), "workflow-terminal-proof-"));
	const asyncDir = path.join(root, "workflow-run");
	fs.mkdirSync(asyncDir, { recursive: true });
	const writeChild = (status: object, proof?: ProcessTerminal) => {
		const childDir = path.join(root, "child-run");
		fs.mkdirSync(childDir, { recursive: true });
		fs.writeFileSync(path.join(childDir, "status.json"), JSON.stringify({ runId: "child-run", mode: "single", startedAt: 1, lastUpdate: 2, ...status }));
		if (proof) fs.writeFileSync(path.join(childDir, "process-terminal.json"), JSON.stringify(proof));
	};
	try {
		run({ root, asyncDir, writeChild });
	} finally {
		fs.rmSync(root, { recursive: true, force: true });
	}
}

const runnerIdentity = { version: 1, runId: "child-run", runnerProcessInstanceId: "runner-1" };

describe("readWorkflowTerminalProof", () => {
	it("keeps an open inventory pending", () => withFixture(({ asyncDir }) => {
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, asyncChild, summary({ inventoryComplete: false, workflowState: "running" }), 0, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "pending", dispatchClosed: false, reason: "Workflow dispatch is still open.",
		});
	}));

	it("keeps closed dispatch pending until the child's exit is observed", () => withFixture(({ asyncDir, writeChild }) => {
		writeChild({ state: "complete", processTerminal: { ...runnerIdentity, state: "pending" } });
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, asyncChild, summary(), 0, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "pending", dispatchClosed: true,
			reason: "async workflow child main process-terminal proof is missing",
		});
	}));

	it("reports unknown instead of pending when a finished child's runner died with nobody watching it", () => withFixture(({ asyncDir, writeChild }) => {
		const exited = spawnSync(process.execPath, ["-e", ""]);
		assert.equal(exited.status, 0);
		const pending: ProcessTerminal = { ...runnerIdentity, state: "pending" };
		writeChild({ state: "complete", pid: exited.pid, pidNamespaceScope: currentPidNamespaceScope(), processTerminal: pending }, pending);
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, asyncChild, summary(), 0, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "unknown", dispatchClosed: true,
			reason: "async workflow child main process-terminal proof is unknown (observer-unavailable)",
		});
	}));

	it("does not accept a missing child roster as an empty workflow", () => withFixture(({ asyncDir }) => {
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, undefined, summary(), 0, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "unknown", dispatchClosed: true,
			reason: "workflow child roster is missing",
		});
		assert.equal(readWorkflowTerminalProof(asyncDir, [], summary({ children: [] }), 0, 2_000).state, "observed");
	}));

	it("returns observed after every async child has writer-exit evidence", () => withFixture(({ asyncDir, writeChild }) => {
		const proof = observedChild();
		writeChild({ state: "complete", processTerminal: { ...runnerIdentity, state: "pending" } }, proof);
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, asyncChild, summary({ workflowState: "stopped" }), 0, 1_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "observed", dispatchClosed: true, observedAt: 1_234, children: [proof],
		});
	}));

	it("accepts a child whose runner failed before starting", () => withFixture(({ asyncDir, writeChild }) => {
		const notStarted = { ...runnerIdentity, state: "not-started" } as ProcessTerminal;
		writeChild({ state: "failed", error: "runner failed to start", processTerminal: notStarted });
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, asyncChild, summary({ workflowState: "failed" }), 0, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "observed", dispatchClosed: true, observedAt: 2_000, children: [notStarted],
		});
	}));

	it("needs no process evidence for an async child whose launch failed before a runner existed", () => withFixture(({ root, asyncDir, writeChild }) => {
		const launchError = "Unknown subagent model 'x/y' in the active Pi model registry.";
		type StepOutcome = Pick<Steps[number], "status" | "error">;
		const steps = (step: StepOutcome): Steps => [{ agent: "worker", workflowKey: "main", runId: "child-run", async: true, ...step }];
		const failedLaunch = steps({ status: "failed", error: launchError });
		const workflow = summary({ workflowState: "failed", children: [{ childId: "main", runId: "child-run", state: "failed" }] });
		const observed = { version: 1, kind: "workflow", runId: "workflow-run", state: "observed", dispatchClosed: true, observedAt: 2_000, children: [] };
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, failedLaunch, workflow, 0, 2_000), observed);
		const childDir = path.join(root, "child-run");
		fs.mkdirSync(childDir);
		fs.writeFileSync(path.join(childDir, "mission.json"), "{}");
		fs.writeFileSync(path.join(childDir, "run-fanout-budget.json"), "{}");
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, failedLaunch, workflow, 0, 2_000), observed);
		for (const artifact of ["process-terminal.json", "runner-startup.json"]) {
			fs.writeFileSync(path.join(childDir, artifact), "{}");
			assert.equal(readWorkflowTerminalProof(asyncDir, failedLaunch, workflow, 0, 2_000).state, "unknown", artifact);
			fs.rmSync(path.join(childDir, artifact));
		}
		const retainedOutcomes: StepOutcome[] = [{ status: "failed" }, { status: "stopped", error: "Stopped." }, { status: "running" }];
		for (const step of retainedOutcomes) {
			assert.equal(readWorkflowTerminalProof(asyncDir, steps(step), workflow, 0, 2_000).state, "unknown", JSON.stringify(step));
		}
		writeChild({ state: "running", processTerminal: { ...runnerIdentity, state: "pending" } });
		assert.equal(readWorkflowTerminalProof(asyncDir, failedLaunch, workflow, 0, 2_000).state, "pending");
	}));

	it("needs no process evidence for synchronous children that run inside the host", () => withFixture(({ asyncDir }) => {
		const syncChild = [{ agent: "worker", workflowKey: "main", async: false, status: "completed" }] as Steps;
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, syncChild, summary({ children: [{ childId: "main", state: "completed" }] }), 0, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "observed", dispatchClosed: true, observedAt: 2_000, children: [],
		});
	}));

	it("reports host commands as unknown", () => withFixture(({ asyncDir }) => {
		assert.deepEqual(readWorkflowTerminalProof(asyncDir, [], summary({ children: [] }), 1, 2_000), {
			version: 1, kind: "workflow", runId: "workflow-run", state: "unknown", dispatchClosed: true, reason: "Workflow host commands have no process-terminal proof.",
		});
	}));
});
