The root keeps README.md; every subdirectory README must be <dir>.readme.md with leading dots stripped. Add a blocking governance checker with 12 tests, wire it into package scripts, CI, and pre-commit, and rename the existing subdirectory READMEs.
107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
|
|
import { tmpdir } from "node:os"
|
|
import { join } from "node:path"
|
|
import {
|
|
checkReadmeNaming,
|
|
expectedReadmeName,
|
|
isReadmeLike,
|
|
validateReadmePaths
|
|
} from "./check-readme-naming.js"
|
|
|
|
describe("isReadmeLike", () => {
|
|
test("matches README variants", () => {
|
|
for (const name of ["README.md", "readme.md", "README.txt", "src.readme.md"]) {
|
|
expect(isReadmeLike(name)).toBe(true)
|
|
}
|
|
})
|
|
|
|
test("ignores unrelated files", () => {
|
|
for (const name of ["index.md", "readme-assets.ts", "README_GUIDE.md"]) {
|
|
expect(isReadmeLike(name)).toBe(false)
|
|
}
|
|
})
|
|
})
|
|
|
|
describe("expectedReadmeName", () => {
|
|
test("prefixes with the directory name", () => {
|
|
expect(expectedReadmeName("src")).toBe("src.readme.md")
|
|
expect(expectedReadmeName("host-infra")).toBe("host-infra.readme.md")
|
|
})
|
|
|
|
test("strips leading dots from dot-directories", () => {
|
|
expect(expectedReadmeName(".agents")).toBe("agents.readme.md")
|
|
})
|
|
})
|
|
|
|
describe("validateReadmePaths", () => {
|
|
test("accepts the conventional layout", () => {
|
|
const issues = validateReadmePaths([
|
|
"README.md",
|
|
"src/src.readme.md",
|
|
".agents/agents.readme.md",
|
|
"host-infra/host-infra.readme.md",
|
|
"src/domains/seal/reports/reports.readme.md"
|
|
])
|
|
expect(issues).toEqual([])
|
|
})
|
|
|
|
test("rejects a plain README.md in a subdirectory", () => {
|
|
const issues = validateReadmePaths(["README.md", "src/README.md"])
|
|
expect(issues).toHaveLength(1)
|
|
expect(issues[0].file).toBe("src/README.md")
|
|
expect(issues[0].rule).toBe("subdir-readme-name")
|
|
})
|
|
|
|
test("rejects a subdirectory README with the wrong prefix", () => {
|
|
const issues = validateReadmePaths(["README.md", "src/docs.readme.md"])
|
|
expect(issues).toHaveLength(1)
|
|
expect(issues[0].rule).toBe("subdir-readme-name")
|
|
expect(issues[0].message).toContain("src.readme.md")
|
|
})
|
|
|
|
test("rejects a lowercase root readme", () => {
|
|
const issues = validateReadmePaths(["readme.md"])
|
|
expect(issues.some((issue) => issue.rule === "root-readme-name")).toBe(true)
|
|
expect(issues.some((issue) => issue.rule === "missing-root-readme")).toBe(true)
|
|
})
|
|
|
|
test("requires a root README", () => {
|
|
const issues = validateReadmePaths(["src/src.readme.md"])
|
|
expect(issues.some((issue) => issue.rule === "missing-root-readme")).toBe(true)
|
|
})
|
|
|
|
test("accepts nested directories with matching prefixes", () => {
|
|
const issues = validateReadmePaths([
|
|
"README.md",
|
|
"src/domains/seal/reports/reports.readme.md",
|
|
"src/domains/seal/reports/templates/templates.readme.md"
|
|
])
|
|
expect(issues).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe("checkReadmeNaming", () => {
|
|
test("skips the local root reports directory but checks nested ones", () => {
|
|
const root = mkdtempSync(join(tmpdir(), "readme-naming-"))
|
|
try {
|
|
writeFileSync(join(root, "README.md"), "# Root")
|
|
mkdirSync(join(root, "reports"))
|
|
writeFileSync(join(root, "reports", "README.md"), "# Local-only")
|
|
mkdirSync(join(root, "src"))
|
|
writeFileSync(join(root, "src", "README.md"), "# Wrong")
|
|
|
|
const issues = checkReadmeNaming(root)
|
|
expect(issues).toHaveLength(1)
|
|
expect(issues[0].file).toBe("src/README.md")
|
|
} finally {
|
|
rmSync(root, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
test("the committed repository passes the naming rule", () => {
|
|
const root = join(import.meta.dirname, "../..")
|
|
expect(checkReadmeNaming(root)).toEqual([])
|
|
})
|
|
})
|