Fix discover, improve LLM heuristics, add integration tests
- discover.ts: Fix ignore package path handling for root '.' directories, add .pi-map.md to default ignore list - llm-extract.ts: Fix export regex to only match line-start exports and handle 'export async function', fix root package role to 'Project root' - init.ts: Minor cleanup - .gitignore: Add .pi-map.md - tests: 6 integration tests covering init, patch (small/large packages), validate (missing, orphaned, stale-signature detection) All 14 tests pass. TypeScript compiles clean.
This commit is contained in:
+16
-4
@@ -1,5 +1,9 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { renderPackageMap, parsePackageMap, type PackageMapData } from "../src/format.js";
|
||||
import {
|
||||
renderPackageMap,
|
||||
parsePackageMap,
|
||||
type PackageMapData,
|
||||
} from "../src/format.js";
|
||||
|
||||
const sampleData: PackageMapData = {
|
||||
path: "pkg/auth",
|
||||
@@ -34,7 +38,9 @@ describe("format", () => {
|
||||
expect(output).toContain("# pkg/auth");
|
||||
expect(output).toContain("## role");
|
||||
expect(output).toContain("## files");
|
||||
expect(output).toContain("- tokens.ts | JWT gen/val | exp: issueToken, verifyToken, refreshToken | dep: crypto/hmac, db/sessions");
|
||||
expect(output).toContain(
|
||||
"- tokens.ts | JWT gen/val | exp: issueToken, verifyToken, refreshToken | dep: crypto/hmac, db/sessions",
|
||||
);
|
||||
expect(output).toContain("## arch");
|
||||
expect(output).toContain("## dirty");
|
||||
expect(output).toContain("-");
|
||||
@@ -53,7 +59,11 @@ describe("format", () => {
|
||||
const tokens = parsed.files.find((f) => f.name === "tokens.ts");
|
||||
expect(tokens).toBeDefined();
|
||||
expect(tokens!.purpose).toBe("JWT gen/val");
|
||||
expect(tokens!.exports).toEqual(["issueToken", "verifyToken", "refreshToken"]);
|
||||
expect(tokens!.exports).toEqual([
|
||||
"issueToken",
|
||||
"verifyToken",
|
||||
"refreshToken",
|
||||
]);
|
||||
expect(tokens!.deps).toEqual(["crypto/hmac", "db/sessions"]);
|
||||
});
|
||||
|
||||
@@ -61,7 +71,9 @@ describe("format", () => {
|
||||
const data: PackageMapData = {
|
||||
path: "pkg/utils",
|
||||
role: "Utilities",
|
||||
files: [{ name: "helpers.ts", purpose: "Helpers", exports: [], deps: [] }],
|
||||
files: [
|
||||
{ name: "helpers.ts", purpose: "Helpers", exports: [], deps: [] },
|
||||
],
|
||||
arch: "Shared helpers",
|
||||
dirty: "2024-01-01: patched",
|
||||
};
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
||||
import { mkdtempSync, writeFileSync, mkdirSync, rmSync, readFileSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { tmpdir } from "os";
|
||||
import { initProject } from "../src/init.js";
|
||||
import { patchFile } from "../src/patch.js";
|
||||
import { validateMaps } from "../src/validate.js";
|
||||
|
||||
describe("integration", () => {
|
||||
let dir: string;
|
||||
|
||||
beforeEach(() => {
|
||||
dir = mkdtempSync(join(tmpdir(), "pi-map-int-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
rmSync(dir, { recursive: true });
|
||||
});
|
||||
|
||||
it("init creates .pi-map.md files", async () => {
|
||||
mkdirSync(join(dir, "src"));
|
||||
writeFileSync(
|
||||
join(dir, "src", "index.ts"),
|
||||
`export function foo() {}\n`,
|
||||
);
|
||||
await initProject(dir);
|
||||
|
||||
const map = readFileSync(join(dir, "src", ".pi-map.md"), "utf8");
|
||||
expect(map).toContain("# src");
|
||||
expect(map).toContain("foo");
|
||||
});
|
||||
|
||||
it("patch updates a file entry", async () => {
|
||||
mkdirSync(join(dir, "src"));
|
||||
writeFileSync(
|
||||
join(dir, "src", "index.ts"),
|
||||
`export function foo() {}\n`,
|
||||
);
|
||||
writeFileSync(
|
||||
join(dir, "src", "utils.ts"),
|
||||
`export const bar = 1;\n`,
|
||||
);
|
||||
await initProject(dir);
|
||||
|
||||
// Modify a file
|
||||
writeFileSync(
|
||||
join(dir, "src", "index.ts"),
|
||||
`export function foo() {}\nexport function baz() {}\n`,
|
||||
);
|
||||
await patchFile(join(dir, "src", "index.ts"));
|
||||
|
||||
const map = readFileSync(join(dir, "src", ".pi-map.md"), "utf8");
|
||||
expect(map).toContain("baz");
|
||||
});
|
||||
|
||||
it("patch adds dirty marker for large packages", async () => {
|
||||
mkdirSync(join(dir, "src"));
|
||||
// Create 11 files so it's a "large" package
|
||||
for (let i = 0; i < 11; i++) {
|
||||
writeFileSync(
|
||||
join(dir, "src", `file${i}.ts`),
|
||||
`export const x${i} = ${i};\n`,
|
||||
);
|
||||
}
|
||||
await initProject(dir);
|
||||
|
||||
// Modify a file
|
||||
writeFileSync(
|
||||
join(dir, "src", "file0.ts"),
|
||||
`export const x0 = 0;\nexport const y = 99;\n`,
|
||||
);
|
||||
await patchFile(join(dir, "src", "file0.ts"));
|
||||
|
||||
const map = readFileSync(join(dir, "src", ".pi-map.md"), "utf8");
|
||||
expect(map).toContain("y");
|
||||
expect(map).toContain("dirty");
|
||||
expect(map).toContain("patched");
|
||||
});
|
||||
|
||||
it("validate detects new files", async () => {
|
||||
mkdirSync(join(dir, "src"));
|
||||
writeFileSync(join(dir, "src", "a.ts"), `export const a = 1;\n`);
|
||||
await initProject(dir);
|
||||
|
||||
// Add new file
|
||||
writeFileSync(join(dir, "src", "b.ts"), `export const b = 2;\n`);
|
||||
|
||||
const result = await validateMaps(dir);
|
||||
expect(result.clean).toBe(false);
|
||||
expect(result.discrepancies.some((d) => d.type === "missing")).toBe(true);
|
||||
});
|
||||
|
||||
it("validate detects deleted files", async () => {
|
||||
mkdirSync(join(dir, "src"));
|
||||
writeFileSync(join(dir, "src", "a.ts"), `export const a = 1;\n`);
|
||||
writeFileSync(join(dir, "src", "b.ts"), `export const b = 2;\n`);
|
||||
await initProject(dir);
|
||||
|
||||
// Delete a file
|
||||
rmSync(join(dir, "src", "b.ts"));
|
||||
|
||||
const result = await validateMaps(dir);
|
||||
expect(result.clean).toBe(false);
|
||||
expect(result.discrepancies.some((d) => d.type === "orphaned")).toBe(true);
|
||||
});
|
||||
|
||||
it("validate detects changed signatures", async () => {
|
||||
mkdirSync(join(dir, "src"));
|
||||
writeFileSync(join(dir, "src", "a.ts"), `export const a = 1;\n`);
|
||||
await initProject(dir);
|
||||
|
||||
// Change exports
|
||||
writeFileSync(join(dir, "src", "a.ts"), `export const a = 1;\nexport const c = 3;\n`);
|
||||
|
||||
const result = await validateMaps(dir);
|
||||
expect(result.clean).toBe(false);
|
||||
expect(result.discrepancies.some((d) => d.type === "stale-signature")).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user