Luigit
repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by admin

extensions/good-job/__serial__/remediation.test.ts

Raw
import {
	existsSync,
	mkdtempSync,
	readFileSync,
	rmSync,
	symlinkSync,
} from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import {
	type ExtensionContext,
	initTheme,
} from "@earendil-works/pi-coding-agent";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
	createPiSession,
	type PiSession,
} from "../../../test/fixture/create-pi-session.ts";
import type { GoodJobRecord, Remediation } from "../core.js";
import {
	atomicWriteJson,
	ensureLayout,
	goodJobDataDirectory,
	layout,
	loadRecords,
	loadRemediation,
	loadRemediations,
	migrateStore,
	parseRemediation,
	saveRemediation,
} from "../storage.js";

const problem: GoodJobRecord = {
	version: 2,
	id: "wtf-0000000001",
	kind: "problem",
	createdAt: "2026-01-01T00:00:00.000Z",
	completedAt: "2026-01-01T00:01:00.000Z",
	feedback: "Unverified completion",
	source: {
		type: "import",
		importer: "test",
		fingerprint: "fixture",
		importedAt: "2026-01-01T00:00:00.000Z",
	},
	analysisModel: null,
	usage: null,
	learning: {
		slug: "verify-consumer-outcome",
		summary: "Consumer outcome remained unverified",
		evidence: ["Consumer failed"],
		behaviors: ["Verify the consumer"],
		candidateRule: null,
	},
};
const verified: Remediation = {
	version: 1,
	recordId: problem.id,
	status: "verified",
	changeRef: "commit abc123: consumer check",
	appliedAt: "2026-01-02",
	verifiedAt: "2026-01-03",
	evidence: "Fresh consumer passed; Linux only.",
	updatedAt: "2026-01-04T00:00:00.000Z",
};
const roots: string[] = [];
let session: PiSession | undefined;
function temporaryRoot(): string {
	const root = mkdtempSync(join(tmpdir(), "good-job-remediation-test-"));
	roots.push(root);
	return root;
}
async function seed(root: string) {
	const paths = layout(root);
	await ensureLayout(paths);
	await atomicWriteJson(join(paths.records, `${problem.id}.json`), problem);
	return paths;
}
async function commandContext(userSettings?: Record<string, unknown>) {
	session = await createPiSession({
		extensionPaths: [fileURLToPath(new URL("../index.ts", import.meta.url))],
		...(userSettings ? { userSettings } : {}),
	});
	const paths = await seed(
		goodJobDataDirectory(session.env, process.platform, session.env["HOME"]),
	);
	const base = session.session.extensionRunner.createContext();
	const ctx = Object.create(base) as ExtensionContext;
	Object.defineProperties(ctx, {
		hasUI: { value: true, configurable: true },
		mode: { value: "rpc", configurable: true },
	});
	const ui = Object.create(base.ui) as ExtensionContext["ui"];
	Object.defineProperty(ctx, "ui", { value: ui });
	const run = (args: string) =>
		session!.extensions[0]!.commands.get("gj")!.handler(args, ctx as never);
	return { ctx, ui, paths, run };
}
afterEach(async () => {
	await session?.dispose();
	session = undefined;
	vi.restoreAllMocks();
	for (const root of roots.splice(0))
		rmSync(root, { recursive: true, force: true });
});

