|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | + |
| 4 | +import { generate } from '../generate.mjs'; |
| 5 | + |
| 6 | +/** |
| 7 | + * A page as `jsx-ast` gives it. |
| 8 | + * |
| 9 | + * @param {string} api |
| 10 | + * @param {string} name |
| 11 | + * @param {object} [extra] - More of its metadata |
| 12 | + */ |
| 13 | +const createPage = (api, name, extra = {}) => ({ |
| 14 | + data: { api, heading: { depth: 1, data: { name } }, ...extra }, |
| 15 | + headings: [{ slug: api }], |
| 16 | + content: '<></>', |
| 17 | +}); |
| 18 | + |
| 19 | +/** |
| 20 | + * Reads every batch the generator yields. |
| 21 | + * |
| 22 | + * @param {AsyncIterable<Array<object>> | Array<object>} input |
| 23 | + */ |
| 24 | +const run = async input => { |
| 25 | + const batches = []; |
| 26 | + |
| 27 | + for await (const batch of generate(input)) { |
| 28 | + batches.push(batch); |
| 29 | + } |
| 30 | + |
| 31 | + return batches; |
| 32 | +}; |
| 33 | + |
| 34 | +describe('html-all', () => { |
| 35 | + const zlib = createPage('zlib', 'Zlib'); |
| 36 | + const fs = createPage('fs', 'File system'); |
| 37 | + const index = createPage('index', 'Index'); |
| 38 | + const chunk = createPage('fs/promises', 'fs.promises', { chunk: {} }); |
| 39 | + const notFound = createPage('404', 'Page not found', { synthetic: true }); |
| 40 | + |
| 41 | + it('adds all.html right after its last part, in sidebar order', async () => { |
| 42 | + async function* input() { |
| 43 | + yield [zlib]; |
| 44 | + yield [{ manifest: [zlib.data, fs.data, index.data, notFound.data] }]; |
| 45 | + yield [index, fs]; |
| 46 | + yield [chunk, notFound]; |
| 47 | + } |
| 48 | + |
| 49 | + const batches = await run(input()); |
| 50 | + |
| 51 | + assert.deepEqual(batches[0], [zlib]); |
| 52 | + |
| 53 | + const [{ manifest }] = batches[1]; |
| 54 | + |
| 55 | + assert.deepEqual(manifest.slice(0, -1), [ |
| 56 | + zlib.data, |
| 57 | + fs.data, |
| 58 | + index.data, |
| 59 | + notFound.data, |
| 60 | + ]); |
| 61 | + assert.equal(manifest.at(-1).api, 'all'); |
| 62 | + |
| 63 | + const [, , all] = batches[2]; |
| 64 | + |
| 65 | + assert.deepEqual(batches[2].slice(0, 2), [index, fs]); |
| 66 | + assert.equal(all.data.api, 'all'); |
| 67 | + assert.deepEqual(all.parts, ['fs', 'zlib']); |
| 68 | + assert.deepEqual(all.headings, [{ slug: 'fs' }, { slug: 'zlib' }]); |
| 69 | + |
| 70 | + assert.deepEqual(batches[3], [chunk, notFound]); |
| 71 | + }); |
| 72 | + |
| 73 | + it('adds all.html after every page given at once, as with one thread', async () => { |
| 74 | + const batches = await run([zlib, fs, { manifest: [zlib.data, fs.data] }]); |
| 75 | + |
| 76 | + const pages = batches.flat(); |
| 77 | + |
| 78 | + assert.equal(pages.at(-1).data.api, 'all'); |
| 79 | + assert.deepEqual(pages.at(-1).parts, ['fs', 'zlib']); |
| 80 | + }); |
| 81 | +}); |
0 commit comments