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

The api-* test files read no answer that disagrees with openapi.json #17990

opened this

Merged 16h 3m ago, live in production

scout/forge:scout/17986-the-api-test-files-read-no-answer-that-d → main 0 commits fork

  • Merged. scout/17986-the-api-test-files-read-no-answer-that-d was merged into main as 2d01f4a.

Review

Sign in to review this change.
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
99 ## Changes clients follow
1010
1111 - `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.
1213
1314 ## Known bends, not yet changed
1415
1516 - `next_cursor` is a number on some lists and a string on others.
1617 - `comments` is a count in lists and an array in an issue's detail.
1718 - `/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).
1820
1921 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 @@
11 import { type Route, limitTo } from "../shared.ts";
22 import { SEARCH_LIMIT_MAX } from "../../../actions/search_limit.ts";
33
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 = {
88 "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",
99 auth: true,
1010 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" },
1212 refuses: [401, 403],
1313 };
test/helpers/contract-fetch.ts +10 −4
@@ -52,14 +52,19 @@ function contract(): Promise<{ doc: S; routes: Route[] }> {
5252 * What a GET answer at `pathname` disagrees with in openapi.json; empty when it conforms or is
5353 * undeclared.
5454 */
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;
5657 const { doc, routes } = await contract();
5758 let best: Route | undefined;
5859 for (const r of routes) if (r.regex.test(pathname) && (!best || r.literals > best.literals)) best = r;
5960 if (!best?.schema) return [];
6061 // A body that is not JSON is the test's own business to find, not the contract's.
6162 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;
6368 }
6469
6570 /** Wrap `globalThis.fetch` once for the rest of the process. */
@@ -70,9 +75,10 @@ export function installContractFetch(): void {
7075 const method = (input instanceof Request ? input.method : (init?.method ?? "GET")).toUpperCase();
7176 if (method !== "GET" || res.status !== 200) return res;
7277 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;
7480 if (!pathname.startsWith("/api/v1/")) return res;
75 const list = await check(pathname, res);
81 const list = await check(url, res);
7682 if (list.length === 0) return res;
7783 if (strict) throw new Error(`GET ${pathname} disagrees with openapi.json (#17944): ${list.join("; ")}`);
7884 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)", ()
3636 fetch: (req) => {
3737 const path = new URL(req.url).pathname;
3838 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 }
3943 return Response.json(valid);
4044 },
4145 });
@@ -56,6 +60,14 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", ()
5660 expect((await fetch(`${url}/alice/lib/pulls/1`)).status).toBe(200);
5761 });
5862
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
5971 test("collect mode reports on stderr and fails nothing", async () => {
6072 contractStrict(false);
6173 const lines: string[] = [];
@@ -69,3 +81,13 @@ describe("the suite's fetch holds /api/v1 answers to openapi.json (#17961)", ()
6981 expect(lines).toEqual(["CONTRACT GET /api/v1/repos/a/b/pulls/1: $.approvedAt: is object, declared string|null"]);
7082 });
7183 });
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 });

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

Keyboard shortcuts

Docs