Files

126 lines
3.8 KiB
TypeScript

import { describe, expect, test } from "bun:test"
import { readdirSync, readFileSync } from "node:fs"
import { join } from "node:path"
import { validateSkillDoc } from "./check-skill-md.js"
const REFERENCE_FILES = ["references/alpha.md", "references/beta.md"]
function lines(...rows: string[]): string {
return rows.join("\n")
}
function cleanSkill(): string {
return lines(
"---",
"name: demo",
"---",
"",
"# Demo Skill",
"",
"## Quick Start",
"",
"Do something first.",
"",
"## 参考文档路由",
"",
"| Signal | Reference |",
"| --- | --- |",
"| first case | `references/alpha.md` |",
"| second case | `references/beta.md` |"
)
}
describe("validateSkillDoc", () => {
test("accepts a well-formed routing hub", () => {
expect(validateSkillDoc(cleanSkill(), REFERENCE_FILES)).toEqual([])
})
test("flags routing that points at a missing reference file", () => {
const content = lines(
"# Demo Skill",
"",
"## 参考文档路由",
"",
"Detailed guide: `references/ghost.md`."
)
const issues = validateSkillDoc(content, REFERENCE_FILES)
const missing = issues.filter((issue) => issue.rule === "skill-reference-exists")
expect(missing.length).toBe(1)
expect(missing[0]!.message).toContain("references/ghost.md")
})
test("flags reference files that exist on disk but are never routed", () => {
const content = lines(
"# Demo Skill",
"",
"## 参考文档路由",
"",
"Only this: `references/alpha.md`."
)
const issues = validateSkillDoc(content, REFERENCE_FILES)
const unrouted = issues.filter((issue) => issue.rule === "skill-reference-routed")
expect(unrouted.length).toBe(1)
expect(unrouted[0]!.message).toContain("references/beta.md")
})
test("requires the exact '## 参考文档路由' section", () => {
const content = lines(
"# Demo Skill",
"",
"## Routing",
"",
"`references/alpha.md` and `references/beta.md`"
)
const issues = validateSkillDoc(content, REFERENCE_FILES)
expect(issues.some((issue) => issue.rule === "skill-routing-section")).toBe(true)
})
test("flags any trailing H2 sections after '## 参考文档路由'", () => {
const content = lines(
"# Demo Skill",
"",
"## 参考文档路由",
"",
"`references/alpha.md` and `references/beta.md`",
"",
"## Trailing Section",
"",
"This should not be here."
)
const issues = validateSkillDoc(content, REFERENCE_FILES)
const terminal = issues.filter((issue) => issue.rule === "skill-routing-terminal")
expect(terminal.length).toBe(1)
expect(terminal[0]!.message).toContain("Trailing Section")
})
test("flags structure and hygiene problems", () => {
const content = lines(
"# Demo Skill",
"",
"## Section",
"",
"#### Skipped level",
"",
"Trailing spaces. ",
"",
"## 参考文档路由",
"",
"`references/alpha.md` and `references/beta.md`"
)
const issues = validateSkillDoc(content, REFERENCE_FILES)
expect(issues.some((issue) => issue.rule === "heading-hierarchy")).toBe(true)
expect(issues.some((issue) => issue.rule === "trailing-whitespace" && issue.line === 7)).toBe(true)
})
test("the committed skills/seal-use/SKILL.md passes its own charter", () => {
const root = join(import.meta.dirname, "../..")
const content = readFileSync(join(root, "skills", "seal-use", "SKILL.md"), "utf8")
const referenceFiles = readdirSync(join(root, "skills", "seal-use", "references"), {
withFileTypes: true
})
.filter((entry) => entry.isFile() && entry.name.endsWith(".md"))
.map((entry) => `references/${entry.name}`)
expect(validateSkillDoc(content, referenceFiles)).toEqual([])
})
})