|
1 | 1 | import assert from 'node:assert/strict'; |
2 | | -import { describe, it } from 'node:test'; |
| 2 | +import { describe, it, mock, afterEach } from 'node:test'; |
3 | 3 |
|
4 | 4 | import { |
5 | 5 | groupNodesByModule, |
6 | 6 | getVersionFromSemVer, |
7 | 7 | coerceSemVer, |
8 | 8 | getCompatibleVersions, |
| 9 | + legacyToJSON, |
| 10 | + buildApiDocURL, |
| 11 | + createLazyGenerator, |
9 | 12 | } from '../generators.mjs'; |
10 | 13 |
|
11 | 14 | describe('groupNodesByModule', () => { |
@@ -79,3 +82,94 @@ describe('getCompatibleVersions', () => { |
79 | 82 | assert.equal(result.length, 2); |
80 | 83 | }); |
81 | 84 | }); |
| 85 | + |
| 86 | +describe('legacyToJSON', () => { |
| 87 | + const base = { |
| 88 | + type: 'module', |
| 89 | + source: 'lib/fs.js', |
| 90 | + introduced_in: 'v0.10.0', |
| 91 | + meta: {}, |
| 92 | + stability: 2, |
| 93 | + stabilityText: 'Stable', |
| 94 | + classes: [], |
| 95 | + methods: ['readFile'], |
| 96 | + properties: [], |
| 97 | + miscs: [], |
| 98 | + modules: ['fs'], |
| 99 | + globals: [], |
| 100 | + }; |
| 101 | + |
| 102 | + it('serialises a normal section with all keys', () => { |
| 103 | + const result = JSON.parse(legacyToJSON({ ...base, api: 'fs' })); |
| 104 | + assert.ok('type' in result); |
| 105 | + assert.ok('methods' in result); |
| 106 | + assert.ok('modules' in result); |
| 107 | + }); |
| 108 | + |
| 109 | + it('omits modules key for index sections', () => { |
| 110 | + const result = JSON.parse(legacyToJSON({ ...base, api: 'index' })); |
| 111 | + assert.ok(!('modules' in result)); |
| 112 | + }); |
| 113 | + |
| 114 | + it('uses all.json key order when api is null', () => { |
| 115 | + const result = JSON.parse(legacyToJSON({ ...base, api: null })); |
| 116 | + // all.json only includes miscs, modules, classes, globals, methods |
| 117 | + assert.ok('miscs' in result); |
| 118 | + assert.ok('modules' in result); |
| 119 | + assert.ok(!('type' in result)); |
| 120 | + assert.ok(!('source' in result)); |
| 121 | + }); |
| 122 | + |
| 123 | + it('passes extra args to JSON.stringify (e.g. indentation)', () => { |
| 124 | + const result = legacyToJSON({ ...base, api: 'fs' }, null, 2); |
| 125 | + assert.ok(result.includes('\n')); |
| 126 | + }); |
| 127 | +}); |
| 128 | + |
| 129 | +describe('buildApiDocURL', () => { |
| 130 | + const entry = { api: 'fs' }; |
| 131 | + const base = 'https://nodejs.org'; |
| 132 | + |
| 133 | + it('builds a .md URL by default', () => { |
| 134 | + const url = buildApiDocURL(entry, base); |
| 135 | + assert.ok(url instanceof URL); |
| 136 | + assert.ok(url.pathname.endsWith('.md')); |
| 137 | + assert.ok(url.pathname.includes('/fs')); |
| 138 | + }); |
| 139 | + |
| 140 | + it('builds a .html URL when useHtml is true', () => { |
| 141 | + const url = buildApiDocURL(entry, base, true); |
| 142 | + assert.ok(url.pathname.endsWith('.html')); |
| 143 | + }); |
| 144 | +}); |
| 145 | + |
| 146 | +describe('createLazyGenerator', () => { |
| 147 | + afterEach(() => mock.restoreAll()); |
| 148 | + |
| 149 | + it('spreads metadata properties onto the returned object', () => { |
| 150 | + const metadata = { name: 'ast', version: '1.0.0', dependsOn: undefined }; |
| 151 | + const gen = createLazyGenerator(metadata); |
| 152 | + assert.equal(gen.name, 'ast'); |
| 153 | + assert.equal(gen.version, '1.0.0'); |
| 154 | + }); |
| 155 | + |
| 156 | + it('exposes generate and processChunk functions that delegate to the lazily loaded module', async () => { |
| 157 | + // Both exports are mocked in a single mock.module() call to avoid ESM import |
| 158 | + // cache collisions that occur when re-mocking the same specifier across two it() blocks. |
| 159 | + const specifier = import.meta.resolve('../../generators/ast/generate.mjs'); |
| 160 | + const fakeGenerate = async input => `processed:${input}`; |
| 161 | + const fakeProcessChunk = async (input, indices) => |
| 162 | + indices.map(i => input[i]); |
| 163 | + mock.module(specifier, { |
| 164 | + namedExports: { generate: fakeGenerate, processChunk: fakeProcessChunk }, |
| 165 | + }); |
| 166 | + |
| 167 | + const gen = createLazyGenerator({ name: 'ast' }); |
| 168 | + |
| 169 | + const generateResult = await gen.generate('hello'); |
| 170 | + assert.equal(generateResult, 'processed:hello'); |
| 171 | + |
| 172 | + const processChunkResult = await gen.processChunk(['a', 'b', 'c'], [0, 2]); |
| 173 | + assert.deepStrictEqual(processChunkResult, ['a', 'c']); |
| 174 | + }); |
| 175 | +}); |
0 commit comments