forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.test.mjs
More file actions
164 lines (137 loc) · 4.6 KB
/
config.test.mjs
File metadata and controls
164 lines (137 loc) · 4.6 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
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';
import { SemVer } from 'semver';
import { setConfig } from '../../../../utils/configuration/index.mjs';
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, 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: {
web: {
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,
category: api === 'fs' ? 'File System' : undefined,
heading: { depth: 1, data: { name } },
},
});
describe('buildVersionEntries', () => {
it('creates version entries with labels and URL templates', () => {
const config = {
changelog: [
{ version: new SemVer('20.0.0'), isLts: true, isCurrent: false },
{ version: new SemVer('22.0.0'), isLts: false, isCurrent: true },
],
};
const result = buildVersionEntries(
config,
'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 config = {
changelog: [
{ version: new SemVer('18.0.0'), isLts: false, isCurrent: false },
],
};
const result = buildVersionEntries(config, '{version}');
assert.equal(result[0].label, 'v18.x');
});
it('formats minor versions when minor is non-zero', () => {
const config = {
changelog: [
{ version: new SemVer('21.7.0'), isLts: false, isCurrent: false },
],
};
const result = buildVersionEntries(config, '{version}');
assert.equal(result[0].label, 'v21.7.x');
assert.equal(result[0].major, 21);
});
});
describe('buildPageList', () => {
it('returns sorted [name, path, category] 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', 'File System']);
assert.deepStrictEqual(result[1], ['HTTP', '/http', undefined]);
});
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', 'File System']);
});
});
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');
});
});