Public: anyone can read this; the crew can change it.

The suite's fetch reports each /api/v1 answer that disagrees with openapi.json, without failing yet #17989

opened this

Merged 12h 11m ago, live in production

Under bun test, the suite's fetch now checks every /api/v1 answer a test reads against openapi.json. For now it only reports what disagrees and fails nothing (part 1 of #17961). test/helpers/contract-fetch.ts (new) wraps globalThis.fetch.

Notes

opened this

Under bun test, the suite's fetch now checks every /api/v1 answer a test reads against openapi.json. For now it only reports what disagrees and fails nothing (part 1 of #17961).

  • test/helpers/contract-fetch.ts (new) wraps globalThis.fetch.
    • What it checks: after the real fetch, a GET whose path starts with /api/v1/ and that answers 200 application/json is matched to its route. The route document is built once, on first use, from openapi("https://forge.test", "test", "admin"). Each {param} matches [^/]+, and when several routes match, the one with the most literal segments wins.
    • How: the body is read with res.clone().json() and checked with the existing disagreements from test/support/conforms.ts.
    • What passes silently: no matching route, no declared 200 JSON schema, or a body that isn't valid JSON.
    • Modes: by default each disagreement goes to stderr as one line, CONTRACT GET <pathname>: <list>. contractStrict(true) throws `GET ${pathname} disagrees with openapi.json (#17944): …` instead.
  • test/helpers/preload.ts installs the wrapper inside the same try that registers the afterEach guard. A child that loads the preload but isn't a test run never gets it.
  • test/support/conforms.test.ts gains two cases against a Bun.serve({ port: 0 }) stub. Its valid PullDetail sample is generated from the declared schema.
    • Strict: an answer with approvedAt: {at:"x"} rejects with $.approvedAt: is object, declared string|null. A valid sample, an undeclared route and a non-API path pass untouched.
    • Collect: the same bad answer gives exactly one CONTRACT line on stderr and the test still reads the body.

Both cases fail when the wrapper isn't installed.

Left out on purpose: schema fixes (#17986, #17987), turning the throw on and the doc wording (#17988). There is no full-suite run and no change to production code or any served shape.

Checks:

  • bun test test/support/conforms.test.ts test/approval-base.test.ts: 7 pass.
  • bun x tsc --noEmit: clean.
  • bun scripts/gate.ts: 0 fail. It notes that the outer install function and the inner wrapper look alike; they are one wrapper and its installer.

To try it: bun test test/support/conforms.test.ts. To list the current disagreements, run any API test file and grep stderr for CONTRACT GET.

🤖 Generated with Claude Code

Closes #17985

commented

Under bun test, the suite's fetch now checks every /api/v1 answer a test reads against openapi.json. For now it only reports what disagrees and fails nothing (part 1 of #17961).

  • test/helpers/contract-fetch.ts (new) wraps globalThis.fetch.
    • What it checks: after the real fetch, a GET whose path starts with /api/v1/ and that answers 200 application/json is matched to its route. The route document is built once, on first use, from openapi("https://forge.test", "test", "admin"). Each {param} matches [^/]+, and when several routes match, the one with the most literal segments wins.
    • How: the body is read with res.clone().json() and checked with the existing disagreements from test/support/conforms.ts.
    • What passes silently: no matching route, no declared 200 JSON schema, or a body that isn't valid JSON.
    • Modes: by default each disagreement goes to stderr as one line, CONTRACT GET <pathname>: <list>. contractStrict(true) throws `GET ${pathname} disagrees with openapi.json (#17944): …` instead.
  • test/helpers/preload.ts installs the wrapper inside the same try that registers the afterEach guard. A child that loads the preload but isn't a test run never gets it.
  • test/support/conforms.test.ts gains two cases against a Bun.serve({ port: 0 }) stub. Its valid PullDetail sample is generated from the declared schema.
    • Strict: an answer with approvedAt: {at:"x"} rejects with $.approvedAt: is object, declared string|null. A valid sample, an undeclared route and a non-API path pass untouched.
    • Collect: the same bad answer gives exactly one CONTRACT line on stderr and the test still reads the body.

Both cases fail when the wrapper isn't installed.

Left out on purpose: schema fixes (#17986, #17987), turning the throw on and the doc wording (#17988). There is no full-suite run and no change to production code or any served shape.

Checks:

  • bun test test/support/conforms.test.ts test/approval-base.test.ts: 7 pass.
  • bun x tsc --noEmit: clean.
  • bun scripts/gate.ts: 0 fail. It notes that the outer install function and the inner wrapper look alike; they are one wrapper and its installer.

To try it: bun test test/support/conforms.test.ts. To list the current disagreements, run any API test file and grep stderr for CONTRACT GET.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs