Skip to content

Commit 9b2c1d5

Browse files
authored
test: check the streamed section pages without copies of what core has
The two tests comparing section-pages' batched output with its whole input become one: both compared against the same output. core's collectAsyncGenerator replaces a local copy of it, and html's test gives section-pages its entries as an array, which it reads as one batch, so it needs no stream of its own. toBatches' test no longer defines a generator body it never runs. The comment on section-pages' chunk pages says batches, as chunk already means a page there. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a13b6c9 commit 9b2c1d5

4 files changed

Lines changed: 18 additions & 60 deletions

File tree

‎packages/core/src/utils/__tests__/generators.test.mjs‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,8 @@ describe('getCompatibleVersions', () => {
141141
});
142142

143143
describe('toBatches', () => {
144-
it('gives an array as a single batch, and batches as they are', async () => {
145-
async function* batches() {
146-
yield [1];
147-
}
148-
149-
const streamed = batches();
144+
it('gives an array as a single batch, and batches as they are', () => {
145+
const streamed = (async function* () {})();
150146

151147
assert.deepEqual(toBatches([1, 2]), [[1, 2]]);
152148
assert.equal(toBatches(streamed), streamed);

‎packages/react/src/html/__tests__/generate.test.mjs‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,6 @@ import { compile, createViteBundler } from '../bundlers/vite.mjs';
1818
import { FONTS } from '../constants.mjs';
1919
import { generate } from '../generate.mjs';
2020

21-
/**
22-
* Yields the given entries as one batch, as a dependency streaming them does.
23-
*
24-
* @param {Array<object>} entries
25-
*/
26-
async function* streamInOneBatch(entries) {
27-
yield entries;
28-
}
29-
30-
/**
31-
* Runs `section-pages` over the given entries, streamed in as one batch, and
32-
* collects its pages.
33-
*
34-
* @param {Array<object>} entries
35-
*/
36-
const chunk = entries =>
37-
collectAsyncGenerator(sectionPages(streamInOneBatch(entries)));
38-
3921
// Pages are rendered with the pipeline of `jsx-ast`
4022
await loadMarkdownPlugins(
4123
await loadGenerator(import.meta.resolve('../../jsx-ast/index.mjs'))
@@ -215,7 +197,10 @@ describe('web generate', () => {
215197
],
216198
});
217199

218-
const pages = Map.groupBy(await chunk(entries), entry => entry.api);
200+
const pages = Map.groupBy(
201+
await collectAsyncGenerator(sectionPages(entries)),
202+
entry => entry.api
203+
);
219204
const contents = await Promise.all(
220205
[...pages.values()].map(group => buildContent(group, group[0]))
221206
);

‎packages/react/src/section-pages/__tests__/generate.test.mjs‎

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
11
import assert from 'node:assert/strict';
22
import { before, describe, it } from 'node:test';
33

4+
import { collectAsyncGenerator } from '@doc-kit/core/caching.mjs';
45
import getConfig, {
56
setConfig,
67
} from '@doc-kit/core/utils/configuration/index.mjs';
78

89
import { generate } from '../generate.mjs';
910

10-
/**
11-
* Collects every entry the generator yields.
12-
*
13-
* @param {AsyncIterable<Array<object>>} output
14-
* @returns {Promise<Array<object>>}
15-
*/
16-
const collect = async output => {
17-
const entries = [];
18-
19-
for await (const batch of output) {
20-
entries.push(...batch);
21-
}
22-
23-
return entries;
24-
};
25-
2611
/**
2712
* Runs the generator over the given batches of entries, streamed in as
2813
* `metadata` yields them, and collects what it yields.
2914
*
3015
* @param {...Array<object>} batches
3116
* @returns {Promise<Array<object>>}
3217
*/
33-
const run = (...batches) => collect(generate(streamOf(batches)));
18+
const run = (...batches) => collectAsyncGenerator(generate(streamOf(batches)));
3419

3520
/**
3621
* Yields each batch in turn, as a streamed input.
@@ -282,29 +267,20 @@ describe('section-pages generate', () => {
282267
}
283268
});
284269

285-
it('builds the same output from several batches as from one', async () => {
270+
it('builds the same output from several batches as from its whole input', async () => {
286271
const tiny = [
287272
createEntry('tiny', 1, 'Tiny'),
288273
createEntry('tiny', 2, '`tiny.only()`', { name: 'only', type: 'method' }),
289274
];
290275

291-
const whole = await run([...createModule(), ...tiny]);
292276
const batched = await run(createModule(), tiny);
293277

294-
assert.deepEqual(batched, whole);
295-
});
296-
297-
it('builds the same output from its whole input, as it gets it with one thread', async () => {
298-
const input = () => [
299-
...createModule(),
300-
createEntry('tiny', 1, 'Tiny'),
301-
createEntry('tiny', 2, '`tiny.only()`', { name: 'only', type: 'method' }),
302-
];
303-
304-
const whole = await collect(generate(input()));
305-
const streamed = await run(input());
278+
// The whole input, as it comes with one thread
279+
const whole = await collectAsyncGenerator(
280+
generate([...createModule(), ...tiny])
281+
);
306282

307-
assert.deepEqual(whole, streamed);
283+
assert.deepEqual(batched, whole);
308284
});
309285

310286
it(
@@ -329,7 +305,7 @@ describe('section-pages generate', () => {
329305

330306
gate.resolve();
331307

332-
const rest = await collect(output);
308+
const rest = await collectAsyncGenerator(output);
333309

334310
assert.deepEqual(
335311
rest.filter(e => !e.chunk).map(e => e.api),

‎packages/react/src/section-pages/generate.mjs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,9 @@ export async function* generate(input) {
159159

160160
const chunkPages = [];
161161

162-
// A module's chunk pages need all of its entries, which arrive together:
163-
// each of `metadata`'s chunks holds whole files, and a file is one module
162+
// A module's chunk pages need all of its entries, which arrive in the same
163+
// batch: each batch `metadata` yields holds whole files, and a file is one
164+
// module
164165
for await (const entries of toBatches(input)) {
165166
yield entries;
166167

0 commit comments

Comments
 (0)