diff --git a/.github/workflows/docs-a11y-scan.yml b/.github/workflows/docs-a11y-scan.yml index 8512df32c62..34583997278 100644 --- a/.github/workflows/docs-a11y-scan.yml +++ b/.github/workflows/docs-a11y-scan.yml @@ -1,5 +1,7 @@ # Scans every docs guide on production for accessibility violations, daily. -# docs-master-alert.yml posts failures to Slack. +# The page list is the guides in apps/docs/content/guides on master that also appear in +# https://supabase.com/docs/sitemap.xml, so a guide merged since the last production deploy waits +# for the next scan. docs-master-alert.yml posts failures to Slack. name: Docs Accessibility Scan @@ -58,6 +60,7 @@ jobs: run: pnpm run e2e:docs:all --grep @a11y env: PLAYWRIGHT_BASE_URL: https://supabase.com + DOCS_E2E_SITEMAP_URL: https://supabase.com/docs/sitemap.xml - name: Upload Playwright report if: failure() diff --git a/e2e/docs/README.md b/e2e/docs/README.md index e1507a6c9c0..b0a4a599949 100644 --- a/e2e/docs/README.md +++ b/e2e/docs/README.md @@ -127,6 +127,18 @@ Run this against a deployed site, not the local dev server — see [Local docs server](#local-docs-server) for why local runs are unreliable for pages linking into reference docs or server-side auth guides. +To test only the guides a deployed site serves, set `DOCS_E2E_SITEMAP_URL` to +its sitemap. Guides missing from the sitemap, such as ones merged since the last +deploy, are skipped and listed in the output: + +```bash +PLAYWRIGHT_BASE_URL=https://supabase.com \ + DOCS_E2E_SITEMAP_URL=https://supabase.com/docs/sitemap.xml pnpm e2e:docs:all +``` + +The run fails instead when the sitemap can't be read, or when more than a tenth +of the guides are missing from it. + ## What the suite covers ### In scope @@ -174,9 +186,11 @@ The `@a11y`-tagged test scans each in-scope page with `@axe-core/playwright`. It reports WCAG 2.1 A/AA violations in the main article, and fails on the rules in `ENFORCED_RULES` in `utils/axe-helpers.ts`, which it checks across the whole page. The scans are slow, so Docs E2E runs skip them. Instead, the Docs Accessibility -Scan workflow (`.github/workflows/docs-a11y-scan.yml`) scans every MDX guide on -production daily at 05:17 UTC, and a failure posts to the docs alerts channel in -Slack. To start it by hand, run `gh workflow run docs-a11y-scan.yml`. +Scan workflow (`.github/workflows/docs-a11y-scan.yml`) scans every MDX guide +that production serves daily at 05:17 UTC, and a failure posts to the docs alerts channel in +Slack. A guide merged since the last production deploy is scanned once it's +deployed, as described in [Run every in-scope page](#run-every-in-scope-page). To +start it by hand, run `gh workflow run docs-a11y-scan.yml`. ```bash PLAYWRIGHT_BASE_URL=https://supabase.com pnpm e2e:docs:a11y diff --git a/e2e/docs/scripts/run-e2e-docs.ts b/e2e/docs/scripts/run-e2e-docs.ts index b256d1fe13d..f97d81e1031 100644 --- a/e2e/docs/scripts/run-e2e-docs.ts +++ b/e2e/docs/scripts/run-e2e-docs.ts @@ -3,6 +3,7 @@ import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { runSuite } from '../../shared/run-suite.ts' +import { onlyProductionPages } from '../utils/production-pages.ts' import { resolveAllDocsPages, resolveDocsScope } from '../utils/resolve-docs-scope.ts' runSuite({ @@ -13,7 +14,11 @@ runSuite({ baseRefEnv: 'DOCS_E2E_BASE_REF', defaultBaseUrl: 'http://localhost:3001', resolveScope: resolveDocsScope, - resolveAllPages: resolveAllDocsPages, + resolveAllPages: async (repoRoot) => { + const pages = await resolveAllDocsPages(repoRoot) + const sitemapUrl = process.env.DOCS_E2E_SITEMAP_URL + return sitemapUrl ? onlyProductionPages(pages, sitemapUrl) : pages + }, }).catch((error) => { console.error(error instanceof Error ? error.message : error) process.exit(1) diff --git a/e2e/docs/utils/production-pages.test.ts b/e2e/docs/utils/production-pages.test.ts new file mode 100644 index 00000000000..ee41fcb7ca7 --- /dev/null +++ b/e2e/docs/utils/production-pages.test.ts @@ -0,0 +1,51 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import { sitemapGuidePaths, splitByProduction } from './production-pages.ts' + +test('reads guide paths from the sitemap, including tags the formatter wrapped', () => { + const xml = ` + + + https://supabase.com/docs/guides/ai + + + https://supabase.com/docs/guides/integrations/build-a-supabase-oauth-integration/oauth-scopes + + + https://supabase.com/docs/guides/auth/ + + + https://supabase.com/docs/reference/javascript/select + + + not a url + +` + + assert.deepEqual( + [...sitemapGuidePaths(xml)], + [ + '/docs/guides/ai', + '/docs/guides/integrations/build-a-supabase-oauth-integration/oauth-scopes', + '/docs/guides/auth', + ] + ) +}) + +test('skips a page that is on master but not yet on production', () => { + const pages = Array.from({ length: 20 }, (_, index) => `/docs/guides/page-${index}`) + const production = new Set(pages.slice(1)) + assert.deepEqual(splitByProduction(pages, production), { + live: pages.slice(1), + notOnProduction: ['/docs/guides/page-0'], + }) +}) + +test('fails when too many pages are missing for the sitemap to be complete', () => { + const pages = Array.from({ length: 20 }, (_, index) => `/docs/guides/page-${index}`) + const production = new Set(pages.slice(3)) + assert.throws(() => splitByProduction(pages, production), /3 of 20 guides are missing/) +}) diff --git a/e2e/docs/utils/production-pages.ts b/e2e/docs/utils/production-pages.ts new file mode 100644 index 00000000000..5fd435a1cba --- /dev/null +++ b/e2e/docs/utils/production-pages.ts @@ -0,0 +1,53 @@ +// The sitemap's formatter can wrap a long tag as `url` +const LOC_RE = /\s*([^<]+?)\s*<\/loc\s*>/g +const DOCS_GUIDES_PREFIX = '/docs/guides/' +const SITEMAP_TIMEOUT_MS = 30_000 + +// More pages than this missing from production points to a broken sitemap, not a deploy lag +export const MAX_NOT_ON_PRODUCTION_SHARE = 0.1 + +export function sitemapGuidePaths(xml: string): Set { + const paths = new Set() + for (const match of xml.matchAll(LOC_RE)) { + let pathname: string + try { + pathname = new URL(match[1]).pathname + } catch { + continue + } + if (pathname.startsWith(DOCS_GUIDES_PREFIX)) paths.add(pathname.replace(/\/+$/, '')) + } + return paths +} + +export type ProductionSplit = { live: string[]; notOnProduction: string[] } + +export function splitByProduction(pages: string[], production: Set): ProductionSplit { + const live = pages.filter((page) => production.has(page)) + const notOnProduction = pages.filter((page) => !production.has(page)) + if (notOnProduction.length > pages.length * MAX_NOT_ON_PRODUCTION_SHARE) { + throw new Error( + `${notOnProduction.length} of ${pages.length} guides are missing from the production ` + + `sitemap, more than a deploy lag explains. Check that the sitemap is complete.` + ) + } + return { live, notOnProduction } +} + +// Keeps the pages production serves, so a guide merged since the last deploy isn't scanned yet +export async function onlyProductionPages(pages: string[], sitemapUrl: string): Promise { + const response = await fetch(sitemapUrl, { + signal: AbortSignal.timeout(SITEMAP_TIMEOUT_MS), + }).catch((error: unknown) => { + throw new Error( + `Couldn't read ${sitemapUrl}: ${error instanceof Error ? error.message : error}` + ) + }) + if (!response.ok) throw new Error(`${sitemapUrl} returned ${response.status}.`) + const production = sitemapGuidePaths(await response.text()) + if (production.size === 0) throw new Error(`${sitemapUrl} lists no docs guides.`) + + const { live, notOnProduction } = splitByProduction(pages, production) + for (const page of notOnProduction) console.error(`Skipping ${page}: not on production yet.`) + return live +}