import { spawnSync } from "node:child_process"; import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vite-plus/test"; const check = (html: string, files: Record = {}) => { const dir = mkdtempSync(join(tmpdir(), "comark-docs-links-")); try { mkdirSync(join(dir, "dist/public"), { recursive: true }); writeFileSync(join(dir, "package.json"), '{"type":"module"}'); for (const file of ["links.ts", "link-excludes.ts"]) copyFileSync(new URL(`../docs/${file}`, import.meta.url), join(dir, file)); writeFileSync(join(dir, "dist/public/index.html"), html); for (const [path, body] of Object.entries(files)) { const file = join(dir, "dist/public", path); mkdirSync(join(file, ".."), { recursive: true }); writeFileSync(file, body); } return spawnSync(process.execPath, [join(dir, "links.ts")], { encoding: "utf8" }); } finally { rmSync(dir, { recursive: true, force: true }); } }; describe("built docs links", () => { it("accepts the dynamic badge endpoint without a static file", () => { const result = check(` Badge `); expect(result.stderr).toBe(""); expect(result.status).toBe(0); expect(result.stdout).toContain("Checked the links of 1 pages."); }); it("still rejects missing pages and files, including names similar to the badge route", () => { const result = check(` Missing page Package `); expect(result.status).toBe(1); expect(result.stderr).toContain("9 broken links"); for (const path of [ "/missing", "/missing.svg", "/badge.svg/missing", "/other/badge.svg", "/package.json", "/doctor/xref/missing.png", "/guide/pond.png", "/pond-typo.png", "/pond.png/missing", ]) expect(result.stderr).toContain(`links to ${path}, which has no page or file.`); }); it("ignores include inputs and source text that are not browser links", () => { const result = check(':include{src="source.md"}', { "doctor/include/open-block-in-partial.html": '', "doctor/include/path-outside-root.html": '', "doctor/include/read-failed.html": '', "doctor/include/remote-file.html": '', "doctor/include/too-deep.html": '', "doctor/include/unclosed-region.html": '', "include.html": '', }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); }); it("excludes the deliberately broken links in Doctor's rendered examples", () => { const result = check("", { "doctor/broken-anchor.html": 'Method

Method

', "doctor/chips/invalid-avatar.html": '', "doctor/xref/inline-figure.html": '', "doctor/xref/missing-id.html": '', "doctor/xref/ref-content.html": '', "doctor/xref/unknown-label.html": '', }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); }); it("excludes the fictional screenshot in Doctor's annotate examples", () => { const result = check("", { "doctor/annotate/undefined-binding.html": '', "doctor/annotate/unknown-mark.html": '', "doctor/annotate/unknown-prop.html": '', "doctor/annotate/unsafe-bound-url.html": '', }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); }); it("still checks other Doctor fragments and similarly named example assets", () => { const result = check('Missing heading', { "doctor/broken-anchor.html": 'Missing heading', "doctor/chips/invalid-avatar.html": '', "doctor/xref/inline-figure.html": '', "guide/example.html": '', }); expect(result.status).toBe(1); expect(result.stderr).toContain("5 broken links"); expect(result.stderr).toContain('no heading or element with the id "methods"'); expect(result.stderr).toContain('no heading or element with the id "typo"'); for (const path of [ "/doctor/chips/octocat.png", "/doctor/xref/roof-typo.png", "/guide/roof.png", ]) expect(result.stderr).toContain(`links to ${path}, which has no page or file.`); }); it("resolves the docs app's flat page URLs and their fragments", () => { const result = check(`ZoomTabs`, { "zoom.html": '

Theme

', "tabs.html": '

Usage

', }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); expect(result.stdout).toContain("Checked the links of 3 pages."); }); it("checks fragments on flat pages and does not accept a directory as a page", () => { const result = check( `Missing headingMissing page`, { "zoom.html": "

Zoom

", "empty/child.html": "

Child

" }, ); expect(result.status).toBe(1); expect(result.stderr).toContain("2 broken links"); expect(result.stderr).toContain('no heading or element with the id "missing"'); expect(result.stderr).toContain("links to /empty, which has no page or file."); }); it("resolves relative links from flat page routes", () => { const result = check("", { "guide/editor.html": 'Theme', "guide/theming.html": '

Theme

', }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); }); it("keeps the Nuxt checker's exclusions for deliberate examples", () => { const result = check(` ExampleExample `); expect(result.stderr).toBe(""); expect(result.status).toBe(0); }); it("reads the playground's fragment as its state, and checks the gallery's as ids", () => { const result = check( `PlaygroundPreset GalleryMissing`, { "play/index.html": "", "play/gallery/index.html": '
' }, ); expect(result.status).toBe(1); expect(result.stderr).toContain("1 broken link"); expect(result.stderr).toContain('no heading or element with the id "missing"'); }); it("reports a trailing slash and a missing fragment", () => { const result = check( `SlashPage Missing headingExisting heading

Present

`, { "zoom.html": "" }, ); expect(result.status).toBe(1); expect(result.stderr).toContain("2 broken links"); expect(result.stderr).toContain( "which Cloudflare redirects to /zoom. Drop the trailing slash.", ); expect(result.stderr).toContain('no heading or element with the id "missing"'); }); });