|
| 1 | +import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import os from "node:os"; |
| 5 | +import { getDocsFiles, getDocsDir } from "./docs.js"; |
| 6 | + |
| 7 | +let tmpDir: string; |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "docs-test-")); |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + fs.rmSync(tmpDir, { recursive: true, force: true }); |
| 15 | +}); |
| 16 | + |
| 17 | +describe("getDocsFiles", () => { |
| 18 | + it("returns empty array for empty directory", () => { |
| 19 | + expect(getDocsFiles(tmpDir)).toEqual([]); |
| 20 | + }); |
| 21 | + |
| 22 | + it("returns a DocEntry for a markdown file", () => { |
| 23 | + fs.writeFileSync(path.join(tmpDir, "guide.md"), "# Guide Title\nContent."); |
| 24 | + const entries = getDocsFiles(tmpDir); |
| 25 | + expect(entries).toHaveLength(1); |
| 26 | + expect(entries[0]).toMatchObject({ |
| 27 | + slug: "guide", |
| 28 | + title: "Guide Title", |
| 29 | + }); |
| 30 | + expect(entries[0]!.filePath).toBe(path.join(tmpDir, "guide.md")); |
| 31 | + }); |
| 32 | + |
| 33 | + it("uses filename as title when no H1 heading present", () => { |
| 34 | + fs.writeFileSync(path.join(tmpDir, "notes.md"), "no heading here"); |
| 35 | + const entries = getDocsFiles(tmpDir); |
| 36 | + expect(entries[0]!.title).toBe("notes"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("ignores non-markdown files", () => { |
| 40 | + fs.writeFileSync(path.join(tmpDir, "readme.txt"), "text file"); |
| 41 | + fs.writeFileSync(path.join(tmpDir, "data.json"), "{}"); |
| 42 | + expect(getDocsFiles(tmpDir)).toHaveLength(0); |
| 43 | + }); |
| 44 | + |
| 45 | + it("recurses into subdirectories", () => { |
| 46 | + const sub = path.join(tmpDir, "sub"); |
| 47 | + fs.mkdirSync(sub); |
| 48 | + fs.writeFileSync(path.join(sub, "nested.md"), "# Nested"); |
| 49 | + const entries = getDocsFiles(tmpDir); |
| 50 | + expect(entries).toHaveLength(1); |
| 51 | + expect(entries[0]!.slug).toBe(path.join("sub", "nested")); |
| 52 | + expect(entries[0]!.title).toBe("Nested"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("collects files from both root and subdirectory", () => { |
| 56 | + fs.writeFileSync(path.join(tmpDir, "root.md"), "# Root"); |
| 57 | + const sub = path.join(tmpDir, "sub"); |
| 58 | + fs.mkdirSync(sub); |
| 59 | + fs.writeFileSync(path.join(sub, "child.md"), "# Child"); |
| 60 | + const entries = getDocsFiles(tmpDir); |
| 61 | + expect(entries).toHaveLength(2); |
| 62 | + const slugs = entries.map((e) => e.slug); |
| 63 | + expect(slugs).toContain("root"); |
| 64 | + expect(slugs).toContain(path.join("sub", "child")); |
| 65 | + }); |
| 66 | + |
| 67 | + it("picks first H1 when multiple headings exist", () => { |
| 68 | + fs.writeFileSync( |
| 69 | + path.join(tmpDir, "multi.md"), |
| 70 | + "# First Heading\n## Second\n# Third" |
| 71 | + ); |
| 72 | + const entries = getDocsFiles(tmpDir); |
| 73 | + expect(entries[0]!.title).toBe("First Heading"); |
| 74 | + }); |
| 75 | +}); |
| 76 | + |
| 77 | +describe("getDocsDir", () => { |
| 78 | + it("returns an absolute path ending with 'docs'", () => { |
| 79 | + const dir = getDocsDir(); |
| 80 | + expect(path.isAbsolute(dir)).toBe(true); |
| 81 | + expect(dir.endsWith("docs")).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it("points to cwd/docs", () => { |
| 85 | + const expected = path.resolve(process.cwd(), "docs"); |
| 86 | + expect(getDocsDir()).toBe(expected); |
| 87 | + }); |
| 88 | +}); |
0 commit comments