|
| 1 | +/* llmsContract.test.ts — agent-facing docs contract regression test. |
| 2 | + * |
| 3 | + * CLAUDE.md rule 22 ("contract changes touch all surfaces in one PR") and |
| 4 | + * rule 18 ("registry-iterating regression tests, not hand-typed lists") |
| 5 | + * together require that any agent-visible contract added to the API has a |
| 6 | + * test that fails if the docs silently drift. This file is the single |
| 7 | + * place that owns the docs-coverage assertions for that contract. |
| 8 | + * |
| 9 | + * The CONTRACT_MARKERS registry below lists every (file, substring) pair |
| 10 | + * the docs must mention. To add a new field to the contract, append a new |
| 11 | + * row — the test iterates the registry so a missing site fails by name, |
| 12 | + * not silently. To remove a row, document why in the PR (the field is no |
| 13 | + * longer agent-facing). |
| 14 | + * |
| 15 | + * The `redeploy` rows below were added with the `docs/deploy-new-redeploy-param` |
| 16 | + * PR, which mirrors the `redeploy=true` form field on POST /deploy/new that |
| 17 | + * the api adds in the parallel PR. Until that api PR ships, the live API |
| 18 | + * will reject the flag, but the docs are additive and safe to land first. |
| 19 | + * |
| 20 | + * NOTE on the build pipeline: `npm run build` runs scripts/fetch-content.mjs |
| 21 | + * before `vite build`, which would normally overwrite public/llms.txt from |
| 22 | + * the InstaNode-dev/content repo. The script now PRESERVES the local |
| 23 | + * public/llms.txt if upstream is missing the required markers (see |
| 24 | + * SYNC_FILES.requireMarkers in fetch-content.mjs) — so this test passes in |
| 25 | + * CI even before the content-repo follow-up PR lands. Once the content-repo |
| 26 | + * PR ships, both sides have the markers and the guard becomes a no-op. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, it, expect } from 'vitest' |
| 30 | +import { readFileSync, existsSync } from 'fs' |
| 31 | +import { resolve } from 'path' |
| 32 | + |
| 33 | +// Resolve from instanode-web/ project root regardless of where vitest is |
| 34 | +// invoked from. import.meta.url is file:///…/src/lib/llmsContract.test.ts. |
| 35 | +const PROJECT_ROOT = resolve(new URL(import.meta.url).pathname, '../../..') |
| 36 | + |
| 37 | +type DocsMarker = { |
| 38 | + // Human-readable contract item — printed in the failure message so an |
| 39 | + // engineer who breaks this test knows what they removed. |
| 40 | + field: string |
| 41 | + // Project-root-relative path to the docs file that MUST mention `field`. |
| 42 | + file: string |
| 43 | + // Substring(s) the file MUST contain. ALL must be present for the row |
| 44 | + // to pass — this is an AND, not an OR. |
| 45 | + mustContain: string[] |
| 46 | +} |
| 47 | + |
| 48 | +// Single source of truth for "what does an agent need to find in the |
| 49 | +// docs to use the contract correctly". Add a row when adding an agent- |
| 50 | +// facing field to the API; delete a row when the field is retired. |
| 51 | +const CONTRACT_MARKERS: DocsMarker[] = [ |
| 52 | + { |
| 53 | + field: 'POST /deploy/new ?redeploy=true form field (in-place update)', |
| 54 | + file: 'public/llms.txt', |
| 55 | + mustContain: ['redeploy=true', '"redeployed":'], |
| 56 | + }, |
| 57 | + { |
| 58 | + field: 'POST /deploy/new ?redeploy=true form field (in-place update)', |
| 59 | + file: 'public/llms-full.txt', |
| 60 | + mustContain: ['redeploy=true', '"redeployed":', 'no_matching_deployment'], |
| 61 | + }, |
| 62 | +] |
| 63 | + |
| 64 | +describe('agent docs contract — POST /deploy/new redeploy=true', () => { |
| 65 | + // One it() per registry row so a failure names the file and field. |
| 66 | + for (const row of CONTRACT_MARKERS) { |
| 67 | + it(`${row.file} documents ${row.field}`, () => { |
| 68 | + const path = resolve(PROJECT_ROOT, row.file) |
| 69 | + expect(existsSync(path), `${row.file} does not exist at ${path}`).toBe(true) |
| 70 | + const body = readFileSync(path, 'utf8') |
| 71 | + for (const needle of row.mustContain) { |
| 72 | + // Includes-check, not regex, because the marker strings contain |
| 73 | + // characters (quotes, equals, colons) that would need escaping |
| 74 | + // and the docs are large enough that a regex backtrack on a |
| 75 | + // typo would dominate the failure output. A plain substring |
| 76 | + // miss prints "expected … to include 'redeploy=true'" which is |
| 77 | + // exactly the signal a docs author needs. |
| 78 | + expect( |
| 79 | + body.includes(needle), |
| 80 | + `${row.file}: expected the ${row.field} contract row to include the substring ${JSON.stringify(needle)}. If you intentionally removed this docs surface, also remove the corresponding row from CONTRACT_MARKERS in src/lib/llmsContract.test.ts and link the deprecation PR in the commit message.` |
| 81 | + ).toBe(true) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | + |
| 86 | + it('CONTRACT_MARKERS registry is non-empty (guards against accidental wipeout)', () => { |
| 87 | + // Defensive: if someone ever lands a refactor that empties the |
| 88 | + // registry, this test fails so the for-loop above doesn't silently |
| 89 | + // pass with zero assertions. Per CLAUDE.md rule 18, the registry |
| 90 | + // IS the contract — a deleted row is a deleted promise. |
| 91 | + expect(CONTRACT_MARKERS.length).toBeGreaterThan(0) |
| 92 | + }) |
| 93 | +}) |
0 commit comments