Repository navigation
Expand file tree
/
Copy pathconfig.test.mjs
More file actions
239 lines (210 loc) · 6.46 KB
/
Copy pathconfig.test.mjs
File metadata and controls
239 lines (210 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';
import { setConfig } from '@doc-kit/core/utils/configuration/index.mjs';
import { SemVer } from 'semver';
mock.module('@node-core/rehype-shiki', {
namedExports: {
LANGS: [
{ name: 'javascript', aliases: ['js'], displayName: 'JavaScript' },
{ name: 'typescript', aliases: ['ts'], displayName: 'TypeScript' },
{ name: 'python', displayName: 'Python' },
],
},
});
const {
buildVersionEntries,
buildPageList,
buildDocumentationIndex,
buildLanguageDisplayNameMap,
} = await import('../config.mjs');
await setConfig({
version: 'v22.0.0',
changelog: [
{ version: new SemVer('20.0.0'), isLts: true, isCurrent: false },
{ version: new SemVer('22.0.0'), isLts: false, isCurrent: true },
],
generators: {
html: {
title: 'Node.js',
repository: 'nodejs/node',
ref: 'main',
baseURL: 'https://nodejs.org/docs',
editURL: 'https://github.com/nodejs/node/edit/main/doc/api{path}.md',
pageURL: '{baseURL}/latest-{version}/api{path}.html',
},
},
});
/**
* Helper to create a minimal JSX content entry.
*/
const makeEntry = (api, name, path) => ({
data: {
api,
path,
heading: { depth: 1, data: { name } },
},
});
describe('buildVersionEntries', () => {
it('creates version entries with labels and URL templates', () => {
const result = buildVersionEntries(
[
{ version: new SemVer('20.0.0'), isLts: true, isCurrent: false },
{ version: new SemVer('22.0.0'), isLts: false, isCurrent: true },
],
'https://nodejs.org/docs/latest-{version}/api{path}.html'
);
assert.equal(result.length, 2);
assert.deepStrictEqual(result[0], {
url: 'https://nodejs.org/docs/latest-v20.x/api{path}.html',
label: 'v20.x (LTS)',
major: 20,
});
assert.deepStrictEqual(result[1], {
url: 'https://nodejs.org/docs/latest-v22.x/api{path}.html',
label: 'v22.x (Current)',
major: 22,
});
});
it('does not append a label suffix for versions that are neither LTS nor Current', () => {
const result = buildVersionEntries(
[{ version: new SemVer('18.0.0'), isLts: false, isCurrent: false }],
'{version}'
);
assert.equal(result[0].label, 'v18.x');
});
it('formats minor versions when minor is non-zero', () => {
const result = buildVersionEntries(
[{ version: new SemVer('21.7.0'), isLts: false, isCurrent: false }],
'{version}'
);
assert.equal(result[0].label, 'v21.7.x');
assert.equal(result[0].major, 21);
});
});
describe('buildPageList', () => {
it('returns sorted [name, path] tuples from input entries', () => {
const input = [
makeEntry('http', 'HTTP', '/http'),
makeEntry('fs', 'File System', '/fs'),
];
const result = buildPageList(input);
assert.equal(result.length, 2);
// Sorted alphabetically by name
assert.deepStrictEqual(result[0], ['File System', '/fs']);
assert.deepStrictEqual(result[1], ['HTTP', '/http']);
});
it('filters out entries whose heading depth is not 1', () => {
const input = [
makeEntry('fs', 'File System', '/fs'),
{
data: {
api: 'http',
path: '/http',
heading: { depth: 2, data: { name: 'HTTP' } },
},
},
];
const result = buildPageList(input);
assert.equal(result.length, 1);
assert.deepStrictEqual(result[0], ['File System', '/fs']);
});
});
describe('buildDocumentationIndex', () => {
it('lists only pages with a stability index, with their descriptions', () => {
const input = [
{
data: {
api: 'fs',
path: '/fs',
heading: { depth: 1, data: { name: 'File System' } },
stability: { data: { index: '2' } },
content: {
type: 'root',
children: [
{
type: 'paragraph',
children: [{ type: 'text', value: 'File system APIs.' }],
},
],
},
},
},
{
data: {
api: 'index',
path: '/index',
heading: { depth: 1, data: { name: 'Index' } },
stability: null,
content: { type: 'root', children: [] },
},
},
{
data: {
api: 'quic',
path: '/quic',
heading: { depth: 1, data: { name: 'QUIC' } },
stability: { data: { index: '1.1' } },
llm_description: 'QUIC protocol support.',
content: { type: 'root', children: [] },
},
},
];
const result = buildDocumentationIndex(input);
assert.deepStrictEqual(result, [
{
api: 'fs',
name: 'File System',
index: '2',
description: 'File system APIs.',
},
{
api: 'quic',
name: 'QUIC',
index: '1.1',
description: 'QUIC protocol support.',
},
]);
});
it('renders descriptions to HTML, without the links entries cannot nest', () => {
const input = [
{
data: {
api: 'fs',
path: '/fs',
heading: { depth: 1, data: { name: 'File System' } },
stability: { data: { index: '2' } },
llm_description:
'Enables interacting with the `file system`, see [fs](/fs).',
content: { type: 'root', children: [] },
},
},
];
const [{ description }] = buildDocumentationIndex(input);
assert.equal(
description,
'Enables interacting with the <code>file system</code>, see fs.'
);
});
});
describe('buildLanguageDisplayNameMap', () => {
it('returns entries suitable for constructing a Map', () => {
const result = buildLanguageDisplayNameMap();
// Should have one entry per unique language name
assert.equal(result.length, 3);
const map = new Map(result);
assert.equal(map.get('JavaScript'), undefined);
// Each entry is [[aliases..., name], displayName]
// Find the javascript entry
const jsEntry = result.find(([keys]) => keys.includes('javascript'));
assert.ok(jsEntry);
assert.deepStrictEqual(jsEntry[0], ['js', 'javascript']);
assert.equal(jsEntry[1], 'JavaScript');
});
it('handles languages without aliases', () => {
const result = buildLanguageDisplayNameMap();
const pyEntry = result.find(([keys]) => keys.includes('python'));
assert.ok(pyEntry);
assert.deepStrictEqual(pyEntry[0], ['python']);
assert.equal(pyEntry[1], 'Python');
});
});