Task #13: lock in the fingerprint/relation logic behind SkillGuard's identical/modified/new version detection with automated tests. What was added - Set up Vitest in artifacts/api-server (dev dep + `test` script + vitest.config.ts using the "workspace" resolve condition so @workspace/* resolve to source). - Unit tests (no DB): - src/lib/skillFingerprint.test.ts — hashText/hashBytes stability & agreement, computeFingerprint stable + order-independent + sensitive to content/path/add/remove, jaccard overlap/symmetry/empty handling. - src/lib/lineDiff.test.ts — lineSimilarity ratios (identical, single-edit, disjoint, symmetric, CRLF), lineDiff context/add/remove with line numbers and the 2000-line cap. - DB-backed tests (use the existing DATABASE_URL): - src/routes/relation.test.ts — computeRelation: identical content under a different name -> "identical" + check-counter (countFingerprint) increments; one-line edit to a single-file skill -> "modified" with sensible similarity; unrelated skill -> "new". Also direct computeContentSimilarity cases. Fixtures use randomized content to avoid collisions with shared dev data and are cleaned up afterEach. - src/routes/compare.test.ts — e2e GET /api/scans/:id/compare/:otherId via a live server: asserts unchanged/modified/added/removed statuses, sorted file order, the line diff for the modified file, null diffs elsewhere, and 404 for missing scans. Production code change - Exported computeRelation, computeContentSimilarity, countFingerprint from src/routes/scans.ts so the relation logic can be unit-tested. No behavior change. Verification - `pnpm --filter @workspace/api-server run test` -> 34 tests, 4 files, all pass. - `pnpm --filter @workspace/api-server run typecheck` passes (rebuilt stale lib/db declarations via `pnpm run typecheck:libs`). - Production build unaffected: esbuild only bundles from src/index.ts, so *.test.ts files are not included. Replit-Task-Id: e9ae5e24-1480-4a09-8436-1718c535573a
98 lines
2.8 KiB
TypeScript
98 lines
2.8 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import {
|
|
hashText,
|
|
hashBytes,
|
|
computeFingerprint,
|
|
jaccard,
|
|
} from "./skillFingerprint";
|
|
|
|
describe("hashText / hashBytes", () => {
|
|
it("produces a stable sha256 hex digest", () => {
|
|
const h = hashText("hello world");
|
|
expect(h).toBe(
|
|
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
|
|
);
|
|
expect(h).toMatch(/^[0-9a-f]{64}$/);
|
|
});
|
|
|
|
it("agrees between hashText and hashBytes for the same content", () => {
|
|
const text = "some skill content\nwith two lines";
|
|
expect(hashText(text)).toBe(hashBytes(Buffer.from(text, "utf-8")));
|
|
});
|
|
|
|
it("changes when the content changes", () => {
|
|
expect(hashText("a")).not.toBe(hashText("b"));
|
|
});
|
|
});
|
|
|
|
describe("computeFingerprint", () => {
|
|
const files = [
|
|
{ path: "SKILL.md", hash: "aaa" },
|
|
{ path: "scripts/run.sh", hash: "bbb" },
|
|
{ path: "data.json", hash: "ccc" },
|
|
];
|
|
|
|
it("is stable across calls with the same input", () => {
|
|
expect(computeFingerprint(files)).toBe(computeFingerprint(files));
|
|
});
|
|
|
|
it("is independent of the order files are supplied in", () => {
|
|
const reordered = [files[2], files[0], files[1]];
|
|
expect(computeFingerprint(reordered)).toBe(computeFingerprint(files));
|
|
});
|
|
|
|
it("changes when a file's content hash changes", () => {
|
|
const changed = [
|
|
files[0],
|
|
{ path: "scripts/run.sh", hash: "DIFFERENT" },
|
|
files[2],
|
|
];
|
|
expect(computeFingerprint(changed)).not.toBe(computeFingerprint(files));
|
|
});
|
|
|
|
it("changes when a file's path changes", () => {
|
|
const renamed = [
|
|
{ path: "SKILL.md", hash: "aaa" },
|
|
{ path: "scripts/start.sh", hash: "bbb" },
|
|
files[2],
|
|
];
|
|
expect(computeFingerprint(renamed)).not.toBe(computeFingerprint(files));
|
|
});
|
|
|
|
it("changes when a file is added or removed", () => {
|
|
const fewer = [files[0], files[1]];
|
|
expect(computeFingerprint(fewer)).not.toBe(computeFingerprint(files));
|
|
});
|
|
|
|
it("returns a 64-char hex digest", () => {
|
|
expect(computeFingerprint(files)).toMatch(/^[0-9a-f]{64}$/);
|
|
});
|
|
});
|
|
|
|
describe("jaccard", () => {
|
|
it("is 1 for identical non-empty sets", () => {
|
|
expect(jaccard(new Set(["a", "b"]), new Set(["a", "b"]))).toBe(1);
|
|
});
|
|
|
|
it("is 0 for disjoint sets", () => {
|
|
expect(jaccard(new Set(["a"]), new Set(["b"]))).toBe(0);
|
|
});
|
|
|
|
it("computes intersection over union for partial overlap", () => {
|
|
// intersection {a} = 1, union {a,b,c} = 3
|
|
expect(jaccard(new Set(["a", "b"]), new Set(["a", "c"]))).toBeCloseTo(
|
|
1 / 3,
|
|
10,
|
|
);
|
|
});
|
|
|
|
it("is symmetric", () => {
|
|
const a = new Set(["a", "b", "c"]);
|
|
const b = new Set(["b", "c", "d"]);
|
|
expect(jaccard(a, b)).toBe(jaccard(b, a));
|
|
});
|
|
|
|
it("treats two empty sets as 0 (no overlap to report)", () => {
|
|
expect(jaccard(new Set(), new Set())).toBe(0);
|
|
});
|
|
});
|