|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import assert from 'node:assert/strict'; |
| 4 | +import { describe, it } from 'node:test'; |
| 5 | + |
| 6 | +const { |
| 7 | + buildSideBarGroups, |
| 8 | + getSidebarItems, |
| 9 | + getMajorVersion, |
| 10 | + getCompatibleVersions, |
| 11 | +} = await import('../index.mjs'); |
| 12 | + |
| 13 | +const pages = [ |
| 14 | + ['File System API', 'fs', 'File System'], |
| 15 | + ['HTTP API', 'http', 'Networking'], |
| 16 | + ['Path API', 'path', 'File System'], |
| 17 | + ['Index', 'index'], |
| 18 | +]; |
| 19 | + |
| 20 | +const versions = [ |
| 21 | + { major: 14, url: '/api/v14/{path}.html', label: 'v14' }, |
| 22 | + { major: 16, url: '/api/v16/{path}.html', label: 'v16' }, |
| 23 | + { major: 18, url: '/api/v18/{path}.html', label: 'v18' }, |
| 24 | +]; |
| 25 | + |
| 26 | +describe('buildSideBarGroups', () => { |
| 27 | + it('groups entries by category and preserves insertion order', () => { |
| 28 | + const metadata = { path: 'fs', basename: 'fs' }; |
| 29 | + |
| 30 | + const result = buildSideBarGroups(pages, metadata); |
| 31 | + |
| 32 | + assert.deepStrictEqual(result, [ |
| 33 | + { |
| 34 | + groupName: 'File System', |
| 35 | + items: [ |
| 36 | + { label: 'File System API', link: 'fs.html' }, |
| 37 | + { label: 'Path API', link: 'path.html' }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + { |
| 41 | + groupName: 'Networking', |
| 42 | + items: [{ label: 'HTTP API', link: 'http.html' }], |
| 43 | + }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it('puts entries without category into an Others group at the end by default', () => { |
| 48 | + const uncategorizedPages = [ |
| 49 | + ['Buffer', 'buffer', 'Binary'], |
| 50 | + ['Unknown', 'unknown'], |
| 51 | + ['Config', 'config', ''], |
| 52 | + ]; |
| 53 | + const metadata = { path: 'buffer', basename: 'buffer' }; |
| 54 | + |
| 55 | + const result = buildSideBarGroups(uncategorizedPages, metadata); |
| 56 | + |
| 57 | + assert.equal(result.at(-1).groupName, 'Others'); |
| 58 | + assert.deepStrictEqual(result.at(-1).items, [ |
| 59 | + { label: 'Unknown', link: 'unknown.html' }, |
| 60 | + { label: 'Config', link: 'config.html' }, |
| 61 | + ]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('uses a custom default group name when provided', () => { |
| 65 | + const metadata = { path: 'unknown', basename: 'unknown' }; |
| 66 | + const result = buildSideBarGroups( |
| 67 | + [['Unknown', 'unknown']], |
| 68 | + metadata, |
| 69 | + 'General' |
| 70 | + ); |
| 71 | + |
| 72 | + assert.deepStrictEqual(result, [ |
| 73 | + { |
| 74 | + groupName: 'General', |
| 75 | + items: [{ label: 'Unknown', link: 'unknown.html' }], |
| 76 | + }, |
| 77 | + ]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns an empty array when given no entries', () => { |
| 81 | + assert.deepStrictEqual( |
| 82 | + buildSideBarGroups([], { path: 'fs', basename: 'fs' }), |
| 83 | + [] |
| 84 | + ); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('getSidebarItems', () => { |
| 89 | + it('maps pages to sidebar items and keeps category values', () => { |
| 90 | + const metadata = { path: 'fs', basename: 'fs' }; |
| 91 | + const result = getSidebarItems(pages.slice(0, 3), metadata); |
| 92 | + |
| 93 | + assert.deepStrictEqual(result, [ |
| 94 | + { |
| 95 | + label: 'File System API', |
| 96 | + link: 'fs.html', |
| 97 | + category: 'File System', |
| 98 | + }, |
| 99 | + { |
| 100 | + label: 'HTTP API', |
| 101 | + link: 'http.html', |
| 102 | + category: 'Networking', |
| 103 | + }, |
| 104 | + { |
| 105 | + label: 'Path API', |
| 106 | + link: 'path.html', |
| 107 | + category: 'File System', |
| 108 | + }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('uses basename html for the current page and relative links for others', () => { |
| 113 | + const metadata = { path: 'guide/fs', basename: 'fs' }; |
| 114 | + const result = getSidebarItems( |
| 115 | + [ |
| 116 | + ['File System API', 'guide/fs', 'File System'], |
| 117 | + ['HTTP API', 'guide/http', 'Networking'], |
| 118 | + ['Child API', 'guide/sub/child'], |
| 119 | + ], |
| 120 | + metadata |
| 121 | + ); |
| 122 | + |
| 123 | + assert.deepStrictEqual(result, [ |
| 124 | + { |
| 125 | + label: 'File System API', |
| 126 | + link: 'fs.html', |
| 127 | + category: 'File System', |
| 128 | + }, |
| 129 | + { |
| 130 | + label: 'HTTP API', |
| 131 | + link: 'http.html', |
| 132 | + category: 'Networking', |
| 133 | + }, |
| 134 | + { |
| 135 | + label: 'Child API', |
| 136 | + link: 'sub/child.html', |
| 137 | + category: undefined, |
| 138 | + }, |
| 139 | + ]); |
| 140 | + }); |
| 141 | +}); |
| 142 | + |
| 143 | +describe('getMajorVersion', () => { |
| 144 | + it('extracts major version from "v" prefixed string', () => { |
| 145 | + assert.strictEqual(getMajorVersion('v14.0.0'), 14); |
| 146 | + assert.strictEqual(getMajorVersion('v18.12.0'), 18); |
| 147 | + }); |
| 148 | + |
| 149 | + it('extracts major version without "v" prefix', () => { |
| 150 | + assert.strictEqual(getMajorVersion('16.0.0'), 16); |
| 151 | + assert.strictEqual(getMajorVersion('20.1.0'), 20); |
| 152 | + }); |
| 153 | + |
| 154 | + it('handles single digit versions', () => { |
| 155 | + assert.strictEqual(getMajorVersion('v4'), 4); |
| 156 | + assert.strictEqual(getMajorVersion('9'), 9); |
| 157 | + }); |
| 158 | + |
| 159 | + it('returns integer only', () => { |
| 160 | + const result = getMajorVersion('v14.5.3'); |
| 161 | + assert.strictEqual(typeof result, 'number'); |
| 162 | + assert.strictEqual(result % 1, 0); |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe('getCompatibleVersions', () => { |
| 167 | + it('includes versions with equal or greater major version', () => { |
| 168 | + const metadata = { added: 'v14.0.0', path: 'fs.md' }; |
| 169 | + const result = getCompatibleVersions(versions, metadata); |
| 170 | + |
| 171 | + assert.deepStrictEqual(result, [ |
| 172 | + { value: '/api/v14/fs.md.html', label: 'v14' }, |
| 173 | + { value: '/api/v16/fs.md.html', label: 'v16' }, |
| 174 | + { value: '/api/v18/fs.md.html', label: 'v18' }, |
| 175 | + ]); |
| 176 | + }); |
| 177 | + |
| 178 | + it('filters out versions with lower major version', () => { |
| 179 | + const metadata = { added: 'v18.0.0', path: 'fs.md' }; |
| 180 | + const result = getCompatibleVersions(versions, metadata); |
| 181 | + |
| 182 | + assert.deepStrictEqual(result, [ |
| 183 | + { value: '/api/v18/fs.md.html', label: 'v18' }, |
| 184 | + ]); |
| 185 | + }); |
| 186 | + |
| 187 | + it('uses introduced_in as fallback when added is missing', () => { |
| 188 | + const metadata = { introduced_in: 'v16.0.0', path: 'fs.md' }; |
| 189 | + const result = getCompatibleVersions(versions, metadata); |
| 190 | + |
| 191 | + assert.deepStrictEqual(result, [ |
| 192 | + { value: '/api/v16/fs.md.html', label: 'v16' }, |
| 193 | + { value: '/api/v18/fs.md.html', label: 'v18' }, |
| 194 | + ]); |
| 195 | + }); |
| 196 | + |
| 197 | + it('defaults to v0 when no version info provided', () => { |
| 198 | + const metadata = { path: 'fs.md' }; |
| 199 | + const result = getCompatibleVersions(versions, metadata); |
| 200 | + |
| 201 | + assert.deepStrictEqual(result, [ |
| 202 | + { value: '/api/v14/fs.md.html', label: 'v14' }, |
| 203 | + { value: '/api/v16/fs.md.html', label: 'v16' }, |
| 204 | + { value: '/api/v18/fs.md.html', label: 'v18' }, |
| 205 | + ]); |
| 206 | + }); |
| 207 | + |
| 208 | + it('replaces {path} placeholder in URL', () => { |
| 209 | + const metadata = { added: 'v14.0.0', path: 'file/system' }; |
| 210 | + const result = getCompatibleVersions(versions, metadata); |
| 211 | + |
| 212 | + result.forEach(item => { |
| 213 | + assert.ok(!item.value.includes('{path}')); |
| 214 | + assert.ok(item.value.includes(metadata.path)); |
| 215 | + }); |
| 216 | + }); |
| 217 | +}); |
0 commit comments