From 64dcf9ff63029938dd5c3a0f727a28d742373e6a Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:16:58 -0500 Subject: [PATCH] test: cover isProd-dependent url/editUrl branches in docusaurus.config.ts docusaurus.config.ts is excluded from the c8 coverage gate, and the existing lib/docusaurusConfigPreprocessor.test.mts only exercises the markdown.preprocessor function. Its three isProd-dependent fields (top-level url, docs preset editUrl, analyses plugin editUrl) had no unit assertion at all. Adds a test-only harness (lib/testHelpers/printConfigBuildUrls.mjs, following the resolveExtensionlessTs.mjs pattern already used to import docusaurus.config.ts directly in tests) that spawns a fresh process per env combination, since getBuildContext() is evaluated once at module load time. Verified the new test actually detects a regression: a manual mutation inverting the isProd check (reverted before commit) made the localhost-fallback test fail with the wrong URL. No production code changed. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- lib/docusaurusConfigBuildContext.test.mts | 87 +++++++++++++++++++++++ lib/testHelpers/printConfigBuildUrls.mjs | 17 +++++ 2 files changed, 104 insertions(+) create mode 100644 lib/docusaurusConfigBuildContext.test.mts create mode 100644 lib/testHelpers/printConfigBuildUrls.mjs diff --git a/lib/docusaurusConfigBuildContext.test.mts b/lib/docusaurusConfigBuildContext.test.mts new file mode 100644 index 00000000..bb9a201e --- /dev/null +++ b/lib/docusaurusConfigBuildContext.test.mts @@ -0,0 +1,87 @@ +import assert from 'node:assert/strict'; +import { describe, it } from 'node:test'; +import { spawnSync } from 'node:child_process'; +import path from 'node:path'; +import url from 'node:url'; + +// docusaurus.config.ts is excluded from the c8 coverage gate (it can't be +// imported into a running test process without reloading its top-level +// `getBuildContext(repo)` call for each env combination), so its three +// `isProd`-dependent fields (`url`, and the `editUrl` on both the docs +// preset and the analyses plugin) have no unit coverage at all. Exercise +// them here via a fresh child process per env combination, using the same +// test-only harness pattern as docusaurusConfigPreprocessor.test.mts. +const __dirname = path.dirname(url.fileURLToPath(import.meta.url)); +const repoRoot = path.join(__dirname, '..'); +const harness = path.join(__dirname, 'testHelpers', 'printConfigBuildUrls.mjs'); + +function readConfigUrls(env: Record) { + const result = spawnSync( + process.execPath, + ['--experimental-strip-types', harness], + { + cwd: repoRoot, + encoding: 'utf-8', + env: { ...process.env, ...env }, + }, + ); + + assert.equal( + result.status, + 0, + `harness exited with ${result.status}:\n${result.stderr}`, + ); + + return JSON.parse(result.stdout) as { + url: string; + docsEditUrl?: string; + analysesEditUrl?: string; + }; +} + +describe('docusaurus.config.ts (isProd-dependent branches)', () => { + it('uses the production site URL and GitHub editUrl links when BUILD_ENV=production', () => { + const { + url: siteUrl, + docsEditUrl, + analysesEditUrl, + } = readConfigUrls({ + BUILD_ENV: 'production', + DEPLOY_PRIME_URL: '', + }); + + assert.equal(siteUrl, 'https://cncf-techdocs.netlify.app'); + assert.equal(docsEditUrl, 'https://github.com/cncf/techdocs/tree/main'); + assert.equal( + analysesEditUrl, + 'https://github.com/cncf/techdocs/tree/main/analyses', + ); + }); + + it('falls back to DEPLOY_PRIME_URL and omits editUrl for non-production deploy previews', () => { + const { + url: siteUrl, + docsEditUrl, + analysesEditUrl, + } = readConfigUrls({ + BUILD_ENV: 'deploy-preview', + DEPLOY_PRIME_URL: 'https://deploy-preview-1--cncf-techdocs.netlify.app', + }); + + assert.equal( + siteUrl, + 'https://deploy-preview-1--cncf-techdocs.netlify.app', + ); + assert.equal(docsEditUrl, undefined); + assert.equal(analysesEditUrl, undefined); + }); + + it('falls back to localhost when neither production nor DEPLOY_PRIME_URL is set', () => { + const { url: siteUrl } = readConfigUrls({ + BUILD_ENV: 'development', + DEPLOY_PRIME_URL: '', + }); + + assert.equal(siteUrl, 'http://localhost:3000'); + }); +}); diff --git a/lib/testHelpers/printConfigBuildUrls.mjs b/lib/testHelpers/printConfigBuildUrls.mjs new file mode 100644 index 00000000..cff066d1 --- /dev/null +++ b/lib/testHelpers/printConfigBuildUrls.mjs @@ -0,0 +1,17 @@ +// Test-only harness: prints the isProd-dependent fields of docusaurus.config.ts +// as JSON so tests can exercise them under different env vars via a fresh +// process (docusaurus.config.ts reads process.env once at module load time). +import { registerHooks } from 'node:module'; +import { resolve } from './resolveExtensionlessTs.mjs'; + +registerHooks({ resolve }); + +const { default: config } = await import('../../docusaurus.config.ts'); + +console.log( + JSON.stringify({ + url: config.url, + docsEditUrl: config.presets[0][1].docs.editUrl, + analysesEditUrl: config.plugins[0][1].editUrl, + }), +);