import { describe, it, expect } from "vitest"; import { compressPaths, compilePathEntries } from "../../extensions/compress/stages/paths.js"; const WS = "/Users/testuser/projects/my-long-workspace-name"; const HOME = "/Users/testuser"; describe("compilePathEntries", () => { it("sorts longer paths first to prevent partial replacements", () => { const entries = [ { path: HOME, sigil: "$HOME" }, { path: WS, sigil: "$WS" }, ]; const compiled = compilePathEntries(entries); expect(compiled[0].path).toBe(WS); // longer first expect(compiled[1].path).toBe(HOME); }); it("filters out paths shorter than 8 chars", () => { const entries = [ { path: "/short", sigil: "$S" }, { path: WS, sigil: "$WS" }, ]; const compiled = compilePathEntries(entries); expect(compiled.every((e) => e.path.length >= 8)).toBe(true); }); it("attaches pre-compiled regex", () => { const entries = [{ path: WS, sigil: "$WS" }]; const compiled = compilePathEntries(entries); expect(compiled[0].re).toBeInstanceOf(RegExp); }); }); describe("compressPaths", () => { it("replaces path when it meets min occurrence threshold", () => { // WS is 48 chars (>= 40), needs 2 occurrences const text = `${WS}/src/index.ts\n${WS}/src/other.ts`; const result = compressPaths(text, [{ path: WS, sigil: "$WS" }]); expect(result).toContain("$WS/src/index.ts"); expect(result).toContain("$WS/src/other.ts"); expect(result).toContain(`[$WS=${WS}]`); }); it("does not replace path below min occurrence threshold", () => { // Only 1 occurrence — below threshold of 2 const text = `See ${WS}/src/index.ts for details`; const result = compressPaths(text, [{ path: WS, sigil: "$WS" }]); expect(result).toBe(text); }); it("longer path replaced first, preventing partial home replacement", () => { const text = Array(3).fill(`${WS}/file.ts`).join("\n"); const result = compressPaths(text, [ { path: HOME, sigil: "$HOME" }, { path: WS, sigil: "$WS" }, ]); // $WS should replace the full workspace path, not leave $HOME in the middle expect(result).not.toContain("$HOME"); expect(result).toContain("$WS"); }); it("prepends legend when replacement occurs", () => { const text = Array(3).fill(`${WS}/file.ts`).join("\n"); const result = compressPaths(text, [{ path: WS, sigil: "$WS" }]); expect(result.startsWith(`[$WS=${WS}]\n`)).toBe(true); }); it("returns original text when no paths qualify", () => { const text = "no paths here at all"; const result = compressPaths(text, [{ path: WS, sigil: "$WS" }]); expect(result).toBe(text); }); it("uses pre-compiled entries for efficiency", () => { const text = Array(3).fill(`${WS}/f.ts`).join("\n"); const compiled = compilePathEntries([{ path: WS, sigil: "$WS" }]); const result = compressPaths(text, [], compiled); expect(result).toContain("$WS"); }); });