|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 4 | +import fs from 'fs'; |
| 5 | +import os from 'os'; |
| 6 | +import path from 'path'; |
| 7 | +import { collectDocsFromSrc, lintDocs, collectAndLintDocs, type DocItem } from './collect-docs.js'; |
| 8 | + |
| 9 | +let tmp: string; |
| 10 | +let configPath: string; |
| 11 | +let docsDir: string; |
| 12 | + |
| 13 | +beforeEach(() => { |
| 14 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'os-docs-')); |
| 15 | + configPath = path.join(tmp, 'objectstack.config.ts'); |
| 16 | + fs.writeFileSync(configPath, '// stub'); |
| 17 | + docsDir = path.join(tmp, 'src', 'docs'); |
| 18 | + fs.mkdirSync(docsDir, { recursive: true }); |
| 19 | +}); |
| 20 | + |
| 21 | +afterEach(() => { |
| 22 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 23 | +}); |
| 24 | + |
| 25 | +const write = (name: string, content: string) => fs.writeFileSync(path.join(docsDir, name), content); |
| 26 | + |
| 27 | +describe('collectDocsFromSrc (ADR-0046 §3.2)', () => { |
| 28 | + it('compiles each flat .md file into a doc item (stem = name)', () => { |
| 29 | + write('crm_index.md', '# CRM Overview\n\nWhat it is.'); |
| 30 | + write('crm_lead_guide.md', '---\ntitle: Lead Guide\n---\n\nBody here.'); |
| 31 | + const { docs, issues } = collectDocsFromSrc(configPath); |
| 32 | + expect(issues).toHaveLength(0); |
| 33 | + expect(docs.map((d) => d.name).sort()).toEqual(['crm_index', 'crm_lead_guide']); |
| 34 | + const index = docs.find((d) => d.name === 'crm_index')!; |
| 35 | + expect(index.label).toBe('CRM Overview'); // first # heading |
| 36 | + const guide = docs.find((d) => d.name === 'crm_lead_guide')!; |
| 37 | + expect(guide.label).toBe('Lead Guide'); // frontmatter title wins |
| 38 | + expect(guide.content).not.toContain('title:'); // frontmatter stripped |
| 39 | + }); |
| 40 | + |
| 41 | + it('errors on subdirectories — flatness is the contract', () => { |
| 42 | + fs.mkdirSync(path.join(docsDir, 'user')); |
| 43 | + write('crm_index.md', '# x'); |
| 44 | + const { docs, issues } = collectDocsFromSrc(configPath); |
| 45 | + expect(issues.some((i) => i.rule === 'docs/flat-directory' && i.severity === 'error')).toBe(true); |
| 46 | + expect(docs).toHaveLength(1); // the valid file still collects |
| 47 | + }); |
| 48 | + |
| 49 | + it('errors on non-snake_case filename stems', () => { |
| 50 | + write('Lead-Guide.md', '# x'); |
| 51 | + const { docs, issues } = collectDocsFromSrc(configPath); |
| 52 | + expect(issues.some((i) => i.rule === 'docs/filename')).toBe(true); |
| 53 | + expect(docs).toHaveLength(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('ignores non-markdown files and returns empty when src/docs is absent', () => { |
| 57 | + write('notes.txt', 'not a doc'); |
| 58 | + expect(collectDocsFromSrc(configPath).docs).toHaveLength(0); |
| 59 | + fs.rmSync(docsDir, { recursive: true }); |
| 60 | + expect(collectDocsFromSrc(configPath).docs).toHaveLength(0); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe('lintDocs (ADR-0046 §3.2–§3.4)', () => { |
| 65 | + const doc = (name: string, content: string): DocItem => ({ name, content }); |
| 66 | + |
| 67 | + it('requires manifest.namespace when docs ship', () => { |
| 68 | + const issues = lintDocs([doc('crm_index', 'x')], undefined); |
| 69 | + expect(issues.some((i) => i.rule === 'docs/namespace-required')).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it('requires the namespace prefix on every doc name', () => { |
| 73 | + const issues = lintDocs([doc('lead_guide', 'x')], 'crm'); |
| 74 | + const hit = issues.find((i) => i.rule === 'docs/namespace-prefix'); |
| 75 | + expect(hit?.severity).toBe('error'); |
| 76 | + expect(hit?.message).toContain('crm_lead_guide'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('rejects duplicate names across inline + collected docs', () => { |
| 80 | + const issues = lintDocs([doc('crm_index', 'a'), doc('crm_index', 'b')], 'crm'); |
| 81 | + expect(issues.some((i) => i.rule === 'docs/duplicate-name')).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + it('bans image references (v1 text-only)', () => { |
| 85 | + const issues = lintDocs([doc('crm_index', 'See ')], 'crm'); |
| 86 | + expect(issues.some((i) => i.rule === 'docs/no-images')).toBe(true); |
| 87 | + }); |
| 88 | + |
| 89 | + it('bans MDX/JSX but tolerates code blocks that mention it', () => { |
| 90 | + expect( |
| 91 | + lintDocs([doc('crm_a', 'Use <Tabs items={x}> here')], 'crm') |
| 92 | + .some((i) => i.rule === 'docs/no-mdx'), |
| 93 | + ).toBe(true); |
| 94 | + expect( |
| 95 | + lintDocs([doc('crm_b', 'Example:\n\n```jsx\n<Tabs items={x} />\n```\n\nplain prose')], 'crm') |
| 96 | + .some((i) => i.rule === 'docs/no-mdx'), |
| 97 | + ).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it('resolves same-package relative links and flags broken ones', () => { |
| 101 | + const docs = [ |
| 102 | + doc('crm_index', 'See the [guide](./crm_lead_guide.md#start) and [missing](./crm_nope.md).'), |
| 103 | + doc('crm_lead_guide', 'Back to [index](crm_index.md).'), |
| 104 | + ]; |
| 105 | + const issues = lintDocs(docs, 'crm'); |
| 106 | + const broken = issues.filter((i) => i.rule === 'docs/broken-link'); |
| 107 | + expect(broken).toHaveLength(1); |
| 108 | + expect(broken[0].message).toContain('crm_nope'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('leaves cross-package links (foreign prefix) to publish-time checks', () => { |
| 112 | + const issues = lintDocs([doc('crm_index', 'See [billing](./billing_setup.md).')], 'crm'); |
| 113 | + expect(issues.some((i) => i.rule === 'docs/broken-link')).toBe(false); |
| 114 | + }); |
| 115 | +}); |
| 116 | + |
| 117 | +describe('collectAndLintDocs', () => { |
| 118 | + it('merges inline stack docs with collected files and lints the union', () => { |
| 119 | + write('crm_admin_setup.md', '# Admin Setup\n\nSee [index](./crm_index.md).'); |
| 120 | + const stack = { |
| 121 | + manifest: { namespace: 'crm' }, |
| 122 | + docs: [{ name: 'crm_index', content: '# CRM' }], |
| 123 | + }; |
| 124 | + const { docs, issues } = collectAndLintDocs(configPath, stack); |
| 125 | + expect(docs.map((d) => d.name).sort()).toEqual(['crm_admin_setup', 'crm_index']); |
| 126 | + expect(issues).toHaveLength(0); |
| 127 | + }); |
| 128 | +}); |
0 commit comments