|
| 1 | +import assert from 'node:assert/strict' |
| 2 | +import test from 'node:test' |
| 3 | +import { |
| 4 | + collectRedirectEntriesForFile, |
| 5 | + mapWithConcurrency, |
| 6 | + type DocsTreeNode, |
| 7 | +} from '../src/utils/docs.functions' |
| 8 | +import { GitHubContentError } from '../src/utils/documents.server' |
| 9 | + |
| 10 | +test('mapWithConcurrency preserves result order regardless of completion order', async () => { |
| 11 | + const delays = [30, 5, 20, 1, 15] |
| 12 | + |
| 13 | + const results = await mapWithConcurrency(delays, 3, async (delay) => { |
| 14 | + await new Promise((resolve) => setTimeout(resolve, delay)) |
| 15 | + return delay |
| 16 | + }) |
| 17 | + |
| 18 | + assert.deepEqual(results, delays) |
| 19 | +}) |
| 20 | + |
| 21 | +test('mapWithConcurrency bounds concurrency instead of running everything in parallel', async () => { |
| 22 | + const items = Array.from({ length: 12 }, (_, index) => index) |
| 23 | + let inFlight = 0 |
| 24 | + let maxInFlight = 0 |
| 25 | + |
| 26 | + await mapWithConcurrency(items, 4, async () => { |
| 27 | + inFlight += 1 |
| 28 | + maxInFlight = Math.max(maxInFlight, inFlight) |
| 29 | + await new Promise((resolve) => setTimeout(resolve, 10)) |
| 30 | + inFlight -= 1 |
| 31 | + }) |
| 32 | + |
| 33 | + assert.ok( |
| 34 | + maxInFlight <= 4, |
| 35 | + `expected at most 4 concurrent calls, saw ${maxInFlight}`, |
| 36 | + ) |
| 37 | + assert.equal(maxInFlight, 4, 'expected concurrency to reach the cap') |
| 38 | +}) |
| 39 | + |
| 40 | +test('mapWithConcurrency runs in a bounded number of batches, not one call per item', async () => { |
| 41 | + const items = Array.from({ length: 18 }, (_, index) => index) |
| 42 | + const perItemDelayMs = 20 |
| 43 | + const concurrency = 6 |
| 44 | + |
| 45 | + const start = Date.now() |
| 46 | + await mapWithConcurrency(items, concurrency, async () => { |
| 47 | + await new Promise((resolve) => setTimeout(resolve, perItemDelayMs)) |
| 48 | + }) |
| 49 | + const elapsed = Date.now() - start |
| 50 | + |
| 51 | + const sequentialWorstCase = items.length * perItemDelayMs |
| 52 | + assert.ok( |
| 53 | + elapsed < sequentialWorstCase / 2, |
| 54 | + `expected concurrent execution well under ${sequentialWorstCase}ms, took ${elapsed}ms`, |
| 55 | + ) |
| 56 | +}) |
| 57 | + |
| 58 | +console.log('docs manifest concurrency tests passed') |
| 59 | + |
| 60 | +test('collectRedirectEntriesForFile returns no entries (not a rejection) when the file fetch throws a recoverable GitHub error', async () => { |
| 61 | + const node: DocsTreeNode = { path: 'docs/guide/old-page.md' } |
| 62 | + const paths: Array<string> = [] |
| 63 | + |
| 64 | + const entries = await collectRedirectEntriesForFile(node, { |
| 65 | + docsRoot: 'docs', |
| 66 | + fetchFile: async () => { |
| 67 | + throw new GitHubContentError('rate-limit', 'GitHub rate limited this file') |
| 68 | + }, |
| 69 | + onCanonicalPath: (path) => paths.push(path), |
| 70 | + }) |
| 71 | + |
| 72 | + assert.deepEqual(entries, []) |
| 73 | + assert.deepEqual(paths, ['guide/old-page']) |
| 74 | +}) |
| 75 | + |
| 76 | +test('collectRedirectEntriesForFile rethrows non-recoverable errors instead of silently swallowing them', async () => { |
| 77 | + const node: DocsTreeNode = { path: 'docs/guide/old-page.md' } |
| 78 | + |
| 79 | + await assert.rejects( |
| 80 | + () => |
| 81 | + collectRedirectEntriesForFile(node, { |
| 82 | + docsRoot: 'docs', |
| 83 | + fetchFile: async () => { |
| 84 | + throw new TypeError('this is a real bug, not a flaky GitHub call') |
| 85 | + }, |
| 86 | + onCanonicalPath: () => {}, |
| 87 | + }), |
| 88 | + TypeError, |
| 89 | + ) |
| 90 | +}) |
| 91 | + |
| 92 | +test('a mix of one failing file and several succeeding files still produces every succeeding redirect', async () => { |
| 93 | + const nodes: Array<DocsTreeNode> = [ |
| 94 | + { path: 'docs/guide/a.md' }, |
| 95 | + { path: 'docs/guide/flaky.md' }, |
| 96 | + { path: 'docs/guide/b.md' }, |
| 97 | + ] |
| 98 | + |
| 99 | + const fileContents: Record<string, string> = { |
| 100 | + 'docs/guide/a.md': '---\nredirect_from:\n - guide/old-a\n---\n# A', |
| 101 | + 'docs/guide/b.md': '---\nredirect_from:\n - guide/old-b\n---\n# B', |
| 102 | + } |
| 103 | + |
| 104 | + const paths: Array<string> = [] |
| 105 | + |
| 106 | + const results = await mapWithConcurrency(nodes, 3, (node) => |
| 107 | + collectRedirectEntriesForFile(node, { |
| 108 | + docsRoot: 'docs', |
| 109 | + fetchFile: async (filePath) => { |
| 110 | + if (filePath === 'docs/guide/flaky.md') { |
| 111 | + throw new GitHubContentError('server', 'GitHub 5xx for this file') |
| 112 | + } |
| 113 | + |
| 114 | + return fileContents[filePath] ?? null |
| 115 | + }, |
| 116 | + onCanonicalPath: (path) => paths.push(path), |
| 117 | + }), |
| 118 | + ) |
| 119 | + |
| 120 | + // The whole build did not reject just because one file was flaky. |
| 121 | + assert.deepEqual( |
| 122 | + results.flat().map((entry) => entry.from), |
| 123 | + ['guide/old-a', 'guide/old-b'], |
| 124 | + ) |
| 125 | + // All three files' paths were still recorded, including the flaky one. |
| 126 | + assert.deepEqual(paths.sort(), ['guide/a', 'guide/b', 'guide/flaky']) |
| 127 | +}) |
| 128 | + |
| 129 | +console.log('buildDocsManifest per-file fault tolerance tests passed') |
0 commit comments