Public: anyone can read this; the crew can change it.
The API's decisions read names its Server-Timing phases: session, decisions, render #4913
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4894-the-api-s-decisions-read-names-its-serve → main 1 commit fork
2 files
changed +71
−2
src/http/api.ts +4 −2
| @@ -3643,11 +3643,12 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3643 | 3643 | // The decision ledger: what the crew's decider answered and what came of |
| 3644 | 3644 | // it, newest first, with the week summed up per kind. |
| 3645 | 3645 | router.add("GET", "/api/v1/repos/:owner/:name/decisions", (ctx) => { |
| 3646 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 3646 | 3647 | const repo = loadRepo(ctx); |
| 3647 | 3648 | if (!repo) return notFound(); |
| 3648 | 3649 | const kind = ctx.url.searchParams.get("kind"); |
| 3649 | 3650 | const week = new Date(Date.now() - 7 * 24 * 60 * 60_000).toISOString(); |
| 3650 | return json({ | |
| 3651 | const read = timePhase(ctx.request, "decisions", () => ({ | |
| 3651 | 3652 | decisions: listDecisions(ctx.app.db, repo.id, { |
| 3652 | 3653 | kind: |
| 3653 | 3654 | kind === "next" || |
| @@ -3665,7 +3666,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3665 | 3666 | // A hand-over's state carries its tapper's private preferences. |
| 3666 | 3667 | .map((d) => forReader(d, ctx.auth.user)), |
| 3667 | 3668 | week: decisionStats(ctx.app.db, repo.id, week), |
| 3668 | }); | |
| 3669 | })); | |
| 3670 | return timePhase(ctx.request, "render", () => json(read)); | |
| 3669 | 3671 | }); |
| 3670 | 3672 | |
| 3671 | 3673 | router.add( |
test/api-decisions-timing.test.ts added +67
| @@ -0,0 +1,67 @@ | ||
| 1 | /** The API decisions read names its Server-Timing phases (#4894). */ | |
| 2 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { createRepo } from "../src/actions/repos.ts"; | |
| 8 | import { createSession } from "../src/core/sessions.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let cookie = ""; | |
| 19 | ||
| 20 | const cells = process.env["FORGE_CELLS"]; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | process.env["FORGE_CELLS"] = "off"; | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-decisions-timing-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | const made = await createUser(app.db, { | |
| 28 | origin: "seed", | |
| 29 | trusted: true, | |
| 30 | username: "alice", | |
| 31 | email: "[email protected]", | |
| 32 | password: "a-good-passphrase", | |
| 33 | adminIfFirst: true, | |
| 34 | }); | |
| 35 | if (!made.ok) throw new Error(String(made.error)); | |
| 36 | const alice = made.value; | |
| 37 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 38 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 39 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 40 | }); | |
| 41 | afterAll(async () => { | |
| 42 | await servers.stop(); | |
| 43 | app.close(); | |
| 44 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 45 | else process.env["FORGE_CELLS"] = cells; | |
| 46 | await rm(dataDir, { recursive: true, force: true }); | |
| 47 | }); | |
| 48 | ||
| 49 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 50 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 51 | const body = await res.text(); | |
| 52 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 53 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 54 | } | |
| 55 | ||
| 56 | test("the decisions read names session, decisions, render in order", async () => { | |
| 57 | const got = await phases("/api/v1/repos/alice/r/decisions"); | |
| 58 | expect(got.status).toBe(200); | |
| 59 | expect(got.names).toEqual(["session", "decisions", "render"]); | |
| 60 | expect(Object.keys(JSON.parse(got.body))).toEqual(["decisions", "week"]); | |
| 61 | }); | |
| 62 | ||
| 63 | test("a missing repository is a 404 naming session", async () => { | |
| 64 | const got = await phases("/api/v1/repos/alice/nope/decisions"); | |
| 65 | expect(got.status).toBe(404); | |
| 66 | expect(got.names).toEqual(["session"]); | |
| 67 | }); |
new above
offline