import { execFileSync } from 'node:child_process' import { mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { fileURLToPath } from 'node:url' import { afterAll, describe, expect, test } from 'bun:test' const entry = fileURLToPath(new URL('../src/index.ts', import.meta.url)) const workDir = mkdtempSync(join(tmpdir(), 'capgo-docs-examples-')) afterAll(() => rmSync(workDir, { recursive: true, force: true })) function runGenerateDocs(...args: string[]) { execFileSync(process.execPath, [entry, 'generate-docs', ...args], { stdio: 'pipe', env: { ...process.env, CAPGO_DISABLE_TELEMETRY: '1' } }) } function generateReadme(): string { const readme = join(workDir, 'README.md') writeFileSync(readme, '\n\n') runGenerateDocs(readme) return readFileSync(readme, 'utf8') } function generateFolder(): string { const folder = join(workDir, 'webdocs') runGenerateDocs('--folder', folder) // Top-level pages skip their own heading anchor, so mark each page with one. return readdirSync(folder).sort().map(file => `\n${readFileSync(join(folder, file), 'utf8')}`).join('\n') } // Every example command written in an Example/Examples block of a command // description in src/index.ts (first line of each command only). function sourceExampleCommands(): string[] { const source = readFileSync(entry, 'utf8') const commands: string[] = [] for (const match of source.matchAll(/\.description\(`([^`]*)`\)/g)) { const lines = match[1].split('\n') for (let index = 0; index < lines.length; index++) { const header = lines[index].match(/^Examples?:(.*)$/) if (!header) continue if (header[1].trim()) commands.push(header[1].trim()) for (let next = index + 1; next < lines.length; next++) { const line = lines[next] if (line.trim() && !/^\s/.test(line)) break if (line.trim().startsWith('npx ')) commands.push(line.trim()) } } } return commands.map(command => command.replaceAll('\\\\', '\\')) } function splitFences(markdown: string) { const fenced: string[] = [] const unfenced: string[] = [] let inFence = false for (const line of markdown.split('\n')) { if (line.trim().startsWith('```')) { inFence = !inFence continue } (inFence ? fenced : unfenced).push(line) } expect(inFence).toBe(false) return { fenced: fenced.map(line => line.trim()), unfenced } } // The fenced example block rendered for one command section, indentation kept. function exampleBlock(markdown: string, anchor: string): string | undefined { const section = markdown.split(``)[1]?.split(' { const markdown = generate() const { fenced, unfenced } = splitFences(markdown) const examples = sourceExampleCommands() test('source has multi-line example blocks to check', () => { expect(examples.length).toBeGreaterThan(50) expect(examples).toContain('npx @capgo/cli@latest get-qr com.example.app production --type channel') expect(examples).toContain('npx @capgo/cli@latest build request com.example.app \\') }) test('every Example block command renders inside a fenced code block', () => { const missing = examples.filter(command => !fenced.includes(command)) expect(missing).toEqual([]) }) test('no Example header or example command leaks into prose', () => { expect(unfenced.filter(line => /^\s*Examples?:/.test(line))).toEqual([]) const leaked = unfenced.map(line => line.trim()).filter(line => examples.includes(line)) expect(leaked).toEqual([]) }) test('multi-line Examples blocks keep labels, continuations and indentation in their own section', () => { expect(exampleBlock(markdown, 'build-request')).toBe(buildRequestExamples) expect(exampleBlock(markdown, 'build-credentials-save')).toBe(credentialsSaveExamples) expect(exampleBlock(markdown, 'get-qr')).toBe([ 'npx @capgo/cli@latest get-qr com.example.app --bundle 1.2.3', 'npx @capgo/cli@latest get-qr com.example.app --bundle 123', 'npx @capgo/cli@latest get-qr com.example.app --channel production', 'npx @capgo/cli@latest get-qr com.example.app production --type channel', ].join('\n')) }) test('command group examples render too', () => { expect(exampleBlock(markdown, 'observe')).toBe('npx @capgo/cli@latest observe summary') }) test('every Example heading is followed by a non-empty bash fence', () => { const sections = markdown.split('**Example:**\n\n').slice(1) expect(sections.length).toBeGreaterThan(0) for (const section of sections) { expect(section.startsWith('```bash\n')).toBe(true) const body = section.slice('```bash\n'.length, section.indexOf('\n```')) expect(body.trim()).not.toBe('') expect(body).not.toMatch(/^Examples?:/m) } }) })