Public: anyone can read this; the crew can change it.
A test that reads an /api/v1 answer disagreeing with openapi.json goes red #18013
Merged 14h 14m ago, live in production
mason/forge:mason/17988-a-test-that-reads-an-api-v1-answer-disag → main 0 commits fork
3 files
changed +16
−7
docs/features/api-shape-changes.md +1 −1
| @@ -4,7 +4,7 @@ row: API shapes are a contract: a field never changes type, and changes are list | ||
| 4 | 4 | --- |
| 5 | 5 | # API shapes are a contract |
| 6 | 6 | |
| 7 | A field in an API answer keeps its type. A richer value is a new field beside the old one; the old one stays what it was. `openapi.json` is the source of truth: `test/approval-base.test.ts` reads the pull detail answer against the schema the document declares (`test/support/conforms.ts`) and fails when they part. A route's tests add the same check as they gain samples. | |
| 7 | A field in an API answer keeps its type. A richer value is a new field beside the old one; the old one stays what it was. `openapi.json` is the source of truth: `test/approval-base.test.ts` reads the pull detail answer against the schema the document declares (`test/support/conforms.ts`) and fails when they part. Every `/api/v1` answer a test reads is checked the same way: the suite's preload wraps `fetch` (`test/helpers/contract-fetch.ts`) and a disagreement fails the test that read it, naming the field. `FORGE_CONTRACT_COLLECT=1 bun test …` only lists them on stderr, for auditing a schema. | |
| 8 | 8 | |
| 9 | 9 | ## Changes clients follow |
| 10 | 10 |
test/helpers/contract-fetch.ts +8 −4
| @@ -9,8 +9,9 @@ | ||
| 9 | 9 | * Forgejo whose repo `id` is an integer, a stub pull for a CLI test) shares the `/api/v1/` prefix |
| 10 | 10 | * but answers its own shape, which openapi.json does not describe (#17987). |
| 11 | 11 | * |
| 12 | * Collect mode (the default) writes each disagreement to stderr as `CONTRACT GET <pathname>: …` | |
| 13 | * and fails nothing; `contractStrict(true)` throws instead, naming the field. | |
| 12 | * Strict by default (#17988): a disagreement throws, naming the field, so the test that read it | |
| 13 | * goes red. `FORGE_CONTRACT_COLLECT=1` starts in collect mode instead, for the next audit of a | |
| 14 | * schema: each disagreement goes to stderr as `CONTRACT GET <pathname>: …` and nothing fails. | |
| 14 | 15 | */ |
| 15 | 16 | import { disagreements } from "../support/conforms.ts"; |
| 16 | 17 | |
| @@ -21,10 +22,13 @@ interface Route { | ||
| 21 | 22 | schema: S | undefined; |
| 22 | 23 | } |
| 23 | 24 | |
| 24 | let strict = false; | |
| 25 | /** The mode a process starts in: strict unless an audit asks to collect. */ | |
| 26 | export const STRICT_BY_DEFAULT = process.env.FORGE_CONTRACT_COLLECT !== "1"; | |
| 27 | ||
| 28 | let strict = STRICT_BY_DEFAULT; | |
| 25 | 29 | let built: Promise<{ doc: S; routes: Route[] }> | undefined; |
| 26 | 30 | |
| 27 | /** Throw on a disagreement (true) or only report it on stderr (false, the default). */ | |
| 31 | /** Throw on a disagreement (true, the default) or only report it on stderr (false). */ | |
| 28 | 32 | export function contractStrict(on: boolean): void { |
| 29 | 33 | strict = on; |
| 30 | 34 | } |
test/support/conforms.test.ts +7 −2
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test"; |
| 2 | 2 | import { openapi } from "../../src/contract/openapi.ts"; |
| 3 | 3 | import { securityFloor } from "../../src/http/respond.ts"; |
| 4 | import { contractStrict } from "../helpers/contract-fetch.ts"; | |
| 4 | import { contractStrict, STRICT_BY_DEFAULT } from "../helpers/contract-fetch.ts"; | |
| 5 | 5 | import { declared, disagreements } from "./conforms.ts"; |
| 6 | 6 | |
| 7 | 7 | const doc = { components: { schemas: {} } }; |
| @@ -50,11 +50,16 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", () | ||
| 50 | 50 | canned = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => Response.json({ id: 7, approvedAt: { at: "x" } }) }); |
| 51 | 51 | }); |
| 52 | 52 | afterAll(() => { |
| 53 | contractStrict(false); | |
| 53 | contractStrict(STRICT_BY_DEFAULT); | |
| 54 | 54 | server.stop(true); |
| 55 | 55 | canned.stop(true); |
| 56 | 56 | }); |
| 57 | 57 | |
| 58 | // First in the block, before any test sets a mode: the process starts strict (#17988). | |
| 59 | test.skipIf(!STRICT_BY_DEFAULT)("strict is the default: a wrong type rejects with no contractStrict call", async () => { | |
| 60 | await expect(fetch(`http://127.0.0.1:${server.port}/api/v1/repos/a/b/pulls/1`)).rejects.toThrow("$.approvedAt: is object, declared string|null"); | |
| 61 | }); | |
| 62 | ||
| 58 | 63 | test("a canned origin's answer under /api/v1/ is not this forge's and is not held (#17987)", async () => { |
| 59 | 64 | contractStrict(true); |
| 60 | 65 | expect((await fetch(`http://127.0.0.1:${canned.port}/api/v1/repos/a/b/pulls/1`)).status).toBe(200); |
new above
offline