Skip to content

Open-source, local-first AI agent for real work.

3.3 KB
Raw Blame
import { describe, expect, it } from "vitest";
import { buildReviewPrompt, excerptFromPatch } from "../src/git/reviewPrompt";
import type { TurnReview } from "../src/git/turnReview";

const PATCH = [
	"diff --git a/src/app.ts b/src/app.ts",
	"--- a/src/app.ts",
	"+++ b/src/app.ts",
	"@@ -10,4 +10,5 @@",
	" const a = 1;",
	"-const b = 2;",
	"+const b = 3;",
	"+const c = b * 2;",
	" export { a };",
	" // end",
].join("\n");

/** Echoes the key with its params, so assertions read the structure, not a language. */
const t = (key: string, params?: Record<string, string | number>): string =>
	params ? `${key}(${Object.entries(params).map(([k, v]) => `${k}=${v}`).join(",")})` : key;

function review(overrides: Partial<TurnReview> = {}): TurnReview {
	return {
		cwd: "/repo",
		root: "/repo",
		before: "a".repeat(40),
		after: "b".repeat(40),
		files: [
			{ path: "src/app.ts", code: "M", additions: 2, deletions: 1, binary: false },
			{ path: "README.md", code: "M", additions: 1, deletions: 0, binary: false },
		],
		additions: 3,
		deletions: 1,
		reverted: new Set(),
		comments: [],
		note: "",
		...overrides,
	};
}

describe("excerptFromPatch", () => {
	it("reads the new numbering for additions and skips removed lines", () => {
		expect(excerptFromPatch(PATCH, "additions", 11, 12)).toEqual(["const b = 3;", "const c = b * 2;"]);
		expect(excerptFromPatch(PATCH, "additions", 13, 10)).toEqual(["const a = 1;", "const b = 3;", "const c = b * 2;", "export { a };"]);
	});

	it("reads the old numbering for deletions and skips added lines", () => {
		expect(excerptFromPatch(PATCH, "deletions", 11, 11)).toEqual(["const b = 2;"]);
	});
});

describe("buildReviewPrompt", () => {
	it("returns nothing when there is no feedback", () => {
		expect(buildReviewPrompt(review({ note: "  " }), t)).toBeNull();
	});

	it("orders comments by file then line, quoting the lines each one is about", () => {
		const prompt = buildReviewPrompt(
			review({
				note: "Mostly good.",
				comments: [
					{ id: "c2", path: "README.md", side: "additions", start: 3, end: 3, body: "Typo here", excerpt: ["Hte"] },
					{ id: "c1", path: "src/app.ts", side: "additions", start: 12, end: 11, body: "Name this better", excerpt: ["const c = b * 2;"] },
					{ id: "c3", path: "src/app.ts", side: "deletions", start: 11, end: 11, body: "Why remove?", excerpt: ["const b = 2;"] },
				],
				reverted: new Set(["README.md"]),
			}),
			t,
		);
		expect(prompt).toBe(
			[
				"review.prompt.intro",
				"Mostly good.",
				"1. `src/app.ts` review.prompt.lines(start=11,end=12)\n```\nconst c = b * 2;\n```\nName this better",
				"2. `src/app.ts` review.prompt.line(line=11) review.prompt.removed\n```\nconst b = 2;\n```\nWhy remove?",
				"3. `README.md` review.prompt.line(line=3)\n```\nHte\n```\nTypo here",
				"review.prompt.reverted(files=`README.md`)",
			].join("\n\n"),
		);
	});

	it("keeps quoted backticks from closing the fence and trims long quotes", () => {
		const lines = ["```js", ...Array.from({ length: 20 }, (_, i) => `line ${i}`)];
		const prompt = buildReviewPrompt(
			review({ comments: [{ id: "c1", path: "src/app.ts", side: "additions", start: 1, end: 21, body: "x", excerpt: lines }] }),
			t,
		);
		expect(prompt).toContain("````\n```js\nline 0");
		expect(prompt).toContain("line 10\n…\n````");
		expect(prompt).not.toContain("line 11");
	});
});