diff --git a/.github/scripts/async-iteration.mjs b/.github/scripts/async-iteration.mjs new file mode 100644 index 00000000..be29f28f --- /dev/null +++ b/.github/scripts/async-iteration.mjs @@ -0,0 +1,42 @@ +/** + * Process items one at a time, stopping immediately if an operation fails. + * Use this for operations sharing a rate limit, deletion budget, or build state. + * + * @param {Iterable|AsyncIterable} items Items to process lazily. + * @param {(item: any) => Promise} operation Operation to complete before advancing. + * @returns {Promise} Completion of all operations. + */ +export async function forEachSequential(items, operation) { + for await (const item of items) { + await operation(item); + } +} + +/** + * Collect pages whose next cursor is only known after loading the current page. + * + * @param {any} initialCursor Cursor for the first page. + * @param {(cursor: any) => Promise<{items: Array, nextCursor: any}>} loadPage Page loader. + * @returns {Promise} Items in page order. A nullish cursor ends pagination. + */ +export async function collectPages(initialCursor, loadPage) { + let cursor = initialCursor; + const pages = { + [Symbol.asyncIterator]() { + return this; + }, + async next() { + if (cursor === null || cursor === undefined) { + return {done: true}; + } + const page = await loadPage(cursor); + cursor = page.nextCursor; + return {done: false, value: page.items}; + }, + }; + const results = []; + for await (const items of pages) { + results.push(...items); + } + return results; +} diff --git a/.github/scripts/cleanup-cloudsmith.mjs b/.github/scripts/cleanup-cloudsmith.mjs index 0b551a62..c1018736 100644 --- a/.github/scripts/cleanup-cloudsmith.mjs +++ b/.github/scripts/cleanup-cloudsmith.mjs @@ -1,3 +1,5 @@ +import {forEachSequential} from './async-iteration.mjs'; + const CLOUDSMITH_API = 'https://api.cloudsmith.io/v1'; const DEFAULT_GRACE_MS = 24 * 60 * 60 * 1000; @@ -187,14 +189,14 @@ async function loadRepositories(github, organization) { async function loadReleaseTags(github, organization, repositoryNames) { const tagsByRepository = new Map(); - for (const repository of repositoryNames) { + await forEachSequential(repositoryNames, async (repository) => { const releases = await github.paginate(github.rest.repos.listReleases, { owner: organization, repo: repository, per_page: 100, }); tagsByRepository.set(repository.toLowerCase(), publishedReleaseTags(releases)); - } + }); return tagsByRepository; } @@ -249,7 +251,7 @@ export async function cleanupCloudsmith({ } } - for (const {package: packageData} of plan.remove) { + await forEachSequential(plan.remove, async ({package: packageData}) => { const identifier = packageData.identifier_perm ?? packageData.slug_perm; if (dryRun) { core.info(`[dry-run] Delete ${packageLabel(packageData)} (${identifier}).`); @@ -257,7 +259,7 @@ export async function cleanupCloudsmith({ core.info(`Deleting ${packageLabel(packageData)} (${identifier}).`); await deleteCloudsmithPackage({owner, repository, token, identifier}); } - } + }); const unknownNames = new Set( packages diff --git a/.github/scripts/cleanup-containers.mjs b/.github/scripts/cleanup-containers.mjs index 8bf05931..aa1fd4d8 100644 --- a/.github/scripts/cleanup-containers.mjs +++ b/.github/scripts/cleanup-containers.mjs @@ -1,3 +1,5 @@ +import {collectPages, forEachSequential} from './async-iteration.mjs'; + const DOCKER_HUB_API = 'https://hub.docker.com/v2'; const DOCKER_REGISTRY = 'https://registry-1.docker.io'; const GHCR_REGISTRY = 'https://ghcr.io'; @@ -145,6 +147,17 @@ export function classifyGhcrVersions(versions, releaseTags, { return result; } +function* pendingDigests(pending, reachable) { + while (pending.length > 0) { + const digest = pending.pop(); + if (!digest || reachable.has(digest)) { + continue; + } + reachable.add(digest); + yield digest; + } +} + /** * Resolve every child manifest reachable from retained GHCR versions. * @@ -155,19 +168,14 @@ export function classifyGhcrVersions(versions, releaseTags, { export async function resolveReachableDigests(rootDigests, getManifest) { const reachable = new Set(); const pending = [...rootDigests]; - while (pending.length > 0) { - const digest = pending.pop(); - if (!digest || reachable.has(digest)) { - continue; - } - reachable.add(digest); + await forEachSequential(pendingDigests(pending, reachable), async (digest) => { const manifest = await getManifest(digest); for (const child of manifest.manifests ?? []) { if (child.digest && !reachable.has(child.digest)) { pending.push(child.digest); } } - } + }); return reachable; } @@ -213,16 +221,12 @@ async function dockerHubToken(username, accessToken) { } async function paginatedHubResults(url, token) { - const results = []; - let next = url; - while (next) { - const page = await fetchJson(next, { + return collectPages(url, async (cursor) => { + const page = await fetchJson(cursor, { headers: {Authorization: `Bearer ${token}`}, }); - results.push(...page.results); - next = page.next; - } - return results; + return {items: page.results, nextCursor: page.next || null}; + }); } async function dockerRegistryToken({namespace, repository, username, accessToken}) { @@ -315,11 +319,13 @@ export async function cleanupDockerHub({ let remaining = maxDeletions; let deferredActions = 0; - for (const hubRepository of hubRepositories.toSorted((a, b) => a.name.localeCompare(b.name))) { + // Keep repository budgets and registry mutations in deterministic order. + const sortedRepositories = hubRepositories.toSorted((a, b) => a.name.localeCompare(b.name)); + await forEachSequential(sortedRepositories, async (hubRepository) => { const githubRepository = githubRepositories.get(hubRepository.name.toLowerCase()); if (!githubRepository) { core.warning(`Skipping Docker Hub ${namespace}/${hubRepository.name}: no matching GitHub repository.`); - continue; + return; } const [tags, releaseTags] = await Promise.all([ @@ -337,7 +343,7 @@ export async function cleanupDockerHub({ deferredActions += plan.actions.length - selectedActions.length; let registryToken; - for (const action of selectedActions) { + await forEachSequential(selectedActions, async (action) => { const description = action.kind === 'manifest' ? `${namespace}/${hubRepository.name}@${action.digest} (${action.tags.length} tags)` : `${namespace}/${hubRepository.name}:${action.tag}`; @@ -368,8 +374,8 @@ export async function cleanupDockerHub({ } deletedActions += 1; remaining -= 1; - } - } + }); + }); await core.summary .addHeading(`Docker Hub ${namespace} cleanup`, 2) @@ -432,7 +438,8 @@ async function deleteSelectedGhcrVersions({ selectedVersions, dryRun, }) { - for (const version of selectedVersions) { + // Tagged parent versions must be deleted before their orphaned children. + await forEachSequential(selectedVersions, async (version) => { const tags = versionTags(version); const description = tags.length > 0 ? `${namespace}/${packageName}:${tags.join(',')}` @@ -448,7 +455,7 @@ async function deleteSelectedGhcrVersions({ versionId: version.id, }); } - } + }); } /** @@ -485,11 +492,12 @@ export async function cleanupGhcr({ let deferred = 0; let remaining = maxDeletions; - for (const packageData of packages.sort((a, b) => a.name.localeCompare(b.name))) { + // Finish each package before consuming the shared deletion budget for the next. + await forEachSequential(packages.sort((a, b) => a.name.localeCompare(b.name)), async (packageData) => { const githubRepository = githubRepositories.get(packageData.name.toLowerCase()); if (!githubRepository) { core.warning(`Skipping GHCR ${namespace}/${packageData.name}: no matching GitHub repository.`); - continue; + return; } const [versions, releaseTags] = await Promise.all([ @@ -528,7 +536,7 @@ export async function cleanupGhcr({ `Skipping GHCR ${namespace}/${packageData.name}: retained manifest graph could not be read: ` + error.message, ); - continue; + return; } const candidates = ghcrCleanupCandidates(classification, reachable); @@ -547,7 +555,7 @@ export async function cleanupGhcr({ }); operations += selectedVersions.length; remaining -= selectedVersions.length; - } + }); await core.summary .addHeading(`GHCR ${namespace} cleanup`, 2) diff --git a/.github/scripts/cloudsmith-backfill.mjs b/.github/scripts/cloudsmith-backfill.mjs index fe4307ce..dd8a7c01 100644 --- a/.github/scripts/cloudsmith-backfill.mjs +++ b/.github/scripts/cloudsmith-backfill.mjs @@ -19,6 +19,7 @@ import { import path from 'node:path'; import {Readable} from 'node:stream'; import {pipeline} from 'node:stream/promises'; +import {forEachSequential} from './async-iteration.mjs'; const SOURCE_OWNER = 'LizardByte'; const CLOUDSMITH_DESTINATION = 'lizardbyte/stable'; @@ -608,18 +609,19 @@ export async function prepareBackfill({ } const manifest = []; - for (const release of selected) { + // Package rebuilds and manifest writes share state across assets and releases. + await forEachSequential(selected, async (release) => { const assets = release.assets.filter((asset) => /\.(?:deb|rpm)$/i.test(asset.name)); if (assets.length === 0) { core.info(`${release.tag_name}: no DEB or RPM assets`); - continue; + return; } core.startGroup(`${release.tag_name}: ${assets.length} package assets`); try { const releaseDirectory = path.join(downloads, release.tag_name.replace(/[^0-9A-Za-z_.-]/g, '_')); mkdirSync(releaseDirectory, {recursive: true}); - for (const asset of assets) { + await forEachSequential(assets, async (asset) => { await stageBackfillAsset({ asset, release, @@ -632,11 +634,11 @@ export async function prepareBackfill({ manifestFile, core, }); - } + }); } finally { core.endGroup(); } - } + }); if (manifest.length === 0) { throw new Error('No stable DEB or RPM assets were staged.'); diff --git a/docs/source/_static/js/projects.js b/docs/source/_static/js/projects.js index 54a5d370..74e95d91 100644 --- a/docs/source/_static/js/projects.js +++ b/docs/source/_static/js/projects.js @@ -218,7 +218,9 @@ function initializeProjectsSection() { createProjectsDescription(section); const projectList = createProjectsList(section); - fetchAndRenderProjects(projectList); + fetchAndRenderProjects(projectList).catch((error) => { + console.error("Error initializing projects:", error); + }); } // Initialize when DOM is fully loaded diff --git a/tests/async-iteration.test.mjs b/tests/async-iteration.test.mjs new file mode 100644 index 00000000..5393d50b --- /dev/null +++ b/tests/async-iteration.test.mjs @@ -0,0 +1,93 @@ +import assert from 'node:assert/strict'; +import {setImmediate} from 'node:timers/promises'; +import test from 'node:test'; + +import {collectPages, forEachSequential} from '../.github/scripts/async-iteration.mjs'; + +test('forEachSequential waits for each operation before starting the next', async () => { + const gate = Promise.withResolvers(); + const events = []; + const completion = forEachSequential([1, 2], async (item) => { + events.push(`start ${item}`); + if (item === 1) { + await gate.promise; + } + events.push(`end ${item}`); + }); + + await setImmediate(); + assert.deepEqual(events, ['start 1']); + gate.resolve(); + await completion; + assert.deepEqual(events, ['start 1', 'end 1', 'start 2', 'end 2']); +}); + +test('forEachSequential stops and closes a lazy iterable on failure', async () => { + const events = []; + function* items() { + try { + events.push('first'); + yield 1; + events.push('second'); + yield 2; + } finally { + events.push('closed'); + } + } + const failure = new Error('operation failed'); + await assert.rejects(forEachSequential(items(), async () => { + throw failure; + }), failure); + assert.deepEqual(events, ['first', 'closed']); +}); + +test('forEachSequential consumes asynchronous iterables', async () => { + async function* items() { + yield 1; + yield 2; + } + const processed = []; + await forEachSequential(items(), async (item) => { + processed.push(item); + }); + assert.deepEqual(processed, [1, 2]); +}); + +test('collectPages follows response cursors and preserves item order', async () => { + const gate = Promise.withResolvers(); + const requested = []; + const completion = collectPages('first', async (cursor) => { + requested.push(cursor); + if (cursor === 'first') { + await gate.promise; + return {items: [1, 2], nextCursor: 'last'}; + } + return {items: [3], nextCursor: null}; + }); + + await setImmediate(); + assert.deepEqual(requested, ['first']); + gate.resolve(); + assert.deepEqual(await completion, [1, 2, 3]); + assert.deepEqual(requested, ['first', 'last']); +}); + +test('collectPages accepts a zero cursor and ends at an undefined cursor', async () => { + assert.deepEqual(await collectPages(0, async (cursor) => { + assert.equal(cursor, 0); + return {items: ['only page']}; + }), ['only page']); +}); + +test('collectPages propagates loader errors without requesting another page', async () => { + const cursors = []; + const failure = new Error('page failed'); + await assert.rejects(collectPages(1, async (cursor) => { + cursors.push(cursor); + if (cursor === 2) { + throw failure; + } + return {items: [cursor], nextCursor: cursor + 1}; + }), failure); + assert.deepEqual(cursors, [1, 2]); +}); diff --git a/tests/cleanup-cloudsmith.test.mjs b/tests/cleanup-cloudsmith.test.mjs index 81f6ea3a..51926450 100644 --- a/tests/cleanup-cloudsmith.test.mjs +++ b/tests/cleanup-cloudsmith.test.mjs @@ -1,7 +1,9 @@ import assert from 'node:assert/strict'; +import {setImmediate} from 'node:timers/promises'; import test from 'node:test'; import { + cleanupCloudsmith, hasCloudsmithTag, packageMatchesRelease, planCloudsmithCleanup, @@ -70,3 +72,75 @@ test('planCloudsmithCleanup retains releases, latest targets, recent and unknown ]); assert.deepEqual(plan.remove.map(({package: item}) => item.identifier_perm), ['stale']); }); + +test('Cloudsmith cleanup collects all pages and processes releases and deletions sequentially', async (t) => { + const pages = []; + const deletions = []; + t.mock.method(globalThis, 'fetch', async (input, options) => { + if (options.method === 'DELETE') { + const identifier = new URL(input).pathname.split('/').at(-2); + deletions.push(`start ${identifier}`); + await setImmediate(); + deletions.push(`end ${identifier}`); + return new Response(null, {status: 204}); + } + const page = Number(new URL(input).searchParams.get('page')); + pages.push(page); + const packages = page === 1 ? Array.from({length: 100}, (_, index) => ({ + name: 'sunshine', version: '1.0.0', identifier_perm: `release-${index}`, + })) : [ + {name: 'beta', version: 'old-build', identifier_perm: 'stale-1'}, + {name: 'beta', version: 'old-build', identifier_perm: 'stale-2'}, + ]; + return Response.json(packages, {headers: {'x-pagination-pagetotal': '2'}}); + }); + const events = []; + const github = { + rest: {repos: {listForOrg: 'repos', listReleases: 'releases'}}, + async paginate(route, options) { + if (route === 'repos') { + return [{name: 'Sunshine'}, {name: 'Beta'}]; + } + events.push(`start ${options.repo}`); + await setImmediate(); + events.push(`end ${options.repo}`); + return [{tag_name: 'v1.0.0', draft: false}]; + }, + }; + const summary = { + addHeading() { return this; }, + addTable() { return this; }, + async write() {}, + }; + const result = await cleanupCloudsmith({ + github, core: {info() {}, warning() {}, summary}, token: 'token', dryRun: false, + }); + assert.deepEqual(pages, [1, 2]); + assert.deepEqual(events, ['start Sunshine', 'end Sunshine', 'start Beta', 'end Beta']); + assert.deepEqual(deletions, ['start stale-1', 'end stale-1', 'start stale-2', 'end stale-2']); + assert.deepEqual(result, {scanned: 102, retained: 100, selected: 2}); +}); + +test('Cloudsmith cleanup stops before deletion if loading release tags fails', async (t) => { + t.mock.method(globalThis, 'fetch', async (input, options) => { + assert.notEqual(options.method, 'DELETE'); + return Response.json([ + {name: 'alpha', version: 'old', identifier_perm: 'alpha'}, + {name: 'beta', version: 'old', identifier_perm: 'beta'}, + ]); + }); + const releases = []; + const failure = new Error('GitHub unavailable'); + const github = { + rest: {repos: {listForOrg: 'repos', listReleases: 'releases'}}, + async paginate(route, options) { + if (route === 'repos') { + return [{name: 'Alpha'}, {name: 'Beta'}]; + } + releases.push(options.repo); + throw failure; + }, + }; + await assert.rejects(cleanupCloudsmith({github, core: {}, token: 'token', dryRun: false}), failure); + assert.deepEqual(releases, ['Alpha']); +}); diff --git a/tests/cleanup-containers.test.mjs b/tests/cleanup-containers.test.mjs index 327db178..f2a59eba 100644 --- a/tests/cleanup-containers.test.mjs +++ b/tests/cleanup-containers.test.mjs @@ -1,8 +1,11 @@ import assert from 'node:assert/strict'; +import {setImmediate} from 'node:timers/promises'; import test from 'node:test'; import { classifyGhcrVersions, + cleanupDockerHub, + cleanupGhcr, ghcrCleanupCandidates, isRetainedContainerTag, planDockerHubCleanup, @@ -110,3 +113,140 @@ test('ghcrCleanupCandidates preserves reachable untagged child manifests', () => assert.deepEqual(candidates.map(({id}) => id), ['tagged', 'orphan']); }); + +test('resolveReachableDigests reads cyclic and duplicate digests only once', async () => { + const loaded = []; + const reachable = await resolveReachableDigests(['root', 'root', null], async (digest) => { + loaded.push(digest); + return {manifests: [{digest: 'root'}, {digest: 'child'}, {digest: 'child'}]}; + }); + assert.deepEqual(loaded, ['root', 'child']); + assert.deepEqual([...reachable], ['root', 'child']); +}); + +function summaryCore() { + const summary = { + addHeading() { return this; }, + addTable() { return this; }, + async write() {}, + }; + return {info() {}, warning() {}, summary}; +} + +for (const dryRun of [false, true]) { + test(`Docker Hub cleanup respects pagination and a shared deletion budget (dryRun=${dryRun})`, async (t) => { + const old = '2020-01-01T00:00:00Z'; + const deleted = []; + let registryAuthentications = 0; + t.mock.method(globalThis, 'fetch', async (input, options = {}) => { + const url = new URL(input); + if (options.method === 'DELETE') { + deleted.push(url.pathname); + await setImmediate(); + return new Response(null, {status: 204}); + } + let data; + if (url.pathname === '/v2/auth/token') { + data = {access_token: 'hub-token'}; + } else if (url.hostname === 'auth.docker.io') { + registryAuthentications += 1; + data = {token: 'registry-token'}; + } else if (url.pathname.endsWith('/repositories')) { + data = url.searchParams.has('page') + ? {results: [{name: 'alpha'}], next: null} + : {results: [{name: 'beta'}], next: `${url.origin}${url.pathname}?page=2`}; + } else if (url.pathname.endsWith('/alpha/tags')) { + data = url.searchParams.has('page') + ? {results: [{name: 'older-2', digest: 'sha256:a2', last_updated: old}], next: null} + : { + results: [{name: 'older-1', digest: 'sha256:a1', last_updated: old}], + next: `${url.origin}${url.pathname}?page=2`, + }; + } else if (url.pathname.endsWith('/beta/tags')) { + data = {results: [{name: 'older', digest: 'sha256:b', last_updated: old}], next: null}; + } else { + assert.fail(`Unexpected request: ${url}`); + } + return Response.json(data); + }); + const github = { + rest: {repos: {listForOrg: 'repos', listReleases: 'releases'}}, + async paginate(route) { + return route === 'repos' ? [{name: 'Alpha'}, {name: 'Beta'}] : []; + }, + }; + + const result = await cleanupDockerHub({ + github, core: summaryCore(), username: 'user', accessToken: 'token', dryRun, maxDeletions: 2, + }); + assert.deepEqual(result, {repositories: 2, scanned: 3, selectedTags: 3, deletedActions: 2, deferredActions: 1}); + assert.deepEqual(deleted, dryRun ? [] : ['/v2/lizardbyte/alpha/manifests/sha256:a1', + '/v2/lizardbyte/alpha/manifests/sha256:a2']); + assert.equal(registryAuthentications, dryRun ? 0 : 1); + }); +} + +test('GHCR deletes parents before children and carries its deletion budget across packages', async (t) => { + t.mock.method(globalThis, 'fetch', async () => Response.json({token: 'registry-token'})); + const events = []; + const github = { + rest: {repos: {listForOrg: 'repos', listReleases: 'releases'}}, + async paginate(route, options) { + if (route === 'repos') { + return [{name: 'Alpha'}, {name: 'Beta'}]; + } + if (route === 'releases') { + return []; + } + if (options.package_name) { + return [ + {id: 'child', name: 'child', metadata: {container: {tags: []}}}, + {id: 'parent', name: 'parent', metadata: {container: {tags: ['old']}}}, + ]; + } + return [{name: 'beta'}, {name: 'alpha'}]; + }, + async request(route, options) { + const label = `${options.package_name}/${options.package_version_id}`; + events.push(`start ${label}`); + await setImmediate(); + events.push(`end ${label}`); + }, + }; + const result = await cleanupGhcr({ + github, core: summaryCore(), username: 'user', accessToken: 'token', dryRun: false, maxDeletions: 3, + }); + assert.deepEqual(result, {packages: 2, scanned: 4, selected: 4, operations: 3, deferred: 1}); + assert.deepEqual(events, ['start alpha/parent', 'end alpha/parent', 'start alpha/child', 'end alpha/child', + 'start beta/parent', 'end beta/parent']); +}); + +test('GHCR makes no deletions when a retained manifest cannot be read', async (t) => { + t.mock.method(globalThis, 'fetch', async (input) => { + const url = new URL(input); + return url.pathname === '/token' + ? Response.json({token: 'registry-token'}) + : new Response('unavailable', {status: 503}); + }); + const github = { + rest: {repos: {listForOrg: 'repos', listReleases: 'releases'}}, + async paginate(route, options) { + if (route === 'repos') { + return [{name: 'Alpha'}]; + } + if (route === 'releases') { + return []; + } + return options.package_name ? [ + {id: 'retained', name: 'root', metadata: {container: {tags: ['latest']}}}, + {id: 'stale', name: 'stale', metadata: {container: {tags: ['old']}}}, + ] : [{name: 'alpha'}]; + }, + async request() { assert.fail('Unsafe deletion'); }, + }; + const result = await cleanupGhcr({ + github, core: summaryCore(), username: 'user', accessToken: 'token', dryRun: false, maxDeletions: 3, + }); + assert.equal(result.operations, 0); + assert.equal(result.selected, 0); +}); diff --git a/tests/cloudsmith-backfill.test.mjs b/tests/cloudsmith-backfill.test.mjs index 696d0fab..5fc30c6f 100644 --- a/tests/cloudsmith-backfill.test.mjs +++ b/tests/cloudsmith-backfill.test.mjs @@ -1,4 +1,7 @@ import assert from 'node:assert/strict'; +import {mkdtempSync, rmSync} from 'node:fs'; +import {tmpdir} from 'node:os'; +import path from 'node:path'; import test from 'node:test'; import { @@ -7,6 +10,7 @@ import { classifyDeb, classifyRpm, debianVersion, + prepareBackfill, replaceDebControlFields, requiresSetarchBypass, rpmBuildCompatibility, @@ -145,3 +149,29 @@ test('setarch is bypassed only for cross-architecture RPM rebuilds', () => { 'buildarch_compat: x86_64: aarch64 x86_64 noarch\n', ); }); + +test('prepareBackfill skips empty releases and closes the group before stopping on an unsafe asset', async (t) => { + const workspace = mkdtempSync(path.join(tmpdir(), 'cloudsmith-backfill-test-')); + t.after(() => rmSync(workspace, {recursive: true, force: true})); + t.mock.method(globalThis, 'fetch', () => assert.fail('Unexpected download')); + const groups = []; + const github = { + rest: {repos: {listReleases: 'releases'}}, + async paginate() { + return [ + {tag_name: 'v1.0.0', published_at: '2020-01-01', assets: []}, + {tag_name: 'v2.0.0', published_at: '2020-02-01', assets: [{name: '../unsafe.deb'}]}, + {tag_name: 'v3.0.0', published_at: '2020-03-01', assets: [{name: 'sunshine.deb'}]}, + ]; + }, + }; + const core = { + info() {}, + startGroup(label) { groups.push(label); }, + endGroup() { groups.push('end'); }, + setOutput() { assert.fail('Unexpected output after failure'); }, + }; + await assert.rejects(prepareBackfill({github, core, token: 'token', workspace, repository: 'Sunshine'}), + /Unsafe release asset name/); + assert.deepEqual(groups, ['v2.0.0: 1 package assets', 'end']); +}); diff --git a/tests/projects.test.mjs b/tests/projects.test.mjs new file mode 100644 index 00000000..129d7fe3 --- /dev/null +++ b/tests/projects.test.mjs @@ -0,0 +1,83 @@ +import assert from 'node:assert/strict'; +import {readFileSync} from 'node:fs'; +import {setImmediate} from 'node:timers/promises'; +import vm from 'node:vm'; +import test from 'node:test'; + +const script = readFileSync(new URL('../docs/source/_static/js/projects.js', import.meta.url), 'utf8'); + +function page({fetch, readyState = 'complete', failList = false, hasOverview = true}) { + const errors = []; + const elements = []; + const listeners = new Map(); + function createElement(tag) { + const element = { + tag, children: [], style: {}, + appendChild(child) { + if (failList && tag === 'ul') { + throw new Error('List unavailable'); + } + this.children.push(child); + }, + }; + elements.push(element); + return element; + } + const overview = createElement('div'); + vm.runInNewContext(script, { + document: { + readyState, + createElement, + getElementById: () => hasOverview ? overview : null, + getElementsByClassName: () => [], + addEventListener: (name, handler) => listeners.set(name, handler), + }, + fetch, + console: {error: (...args) => errors.push(args)}, + }); + return {errors, elements, listeners}; +} + +test('projects initialize after DOMContentLoaded and render active projects before archived ones', async () => { + let requests = 0; + const result = page({ + readyState: 'loading', + fetch: async () => { + requests += 1; + return Response.json({ + archived: {child: {name: 'Alpha', tags: ['archived']}}, + active: {child: {name: 'Zulu', urls: {documentation: 'https://example.com/docs'}}}, + }); + }, + }); + assert.equal(requests, 0); + result.listeners.get('DOMContentLoaded')(); + await setImmediate(); + assert.equal(requests, 1); + const list = result.elements.find(({tag}) => tag === 'ul'); + assert.deepEqual(list.children.map((item) => item.children[0].textContent), ['Zulu', 'Alpha (Archived)']); + assert.deepEqual(result.errors, []); +}); + +test('projects show their existing error message when fetching fails', async () => { + const result = page({fetch: async () => { throw new Error('Offline'); }}); + await setImmediate(); + const list = result.elements.find(({tag}) => tag === 'ul'); + assert.equal(list.children[0].textContent, 'Failed to load projects. Please try again later.'); + assert.equal(result.errors.length, 1); + assert.equal(result.errors[0][0], 'Error fetching projects:'); +}); + +test('projects handle rejection when rendering the error message also fails', async () => { + const result = page({fetch: async () => { throw new Error('Offline'); }, failList: true}); + await setImmediate(); + assert.deepEqual(result.errors.map(([message]) => message), [ + 'Error fetching projects:', 'Error initializing projects:', + ]); + assert.equal(result.errors[1][1].message, 'List unavailable'); +}); + +test('projects skip initialization on pages without an overview', () => { + const result = page({fetch: () => assert.fail('Unexpected request'), hasOverview: false}); + assert.equal(result.elements.length, 1); +});