import { mkdir, mkdtemp, realpath, rm, symlink, writeFile, } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { fingerprintSuite } from "../lib/checker.ts"; import { getValidatedSuiteItems, parseSourceSkill, resolveLoadedSkill, validateTrustedSuite, } from "../lib/contracts.ts"; const temporaryPaths: string[] = []; afterEach(async () => { await Promise.all( temporaryPaths .splice(0) .map((path) => rm(path, { force: true, recursive: true })), ); }); async function temporaryDirectory(): Promise { const path = await mkdtemp(join(tmpdir(), "overkill-contracts-")); temporaryPaths.push(path); return path; } async function writeSuite(root: string): Promise { const suite = join(root, "suite"); for (const split of ["train", "val", "test"]) { await mkdir(join(suite, split), { recursive: true }); await writeFile( join(suite, split, "items.json"), JSON.stringify([{ id: `${split}-1`, prompt: `${split} task` }]), ); } await writeFile( join(suite, "check.mjs"), "process.stdout.write(JSON.stringify({ score: 1, passed: true }));", ); return suite; } describe("source skill contract", () => { it("requires exact canonical loaded-skill provenance and project trust", async () => { const root = await temporaryDirectory(); const skill = join(root, "SKILL.md"); const alias = join(root, "alias.md"); await writeFile(skill, "---\nname: demo\ndescription: Demo\n---\nBody\n"); await symlink(skill, alias); const source = await resolveLoadedSkill( alias, [ { name: "skill:demo", source: "skill", sourceInfo: { path: skill, scope: "project" }, }, ], true, ); expect(source.path).toBe(await realpath(skill)); expect( await resolveLoadedSkill( "SKILL.md", [ { name: "skill:demo", source: "skill", sourceInfo: { path: skill, scope: "user" }, }, ], true, root, ), ).toMatchObject({ path: await realpath(skill) }); await expect( resolveLoadedSkill( skill, [ { name: "skill:demo", source: "skill", sourceInfo: { path: skill, scope: "project" }, }, ], false, ), ).rejects.toThrow("trusted"); await expect( resolveLoadedSkill( skill, [ { name: "skill:demo", source: "skill", sourceInfo: { path: skill, scope: "user" }, }, { name: "skill:other", source: "skill", sourceInfo: { path: skill, scope: "user" }, }, ], true, ), ).rejects.toThrow("exactly one"); }); it("freezes exact valid YAML frontmatter and rejects invalid source contracts", () => { const source = parseSourceSkill( Buffer.from("---\nname: demo-skill\ndescription: Demo\n---\n# Body\n"), ); expect(source.frontmatter).toBe( "---\nname: demo-skill\ndescription: Demo\n---\n", ); expect(source.body).toBe("# Body\n"); expect(() => parseSourceSkill( Buffer.from("---\nname: valid-name\ndescription: \n---\n"), ), ).toThrow("description"); expect(() => parseSourceSkill(Buffer.from("no frontmatter"))).toThrow( "frontmatter", ); expect(() => parseSourceSkill(Buffer.alloc(256 * 1024 + 1))).toThrow( "256 KiB", ); }); }); describe("trusted explicit suite contract", () => { it("requires canonical trusted-project containment and validates all three item files", async () => { const root = await temporaryDirectory(); const suite = await writeSuite(root); const result = await validateTrustedSuite(suite, root, true); expect(result.splitCounts).toEqual({ test: 1, train: 1, val: 1 }); expect(result.fingerprint).toBe(await fingerprintSuite(suite)); expect((await validateTrustedSuite("suite", root, true)).suitePath).toBe( await realpath(suite), ); expect(result.checkerPath).toBe(await realpath(join(suite, "check.mjs"))); await expect(validateTrustedSuite(suite, root, false)).rejects.toThrow( "trusted", ); const outside = join(root, "other"); await mkdir(outside); await expect(validateTrustedSuite(suite, outside, true)).rejects.toThrow( "contained", ); }); it("retains checker-owned records in code-point ID order and rejects extra layout entries", async () => { const root = await temporaryDirectory(); const suite = await writeSuite(root); await writeFile( join(suite, "train", "items.json"), JSON.stringify([ { id: "\u{10000}", prompt: "astral", extra: true }, { id: "\uE000", prompt: "bmp" }, ]), ); const validated = await validateTrustedSuite(suite, root, true); expect(getValidatedSuiteItems(validated, "train")).toEqual([ { id: "\uE000", prompt: "bmp" }, { id: "\u{10000}", prompt: "astral", extra: true }, ]); await writeFile(join(suite, "unexpected.txt"), "not allowed"); await expect(validateTrustedSuite(suite, root, true)).rejects.toThrow( "exactly", ); }); it("rejects missing files, malformed items, duplicate IDs, and oversized prompts", async () => { const root = await temporaryDirectory(); const suite = await writeSuite(root); await writeFile( join(suite, "val", "items.json"), JSON.stringify([{ id: "train-1", prompt: "duplicate" }]), ); await expect(validateTrustedSuite(suite, root, true)).rejects.toThrow( "duplicate", ); await writeFile( join(suite, "val", "items.json"), JSON.stringify([{ id: "val-1", prompt: "x".repeat(16 * 1024 + 1) }]), ); await expect(validateTrustedSuite(suite, root, true)).rejects.toThrow( "16 KiB", ); }); });