import { createFormatter, escapeHtml, renderHtmlDocument, type Formatter } from '../formatter'; import type { RenderedFormat } from '../types'; /** Render a fixed sequence of formatter calls and capture the emitted lines. */ function render(format: RenderedFormat, body: (f: Formatter) => void): string { const lines: string[] = []; const f = createFormatter(format, line => lines.push(line)); body(f); return lines.join('\n'); } describe('createFormatter', () => { it('returns a formatter whose `format` matches the requested format', () => { expect(createFormatter('cli').format).toBe('cli'); expect(createFormatter('md').format).toBe('md'); expect(createFormatter('html').format).toBe('html'); }); }); describe('escapeHtml', () => { it('escapes the five HTML-sensitive characters', () => { expect(escapeHtml(` & `)).toBe( '<a href="x" title='y'> & </a>', ); }); }); describe('CliFormatter', () => { it('renders headings, tables and notes as aligned plain text', () => { const out = render('cli', f => { f.heading(1, 'Title'); f.heading(3, 'Section'); f.table(['Name', 'Count'], [['alpha', 1]]); f.line('- **bold** item'); f.line('> a note'); }); expect(out).toMatchInlineSnapshot(` "Title ═════ ▸ Section Name Count ───── ───── alpha 1 - bold item a note" `); }); }); describe('MarkdownFormatter', () => { it('escapes HTML, table separators, and line endings in headers and cells', () => { const out = render('md', f => f.table( ['Error | '], [['Expected
| & value\r\nnext'], [{ value: '**bold** `code`', title: 'x' }]], ), ); expect(out).toContain('| Error \\| <tag> |'); expect(out).toContain('| Expected <div> \\| <span> & value
next |'); expect(out).toContain('| **bold** `code` |'); }); it('renders GitHub-flavored markdown', () => { const out = render('md', f => { f.heading(2, 'Section'); f.table(['Name', 'Count'], [['alpha', 1]]); f.line('- **bold** item'); }); expect(out).toMatchInlineSnapshot(` "## Section | Name | Count | |------|-------| | alpha | 1 | - **bold** item" `); }); }); describe('HtmlFormatter', () => { it('renders HTML elements with escaping and inline emphasis', () => { const out = render('html', f => { f.heading(2, 'Section'); f.table(['Name', 'Count'], [['', 1]]); f.line('- **bold** & `code`'); f.line('> a note'); f.code('
raw
'); }); expect(out).toMatchInlineSnapshot(` "

Section

NameCount
<alpha>1
bold & code

a note

<div>raw</div>
" `); }); it('renders HTML group headings as package navigation targets', () => { const out = render('html', f => f.groupHeading('@scope/pkg-a')); expect(out).toBe( '

@scope/pkg-a

', ); }); it('renders titled table headers and cells as native HTML tooltips', () => { const out = render('html', f => f.table([{ value: 'Action', title: 'Recommended action' }], [[{ value: 'review', title: 'Review this value' }]]), ); expect(out).toContain('Action'); expect(out).toContain('review'); }); it('renders details blocks', () => { const out = render('html', f => { f.details('More', () => { f.heading(4, 'Inner'); }); }); expect(out).toMatchInlineSnapshot(` "
More

Inner

" `); }); it('adds a status class to colored headings', () => { const out = render('html', f => { f.heading(3, 'Compiler accepted (memo cache emitted)', 'success'); f.heading(3, 'Errors (compiler bailout)', 'error'); f.heading(3, 'Skipped (not a component/hook)', 'warning'); f.heading(2, 'Migration Candidates', 'info'); }); expect(out).toContain('

Compiler accepted (memo cache emitted)

'); expect(out).toContain('

Errors (compiler bailout)

'); expect(out).toContain('

Skipped (not a component/hook)

'); expect(out).toContain('

Migration Candidates

'); }); it('wraps a status section in a tinted container', () => { const out = render('html', f => { f.section('success', () => { f.heading(3, 'Compiled', 'success'); f.table(['A'], [['1']]); }); }); expect(out).toMatchInlineSnapshot(` "

Compiled

A
1
" `); }); }); describe('section passthrough', () => { it('cli and md emit section body inline with no wrapper markup', () => { const cli = render('cli', f => f.section('success', () => f.line('hi'))); const md = render('md', f => f.section('error', () => f.line('hi'))); expect(cli).toBe('hi'); expect(md).toBe('hi'); }); }); describe('foldableSection', () => { it('html renders a collapsible
with count badge, status class and slug id', () => { const out = render('html', f => { f.foldableSection({ title: 'Compiled but Risky', status: 'warning', count: 4 }, () => { f.line('body'); }); }); expect(out).toContain( '
', ); expect(out).toContain('Compiled but Risky 4'); expect(out).toContain('
'); expect(out).toContain('
'); // Collapsed by default (no open attribute). expect(out).not.toContain('data-count="4" open'); }); it('html adds the open attribute when defaultOpen is set', () => { const out = render('html', f => { f.foldableSection({ title: 'Summary', count: 3, defaultOpen: true }, () => f.line('x')); }); expect(out).toContain('id="summary" data-title="Summary" data-count="3" open>'); }); it('html omits the badge when count is undefined', () => { const out = render('html', f => { f.foldableSection({ title: 'Notes' }, () => f.line('x')); }); expect(out).toContain('Notes'); expect(out).not.toContain('fold-count'); }); it('cli renders a heading with the (N) count, no folding', () => { const out = render('cli', f => f.foldableSection({ title: 'Migration Candidates', count: 2 }, () => f.line('x'))); expect(out).toBe('Migration Candidates (2)\n────────────────────────\n\nx'); }); it('md renders a heading with the (N) count, no folding', () => { const out = render('md', f => f.foldableSection({ title: 'Errors', count: 5, level: 3 }, () => f.line('x'))); expect(out).toBe('### Errors (5)\n\nx'); }); it('html prefixes the id and emits data-group when grouped, keeping repeated titles unique', () => { const out = render('html', f => { f.foldableSection({ title: 'Compiled', status: 'success', count: 3, group: '@scope/pkg-a' }, () => f.line('x')); f.foldableSection({ title: 'Compiled', status: 'success', count: 9, group: '@scope/pkg-b' }, () => f.line('y')); }); expect(out).toContain('id="scope-pkg-a--compiled" data-title="Compiled" data-group="@scope/pkg-a"'); expect(out).toContain('id="scope-pkg-b--compiled" data-title="Compiled" data-group="@scope/pkg-b"'); // The two "Compiled" chapters now have distinct ids. expect(out).not.toContain('id="compiled"'); }); it('cli/md ignore the group (no id/prefix concept in plain text)', () => { const cli = render('cli', f => f.foldableSection({ title: 'Compiled', count: 3, group: 'pkg' }, () => f.line('x'))); expect(cli).toBe('Compiled (3)\n────────────\n\nx'); }); }); describe('html table of contents', () => { it('injects the sticky TOC element and builder script into the document', () => { const doc = renderHtmlDocument('Report', '
'); expect(doc).toContain(''); expect(doc).toContain("querySelectorAll('main.report > h2.toc-group-heading, main.report > details.fold')"); expect(doc).toContain('On this page'); expect(doc).toContain('Expand all'); expect(doc).toContain('Collapse all'); }); it('builder script renders package and child rows and supports scrollspy', () => { const doc = renderHtmlDocument('Report', ''); expect(doc).toContain("getAttribute('data-group')"); expect(doc).toContain("'toc-package'"); expect(doc).toContain("'toc-child'"); expect(doc).toContain("'toc-row'"); expect(doc).toContain("classList.add('active')"); }); it('builder script wires a collapse toggle for the whole rail', () => { const doc = renderHtmlDocument('Report', ''); expect(doc).toContain("'toc-toggle'"); expect(doc).toContain("classList.toggle('collapsed')"); }); }); describe('status coloring', () => { it('md ignores the status argument (no color markup)', () => { const out = render('md', f => f.heading(3, 'Compiled', 'success')); expect(out).toBe('### Compiled'); }); it('cli emits ANSI color when FORCE_COLOR is set', () => { // Use variable keys so neither tsconfig (index-signature access) nor eslint (dot-notation) complains. const forceKey = 'FORCE_COLOR'; const noKey = 'NO_COLOR'; const prev = process.env[forceKey]; const prevNo = process.env[noKey]; delete process.env[noKey]; process.env[forceKey] = '1'; try { const out = render('cli', f => f.heading(3, 'Errors', 'error')); // Red SGR (31) around the heading, reset at the end. expect(out).toBe('\x1b[31m▸ Errors\x1b[0m'); } finally { if (prev === undefined) { delete process.env[forceKey]; } else { process.env[forceKey] = prev; } if (prevNo !== undefined) { process.env[noKey] = prevNo; } } }); it('cli stays plain when NO_COLOR is set even with a status', () => { const out = render('cli', f => f.heading(3, 'Errors', 'error')); expect(out).toBe('▸ Errors'); }); }); describe('renderHtmlDocument', () => { it('wraps body content in a self-contained document with an escaped title', () => { const doc = renderHtmlDocument('A & B', '

hi

'); expect(doc.startsWith('')).toBe(true); expect(doc).toContain('A & B'); expect(doc).toContain('

A & B

'); expect(doc).toContain('