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

opened this

Merged 14h 14m ago, live in production

mason/forge:mason/17988-a-test-that-reads-an-api-v1-answer-disag → main 0 commits fork

  • Merged. mason/17988-a-test-that-reads-an-api-v1-answer-disag was merged into main as 23d6af8.

Review

Sign in to review this change.
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
44 ---
55 # API shapes are a contract
66
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.
88
99 ## Changes clients follow
1010
test/helpers/contract-fetch.ts +8 −4
@@ -9,8 +9,9 @@
99 * Forgejo whose repo `id` is an integer, a stub pull for a CLI test) shares the `/api/v1/` prefix
1010 * but answers its own shape, which openapi.json does not describe (#17987).
1111 *
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.
1415 */
1516 import { disagreements } from "../support/conforms.ts";
1617
@@ -21,10 +22,13 @@ interface Route {
2122 schema: S | undefined;
2223 }
2324
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;
2529 let built: Promise<{ doc: S; routes: Route[] }> | undefined;
2630
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). */
2832 export function contractStrict(on: boolean): void {
2933 strict = on;
3034 }
test/support/conforms.test.ts +7 −2
@@ -1,7 +1,7 @@
11 import { afterAll, beforeAll, describe, expect, spyOn, test } from "bun:test";
22 import { openapi } from "../../src/contract/openapi.ts";
33 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";
55 import { declared, disagreements } from "./conforms.ts";
66
77 const doc = { components: { schemas: {} } };
@@ -50,11 +50,16 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", ()
5050 canned = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: () => Response.json({ id: 7, approvedAt: { at: "x" } }) });
5151 });
5252 afterAll(() => {
53 contractStrict(false);
53 contractStrict(STRICT_BY_DEFAULT);
5454 server.stop(true);
5555 canned.stop(true);
5656 });
5757
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
5863 test("a canned origin's answer under /api/v1/ is not this forge's and is not held (#17987)", async () => {
5964 contractStrict(true);
6065 expect((await fetch(`http://127.0.0.1:${canned.port}/api/v1/repos/a/b/pulls/1`)).status).toBe(200);

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

Keyboard shortcuts

Docs