import assert from "node:assert/strict";
import childProcess from "node:child_process";
import * as fs from "node:fs";
import * as nodeModule from "node:module";
import * as os from "node:os";
import * as path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
import { PI_CODING_AGENT_PACKAGE_ROOT_ENV } from "../../src/shared/utils.ts";

test("executeAsyncSingle preloads all peer aliases before the selected runner loader when aliases exist", async (t) => {
	const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "async-spawn-preload-")));
	const host = path.join(root, "host");
	const expectedAliases: Record<string, string> = {};
	const hostExports: Record<string, string[]> = {
		"@earendil-works/pi-coding-agent": ["."],
		"@earendil-works/pi-agent-core": [".", "./node"],
		"@earendil-works/chord": [".", "./context"],
		"@earendil-works/pi-tui": ["."],
		"@earendil-works/pi-ai": ["./compat", "./oauth", "./providers/all"],
		"typebox": [".", "./compile", "./value"],
	};
	// Use real package manifests/targets, as in host-peer-runtime-imports.test.ts.
	function writeHostPackage(pkg: string) {
		const dir = pkg === "@earendil-works/pi-coding-agent" ? host : path.join(host, "node_modules", pkg);
		fs.mkdirSync(dir, { recursive: true });
		const exports = Object.fromEntries(hostExports[pkg]!.map(subpath => {
			const target = `./${subpath === "." ? "index" : subpath.slice(2).replaceAll("/", "-")}.mjs`;
			fs.writeFileSync(path.join(dir, target), "export {};\n");
			expectedAliases[subpath === "." ? pkg : `${pkg}/${subpath.slice(2)}`] = path.join(dir, target);
			if (pkg === "@earendil-works/pi-ai" && subpath === "./compat") expectedAliases[pkg] = path.join(dir, target);
			return [subpath, target];
		}));
		fs.writeFileSync(path.join(dir, "package.json"), JSON.stringify({ name: pkg, version: "0.85.1", exports }));
	}
	const originalArgv1 = process.argv[1];
	try {
		for (const pkg of Object.keys(hostExports)) writeHostPackage(pkg);
		// Production discovers the host from the Pi entrypoint at module load.
		process.argv[1] = expectedAliases["@earendil-works/pi-coding-agent"];
		// helpers -> mock-pi -> child-session -> readonly evidence -> child hooks
		// also loads async-execution. Set the host before importing that graph.
		const { makeAgent } = await import("../support/helpers.ts");
		const { executeAsyncSingle } = await import("../../src/runs/background/async-execution.ts");
		const spawn = t.mock.method(childProcess, "spawn", () => {
			// Stop at the only external I/O seam: no fake pid or detached lifecycle.
			throw new Error("spawn boundary captured");
		});
		nodeModule.syncBuiltinESMExports();
		for (const scenario of ["stable", "pre-chord", "missing-pre-chord"]) {
			if (scenario === "pre-chord") {
				fs.writeFileSync(path.join(host, "package.json"), JSON.stringify({ name: "@earendil-works/pi-coding-agent", version: "0.84.3", exports: { ".": "./index.mjs" } }));
				fs.rmSync(path.join(host, "node_modules", "@earendil-works/chord"), { recursive: true });
				for (const specifier of ["@earendil-works/chord", "@earendil-works/chord/context"]) delete expectedAliases[specifier];
			}
			if (scenario === "missing-pre-chord") fs.unlinkSync(expectedAliases["@earendil-works/pi-agent-core/node"]!);
			const configuredExtension = scenario === "pre-chord" ? fileURLToPath(import.meta.url) : undefined;
			const result = executeAsyncSingle(`spawn-preload-${scenario}`, {
				agent: "worker", task: "Inspect launch wiring", agentConfig: makeAgent("worker", configuredExtension ? { extensions: [configuredExtension] } : {}),
				ctx: { pi: { events: { emit() {} } }, cwd: root, currentSessionId: "spawn-preload-session" },
				artifactConfig: { enabled: false, includeInput: false, includeOutput: false, includeJsonl: false, includeMetadata: false, cleanupDays: 7 },
				shareEnabled: false, sessionRoot: path.join(root, "sessions"), maxSubagentDepth: 1, acceptance: false,
			});
			assert.equal(result.isError, true);
			if (scenario === "missing-pre-chord") {
				assert.match(result.content[0]!.text, /@earendil-works\/pi-agent-core\/node/);
				assert.equal(spawn.mock.callCount(), 2);
				continue;
			}
			assert.match(result.content[0]!.text, /spawn boundary captured/);
			assert.equal(spawn.mock.callCount(), scenario === "stable" ? 1 : 2);
			const [command, args, options] = spawn.mock.calls.at(-1)!.arguments;
			assert.ok(path.isAbsolute(command));
			assert.equal(options.env[PI_CODING_AGENT_PACKAGE_ROOT_ENV], host);
			const actualAliases = JSON.parse(options.env.JITI_ALIAS) as Record<string, string>;
			assert.deepEqual(Object.fromEntries(Object.entries(actualAliases).map(([key, target]) => [key, fs.realpathSync(target)])), expectedAliases);
			assert.equal(args[0], "--import");
			assert.equal(args[1], new URL("../../runner-peer-preload.mjs", import.meta.url).href);
			assert.ok(fs.existsSync(fileURLToPath(args[1])));
			const nativeRunner = Boolean(process.features.typescript) && typeof nodeModule.registerHooks === "function";
			if (nativeRunner) assert.equal(args[2], "--experimental-strip-types");
			else assert.match(args[2], /[/\\]jiti-cli\.mjs$/);
			assert.match(args[3], /[/\\]subagent-runner-bootstrap\.ts$/);
			assert.equal(args.length, 5);
			assert.equal(options.env.PI_ASYNC_NATIVE_RUNNER, nativeRunner ? "1" : "0");
		}
	} finally {
		t.mock.restoreAll();
		nodeModule.syncBuiltinESMExports();
		if (originalArgv1 === undefined) delete process.argv[1];
		else process.argv[1] = originalArgv1;
		fs.rmSync(root, { recursive: true, force: true });
	}
});
