125 lines
5.5 KiB
JavaScript
125 lines
5.5 KiB
JavaScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { classifyPath, CODE_IMPORT_BASELINE, collectSnapshots, countNonEmptyLines, parseArgs, renderCsv, renderSvg } from "./loc-history.mjs";
|
|
|
|
const PRE_BASELINE = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
|
const SAME_DAY_EARLY = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
|
const SAME_DAY_LATEST = "cccccccccccccccccccccccccccccccccccccccc";
|
|
const TIP = "dddddddddddddddddddddddddddddddddddddddd";
|
|
|
|
function fixtureLog() {
|
|
return [
|
|
`${PRE_BASELINE}\t\t2026-06-03\t2026-06-03T09:00:00+02:00\tpre-fork root`,
|
|
`${CODE_IMPORT_BASELINE}\t${PRE_BASELINE}\t2026-06-04\t2026-06-04T10:00:00+02:00\tfork import`,
|
|
`${SAME_DAY_EARLY}\t${CODE_IMPORT_BASELINE}\t2026-06-04\t2026-06-04T12:00:00+02:00\tfirst same-day change`,
|
|
`${SAME_DAY_LATEST}\t${SAME_DAY_EARLY}\t2026-06-04\t2026-06-04T08:00:00+02:00\tlatest same-day state`,
|
|
`${TIP}\t${SAME_DAY_LATEST}\t2026-06-05\t2026-06-05T09:00:00+02:00\tnext-day state`,
|
|
].join("\n");
|
|
}
|
|
|
|
function snapshotRows() {
|
|
return [
|
|
{
|
|
date: "2026-06-04",
|
|
timestamp: "2026-06-04T10:00:00+02:00",
|
|
sha: CODE_IMPORT_BASELINE,
|
|
anchor: "code-import",
|
|
subject: 'Import, "fork"',
|
|
loc: { product: 100, test: 20, migration: 5 },
|
|
files: { product: 10, test: 2, migration: 1 },
|
|
total: 125,
|
|
},
|
|
{
|
|
date: "2026-06-05",
|
|
timestamp: "2026-06-05T10:00:00+02:00",
|
|
sha: TIP,
|
|
anchor: "",
|
|
subject: "Add feature",
|
|
loc: { product: 150, test: 30, migration: 10 },
|
|
files: { product: 15, test: 3, migration: 2 },
|
|
total: 190,
|
|
},
|
|
];
|
|
}
|
|
|
|
describe("loc-history", () => {
|
|
it("pins the fork baseline and samples the last first-parent commit per active day", () => {
|
|
const calls = [];
|
|
const runGit = vi.fn((args) => {
|
|
calls.push(args);
|
|
if (args[0] === "rev-parse" && args[2] === "HEAD^{commit}") return `${TIP}\n`;
|
|
if (args[0] === "rev-parse" && args[2] === `${CODE_IMPORT_BASELINE}^{commit}`) return `${CODE_IMPORT_BASELINE}\n`;
|
|
if (args[0] === "merge-base") return "";
|
|
if (args[0] === "log") return fixtureLog();
|
|
throw new Error(`Unexpected git call: ${args.join(" ")}`);
|
|
});
|
|
|
|
const snapshots = collectSnapshots("HEAD", { runGit });
|
|
|
|
expect(CODE_IMPORT_BASELINE).toBe("1631a5d84e079f71ef377a7ca4f59bfad4d770b4");
|
|
expect(snapshots.map((snapshot) => snapshot.sha)).toEqual([CODE_IMPORT_BASELINE, SAME_DAY_LATEST, TIP]);
|
|
expect(snapshots[0]).toEqual(expect.objectContaining({ anchor: "code-import", subject: "fork import" }));
|
|
const logCall = calls.find((args) => args[0] === "log");
|
|
expect(logCall).toEqual(expect.arrayContaining(["--first-parent", "--reverse"]));
|
|
});
|
|
|
|
it("fails closed when the pinned baseline is not an ancestor", () => {
|
|
const runGit = vi.fn((args) => {
|
|
if (args[0] === "rev-parse" && args[2] === "HEAD^{commit}") return `${TIP}\n`;
|
|
if (args[0] === "rev-parse") return `${CODE_IMPORT_BASELINE}\n`;
|
|
if (args[0] === "merge-base") throw new Error("not an ancestor");
|
|
throw new Error(`Unexpected git call: ${args.join(" ")}`);
|
|
});
|
|
|
|
expect(() => collectSnapshots("HEAD", { runGit })).toThrow(`LOC baseline ${CODE_IMPORT_BASELINE} is not an ancestor of HEAD`);
|
|
expect(runGit.mock.calls.some(([args]) => args[0] === "log")).toBe(false);
|
|
});
|
|
|
|
it("classifies source, tests, migrations, and excluded paths", () => {
|
|
expect(classifyPath("apps/web/app/routes/projects.tsx")).toBe("product");
|
|
expect(classifyPath("packages/ui-subframe/src/Button.tsx")).toBe("product");
|
|
expect(classifyPath("Dockerfile")).toBe("product");
|
|
expect(classifyPath("apps/web/app/routes/projects.test.tsx")).toBe("test");
|
|
expect(classifyPath("apps\\web\\e2e\\login.ts")).toBe("test");
|
|
expect(classifyPath("packages/ui-app/src/Button.stories.tsx")).toBe("test");
|
|
expect(classifyPath("prisma/migrations/202607_tests/migration.sql")).toBe("migration");
|
|
expect(classifyPath("docs/example.ts")).toBeNull();
|
|
expect(classifyPath(".planning/example.ts")).toBeNull();
|
|
expect(classifyPath("packages/db/generated/client.ts")).toBeNull();
|
|
expect(classifyPath("packages/db/src/contract.generated.ts")).toBeNull();
|
|
expect(classifyPath("apps/web/app/client.min.js")).toBeNull();
|
|
expect(classifyPath("apps/web/app/data.json")).toBeNull();
|
|
expect(classifyPath("apps/web/app/components/ui/icons/vendor.tsx")).toBeNull();
|
|
});
|
|
|
|
it("counts only non-empty physical lines", () => {
|
|
expect(countNonEmptyLines(Buffer.from("one\n\n \ntwo\n"))).toBe(2);
|
|
expect(countNonEmptyLines(Buffer.from([0, 1, 2]))).toBe(0);
|
|
});
|
|
|
|
it("parses a detached CI ref with a stable display label", () => {
|
|
expect(parseArgs(["--ref", "HEAD", "--label", "origin/main", "--output-dir", "/tmp/loc"])).toEqual(
|
|
expect.objectContaining({
|
|
baseline: CODE_IMPORT_BASELINE,
|
|
label: "origin/main",
|
|
outputDir: "/tmp/loc",
|
|
ref: "HEAD",
|
|
})
|
|
);
|
|
});
|
|
|
|
it("renders deterministic CSV and SVG without a fork-import jump annotation", () => {
|
|
const rows = snapshotRows();
|
|
const csv = renderCsv(rows, "origin/main");
|
|
const svg = renderSvg(rows, "origin/main");
|
|
|
|
expect(renderCsv(rows, "origin/main")).toBe(csv);
|
|
expect(renderSvg(rows, "origin/main")).toBe(svg);
|
|
expect(csv).toContain(`# resolved_tip=${TIP}`);
|
|
expect(csv).toContain(`"Import, ""fork"""`);
|
|
expect(svg).toContain("SEIT CODE-IMPORT");
|
|
expect(svg).toContain("+65 · +52 %");
|
|
expect(svg).not.toMatch(/GRÖSSTER SPRUNG|largest jump|\+490\.231 LOC/i);
|
|
});
|
|
});
|