58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import { describe, expect, test } from "bun:test"
|
|
import { readFileSync } from "node:fs"
|
|
import { join } from "node:path"
|
|
import { validateReadme } from "./check-readme.js"
|
|
|
|
function lines(...rows: string[]): string {
|
|
return rows.join("\n")
|
|
}
|
|
|
|
describe("validateReadme", () => {
|
|
test("accepts a clean README", () => {
|
|
const content = lines("# My Project", "", "## Installation", "", "Run `npm install`.", "", "## Usage", "", "Done.")
|
|
expect(validateReadme(content)).toEqual([])
|
|
})
|
|
|
|
test("all issues are warnings, never errors", () => {
|
|
const content = lines(
|
|
"# Title",
|
|
"",
|
|
"## Section",
|
|
"",
|
|
"#### Skipped level",
|
|
"",
|
|
"Trailing spaces. ",
|
|
"",
|
|
"# Duplicate Title"
|
|
)
|
|
const issues = validateReadme(content)
|
|
expect(issues.length).toBeGreaterThan(0)
|
|
expect(issues.every((issue) => issue.severity === "warn")).toBe(true)
|
|
})
|
|
|
|
test("warns on heading hierarchy jumps", () => {
|
|
const content = lines("# Title", "", "## Section", "", "#### Skipped")
|
|
const issues = validateReadme(content)
|
|
expect(issues.some((issue) => issue.rule === "heading-hierarchy")).toBe(true)
|
|
})
|
|
|
|
test("warns on trailing whitespace outside fences", () => {
|
|
const content = lines("# Title", "", "## Section", "Text with spaces. ", "```", "code ", "```")
|
|
const issues = validateReadme(content)
|
|
expect(issues.some((issue) => issue.rule === "trailing-whitespace" && issue.line === 4)).toBe(true)
|
|
expect(issues.some((issue) => issue.rule === "trailing-whitespace" && issue.line === 6)).toBe(false)
|
|
})
|
|
|
|
test("warns on unclosed code fences", () => {
|
|
const content = lines("# Title", "", "```bash", "echo hello")
|
|
const issues = validateReadme(content)
|
|
expect(issues.some((issue) => issue.rule === "unclosed-fence")).toBe(true)
|
|
})
|
|
|
|
test("the committed root README.md passes its own checks", () => {
|
|
const root = join(import.meta.dirname, "../..")
|
|
const content = readFileSync(join(root, "README.md"), "utf8")
|
|
expect(validateReadme(content)).toEqual([])
|
|
})
|
|
})
|