feat(M3): Shared-source generator for agent variants
This commit is contained in:
@@ -0,0 +1,350 @@
|
||||
/**
|
||||
* Unit tests for generate-skills.mjs — RED phase of TDD.
|
||||
*
|
||||
* Tests cover:
|
||||
* - detectFileType: classification of files by extension
|
||||
* - applyHeader: insertion per file-type-aware policy
|
||||
* - makePackageJsonContent: unique name + private:true
|
||||
* - getGeneratedRoots: returns the canonical generated-root list
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
|
||||
import crypto from "node:crypto";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const SCRIPTS_DIR = path.resolve(__dirname, "..");
|
||||
|
||||
const {
|
||||
detectFileType,
|
||||
applyHeader,
|
||||
makePackageJsonContent,
|
||||
getGeneratedRoots,
|
||||
buildManifest,
|
||||
} = await import(`${SCRIPTS_DIR}/generate-skills.mjs`);
|
||||
|
||||
// ── detectFileType ────────────────────────────────────────────────────────
|
||||
|
||||
test("detectFileType: .md files → markdown", () => {
|
||||
assert.equal(detectFileType("SKILL.md"), "markdown");
|
||||
assert.equal(detectFileType("templates/milestone-plan.md"), "markdown");
|
||||
assert.equal(detectFileType("README.md"), "markdown");
|
||||
});
|
||||
|
||||
test("detectFileType: .sh files → shell", () => {
|
||||
assert.equal(detectFileType("run-review.sh"), "shell");
|
||||
assert.equal(detectFileType("scripts/install.sh"), "shell");
|
||||
});
|
||||
|
||||
test("detectFileType: .ts and .d.ts files → ts", () => {
|
||||
assert.equal(detectFileType("src/cli.ts"), "ts");
|
||||
assert.equal(detectFileType("turndown-plugin-gfm.d.ts"), "ts");
|
||||
assert.equal(detectFileType("auth.ts"), "ts");
|
||||
});
|
||||
|
||||
test("detectFileType: .js files → js", () => {
|
||||
assert.equal(detectFileType("check-install.js"), "js");
|
||||
assert.equal(detectFileType("extract.js"), "js");
|
||||
});
|
||||
|
||||
test("detectFileType: .json files → json", () => {
|
||||
assert.equal(detectFileType("package.json"), "json");
|
||||
assert.equal(detectFileType("tsconfig.json"), "json");
|
||||
});
|
||||
|
||||
test("detectFileType: .yaml and .yml files → yaml", () => {
|
||||
assert.equal(detectFileType("pnpm-lock.yaml"), "yaml");
|
||||
assert.equal(detectFileType("other.yml"), "yaml");
|
||||
});
|
||||
|
||||
test("detectFileType: .jsonc files → jsonc", () => {
|
||||
assert.equal(detectFileType(".markdownlint.jsonc"), "jsonc");
|
||||
});
|
||||
|
||||
test("detectFileType: unknown extension → unknown", () => {
|
||||
assert.equal(detectFileType("Makefile"), "unknown");
|
||||
assert.equal(detectFileType("somefile"), "unknown");
|
||||
});
|
||||
|
||||
// ── applyHeader ───────────────────────────────────────────────────────────
|
||||
|
||||
test("applyHeader: markdown with YAML front matter inserts HTML comment after closing ---", () => {
|
||||
const content = "---\nname: create-plan\n---\n\n# Create Plan\n\nBody.\n";
|
||||
const result = applyHeader(content, "markdown", "skills/create-plan/_source/claude-code/SKILL.md");
|
||||
|
||||
// Front matter block preserved verbatim at start
|
||||
assert.ok(result.startsWith("---\nname: create-plan\n---\n"), "front matter at start");
|
||||
|
||||
// HTML comment present
|
||||
assert.ok(result.includes("<!-- ⚠️"), "HTML comment present");
|
||||
|
||||
// Comment comes after front matter closer and before the title
|
||||
const commentIdx = result.indexOf("<!-- ⚠️");
|
||||
const titleIdx = result.indexOf("# Create Plan");
|
||||
assert.ok(commentIdx !== -1 && titleIdx !== -1, "both positions found");
|
||||
assert.ok(commentIdx < titleIdx, "comment before title");
|
||||
|
||||
// Comment must NOT appear before the first ---
|
||||
assert.ok(commentIdx > 3, "comment not before front matter");
|
||||
});
|
||||
|
||||
test("applyHeader: markdown without front matter inserts HTML comment at top", () => {
|
||||
const content = "# Heading\n\nContent\n";
|
||||
const result = applyHeader(content, "markdown", "skills/create-plan/_source/claude-code/templates/plan.md");
|
||||
assert.ok(result.startsWith("<!-- ⚠️"), "comment at very top");
|
||||
assert.ok(result.includes("# Heading"), "original content preserved");
|
||||
});
|
||||
|
||||
test("applyHeader: shell with shebang inserts # comment after shebang", () => {
|
||||
const content = "#!/usr/bin/env bash\nset -euo pipefail\necho hello\n";
|
||||
const result = applyHeader(content, "shell", "skills/reviewer-runtime/run-review.sh");
|
||||
|
||||
assert.ok(result.startsWith("#!/usr/bin/env bash\n"), "shebang preserved at top");
|
||||
assert.ok(result.includes("# ⚠️"), "hash comment present");
|
||||
|
||||
const shebangEnd = result.indexOf("\n") + 1;
|
||||
const commentStart = result.indexOf("# ⚠️");
|
||||
assert.ok(commentStart === shebangEnd, "comment immediately after shebang line");
|
||||
});
|
||||
|
||||
test("applyHeader: ts file inserts // comment at top", () => {
|
||||
const content = "import path from 'path';\nexport const x = 1;\n";
|
||||
const result = applyHeader(content, "ts", "skills/atlassian/shared/scripts/src/cli.ts");
|
||||
assert.ok(result.startsWith("// ⚠️"), "// comment at top");
|
||||
assert.ok(result.includes("import path from 'path';"), "original content preserved");
|
||||
});
|
||||
|
||||
test("applyHeader: ts file with shebang inserts // comment after shebang", () => {
|
||||
const content = "#!/usr/bin/env npx tsx\nimport foo from 'foo';\n";
|
||||
const result = applyHeader(content, "ts", "auth.ts");
|
||||
assert.ok(result.startsWith("#!/usr/bin/env npx tsx\n"), "shebang preserved at top");
|
||||
assert.ok(result.includes("// ⚠️"), "// comment present");
|
||||
const shebangEnd = result.indexOf("\n") + 1;
|
||||
const commentStart = result.indexOf("// ⚠️");
|
||||
assert.ok(commentStart === shebangEnd, "// comment immediately after shebang");
|
||||
});
|
||||
|
||||
test("applyHeader: js file with shebang inserts // comment after shebang", () => {
|
||||
const content = "#!/usr/bin/env node\nconst x = 1;\n";
|
||||
const result = applyHeader(content, "js", "check-install.js");
|
||||
assert.ok(result.startsWith("#!/usr/bin/env node\n"), "shebang preserved at top");
|
||||
assert.ok(result.includes("// ⚠️"), "// comment present");
|
||||
});
|
||||
|
||||
test("applyHeader: js file inserts // comment at top", () => {
|
||||
const content = "const x = require('x');\n";
|
||||
const result = applyHeader(content, "js", "check-install.js");
|
||||
assert.ok(result.startsWith("// ⚠️"), "// comment at top");
|
||||
});
|
||||
|
||||
test("applyHeader: yaml file inserts # comment at top", () => {
|
||||
const content = "lockfileVersion: '9.0'\n\npackages:\n";
|
||||
const result = applyHeader(content, "yaml", "pnpm-lock.yaml");
|
||||
assert.ok(result.startsWith("# ⚠️"), "# comment at top");
|
||||
});
|
||||
|
||||
test("applyHeader: jsonc file inserts // comment at top", () => {
|
||||
const content = "// existing comment\n{\n \"key\": true\n}\n";
|
||||
const result = applyHeader(content, "jsonc", ".markdownlint.jsonc");
|
||||
assert.ok(result.startsWith("// ⚠️"), "// comment at top");
|
||||
});
|
||||
|
||||
test("applyHeader: json file returns content unchanged (no header)", () => {
|
||||
const content = '{\n "name": "test"\n}\n';
|
||||
const result = applyHeader(content, "json", "package.json");
|
||||
assert.equal(result, content, "JSON file must not be modified");
|
||||
});
|
||||
|
||||
test("applyHeader: unknown type returns content unchanged", () => {
|
||||
const content = "raw content\n";
|
||||
const result = applyHeader(content, "unknown", "Makefile");
|
||||
assert.equal(result, content);
|
||||
});
|
||||
|
||||
test("applyHeader: header contains canonical source hint", () => {
|
||||
const hint = "skills/create-plan/_source/pi/SKILL.md";
|
||||
const content = "---\nname: create-plan\n---\n\n# Title\n";
|
||||
const result = applyHeader(content, "markdown", hint);
|
||||
assert.ok(result.includes(hint), "canonical hint appears in header");
|
||||
});
|
||||
|
||||
test("applyHeader: never inserts header before shebang", () => {
|
||||
const content = "#!/usr/bin/env bash\nset -euo pipefail\n";
|
||||
const result = applyHeader(content, "shell", "run.sh");
|
||||
assert.ok(result.startsWith("#!/"), "shebang still first");
|
||||
});
|
||||
|
||||
// ── makePackageJsonContent ────────────────────────────────────────────────
|
||||
|
||||
test("makePackageJsonContent: renames name to scoped unique form", () => {
|
||||
const src = { name: "atlassian-skill-scripts", version: "1.0.0" };
|
||||
const result = makePackageJsonContent(src, "atlassian", "claude-code");
|
||||
assert.equal(result.name, "@ai-coding-skills/atlassian-claude-code");
|
||||
});
|
||||
|
||||
test("makePackageJsonContent: adds private:true", () => {
|
||||
const src = { name: "web-automation-scripts", version: "1.0.0" };
|
||||
const result = makePackageJsonContent(src, "web-automation", "codex");
|
||||
assert.equal(result.private, true);
|
||||
});
|
||||
|
||||
test("makePackageJsonContent: preserves all other top-level fields", () => {
|
||||
const src = {
|
||||
name: "atlassian-skill-scripts",
|
||||
version: "1.0.0",
|
||||
type: "module",
|
||||
scripts: { typecheck: "tsc --noEmit" },
|
||||
dependencies: { commander: "^13.1.0" },
|
||||
devDependencies: { tsx: "^4.20.5" },
|
||||
packageManager: "pnpm@10.18.1",
|
||||
};
|
||||
const result = makePackageJsonContent(src, "atlassian", "cursor");
|
||||
assert.equal(result.version, "1.0.0");
|
||||
assert.equal(result.type, "module");
|
||||
assert.deepEqual(result.scripts, { typecheck: "tsc --noEmit" });
|
||||
assert.deepEqual(result.dependencies, { commander: "^13.1.0" });
|
||||
assert.equal(result.packageManager, "pnpm@10.18.1");
|
||||
});
|
||||
|
||||
test("makePackageJsonContent: pi-package mirror uses -pi agent suffix", () => {
|
||||
const src = { name: "atlassian-skill-scripts" };
|
||||
const result = makePackageJsonContent(src, "atlassian", "pi");
|
||||
assert.equal(result.name, "@ai-coding-skills/atlassian-pi");
|
||||
});
|
||||
|
||||
test("makePackageJsonContent: does not mutate the source object", () => {
|
||||
const src = { name: "original", version: "1.0.0" };
|
||||
makePackageJsonContent(src, "atlassian", "codex");
|
||||
assert.equal(src.name, "original", "source object not mutated");
|
||||
});
|
||||
|
||||
// ── getGeneratedRoots ─────────────────────────────────────────────────────
|
||||
|
||||
test("getGeneratedRoots: returns at least one root per agent per skills skill", () => {
|
||||
const roots = getGeneratedRoots();
|
||||
assert.ok(Array.isArray(roots), "returns array");
|
||||
assert.ok(roots.length > 0, "non-empty");
|
||||
|
||||
const agents = ["claude-code", "codex", "cursor", "opencode", "pi"];
|
||||
const skillsWithAgents = ["create-plan", "do-task", "implement-plan", "atlassian", "web-automation"];
|
||||
for (const skill of skillsWithAgents) {
|
||||
for (const agent of agents) {
|
||||
const expected = `skills/${skill}/${agent}`;
|
||||
assert.ok(roots.includes(expected), `missing generated root: ${expected}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test("getGeneratedRoots: includes pi-package mirrors for all skills", () => {
|
||||
const roots = getGeneratedRoots();
|
||||
const piPackageSkills = ["atlassian", "create-plan", "do-task", "implement-plan", "web-automation"];
|
||||
for (const skill of piPackageSkills) {
|
||||
const expected = `pi-package/skills/${skill}`;
|
||||
assert.ok(roots.includes(expected), `missing pi-package mirror root: ${expected}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("getGeneratedRoots: includes reviewer-runtime/pi", () => {
|
||||
const roots = getGeneratedRoots();
|
||||
assert.ok(roots.includes("skills/reviewer-runtime/pi"), "reviewer-runtime/pi missing");
|
||||
});
|
||||
|
||||
test("getGeneratedRoots: does not include _source or shared directories", () => {
|
||||
const roots = getGeneratedRoots();
|
||||
for (const r of roots) {
|
||||
assert.ok(!r.includes("_source"), `root should not contain _source: ${r}`);
|
||||
assert.ok(!r.endsWith("/shared"), `root should not be shared: ${r}`);
|
||||
assert.ok(!r.includes("shared/scripts"), `root should not contain shared/scripts: ${r}`);
|
||||
}
|
||||
});
|
||||
|
||||
// ── buildManifest ─────────────────────────────────────────────────────────
|
||||
|
||||
test("buildManifest: returns object with $schema and generator markers", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "manifest-test-"));
|
||||
try {
|
||||
await writeFile(path.join(dir, "SKILL.md"), "---\nname: test\n---\n# Test\n");
|
||||
await mkdir(path.join(dir, "templates"), { recursive: true });
|
||||
await writeFile(path.join(dir, "templates", "plan.md"), "# Plan\n");
|
||||
// Write .generated-manifest.json itself (should be excluded from listing)
|
||||
await writeFile(path.join(dir, ".generated-manifest.json"), "{}");
|
||||
|
||||
const manifest = await buildManifest(dir, "skills/test/claude-code");
|
||||
|
||||
assert.ok(manifest.$schema, "$schema field present");
|
||||
assert.ok(manifest.generator, "generator field present");
|
||||
assert.equal(manifest.generatedRoot, "skills/test/claude-code");
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("buildManifest: does not include .generated-manifest.json in files list", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "manifest-self-"));
|
||||
try {
|
||||
await writeFile(path.join(dir, "SKILL.md"), "# skill\n");
|
||||
await writeFile(path.join(dir, ".generated-manifest.json"), "{}");
|
||||
|
||||
const manifest = await buildManifest(dir, "skills/test/pi");
|
||||
|
||||
const paths = manifest.files.map((f) => f.path);
|
||||
assert.ok(!paths.includes(".generated-manifest.json"), "manifest must not list itself");
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("buildManifest: files list includes path, kind, mode, sha256", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "manifest-fields-"));
|
||||
try {
|
||||
await writeFile(path.join(dir, "SKILL.md"), "# skill\n");
|
||||
|
||||
const manifest = await buildManifest(dir, "skills/test/codex");
|
||||
|
||||
assert.equal(manifest.files.length, 1);
|
||||
const entry = manifest.files[0];
|
||||
assert.equal(entry.path, "SKILL.md");
|
||||
assert.equal(entry.kind, "file");
|
||||
assert.ok(typeof entry.mode === "string" && entry.mode.match(/^\d{3}$/), "mode is 3-digit octal string");
|
||||
assert.ok(typeof entry.sha256 === "string" && entry.sha256.length === 64, "sha256 is 64-char hex");
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("buildManifest: files are sorted by path", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "manifest-sorted-"));
|
||||
try {
|
||||
await mkdir(path.join(dir, "templates"), { recursive: true });
|
||||
await writeFile(path.join(dir, "SKILL.md"), "# skill\n");
|
||||
await writeFile(path.join(dir, "templates", "z.md"), "z\n");
|
||||
await writeFile(path.join(dir, "templates", "a.md"), "a\n");
|
||||
|
||||
const manifest = await buildManifest(dir, "skills/test/cursor");
|
||||
|
||||
const paths = manifest.files.map((f) => f.path);
|
||||
const sorted = [...paths].sort();
|
||||
assert.deepEqual(paths, sorted, "files must be sorted by path");
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("buildManifest: sha256 matches actual file content", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "manifest-hash-"));
|
||||
try {
|
||||
const content = "---\nname: test\n---\n# Title\n";
|
||||
await writeFile(path.join(dir, "SKILL.md"), content);
|
||||
|
||||
const manifest = await buildManifest(dir, "skills/test/opencode");
|
||||
|
||||
const expected = crypto.createHash("sha256").update(content, "utf8").digest("hex");
|
||||
assert.equal(manifest.files[0].sha256, expected, "sha256 matches file content");
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,348 @@
|
||||
/**
|
||||
* Unit tests for verify-generated.mjs — RED phase of TDD.
|
||||
*
|
||||
* Key contract from acceptance criteria:
|
||||
* - A stray file under skills/<skill>/_source/ or skills/<skill>/shared/
|
||||
* does NOT cause verify:generated to flag it as stale.
|
||||
* - A stray file under skills/<skill>/<agent>/ (other than
|
||||
* .generated-manifest.json) DOES cause verify:generated to flag it.
|
||||
*/
|
||||
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
import test from "node:test";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const SCRIPTS_DIR = path.resolve(__dirname, "..");
|
||||
|
||||
const { verifyGenerated } = await import(`${SCRIPTS_DIR}/verify-generated.mjs`);
|
||||
|
||||
// ── Stray-file detection boundary tests ──────────────────────────────────
|
||||
|
||||
test("verifyGenerated: stray file in _source/ is NOT flagged as stale", async () => {
|
||||
// This uses the real repo's canonical source - if stray file under _source
|
||||
// is added, verify-generated should not complain about it.
|
||||
|
||||
// We test this by verifying the function signature accepts a repoRoot and
|
||||
// generatedRoots override, and that the verifier only walks declared roots.
|
||||
// (Full integration test against the real repo runs in pnpm run verify:generated)
|
||||
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-stray-source-"));
|
||||
try {
|
||||
// Create a fake skill structure with _source/ and a generated root
|
||||
const skillName = "test-skill";
|
||||
const agentName = "claude-code";
|
||||
|
||||
const sourceDir = path.join(dir, "skills", skillName, "_source", agentName);
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
const sharedDir = path.join(dir, "skills", skillName, "shared");
|
||||
|
||||
await mkdir(sourceDir, { recursive: true });
|
||||
await mkdir(sharedDir, { recursive: true });
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const skillContent = "---\nname: test-skill\n---\n\n# Test Skill\n";
|
||||
await writeFile(path.join(sourceDir, "SKILL.md"), skillContent);
|
||||
|
||||
// Add a STRAY file in _source/ — this should NOT trigger stale detection
|
||||
await writeFile(path.join(sourceDir, "STRAY.md"), "stray content");
|
||||
|
||||
// Add a STRAY file in shared/ — this should NOT trigger stale detection
|
||||
await writeFile(path.join(sharedDir, "shared-stray.txt"), "shared stray");
|
||||
|
||||
// The generated root is EMPTY (no manifest, no files) — but we're testing
|
||||
// that _source/ and shared/ stray files don't appear in stale detection.
|
||||
// We can test this indirectly: verifyGenerated with no declared roots for
|
||||
// this dir returns no stale errors about _source/ or shared/.
|
||||
const result = await verifyGenerated(dir, {
|
||||
// Override generated roots to be empty for this minimal test
|
||||
generatedRootsOverride: [],
|
||||
});
|
||||
|
||||
// No stale errors from _source/ or shared/
|
||||
const sourceErrors = result.errors.filter(
|
||||
(e) => e.includes("_source") || e.includes("shared"),
|
||||
);
|
||||
assert.equal(sourceErrors.length, 0, `unexpected stale errors from _source/shared: ${JSON.stringify(sourceErrors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("verifyGenerated: stray file in agent dir IS flagged as stale", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-stray-agent-"));
|
||||
try {
|
||||
const skillName = "test-skill";
|
||||
const agentName = "claude-code";
|
||||
|
||||
const sourceDir = path.join(dir, "skills", skillName, "_source", agentName);
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
|
||||
await mkdir(sourceDir, { recursive: true });
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const skillContent = "---\nname: test-skill\n---\n\n# Test Skill\n";
|
||||
await writeFile(path.join(sourceDir, "SKILL.md"), skillContent);
|
||||
|
||||
// Generate the agent dir with proper content + manifest
|
||||
const headerLine =
|
||||
`<!-- ⚠️ GENERATED FILE – do not edit directly. ` +
|
||||
`Edit the canonical source in skills/${skillName}/_source/${agentName}/SKILL.md ` +
|
||||
`and run \`pnpm run sync:pi\`. -->`;
|
||||
const generatedContent = `---\nname: test-skill\n---\n\n${headerLine}\n\n# Test Skill\n`;
|
||||
await writeFile(path.join(agentDir, "SKILL.md"), generatedContent);
|
||||
|
||||
// Add a STRAY file in the agent dir — this SHOULD be flagged
|
||||
await writeFile(path.join(agentDir, "STRAY.md"), "stray content");
|
||||
|
||||
// Write a manifest that does NOT include STRAY.md
|
||||
const { buildManifest } = await import(`${SCRIPTS_DIR}/generate-skills.mjs`);
|
||||
const manifest = await buildManifest(agentDir, `skills/${skillName}/${agentName}`);
|
||||
// Remove STRAY.md from manifest (simulate pre-stray-add manifest)
|
||||
manifest.files = manifest.files.filter((f) => f.path !== "STRAY.md");
|
||||
await writeFile(
|
||||
path.join(agentDir, ".generated-manifest.json"),
|
||||
JSON.stringify(manifest, null, 2) + "\n",
|
||||
);
|
||||
|
||||
const result = await verifyGenerated(dir, {
|
||||
generatedRootsOverride: [`skills/${skillName}/${agentName}`],
|
||||
});
|
||||
|
||||
assert.equal(result.ok, false, "should fail when stray file present");
|
||||
const strayError = result.errors.some((e) => e.includes("STRAY.md"));
|
||||
assert.ok(strayError, `STRAY.md should appear in errors: ${JSON.stringify(result.errors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("verifyGenerated: .generated-manifest.json is excluded from stale-file detection", async () => {
|
||||
// Even though .generated-manifest.json is in the generated root, it should
|
||||
// not be considered a "stale file" just because it's not in the files list
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-manifest-self-"));
|
||||
try {
|
||||
const skillName = "test-skill";
|
||||
const agentName = "pi";
|
||||
|
||||
const sourceDir = path.join(dir, "skills", skillName, "_source", agentName);
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
|
||||
await mkdir(sourceDir, { recursive: true });
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const skillContent = "---\nname: test-skill\n---\n\n# Test Skill\n";
|
||||
await writeFile(path.join(sourceDir, "SKILL.md"), skillContent);
|
||||
|
||||
const headerLine =
|
||||
`<!-- ⚠️ GENERATED FILE – do not edit directly. ` +
|
||||
`Edit the canonical source in skills/${skillName}/_source/${agentName}/SKILL.md ` +
|
||||
`and run \`pnpm run sync:pi\`. -->`;
|
||||
const generatedContent = `---\nname: test-skill\n---\n\n${headerLine}\n\n# Test Skill\n`;
|
||||
await writeFile(path.join(agentDir, "SKILL.md"), generatedContent);
|
||||
|
||||
// Write manifest (will include SKILL.md, not itself)
|
||||
const { buildManifest } = await import(`${SCRIPTS_DIR}/generate-skills.mjs`);
|
||||
const manifest = await buildManifest(agentDir, `skills/${skillName}/${agentName}`);
|
||||
await writeFile(
|
||||
path.join(agentDir, ".generated-manifest.json"),
|
||||
JSON.stringify(manifest, null, 2) + "\n",
|
||||
);
|
||||
|
||||
const result = await verifyGenerated(dir, {
|
||||
generatedRootsOverride: [`skills/${skillName}/${agentName}`],
|
||||
});
|
||||
|
||||
// Should pass — .generated-manifest.json is excluded from stale detection
|
||||
const manifestErrors = result.errors.filter((e) => e.includes(".generated-manifest.json"));
|
||||
assert.equal(manifestErrors.length, 0, `manifest file should not appear as stale: ${JSON.stringify(manifestErrors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("verifyGenerated: missing file from manifest is flagged as deleted", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-missing-file-"));
|
||||
try {
|
||||
const skillName = "test-skill";
|
||||
const agentName = "cursor";
|
||||
|
||||
const sourceDir = path.join(dir, "skills", skillName, "_source", agentName);
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
|
||||
await mkdir(sourceDir, { recursive: true });
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const skillContent = "---\nname: test-skill\n---\n\n# Test Skill\n";
|
||||
await writeFile(path.join(sourceDir, "SKILL.md"), skillContent);
|
||||
await writeFile(path.join(agentDir, "SKILL.md"), "generated content\n");
|
||||
|
||||
// Manifest claims templates/plan.md exists, but the file doesn't
|
||||
const manifest = {
|
||||
$schema: "https://ai-coding-skills.dev/schemas/generated-manifest/v1.json",
|
||||
generator: "scripts/generate-skills.mjs",
|
||||
generatedRoot: `skills/${skillName}/${agentName}`,
|
||||
files: [
|
||||
{ path: "SKILL.md", kind: "file", mode: "644", sha256: "aaa" },
|
||||
{ path: "templates/plan.md", kind: "file", mode: "644", sha256: "bbb" },
|
||||
],
|
||||
};
|
||||
await writeFile(
|
||||
path.join(agentDir, ".generated-manifest.json"),
|
||||
JSON.stringify(manifest, null, 2) + "\n",
|
||||
);
|
||||
|
||||
const result = await verifyGenerated(dir, {
|
||||
generatedRootsOverride: [`skills/${skillName}/${agentName}`],
|
||||
});
|
||||
|
||||
assert.equal(result.ok, false, "should fail on missing file");
|
||||
const missingError = result.errors.some((e) => e.includes("templates/plan.md"));
|
||||
assert.ok(missingError, `missing file should appear in errors: ${JSON.stringify(result.errors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("verifyGenerated: content mismatch is flagged", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-content-mismatch-"));
|
||||
try {
|
||||
const skillName = "test-skill";
|
||||
const agentName = "opencode";
|
||||
|
||||
const sourceDir = path.join(dir, "skills", skillName, "_source", agentName);
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
|
||||
await mkdir(sourceDir, { recursive: true });
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
await writeFile(path.join(sourceDir, "SKILL.md"), "---\nname: test-skill\n---\n\n# Original\n");
|
||||
// Agent dir has DIFFERENT content than what manifest says
|
||||
await writeFile(path.join(agentDir, "SKILL.md"), "---\nname: test-skill\n---\n\n# Modified!\n");
|
||||
|
||||
const manifest = {
|
||||
$schema: "https://ai-coding-skills.dev/schemas/generated-manifest/v1.json",
|
||||
generator: "scripts/generate-skills.mjs",
|
||||
generatedRoot: `skills/${skillName}/${agentName}`,
|
||||
files: [
|
||||
{
|
||||
path: "SKILL.md",
|
||||
kind: "file",
|
||||
mode: "644",
|
||||
// SHA of the ORIGINAL content (not what's on disk)
|
||||
sha256: "0000000000000000000000000000000000000000000000000000000000000000",
|
||||
},
|
||||
],
|
||||
};
|
||||
await writeFile(
|
||||
path.join(agentDir, ".generated-manifest.json"),
|
||||
JSON.stringify(manifest, null, 2) + "\n",
|
||||
);
|
||||
|
||||
const result = await verifyGenerated(dir, {
|
||||
generatedRootsOverride: [`skills/${skillName}/${agentName}`],
|
||||
});
|
||||
|
||||
assert.equal(result.ok, false, "should fail on content mismatch");
|
||||
const mismatchError = result.errors.some((e) => e.includes("SKILL.md"));
|
||||
assert.ok(mismatchError, `content mismatch should appear in errors: ${JSON.stringify(result.errors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("verifyGenerated: manifest entry with wrong sha256 is flagged even when paths match", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-manifest-sha-"));
|
||||
try {
|
||||
const skillName = "test-skill";
|
||||
const agentName = "codex";
|
||||
|
||||
const sourceDir = path.join(dir, "skills", skillName, "_source", agentName);
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
|
||||
await mkdir(sourceDir, { recursive: true });
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const content = "---\nname: test-skill\n---\n\n# Test Skill\n";
|
||||
await writeFile(path.join(sourceDir, "SKILL.md"), content);
|
||||
await writeFile(path.join(agentDir, "SKILL.md"), content);
|
||||
|
||||
// Manifest has correct path but deliberately wrong sha256 (simulates corrupted metadata)
|
||||
const manifest = {
|
||||
$schema: "https://ai-coding-skills.dev/schemas/generated-manifest/v1.json",
|
||||
generator: "scripts/generate-skills.mjs",
|
||||
generatedRoot: `skills/${skillName}/${agentName}`,
|
||||
files: [
|
||||
{
|
||||
path: "SKILL.md",
|
||||
kind: "file",
|
||||
mode: "644",
|
||||
sha256: "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef",
|
||||
},
|
||||
],
|
||||
};
|
||||
await writeFile(
|
||||
path.join(agentDir, ".generated-manifest.json"),
|
||||
JSON.stringify(manifest, null, 2) + "\n",
|
||||
);
|
||||
|
||||
const result = await verifyGenerated(dir, {
|
||||
generatedRootsOverride: [`skills/${skillName}/${agentName}`],
|
||||
});
|
||||
|
||||
// Even though file paths match, the sha256 mismatch in the manifest should be detected
|
||||
assert.equal(result.ok, false, "should fail on sha256 mismatch in manifest");
|
||||
const sha256Error = result.errors.some(
|
||||
(e) => e.includes("sha256") || e.includes("SKILL.md"),
|
||||
);
|
||||
assert.ok(sha256Error, `sha256 mismatch should appear in errors: ${JSON.stringify(result.errors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("verifyGenerated: manifest entry with wrong mode is flagged even when paths match", async () => {
|
||||
const dir = await mkdtemp(path.join(tmpdir(), "vg-manifest-mode-"));
|
||||
try {
|
||||
const skillName = "test-skill";
|
||||
const agentName = "cursor";
|
||||
|
||||
const agentDir = path.join(dir, "skills", skillName, agentName);
|
||||
await mkdir(agentDir, { recursive: true });
|
||||
|
||||
const content = "# Test Skill\n";
|
||||
await writeFile(path.join(agentDir, "SKILL.md"), content);
|
||||
|
||||
// Build a correct manifest first (with real sha256)
|
||||
const { buildManifest } = await import(`${SCRIPTS_DIR}/generate-skills.mjs`);
|
||||
const correctManifest = await buildManifest(agentDir, `skills/${skillName}/${agentName}`);
|
||||
|
||||
// Tamper: change the mode field for the SKILL.md entry
|
||||
const tamperedManifest = {
|
||||
...correctManifest,
|
||||
files: correctManifest.files.map((f) =>
|
||||
f.path === "SKILL.md" ? { ...f, mode: "777" } : f,
|
||||
),
|
||||
};
|
||||
await writeFile(
|
||||
path.join(agentDir, ".generated-manifest.json"),
|
||||
JSON.stringify(tamperedManifest, null, 2) + "\n",
|
||||
);
|
||||
|
||||
const result = await verifyGenerated(dir, {
|
||||
generatedRootsOverride: [`skills/${skillName}/${agentName}`],
|
||||
});
|
||||
|
||||
// The mode mismatch in the manifest should be detected by diffManifests
|
||||
assert.equal(result.ok, false, "should fail on mode mismatch in manifest");
|
||||
const modeError = result.errors.some(
|
||||
(e) => e.includes("mode") || e.includes("SKILL.md"),
|
||||
);
|
||||
assert.ok(modeError, `mode mismatch should appear in errors: ${JSON.stringify(result.errors)}`);
|
||||
} finally {
|
||||
await rm(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user