Files
supabase/apps/docs/scripts/check-removed-anchors.test.ts
Miranda Limonczenko aec7aed134 ci(docs): fail PRs that remove a linked guide section (#52100)
Closes DOCS-1486

## Problem

Agents are slow to update what they remember about the docs, and nothing
protects a section when it moves to another page.

This leaves a trail when sections move: the PR fails unless the heading
stays with a summary and a link to where the content went.

## Solution

A new `Docs removed anchors` workflow runs `pnpm check:removed-anchors`
from `apps/docs`.

- **What it compares:** H2 and H3 anchors between the base branch and
the PR, only on changed guide pages and pages that include a changed
partial. Partials are expanded, nested ones too.
- **When it fails:** a file anywhere in `apps`, `packages`, or
`examples` still links to a removed anchor. That covers docs content,
Studio, www and the blog, and kb, plus same-page `#anchor` links. Test
files and generated output are skipped. The failure names each anchor
and every linking `file:line`, and annotates those lines.
- **How anchors are computed:** with `getAnchor` from `packages/ui`, the
same function that sets rendered heading ids, so `## Title [#custom-id]`
matches on the custom id.
- **Bypass:** the `skip-anchor-check` label turns the errors into
warnings and passes the check. The failure message names the label and
the fixes.
- **No `paths` filter:** a required check whose workflow never starts
leaves unrelated PRs waiting. The job runs on every PR and exits before
installing anything when docs content is unchanged.

Out of scope: deleted or moved pages, and links from outside this repo.

The workflow checks out text files across `apps`, `packages`, and
`examples`, and skips images and video, so the search doesn't download
them.

The check only blocks merges once it's a required status check on
`master`. That needs a repo admin.

## Skip the check

For a deliberate removal:

1. Add the `skip-anchor-check` label to the PR.
2. The check re-runs and passes. The links it would have failed on show
as warnings.

Remove the label to turn the check back on.

## Manual testing

1. From `apps/docs`, delete `[#leaked-key]` from the heading in
`content/guides/getting-started/api-keys.mdx`.
2. Run `pnpm check:removed-anchors`. It exits 1 and names
`troubleshooting/rotating-anon-service-and-jwt-secrets-1Jq6yd.mdx:39`.
3. Run `PR_LABELS='["skip-anchor-check"]' pnpm check:removed-anchors`.
It prints the same report and exits 0.
4. Run `pnpm vitest run scripts/check-removed-anchors.test.ts`. All
tests pass.
2026-10-29 14:23:13 -07:00

98 lines
3.5 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { findBrokenAnchors, hasBypassLabel } from './check-removed-anchors'
const page = 'apps/docs/content/guides/example.mdx'
const route = '/guides/example'
function check(base: Record<string, string>, head: Record<string, string>) {
return findBrokenAnchors({
changedFiles: Object.keys(head).filter((file) => base[file] !== head[file]),
readBase: async (file) => base[file],
readHead: async (file) => head[file],
findFiles: async (needle) => Object.keys(head).filter((file) => head[file].includes(needle)),
})
}
describe('findBrokenAnchors', () => {
it('reports only removed headings that something links to, matching custom ids', async () => {
const linker = 'apps/docs/content/troubleshooting/example.mdx'
const base = {
[page]: '## Kept\n\nOld body.\n\n## Rotate a key [#leaked-key]\n\n## Unlinked\n',
[linker]: '[a](/docs/guides/example#kept)\n[b](/docs/guides/example#leaked-key)\n',
}
const head = {
...base,
[page]:
'## Kept\n\nFor more information about rotating keys, see [Rotate keys](/docs/guides/rotate).\n',
}
expect(await check(base, head)).toEqual([
{ page, route, anchor: 'leaked-key', linkedFrom: [{ file: linker, line: 2 }] },
])
})
it('names links from other apps and from the same page', async () => {
const studio = 'apps/studio/components/Example.tsx'
const blog = 'apps/www/_blog/example.mdx'
const base = {
[page]: 'See [the setup](#set-up).\n\n## Set up\n',
[studio]: "const DOCS_LINK = '/guides/example#set-up'\n",
[blog]: '[Set up](https://supabase.com/docs/guides/example/?ref=nav#set-up)\n',
}
const head = { ...base, [page]: 'See [the setup](#set-up).\n' }
expect(await check(base, head)).toEqual([
{
page,
route,
anchor: 'set-up',
linkedFrom: [
{ file: page, line: 1 },
{ file: studio, line: 1 },
{ file: blog, line: 1 },
],
},
])
})
it('passes when the same change updates every link', async () => {
const linker = 'apps/studio/components/Example.tsx'
const base = {
[page]: '## Set up\n',
[linker]: "const DOCS_LINK = '/guides/example#set-up'\n",
}
const head = {
[page]: '## New home\n',
[linker]: "const DOCS_LINK = '/guides/example#new-home'\n",
}
expect(await check(base, head)).toEqual([])
})
it('fails when a heading is removed from a nested partial', async () => {
const linker = 'apps/docs/content/troubleshooting/example.mdx'
const outer = 'apps/docs/content/_partials/outer.mdx'
const inner = 'apps/docs/content/_partials/inner.mdx'
const shared = {
[page]: '<$Partial path="outer.mdx" variables={{ "name": "Shared" }} />\n',
[outer]: '<$Partial path="inner.mdx" variables={{ "name": "{{ .name }}" }} />\n',
[linker]: '[Shared section](/docs/guides/example#shared-section)\n',
}
const base = { ...shared, [inner]: '## {{ .name }} section\n' }
const head = { ...shared, [inner]: '## Other section\n' }
expect(await check(base, head)).toEqual([
{ page, route, anchor: 'shared-section', linkedFrom: [{ file: linker, line: 1 }] },
])
})
})
describe('hasBypassLabel', () => {
it('recognizes the bypass label among the PR labels', () => {
expect(hasBypassLabel('["documentation","skip-anchor-check"]')).toBe(true)
expect(hasBypassLabel('["documentation"]')).toBe(false)
expect(hasBypassLabel(undefined)).toBe(false)
})
})