describe("good-job remediation", () => {
	it("stores proposed, applied, and verified metadata without modifying learning bytes", async () => {
		const paths = await seed(temporaryRoot());
		const file = join(paths.records, `${problem.id}.json`);
		const original = readFileSync(file, "utf8");
		expect(await loadRemediation(problem.id, paths)).toBeUndefined();
		for (const value of [
			{
				...verified,
				status: "proposed" as const,
				appliedAt: null,
				verifiedAt: null,
				evidence: "",
			},
			{
				...verified,
				status: "applied" as const,
				verifiedAt: null,
				evidence: "",
			},
			verified,
		]) {
			await saveRemediation(value, paths);
			expect(await loadRemediation(problem.id, paths)).toEqual(value);
			expect(readFileSync(file, "utf8")).toBe(original);
		}
		expect((await loadRecords(paths)).records).toEqual([problem]);
	});

	it.each([
		{ status: "closed" },
		{ changeRef: " " },
		{ evidence: " " },
		{ appliedAt: "2026-02-30" },
		{ verifiedAt: "2026-01-01" },
		{ recordId: "../../outside" },
		{ recordId: "gj-0000000001" },
		{ changeRef: "x".repeat(1001) },
		{ evidence: "x".repeat(4001) },
	])("rejects invalid remediation metadata %j", (patch) => {
		expect(parseRemediation({ ...verified, ...patch })).toBeUndefined();
	});

	it("requires an existing problem, rejects praise, and surfaces malformed sidecars", async () => {
		const paths = await seed(temporaryRoot());
		await expect(
			saveRemediation({ ...verified, recordId: "wtf-0000000002" }, paths),
		).rejects.toThrow();
		await expect(
			saveRemediation({ ...verified, recordId: "gj-0000000001" }, paths),
		).rejects.toThrow();
		await atomicWriteJson(join(paths.remediations, `${problem.id}.json`), {
			...verified,
			recordId: "wtf-0000000002",
		});
		await expect(loadRemediation(problem.id, paths)).rejects.toThrow(
			"invalid remediation",
		);
		const loaded = await loadRemediations(paths, [problem]);
		expect(loaded.remediations.size).toBe(0);
		expect(loaded.errors).toHaveLength(1);
		await expect(migrateStore(paths.root, paths.root)).resolves.toMatchObject({
			remediations: 0,
		});
		await saveRemediation(verified, paths);
		const original = readFileSync(
			join(paths.remediations, `${problem.id}.json`),
			"utf8",
		);
		await expect(
			saveRemediation({ ...verified, evidence: "" }, paths),
		).rejects.toThrow("invalid remediation");
		expect(
			readFileSync(join(paths.remediations, `${problem.id}.json`), "utf8"),
		).toBe(original);
	});

	it("moves linked remediation with its record and preserves it for aliased/no-op migration", async () => {
		const paths = await seed(temporaryRoot());
		await saveRemediation(verified, paths);
		const target = layout(temporaryRoot());
		expect(await migrateStore(paths.root, target.root)).toMatchObject({
			records: 1,
			remediations: 1,
		});
		expect(await loadRemediation(problem.id, target)).toEqual(verified);
		expect(existsSync(join(paths.remediations, `${problem.id}.json`))).toBe(
			false,
		);
		const alias = join(temporaryRoot(), "alias");
		symlinkSync(target.root, alias, "dir");
		await migrateStore(target.root, alias);
		expect(await loadRemediation(problem.id, target)).toEqual(verified);
	});

	it("migrates remediation on startup when a configured directory replaces the default", async () => {
		const target = layout(temporaryRoot());
		const { ctx, paths, run } = await commandContext({
			"good-job": { directory: target.root },
		});
		await saveRemediation(verified, paths);
		const start = session!.extensions[0]!.handlers.get("session_start")![0]!;
		start({ type: "session_start", reason: "startup" }, ctx);
		await run("status");
		expect(await loadRemediation(problem.id, target)).toEqual(verified);
		expect((await loadRecords(target)).records).toEqual([problem]);
		expect(existsSync(join(paths.remediations, `${problem.id}.json`))).toBe(
			false,
		);
	});

	it("shows persisted status in /gj list and complete remediation in its selected detail", async () => {
		const { ctx, ui, paths, run } = await commandContext();
		await saveRemediation(verified, paths);
		Object.defineProperty(ctx, "mode", { value: "tui" });
		initTheme(undefined, false);
		const screens: string[] = [];
		vi.spyOn(ui, "custom").mockImplementation(
			async (factory) =>
				new Promise((resolve) => {
					void Promise.resolve(
						factory(
							{ requestRender: vi.fn() } as never,
							ui.theme,
							{} as never,
							resolve,
						),
					).then((component) => {
						screens.push(component.render(180).join("\n"));
						component.handleInput?.("\r");
					});
				}),
		);
		await run("list");
		expect(screens).toHaveLength(2);
		expect(screens[0]).toContain(problem.id);
		expect(screens[0]).toContain("verified");
		for (const text of [
			verified.changeRef,
			verified.appliedAt!,
			verified.verifiedAt!,
			verified.evidence,
		])
			expect(screens[1]).toContain(text);
	});

	it.each(["tui", "rpc"])(
		"remediates the selected problem through /gj in %s without entering an ID",
		async (mode) => {
			const { ctx, ui, paths, run } = await commandContext();
			Object.defineProperty(ctx, "mode", { value: mode });
			initTheme(undefined, false);
			const select = vi.spyOn(ui, "select");
			if (mode === "tui") {
				let screen = 0;
				vi.spyOn(ui, "custom").mockImplementation(
					async (factory) =>
						new Promise((resolve) => {
							void Promise.resolve(
								factory(
									{ requestRender: vi.fn() } as never,
									ui.theme,
									{} as never,
									resolve,
								),
							).then((component) => {
								component.render(120);
								component.handleInput?.(screen++ === 0 ? "\r" : "r");
							});
						}),
				);
			} else {
				select
					.mockImplementationOnce(async (_title, choices) =>
						choices.find((choice) => choice.includes(problem.id)),
					)
					.mockResolvedValueOnce("Remediate");
			}
			select.mockResolvedValueOnce("applied");
			vi.spyOn(ui, "editor").mockResolvedValue(verified.changeRef);
			vi.spyOn(ui, "input").mockResolvedValue(verified.appliedAt!);
			vi.spyOn(ui, "confirm").mockResolvedValue(true);
			await run("list");
			expect(await loadRemediation(problem.id, paths)).toMatchObject({
				status: "applied",
				changeRef: verified.changeRef,
			});
		},
	);

	it("saves directly verified remediation via public command after confirmation", async () => {
		const { ui, paths, run } = await commandContext();
		const original = readFileSync(
			join(paths.records, `${problem.id}.json`),
			"utf8",
		);
		vi.spyOn(ui, "select").mockResolvedValue("verified");
		vi.spyOn(ui, "editor")
			.mockResolvedValueOnce(verified.changeRef)
			.mockResolvedValueOnce(verified.evidence);
		vi.spyOn(ui, "input")
			.mockResolvedValueOnce(verified.appliedAt!)
			.mockResolvedValueOnce(verified.verifiedAt!);
		vi.spyOn(ui, "confirm").mockResolvedValue(true);
		await run(`remediate ${problem.id}`);
		expect(await loadRemediation(problem.id, paths)).toMatchObject({
			...verified,
			updatedAt: expect.any(String),
		});
		expect(
			readFileSync(join(paths.records, `${problem.id}.json`), "utf8"),
		).toBe(original);
	});

	it.each(["status", "reference", "confirmation"])(
		"leaves existing remediation unchanged when cancelling %s",
		async (stage) => {
			const { ui, paths, run } = await commandContext();
			await saveRemediation(verified, paths);
			vi.spyOn(ui, "select").mockResolvedValue(
				stage === "status" ? undefined : "proposed",
			);
			vi.spyOn(ui, "editor").mockResolvedValue(
				stage === "reference" ? undefined : "different proposal",
			);
			vi.spyOn(ui, "confirm").mockResolvedValue(false);
			await run(`remediate ${problem.id}`);
			expect(await loadRemediation(problem.id, paths)).toEqual(verified);
		},
	);

	it.each(["applied date", "verification date", "evidence"])(
		"preserves sidecar bytes after invalid input then cancellation at %s",
		async (stage) => {
			const { ui, paths, run } = await commandContext();
			await saveRemediation(verified, paths);
			const file = join(paths.remediations, `${problem.id}.json`);
			const original = readFileSync(file, "utf8");
			vi.spyOn(ui, "notify").mockImplementation(() => {});
			vi.spyOn(ui, "select").mockResolvedValue("verified");
			const editor = vi
				.spyOn(ui, "editor")
				.mockResolvedValueOnce(verified.changeRef);
			const input = vi.spyOn(ui, "input");
			if (stage === "applied date")
				input
					.mockResolvedValueOnce("2026-02-30")
					.mockResolvedValueOnce(undefined);
			else {
				input.mockResolvedValueOnce(verified.appliedAt!);
				if (stage === "verification date")
					input
						.mockResolvedValueOnce("2026-01-01")
						.mockResolvedValueOnce(undefined);
				else {
					input.mockResolvedValueOnce(verified.verifiedAt!);
					editor.mockResolvedValueOnce(" ").mockResolvedValueOnce(undefined);
				}
			}
			await run(`remediate ${problem.id}`);
			expect(readFileSync(file, "utf8")).toBe(original);
		},
	);

	it("selects only problems and retries invalid input before saving applied status", async () => {
		const { ui, paths, run } = await commandContext();
		await atomicWriteJson(join(paths.records, "gj-0000000001.json"), {
			...problem,
			id: "gj-0000000001",
			kind: "praise",
		});
		vi.spyOn(ui, "notify").mockImplementation(() => {});
		vi.spyOn(ui, "select")
			.mockImplementationOnce(async (_title, choices) => {
				expect(choices).toHaveLength(1);
				return choices.find((choice) => choice.includes(problem.id));
			})
			.mockResolvedValueOnce("applied");
		vi.spyOn(ui, "editor")
			.mockResolvedValueOnce(" ")
			.mockResolvedValueOnce(verified.changeRef);
		vi.spyOn(ui, "input")
			.mockResolvedValueOnce("not a date")
			.mockResolvedValueOnce(verified.appliedAt!);
		vi.spyOn(ui, "confirm").mockResolvedValue(true);
		await run("remediate");
		expect(await loadRemediation(problem.id, paths)).toMatchObject({
			status: "applied",
			changeRef: verified.changeRef,
			appliedAt: verified.appliedAt,
			verifiedAt: null,
			evidence: "",
		});
	});

	it("rejects praise before prompting and refuses unavailable dialogs without writing", async () => {
		const { ctx, ui, paths, run } = await commandContext();
		const select = vi.spyOn(ui, "select");
		const notify = vi.spyOn(ui, "notify").mockImplementation(() => {});
		await run("remediate gj-0000000001");
		expect(notify).toHaveBeenCalledWith(
			expect.stringContaining("praise"),
			"warning",
		);
		expect(select).not.toHaveBeenCalled();
		Object.defineProperty(ctx, "hasUI", { value: false });
		await expect(run(`remediate ${problem.id}`)).rejects.toThrow(
			"requires interactive dialogs",
		);
		expect(await loadRemediation(problem.id, paths)).toBeUndefined();
	});
});