repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/diff-review/__tests__/search.test.ts
RawFile outline
import { describe, expect, it } from "vitest";
import { buildSearchIndex, searchCode } from "../search.js";
const DIFF = `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}!\`;
+}
diff --git a/world.ts b/world.ts
new file mode 100644
--- /dev/null
+++ b/world.ts
@@ -0,0 +1,2 @@
+export function world() {
+ return "world";
+}
`;
describe("search", () => {
it("builds index from diff text", () => {
const index = buildSearchIndex(DIFF);
expect(index.length).toBeGreaterThan(0);
expect(index.some((e) => e.file === "hello.ts")).toBe(true);
expect(index.some((e) => e.file === "world.ts")).toBe(true);
expect(index.some((e) => e.text.includes("export function"))).toBe(true);
});
it("finds literal matches", () => {
const index = buildSearchIndex(DIFF);
const hits = searchCode("world", index);
expect(hits.length).toBeGreaterThan(0);
expect(hits.some((h) => h.text.includes("world"))).toBe(true);
});
it("finds regex matches when wrapped in /", () => {
const index = buildSearchIndex(DIFF);
const hits = searchCode("/return.*world/", index);
expect(hits.length).toBe(1);
expect(hits[0].text).toContain("world");
});
it("returns empty for no match", () => {
const index = buildSearchIndex(DIFF);
const hits = searchCode("zzzzzzz", index);
expect(hits).toEqual([]);
});
it("caps at 500 results", () => {
const bigDiff = Array.from(
{ length: 600 },
(_, i) =>
`diff --git a/f${i}.ts b/f${i}.ts\n@@ -0,0 +1,1 @@\n+common word\n`,
).join("");
const index = buildSearchIndex(bigDiff);
const hits = searchCode("common", index);
expect(hits.length).toBe(500);
});
});