Public: anyone can read this; the crew can change it.
The api-* test files read no answer that disagrees with openapi.json #17990
Merged 16h 3m ago, live in production
scout/forge:scout/17986-the-api-test-files-read-no-answer-that-d → main 0 commits fork
5 files
changed +36
−6
docs/features/api-shape-changes.md +2
| @@ -9,11 +9,13 @@ A field in an API answer keeps its type. A richer value is a new field beside th | ||
| 9 | 9 | ## Changes clients follow |
| 10 | 10 | |
| 11 | 11 | - `GET /api/v1/repos/{owner}/{name}/pulls/{number}`: `approvedAt` is a date string or null again (it was briefly the object, #16761); the object `{at, head, base, current}` is the new field `approval`. A client that decoded the object reads `approval` instead. |
| 12 | - `GET /api/v1/telemetry/home` and `GET /api/v1/search`: the declaration now says what was always served (#17986). `live`, `main` and `behind` are an object or null; `docs` is an array of objects and `note` a string (their type slot held prose before). A generated client regenerates; the answers did not change. | |
| 12 | 13 | |
| 13 | 14 | ## Known bends, not yet changed |
| 14 | 15 | |
| 15 | 16 | - `next_cursor` is a number on some lists and a string on others. |
| 16 | 17 | - `comments` is a count in lists and an array in an issue's detail. |
| 17 | 18 | - `/user/repos` answers a bare array where other lists answer `{items, next_cursor}`. |
| 19 | - `?fields=a,b` on a cursor list answers items with only the keys named, though each item's schema declares the full set as required; the suite's contract check reads a left-out key there as asked for and still holds the kept keys' types (#17986). | |
| 18 | 20 | |
| 19 | 21 | Each of these will be fixed as a new field or a version, never in place. Not done here: a `GET /api/v1/changes` feed; this page is the list for now. No terminal screen shows it: the terminal reads the same pull through `src/tui/screens/pull.ts`, which does not use `approvedAt`. |
src/contract/openapi/routes/getApiV1Search.ts +1 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | 1 | import { type Route, limitTo } from "../shared.ts"; |
| 2 | 2 | import { SEARCH_LIMIT_MAX } from "../../../actions/search_limit.ts"; |
| 3 | 3 | |
| 4 | export const route: Route = { method: "get", path: "/api/v1/search", summary: "Search guides, issues, pulls, repositories and code; `kind` narrows it", auth: false, role: "anonymous", query: { q: "string", kind: "string", repo: "string", limit: limitTo(SEARCH_LIMIT_MAX) }, refusals: { invalid_kind: "kind is not all, repos, issues, pulls or code (422)", invalid_repo: "repo is not owner/name, or q names a different one with repo: (422)", not_found: "the repository does not exist or is unreadable (404)", invalid_limit: "limit is not a whole number from 1 to 100 (422)" }, reply: { repos: "array", issues: "array", code: "array", docs: "array of { title, path, snippet }, at most 5, guides matching; only for kind all", truncated: "object", moreCode: "boolean", codeSkipped: "boolean", note: "string, only when q is blank: what to send" } }; | |
| 4 | export const route: Route = { method: "get", path: "/api/v1/search", summary: "Search guides, issues, pulls, repositories and code; `kind` narrows it", detail: "Search guides, issues, pulls, repositories and code; `kind` narrows it. `docs` is an array of { title, path, snippet }, at most 5 guides matching, only for kind all; `note`, only when q is blank, says what to send", auth: false, role: "anonymous", query: { q: "string", kind: "string", repo: "string", limit: limitTo(SEARCH_LIMIT_MAX) }, refusals: { invalid_kind: "kind is not all, repos, issues, pulls or code (422)", invalid_repo: "repo is not owner/name, or q names a different one with repo: (422)", not_found: "the repository does not exist or is unreadable (404)", invalid_limit: "limit is not a whole number from 1 to 100 (422)" }, reply: { repos: "array", issues: "array", code: "array", docs: "array", truncated: "object", moreCode: "boolean", codeSkipped: "boolean", note: "string" } }; |
src/contract/openapi/routes/getApiV1TelemetryHome.ts +1 −1
| @@ -8,6 +8,6 @@ export const route: Route = { | ||
| 8 | 8 | "Read the figures the home is scored against: landed in 24 h; live {sha, version}, the commit this process runs (null when unstamped); main {sha}, the head of the forge's own repository's default branch, and behind {commits, pulls}, what main holds that live does not (0 and 0 when they match; both null until the forge has read its own main); builders and why idle, spend against the ceiling, what waits on the caller; the owner or an agent holding telemetry:read, with a ro token; every agent read is logged", |
| 9 | 9 | auth: true, |
| 10 | 10 | role: "member", |
| 11 | reply: { landed: "integer", live: "object", main: "object", behind: "object", builders: "object", spend: "object", waiting: "object" }, | |
| 11 | reply: { landed: "integer", live: "object|null", main: "object|null", behind: "object|null", builders: "object", spend: "object", waiting: "object" }, | |
| 12 | 12 | refuses: [401, 403], |
| 13 | 13 | }; |
test/helpers/contract-fetch.ts +10 −4
| @@ -52,14 +52,19 @@ function contract(): Promise<{ doc: S; routes: Route[] }> { | ||
| 52 | 52 | * What a GET answer at `pathname` disagrees with in openapi.json; empty when it conforms or is |
| 53 | 53 | * undeclared. |
| 54 | 54 | */ |
| 55 | async function check(pathname: string, res: Response): Promise<string[]> { | |
| 55 | async function check(url: URL, res: Response): Promise<string[]> { | |
| 56 | const pathname = url.pathname; | |
| 56 | 57 | const { doc, routes } = await contract(); |
| 57 | 58 | let best: Route | undefined; |
| 58 | 59 | for (const r of routes) if (r.regex.test(pathname) && (!best || r.literals > best.literals)) best = r; |
| 59 | 60 | if (!best?.schema) return []; |
| 60 | 61 | // A body that is not JSON is the test's own business to find, not the contract's. |
| 61 | 62 | const body: unknown = await res.clone().json().catch(() => undefined); |
| 62 | return body === undefined ? [] : disagreements(body, best.schema, doc); | |
| 63 | if (body === undefined) return []; | |
| 64 | const found = disagreements(body, best.schema, doc); | |
| 65 | // `?fields=` keeps only the keys it names: a key it left out is the answer asked for, a known | |
| 66 | // bend (docs/features/api-shape-changes.md), not a disagreement. The kept keys' types still are. | |
| 67 | return url.searchParams.has("fields") ? found.filter((d) => !/^\$\.items\[\d+\]\.[^.]+: missing$/.test(d)) : found; | |
| 63 | 68 | } |
| 64 | 69 | |
| 65 | 70 | /** Wrap `globalThis.fetch` once for the rest of the process. */ |
| @@ -70,9 +75,10 @@ export function installContractFetch(): void { | ||
| 70 | 75 | const method = (input instanceof Request ? input.method : (init?.method ?? "GET")).toUpperCase(); |
| 71 | 76 | if (method !== "GET" || res.status !== 200) return res; |
| 72 | 77 | if (!(res.headers.get("content-type") ?? "").startsWith("application/json")) return res; |
| 73 | const pathname = new URL(input instanceof Request ? input.url : String(input)).pathname; | |
| 78 | const url = new URL(input instanceof Request ? input.url : String(input)); | |
| 79 | const pathname = url.pathname; | |
| 74 | 80 | if (!pathname.startsWith("/api/v1/")) return res; |
| 75 | const list = await check(pathname, res); | |
| 81 | const list = await check(url, res); | |
| 76 | 82 | if (list.length === 0) return res; |
| 77 | 83 | if (strict) throw new Error(`GET ${pathname} disagrees with openapi.json (#17944): ${list.join("; ")}`); |
| 78 | 84 | console.error(`CONTRACT GET ${pathname}: ${list.join("; ")}`); |
test/support/conforms.test.ts +22
| @@ -36,6 +36,10 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", () | ||
| 36 | 36 | fetch: (req) => { |
| 37 | 37 | const path = new URL(req.url).pathname; |
| 38 | 38 | if (path === "/api/v1/repos/a/b/pulls/1") return Response.json({ ...valid, approvedAt: { at: "x" } }); |
| 39 | if (path === "/api/v1/repos/a/b/pulls") { | |
| 40 | const number = new URL(req.url).searchParams.get("fields") === "number,title" ? 7 : "seven"; | |
| 41 | return Response.json({ items: [{ number, title: "t" }], next_cursor: null }); | |
| 42 | } | |
| 39 | 43 | return Response.json(valid); |
| 40 | 44 | }, |
| 41 | 45 | }); |
| @@ -56,6 +60,14 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", () | ||
| 56 | 60 | expect((await fetch(`${url}/alice/lib/pulls/1`)).status).toBe(200); |
| 57 | 61 | }); |
| 58 | 62 | |
| 63 | test("a ?fields= list's left-out keys are asked for, not missing; a kept key's type still is held", async () => { | |
| 64 | contractStrict(true); | |
| 65 | const url = `http://127.0.0.1:${server.port}/api/v1/repos/a/b/pulls`; | |
| 66 | expect((await fetch(`${url}?fields=number,title`)).status).toBe(200); | |
| 67 | await expect(fetch(`${url}?fields=number`)).rejects.toThrow("$.items[0].number: is string, declared integer"); | |
| 68 | await expect(fetch(url)).rejects.toThrow("$.items[0].id: missing"); | |
| 69 | }); | |
| 70 | ||
| 59 | 71 | test("collect mode reports on stderr and fails nothing", async () => { |
| 60 | 72 | contractStrict(false); |
| 61 | 73 | const lines: string[] = []; |
| @@ -69,3 +81,13 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", () | ||
| 69 | 81 | expect(lines).toEqual(["CONTRACT GET /api/v1/repos/a/b/pulls/1: $.approvedAt: is object, declared string|null"]); |
| 70 | 82 | }); |
| 71 | 83 | }); |
| 84 | ||
| 85 | test("the answers the api-* files read are declared as served (#17986)", () => { | |
| 86 | const full = openapi("https://forge.test", "test", "admin") as Record<string, unknown>; | |
| 87 | // /search: `docs` and `note` were prose in the type slot, so no array or string matched them. | |
| 88 | const search = declared(full, "/api/v1/search", "get"); | |
| 89 | expect(disagreements({ docs: [{ title: "t", path: "p", snippet: "s" }], note: "send q" }, search, full)).toEqual([]); | |
| 90 | // /telemetry/home: live, main and behind are null until the forge has stamped and read its main. | |
| 91 | const home = declared(full, "/api/v1/telemetry/home", "get"); | |
| 92 | expect(disagreements({ live: null, main: null, behind: null }, home, full)).toEqual([]); | |
| 93 | }); |
new above
offline