repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/diff-review/__tests__/server-html.test.ts

Raw
File outline
import { parsePatchFiles } from "@pierre/diffs";
import { preloadFileDiff } from "@pierre/diffs/ssr";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { startReviewServer } from "../server.js";
import type { CollectedDiff, ReviewState } from "../types.js";

const DIFF_TEXT = `diff --git a/hello.ts b/hello.ts
new file mode 100644
--- /dev/null
+++ b/hello.ts
@@ -0,0 +1,3 @@
+export function hello(name: string): string {
+  return \`Hello, \${name}!\`;
+}
`;

const mockDiff: CollectedDiff = {
	repoRoot: "/tmp",
	source: { kind: "working-tree" },
	diffText: DIFF_TEXT,
	files: [{ path: "hello.ts", status: "added", additions: 3, deletions: 0 }],
	diffIndex: new Map([
		[
			"hello.ts",
			{
				file: "hello.ts",
				status: "added" as const,
				additions: new Map([
					[1, "export function hello(name: string): string {"],
					[2, "  return `Hello, ${name}!`;"],
					[3, "}"],
				]),
				deletions: new Map(),
				oldLines: new Map(),
				newLines: new Map([
					[1, "export function hello(name: string): string {"],
					[2, "  return `Hello, ${name}!`;"],
					[3, "}"],
				]),
			},
		],
	]),
	skippedFiles: [],
	additions: 3,
	deletions: 0,
};

const mockState: ReviewState = { round: 1, findings: [], rounds: [] };

// Full route matrix lives in server-html-full.test.ts; this shard keeps SSR smoke under the per-file budget.
describe("diff-review SSR and server HTML smoke", () => {
	let server: Awaited<ReturnType<typeof startReviewServer>>;
	let baseUrl: string;

	beforeAll(async () => {
		server = await startReviewServer({
			diff: mockDiff,
			state: mockState,
			round: 1,
			token: "tst-tok",
		});
		baseUrl = server.url.replace(/\?.*$/, "");
	});

	afterAll(() => server.close("command"));

	it("preloads diff SSR payload", async () => {
		const [fileDiff] = parsePatchFiles(DIFF_TEXT, "test-diff", true).flatMap(
			(patch) => patch.files,
		);
		if (!fileDiff) throw new Error("expected parsed file diff");
		const result = await preloadFileDiff({
			fileDiff,
			options: { diffStyle: "split", overflow: "scroll", tokenizeMaxLength: 0 },
		});
		expect(result.prerenderedHTML).toContain("hello");
	});

	it("GET / returns HTML containing diff content", async () => {
		const res = await fetch(`${baseUrl}?token=tst-tok`);
		expect(res.status).toBe(200);
		expect(res.headers.get("content-type")).toContain("text/html");
		const html = await res.text();
		expect(html).toContain("hello.ts");
		expect(html).toContain("Diff Review");
		expect(html).toContain('data-diff=""');
	});

	it("POST /api/heartbeat still works", async () => {
		const res = await fetch(`${baseUrl}api/heartbeat`, {
			method: "POST",
			headers: {
				Authorization: "Bearer tst-tok",
				"Content-Type": "application/json",
			},
			body: JSON.stringify({}),
		});
		expect(res.status).toBe(200);
		expect(await res.json()).toEqual({ ok: true });
	});
});