Public: anyone can read this; the crew can change it.
A test fails when a new GET list in the API answers a bare array #5084
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/5062-a-test-fails-when-a-new-get-list-in-the → main 1 commit fork
2 files
changed +95
−1
CLAUDE.md +2 −1
| @@ -242,7 +242,8 @@ flag exists only in the stdio dev runner and is never reachable over SSH. | ||
| 242 | 242 | scope floor (`requireScope`, or `requireAccount` for account-level changes), |
| 243 | 243 | call the action, and map its Result with `respondAccess(error, status)` — |
| 244 | 244 | access errors have fixed shapes, anything else is a 422 code. Lists are |
| 245 | `{ items, next_cursor }` via `paged(...)`. Web forms follow the same shape | |
| 245 | `{ items, next_cursor }` via `paged(...)`, never a bare array | |
| 246 | (`test/api-list-shape.test.ts` fails on one). Web forms follow the same shape | |
| 246 | 247 | via `readForm(ctx, scope)`. |
| 247 | 248 | - **Add a TUI screen:** one file under `tui/screens/`, exporting |
| 248 | 249 | `(f: Session, ctx: Ctx, …) => Screen`. Compose `ui/widgets`; take navigation |
test/api-list-shape.test.ts added +93
| @@ -0,0 +1,93 @@ | ||
| 1 | /** | |
| 2 | * A list the API answers is `{ items, next_cursor }`, never a bare JSON array | |
| 3 | * (#5062). Every GET route of `apiRoutes` is read from the router's own table | |
| 4 | * and called with a fixture's names filled in; a route that answers an array | |
| 5 | * fails unless `BARE` names it with the open issue that will change it. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createIssue } from "../src/core/issues.ts"; | |
| 14 | import { apiRoutes } from "../src/http/api.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 17 | ||
| 18 | /** Routes that answer a bare array today → the open issue that changes them. */ | |
| 19 | const BARE: Record<string, string> = { | |
| 20 | "/api/v1/user/orgs": "#5043", | |
| 21 | "/api/v1/user/keys": "#5043", | |
| 22 | "/api/v1/user/pins": "#5043", | |
| 23 | "/api/v1/repos/:owner/:name/forks": "#4843", | |
| 24 | "/api/v1/repos/:owner/:name/labels": "#4843", | |
| 25 | "/api/v1/repos/:owner/:name/hooks": "#4843", | |
| 26 | "/api/v1/repos/:owner/:name/branches": "#5060", | |
| 27 | "/api/v1/repos/:owner/:name/tags": "#5061", | |
| 28 | "/api/v1/repos/:owner/:name/commits": "#5083", | |
| 29 | "/api/v1/user/origins": "#4841", | |
| 30 | "/api/v1/user/origin-apps": "#4841", | |
| 31 | }; | |
| 32 | ||
| 33 | const FILL: Record<string, string> = { | |
| 34 | owner: "alice", username: "alice", name: "notes", number: "1", org: "alice", | |
| 35 | slug: "main", id: "1", team: "core", scope: "alice", job: "1", oid: "0", env: "prod", | |
| 36 | }; | |
| 37 | ||
| 38 | const getPatterns = (): string[] => { | |
| 39 | const seen: string[] = []; | |
| 40 | const spy = { add(method: string, pattern: string) { if (method === "GET") seen.push(pattern); return spy; } }; | |
| 41 | apiRoutes(spy as never); | |
| 42 | return [...new Set(seen)]; | |
| 43 | }; | |
| 44 | ||
| 45 | let dataDir = ""; | |
| 46 | let app: App, servers: Servers, bearer = ""; | |
| 47 | ||
| 48 | beforeAll(async () => { | |
| 49 | dataDir = await mkdtemp(join(tmpdir(), "forge-listshape-")); | |
| 50 | app = createApp({ dataDir, port: 0 }); | |
| 51 | servers = await serve(app); | |
| 52 | const admin = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 53 | if (!admin.ok) throw new Error(admin.error); | |
| 54 | const made = await createRepo(app, admin.value, { name: "notes", visibility: "public", description: "" }); | |
| 55 | if (!made.ok) throw new Error(String(made.error)); | |
| 56 | createIssue(app.db, { repoId: made.value.id, title: "one", body: "b", authorId: admin.value.id }); | |
| 57 | const minted = await fetch(`${servers.url}/api/v1/tokens`, { | |
| 58 | method: "POST", | |
| 59 | headers: { "content-type": "application/json" }, | |
| 60 | body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }), | |
| 61 | }); | |
| 62 | bearer = ((await minted.json()) as { token: string }).token; | |
| 63 | }); | |
| 64 | ||
| 65 | afterAll(async () => { | |
| 66 | servers.stop(); | |
| 67 | app.close(); | |
| 68 | await rm(dataDir, { recursive: true, force: true }); | |
| 69 | }); | |
| 70 | ||
| 71 | async function answersArray(pattern: string): Promise<boolean> { | |
| 72 | const path = pattern.replace(/:([a-z_]+)/g, (_, k: string) => FILL[k] ?? "x").replace(/\*[a-z_]+/, "x"); | |
| 73 | const res = await fetch(`${servers.url}${path}`, { headers: { authorization: `Bearer ${bearer}` } }); | |
| 74 | const text = await res.text(); | |
| 75 | if (res.status !== 200 || !(res.headers.get("content-type") ?? "").includes("json")) return false; | |
| 76 | return Array.isArray(JSON.parse(text)); | |
| 77 | } | |
| 78 | ||
| 79 | test("no GET route answers a bare array unless the allowlist names it", async () => { | |
| 80 | const bare: string[] = []; | |
| 81 | for (const pattern of getPatterns()) if (await answersArray(pattern)) bare.push(pattern); | |
| 82 | expect(bare.filter((p) => !(p in BARE))).toEqual([]); | |
| 83 | }); | |
| 84 | ||
| 85 | test("an allowlisted route that no longer answers a bare array leaves the list", async () => { | |
| 86 | const patterns = new Set(getPatterns()); | |
| 87 | const stale: string[] = []; | |
| 88 | for (const [pattern, issue] of Object.entries(BARE)) { | |
| 89 | expect(issue).toMatch(/^#\d+$/); | |
| 90 | if (!patterns.has(pattern) || !(await answersArray(pattern))) stale.push(pattern); | |
| 91 | } | |
| 92 | expect(stale).toEqual([]); | |
| 93 | }); |
new above
offline