import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync, } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { test } from "node:test"; import { fileURLToPath } from "node:url"; import { checkSkill } from "./check.mjs"; const root = fileURLToPath(new URL("../", import.meta.url)); const script = fileURLToPath(new URL("./check.mjs", import.meta.url)); function invoke(args, cwd = root) { const result = spawnSync(process.execPath, [script, ...args], { cwd, encoding: "utf8", timeout: 10_000, }); assert.ifError(result.error); assert.equal(result.signal, null); assert.equal(result.stderr, ""); return { status: result.status, report: JSON.parse(result.stdout) }; } function scriptedSkill(context, { manifest, tests = true, body = "" } = {}) { const temporary = mkdtempSync(join(tmpdir(), "writing-skills-check-")); context.after(() => rmSync(temporary, { recursive: true, force: true })); const directory = join(temporary, "fixture-skill"); mkdirSync(directory); writeFileSync( join(directory, "SKILL.md"), `---\nname: fixture-skill\ndescription: Test fixture\n---\n${body}\n`, ); writeFileSync(join(directory, "helper.mjs"), "export const value = 1;\n"); if (tests) writeFileSync(join(directory, "helper.test.mjs"), 'import "node:test";\n'); if (manifest !== undefined) writeFileSync(join(directory, "package.json"), manifest); return directory; } test("accepts repository-managed scripted skills without a local manifest", (context) => { const directory = scriptedSkill(context); const result = invoke([directory]); assert.equal(result.status, 0); assert.equal(result.report.ok, true); assert.deepEqual(result.report.issues, []); }); test("does not prescribe a package test runner", (context) => { for (const manifest of [ {}, { scripts: { test: "mise run test" } }, { scripts: { test: "python -m pytest" } }, ]) { const directory = scriptedSkill(context, { manifest: JSON.stringify(manifest) }); assert.deepEqual(checkSkill(directory).issues, []); } }); test("still rejects malformed or non-object package manifests", (context) => { for (const manifest of ["{", "null", "[]", '"text"']) { const directory = scriptedSkill(context, { manifest }); assert.deepEqual(checkSkill(directory).issues.map((issue) => issue.code), [ "package.json", ]); } }); test("still requires a test file without a local manifest", (context) => { const directory = scriptedSkill(context, { tests: false }); assert.deepEqual(checkSkill(directory).issues.map((issue) => issue.code), [ "tests.missing", ]); }); test("still validates frontmatter and links without a local manifest", (context) => { const directory = scriptedSkill(context, { body: "[Missing](missing.md)" }); writeFileSync( join(directory, "SKILL.md"), "---\nname: fixture-skill\nname: duplicate\ndescription: Test fixture\n---\n[Missing](missing.md)\n", ); assert.deepEqual(checkSkill(directory).issues.map((issue) => issue.code), [ "frontmatter.yaml", "link.missing", ]); }); test("writing-skills satisfies its own scripted-skill contract", () => { const result = checkSkill(root); assert.equal(result.ok, true, JSON.stringify(result.issues)); assert.deepEqual(result.issues, []); }); test("CLI returns a clean report from a different working directory", () => { const result = invoke([root], join(root, "scripts")); assert.equal(result.status, 0); assert.deepEqual(result.report, checkSkill(root)); }); test("CLI rejects missing and excess arguments with exit code 2", () => { for (const args of [[], [root, root]]) { const result = invoke(args); assert.equal(result.status, 2); assert.deepEqual(result.report, { error: "Usage: node scripts/check.mjs ", }); } }); test("CLI rejects a file instead of a skill directory", () => { const result = invoke([script]); assert.equal(result.status, 2); assert.deepEqual(result.report, { error: "Expected a skill directory" }); }); test("CLI reports a nonexistent target as a filesystem failure", () => { const missing = join(root, "__missing_check_fixture__"); assert.equal(existsSync(missing), false); const result = invoke([missing]); assert.equal(result.status, 2); assert.match(result.report.error, /ENOENT/); }); test("CLI reports a directory without SKILL.md as a filesystem failure", () => { const directory = join(root, "scripts"); assert.equal(existsSync(join(directory, "SKILL.md")), false); const result = invoke([directory]); assert.equal(result.status, 2); assert.match(result.report.error, /ENOENT/); assert.match(result.report.error, /SKILL\.md/); }); test("library rejects non-directory targets instead of returning a clean audit", () => { assert.throws(() => checkSkill(script), /Expected a skill directory/); });