|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync } from 'node:child_process'; |
| 3 | +import { createRequire } from 'node:module'; |
| 4 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import test from 'node:test'; |
| 8 | +import React from 'react'; |
| 9 | +import { renderToStaticMarkup } from 'react-dom/server'; |
| 10 | + |
| 11 | +const libDirectory = path.dirname(fileURLToPath(import.meta.url)); |
| 12 | +const appDirectory = path.resolve(libDirectory, '..'); |
| 13 | +const buildDirectory = mkdtempSync(path.join(appDirectory, '.test-navigation-')); |
| 14 | +let getPageHeadings; |
| 15 | +let getMigrationReleasePage; |
| 16 | +let OnThisPage; |
| 17 | + |
| 18 | +try { |
| 19 | + // Compile only the modules under test so Node can exercise the actual TS and |
| 20 | + // TSX implementation without adding a separate test stack. |
| 21 | + const outputDirectory = path.join(buildDirectory, 'out'); |
| 22 | + const configPath = path.join(buildDirectory, 'tsconfig.json'); |
| 23 | + writeFileSync(configPath, JSON.stringify({ |
| 24 | + extends: path.join(appDirectory, 'tsconfig.json'), |
| 25 | + compilerOptions: { |
| 26 | + incremental: false, |
| 27 | + module: 'Node16', |
| 28 | + moduleResolution: 'Node16', |
| 29 | + noEmit: false, |
| 30 | + outDir: outputDirectory, |
| 31 | + rootDir: appDirectory, |
| 32 | + typeRoots: [path.join(appDirectory, 'node_modules', '@types')], |
| 33 | + types: ['node', 'react', 'react-dom'], |
| 34 | + }, |
| 35 | + files: [ |
| 36 | + path.join(appDirectory, 'lib', 'content.ts'), |
| 37 | + path.join(appDirectory, 'components', 'OnThisPage.tsx'), |
| 38 | + ], |
| 39 | + include: [], |
| 40 | + })); |
| 41 | + execFileSync('tsc', ['--project', configPath], { cwd: appDirectory, stdio: 'inherit' }); |
| 42 | + |
| 43 | + const requireFromApp = createRequire(import.meta.url); |
| 44 | + ({ getPageHeadings, getMigrationReleasePage } = requireFromApp( |
| 45 | + path.join(outputDirectory, 'lib', 'content.js'), |
| 46 | + )); |
| 47 | + ({ OnThisPage } = requireFromApp( |
| 48 | + path.join(outputDirectory, 'components', 'OnThisPage.js'), |
| 49 | + )); |
| 50 | +} finally { |
| 51 | + rmSync(buildDirectory, { recursive: true, force: true }); |
| 52 | +} |
| 53 | + |
| 54 | +test('getPageHeadings keeps H2-only pages unchanged', () => { |
| 55 | + assert.deepEqual(getPageHeadings('# Page title\n\n## Overview\n\n## API Reference'), [ |
| 56 | + { title: 'Overview', anchor: 'overview', level: 2 }, |
| 57 | + { title: 'API Reference', anchor: 'api-reference', level: 2 }, |
| 58 | + ]); |
| 59 | +}); |
| 60 | + |
| 61 | +test('getPageHeadings includes ordered H3 headings only under Capabilities', () => { |
| 62 | + const content = [ |
| 63 | + '## Overview', |
| 64 | + '### Intro detail', |
| 65 | + '## Capabilities', |
| 66 | + '### Multiple Series', |
| 67 | + '### Fixed Value Range', |
| 68 | + '## API Reference', |
| 69 | + '### Internal Detail', |
| 70 | + ].join('\n'); |
| 71 | + |
| 72 | + assert.deepEqual(getPageHeadings(content), [ |
| 73 | + { title: 'Overview', anchor: 'overview', level: 2 }, |
| 74 | + { title: 'Capabilities', anchor: 'capabilities', level: 2 }, |
| 75 | + { title: 'Multiple Series', anchor: 'multiple-series', level: 3 }, |
| 76 | + { title: 'Fixed Value Range', anchor: 'fixed-value-range', level: 3 }, |
| 77 | + { title: 'API Reference', anchor: 'api-reference', level: 2 }, |
| 78 | + ]); |
| 79 | +}); |
| 80 | + |
| 81 | +test('getPageHeadings ignores heading-like lines inside fenced code blocks', () => { |
| 82 | + const content = [ |
| 83 | + '## Capabilities', |
| 84 | + '```markdown', |
| 85 | + '## Fake section', |
| 86 | + '### Fake capability', |
| 87 | + '```', |
| 88 | + '### Real capability', |
| 89 | + '~~~', |
| 90 | + '### Also code', |
| 91 | + '~~~', |
| 92 | + ].join('\n'); |
| 93 | + |
| 94 | + assert.deepEqual(getPageHeadings(content), [ |
| 95 | + { title: 'Capabilities', anchor: 'capabilities', level: 2 }, |
| 96 | + { title: 'Real capability', anchor: 'real-capability', level: 3 }, |
| 97 | + ]); |
| 98 | +}); |
| 99 | + |
| 100 | +test('migration-style repeated H3 headings stay excluded while H2 links remain', () => { |
| 101 | + const content = [ |
| 102 | + '## charts-core', |
| 103 | + '### Before', |
| 104 | + '### After', |
| 105 | + '## charts-ui', |
| 106 | + '### Before', |
| 107 | + '### After', |
| 108 | + ].join('\n'); |
| 109 | + |
| 110 | + assert.deepEqual(getPageHeadings(content), [ |
| 111 | + { title: 'charts-core', anchor: 'charts-core', level: 2 }, |
| 112 | + { title: 'charts-ui', anchor: 'charts-ui', level: 2 }, |
| 113 | + ]); |
| 114 | +}); |
| 115 | + |
| 116 | +test('generated migration release pages continue excluding demoted H3 headings', () => { |
| 117 | + const migrationPage = getMigrationReleasePage('2.3.0', '2.3.0'); |
| 118 | + assert.ok(migrationPage, 'expected the 2.3.0 migration page to exist'); |
| 119 | + assert.match(migrationPage.content, /^### /m, 'expected migration subsections to use H3'); |
| 120 | + |
| 121 | + assert.deepEqual(getPageHeadings(migrationPage.content), []); |
| 122 | +}); |
| 123 | + |
| 124 | +test('OnThisPage nests capability links in order with their generated anchors', () => { |
| 125 | + const headings = getPageHeadings([ |
| 126 | + '## Overview', |
| 127 | + '## Capabilities', |
| 128 | + '### Multiple Series', |
| 129 | + '### Fixed Value Range', |
| 130 | + '## API Reference', |
| 131 | + ].join('\n')); |
| 132 | + |
| 133 | + const html = renderToStaticMarkup(React.createElement(OnThisPage, { headings })); |
| 134 | + const overview = html.indexOf('href="#overview"'); |
| 135 | + const capabilities = html.indexOf('href="#capabilities"'); |
| 136 | + const multipleSeries = html.indexOf('href="#multiple-series"'); |
| 137 | + const fixedRange = html.indexOf('href="#fixed-value-range"'); |
| 138 | + const apiReference = html.indexOf('href="#api-reference"'); |
| 139 | + |
| 140 | + assert.ok(overview < capabilities); |
| 141 | + assert.ok(capabilities < multipleSeries); |
| 142 | + assert.ok(multipleSeries < fixedRange); |
| 143 | + assert.ok(fixedRange < apiReference); |
| 144 | + assert.match(html, /<li class="min-w-0"><a href="#capabilities"[^>]*>Capabilities<\/a><ul/); |
| 145 | + assert.match(html, /href="#multiple-series"[^>]*>Multiple Series<\/a>/); |
| 146 | + assert.match(html, /href="#fixed-value-range"[^>]*>Fixed Value Range<\/a>/); |
| 147 | +}); |
| 148 | + |
| 149 | +test('OnThisPage renders an H2-only page without an empty child list', () => { |
| 150 | + const headings = getPageHeadings('## Overview\n\n## API Reference'); |
| 151 | + const html = renderToStaticMarkup(React.createElement(OnThisPage, { headings })); |
| 152 | + |
| 153 | + assert.equal((html.match(/<ul/g) ?? []).length, 1); |
| 154 | + assert.match(html, /href="#overview"/); |
| 155 | + assert.match(html, /href="#api-reference"/); |
| 156 | +}); |
| 157 | + |
| 158 | +test('OnThisPage wraps long labels within the existing sidebar width', () => { |
| 159 | + const headings = getPageHeadings('## Capabilities\n### A Capability Heading With A Long Name'); |
| 160 | + const html = renderToStaticMarkup(React.createElement(OnThisPage, { headings })); |
| 161 | + |
| 162 | + assert.match(html, /break-words/); |
| 163 | + assert.match(html, /href="#a-capability-heading-with-a-long-name"/); |
| 164 | +}); |
0 commit comments