repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/diff-review/__tests__/server-html.test.ts
RawFile outline
- DIFF_TEXT line 5
- mockDiff line 15
- mockState line 46
- server HTML routes line 48
- server line 49
- baseUrl line 50
- beforeAll line 52
- afterAll line 62
- GET / returns HTML containing diff content line 64
- POST /finding stores a draft and returns updated findings HTML line 74
- GET / reflects stored findings after POST line 103
- POST /api/heartbeat still works line 111
- POST /finding/delete removes a finding line 124
- POST /submit form calls onSubmit line 138
- GET / includes finding dialog and theme toggle line 182
- GET / renders with carried findings line 191
- GET /search returns HTML results line 225
- POST /api/cancel still works line 236
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: [] };
describe("server HTML routes", () => {
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("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 /finding stores a draft and returns updated findings HTML", async () => {
const res = await fetch(`${baseUrl}finding`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Cookie: "diff_review_token=tst-tok",
},
body: new URLSearchParams({
file: "hello.ts",
side: "additions",
startLine: "1",
endLine: "1",
severity: "medium",
category: "bug",
comment: "test finding",
}),
});
if (res.status !== 200) {
const body = await res.text();
console.log("POST /finding error:", res.status, body);
throw new Error(`POST /finding failed with ${res.status}: ${body}`);
}
expect(res.headers.get("content-type")).toContain("text/html");
const html = await res.text();
expect(html).toContain("test finding");
expect(html).toContain("medium");
expect(html).toContain("bug");
});
it("GET / reflects stored findings after POST", async () => {
const res = await fetch(`${baseUrl}?token=tst-tok`, {
headers: { Cookie: "diff_review_token=tst-tok" },
});
const html = await res.text();
expect(html).toContain("test finding");
});
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 });
});
it("POST /finding/delete removes a finding", async () => {
const res = await fetch(`${baseUrl}finding/delete`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Cookie: "diff_review_token=tst-tok",
},
body: new URLSearchParams({ index: "0" }),
});
expect(res.status).toBe(200);
const html = await res.text();
expect(html).toContain("No findings yet.");
});
it("POST /submit form calls onSubmit", async () => {
let submitted = false;
const submitServer = await startReviewServer({
diff: mockDiff,
state: mockState,
round: 1,
token: "sub-tok",
onSubmit: () => {
submitted = true;
},
});
const url = submitServer.url.replace(/\?.*$/, "");
// add a finding first
await fetch(`${url}finding`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Cookie: "diff_review_token=sub-tok",
},
body: new URLSearchParams({
file: "hello.ts",
side: "additions",
startLine: "1",
endLine: "1",
severity: "high",
category: "bug",
comment: "submit test",
}),
});
const res = await fetch(`${url}submit`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Cookie: "diff_review_token=sub-tok",
},
body: new URLSearchParams({ notes: "some notes" }),
});
expect(res.status).toBe(200);
expect(submitted).toBe(true);
const html = await res.text();
expect(html).toContain("Review sent to Pi");
submitServer.close("command");
});
it("GET / includes finding dialog and theme toggle", async () => {
const res = await fetch(`${baseUrl}?token=tst-tok`);
const html = await res.text();
expect(html).toContain('id="finding-dialog"');
expect(html).toContain('id="finding-form"');
expect(html).toContain("data-line");
expect(html).toContain('id="theme-toggle"');
});
it("GET / renders with carried findings", async () => {
const carriedServer = await startReviewServer({
diff: mockDiff,
state: {
round: 1,
findings: [
{
id: "f-1",
round: 1,
file: "hello.ts",
side: "additions",
startLine: 1,
endLine: 1,
category: "bug",
severity: "high",
comment: "carried",
status: "open",
anchor: { before: "", selected: "", after: "" },
},
],
rounds: [],
},
round: 1,
token: "carry-tk",
});
const url = carriedServer.url.replace(/\?.*$/, "");
const res = await fetch(`${url}?token=carry-tk`);
expect(res.status).toBe(200);
const html = await res.text();
// annotations rendered by @pierre/diffs will have data-line-annotation or similar
expect(html).toContain("hello.ts");
carriedServer.close("command");
});
it("GET /search returns HTML results", async () => {
const res = await fetch(`${baseUrl}search?q=export`, {
headers: { Cookie: "diff_review_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("export");
});
it("POST /api/cancel still works", async () => {
const res = await fetch(`${baseUrl}api/cancel`, {
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 });
});
});