repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/diff-review/__e2e__/e2e.spec.ts

Raw
File outline
import { execFile } from "node:child_process";
import { mkdir, mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, test } from "@playwright/test";
import { collectDiff } from "../git.js";
import { startReviewServer } from "../server.js";
import type { BrowserResult } from "../types.js";

const execFileAsync = promisify(execFile);

let submittedResults: Extract<BrowserResult, { type: "submit" }>[] = [];
let server: Awaited<ReturnType<typeof startReviewServer>>;
let _baseUrl: string;

async function git(cwd: string, args: string[]) {
	await execFileAsync("git", args, { cwd, maxBuffer: 20 * 1024 * 1024 });
}

test.beforeAll(async () => {
	const repo = await mkdtemp(join(tmpdir(), "diff-review-e2e-"));
	await git(repo, ["init", "-q"]);
	await git(repo, ["config", "user.email", "e2e@example.invalid"]);
	await git(repo, ["config", "user.name", "E2E"]);

	await mkdir(join(repo, "src"), { recursive: true });
	await writeFile(
		join(repo, "src/hello.ts"),
		"export function hello(name: string): string {\n" +
			"  return `Hello, ${name}!`;\n" +
			"}\n",
	);
	await writeFile(
		join(repo, "src/util.ts"),
		"export function add(a: number, b: number): number {\n" +
			"  return a + b;\n" +
			"}\n" +
			"export function mul(a: number, b: number): number {\n" +
			"  return a * b;\n" +
			"}\n",
	);
	await git(repo, ["add", "."]);
	await git(repo, ["commit", "-qm", "initial"]);

	await writeFile(
		join(repo, "src/hello.ts"),
		"export function hello(name: string): string {\n" +
			"  if (!name) throw new Error('name required');\n" +
			"  return `Hello, ${name}!`;\n" +
			"}\n",
	);
	await writeFile(
		join(repo, "src/util.ts"),
		"export function add(a: number, b: number): number {\n" +
			"  return a + b;\n" +
			"}\n",
	);

	submittedResults = [];
	const diff = await collectDiff(repo, { staged: false });
	server = await startReviewServer({
		diff,
		state: { round: 1, findings: [], rounds: [] },
		round: 1,
		onSubmit: (result) => {
			submittedResults.push(result);
		},
	});
	_baseUrl = server.url.replace(/\?.*$/, "");
});

test.afterAll(() => {
	server.close("command");
});

test("loads review page with diff content", async ({ page }) => {
	await page.goto(server.url);
	await expect(page.locator("h1")).toHaveText("Diff Review");
	await expect(page.locator(".file-title").first()).toContainText("hello.ts");
	await expect(page.locator(".file-title")).toHaveCount(2);
	await expect(page.locator("[data-line]").first()).toBeVisible();
});

test("shows meta info (round, source, stats)", async ({ page }) => {
	await page.goto(server.url);
	await expect(page.locator(".meta-value").first()).toHaveText("1");
	await expect(page.locator(".meta-value").nth(1)).toContainText("unstaged");
	await expect(page.locator("#summary")).toContainText("2 files");
});

test("clicking a changed line opens finding dialog", async ({ page }) => {
	await page.goto(server.url);
	const line = page.locator("[data-line]").first();
	await line.click();
	await expect(page.locator("#finding-dialog")).toBeVisible();
	await expect(page.locator("#selection-label")).toContainText("hello.ts");
});

test("add finding via dialog", async ({ page }) => {
	await page.goto(server.url);

	// click a line
	await page.locator("[data-line]").first().click();
	await expect(page.locator("#finding-dialog")).toBeVisible();

	// fill form
	await page
		.locator("#finding-dialog textarea[name='comment']")
		.fill("e2e test finding");
	await page.locator("#finding-dialog button[type='submit']").click();

	// page reloads — wait for findings panel
	await expect(page.locator("#findings")).toContainText("e2e test finding", {
		timeout: 5000,
	});
	await expect(page.locator(".finding-item")).toHaveCount(1);
	await expect(page.locator("#drawer-count")).toHaveText("1");
});

test("remove finding via delete button", async ({ page }) => {
	await page.goto(server.url);

	// Ensure clean state — remove any leftover findings
	const removeButtons = page.locator(".remove-finding");
	while ((await removeButtons.count()) > 0) {
		await removeButtons.first().click();
		await page.waitForTimeout(500);
	}

	// add a finding
	await page.locator("[data-line]").first().click();
	await expect(page.locator("#finding-dialog")).toBeVisible();
	await page
		.locator("#finding-dialog textarea[name='comment']")
		.fill("to be removed");
	await page.locator("#finding-dialog button[type='submit']").click();
	await expect(page.locator("#findings")).toContainText("to be removed", {
		timeout: 5000,
	});

	// remove it
	await page.locator(".remove-finding").first().click();
	await expect(page.locator("#findings")).toContainText("No findings yet.", {
		timeout: 5000,
	});
});

test("search returns results", async ({ page }) => {
	await page.goto(server.url);
	const input = page.locator("#search");
	await input.fill("throw");
	await expect(page.locator("#search-results")).toBeVisible({ timeout: 3000 });
	await expect(page.locator(".search-result").first()).toContainText(
		"hello.ts",
	);
});

test("theme toggle cycles through modes", async ({ page }) => {
	await page.goto(server.url);
	const html = page.locator("html");
	// Default: system → resolves to browser preference
	const initial = (await html.getAttribute("data-theme")) ?? "system";
	// Click cycles: system → dark → light → system
	await page.locator("#theme-toggle").click();
	await expect(html).toHaveAttribute("data-theme", "dark");
	await page.locator("#theme-toggle").click();
	await expect(html).toHaveAttribute("data-theme", "light");
	await page.locator("#theme-toggle").click();
	// Back to system, which matches initial
	await expect(html).toHaveAttribute("data-theme", initial);
});

test("pane collapse toggles", async ({ page }) => {
	await page.goto(server.url);
	const app = page.locator("#app");

	await page.locator("#toggle-left").click();
	await expect(app).toHaveAttribute("data-left-collapsed", "true");

	await page.locator("#toggle-left").click();
	await expect(app).toHaveAttribute("data-left-collapsed", "false");

	await page.locator("#toggle-right").click();
	await expect(app).toHaveAttribute("data-right-collapsed", "true");

	await page.locator("#toggle-right").click();
	await expect(app).toHaveAttribute("data-right-collapsed", "false");
});

test("submit review sends findings to server", async ({ page }) => {
	await page.goto(server.url);

	// add a finding
	await page.locator("[data-line]").first().click();
	await expect(page.locator("#finding-dialog")).toBeVisible();
	await page
		.locator("#finding-dialog textarea[name='comment']")
		.fill("submit e2e");
	await page.locator("#finding-dialog button[type='submit']").click();
	await expect(page.locator("#findings")).toContainText("submit e2e", {
		timeout: 5000,
	});

	// fill notes and submit
	await page.locator("textarea[name='notes']").fill("e2e notes");
	const prevSubmissions = submittedResults.length;
	await page
		.locator("form[action='submit'] button[type='submit']:last-child")
		.click();
	await expect(page.locator("body")).toContainText("Review sent to Pi", {
		timeout: 5000,
	});

	// verify server received the submission
	expect(submittedResults.length).toBe(prevSubmissions + 1);
	const last = submittedResults.at(-1);
	expect(last).toBeDefined();
	if (!last) throw new Error("expected submitted result");
	expect(last.type).toBe("submit");
	expect(last.notes).toBe("e2e notes");
	expect(last.findings.length).toBeGreaterThanOrEqual(1);
});