Public: anyone can read this; the crew can change it.
The API's run read names its Server-Timing phases: session, run, carried #5170
scout/forge:scout/5157-the-api-s-run-read-names-its-server-timi → main 1 commit fork
2 files
changed +78
−5
src/http/api.ts +6 −5
| @@ -6102,13 +6102,14 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 6102 | 6102 | router.add("GET", "/api/v1/repos/:owner/:name/runs/:id", (ctx) => { |
| 6103 | 6103 | const repo = loadRepo(ctx); |
| 6104 | 6104 | if (!repo) return notFound(); |
| 6105 | const run = getRepoRun(ctx.app, ctx.auth.user, repo, ctx.params.id ?? ""); | |
| 6105 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); | |
| 6106 | const run = timePhase(ctx.request, "run", () => | |
| 6107 | getRepoRun(ctx.app, ctx.auth.user, repo, ctx.params.id ?? ""), | |
| 6108 | ); | |
| 6106 | 6109 | if (!run.ok) return notFound(); |
| 6107 | 6110 | // The merged pulls a finished production deploy carries (`core/carried.ts`). |
| 6108 | return json({ | |
| 6109 | ...run.value, | |
| 6110 | carries: carriedBy(ctx.app.db, run.value.id), | |
| 6111 | }); | |
| 6111 | const carries = timePhase(ctx.request, "carried", () => carriedBy(ctx.app.db, run.value.id)); | |
| 6112 | return json({ ...run.value, carries }); | |
| 6112 | 6113 | }); |
| 6113 | 6114 | |
| 6114 | 6115 | // The log: text, or a live tail as server-sent events (`?follow=1`, or |
test/api-run-timing.test.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** The API run read names its Server-Timing phases (#5157). */ | |
| 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 { createRun } from "../src/core/runs.ts"; | |
| 9 | import { createSession } from "../src/core/sessions.ts"; | |
| 10 | import { createUser } from "../src/core/users.ts"; | |
| 11 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let cookie = ""; | |
| 20 | let runId = ""; | |
| 21 | ||
| 22 | const cells = process.env["FORGE_CELLS"]; | |
| 23 | ||
| 24 | beforeAll(async () => { | |
| 25 | process.env["FORGE_CELLS"] = "off"; | |
| 26 | dataDir = await mkdtemp(join(tmpdir(), "forge-api-run-timing-")); | |
| 27 | app = createApp({ dataDir, port: 0 }); | |
| 28 | servers = await serve(app); | |
| 29 | const made = await createUser(app.db, { | |
| 30 | origin: "seed", | |
| 31 | trusted: true, | |
| 32 | username: "alice", | |
| 33 | email: "[email protected]", | |
| 34 | password: "a-good-passphrase", | |
| 35 | adminIfFirst: true, | |
| 36 | }); | |
| 37 | if (!made.ok) throw new Error(String(made.error)); | |
| 38 | const alice = made.value; | |
| 39 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 40 | const repo = await createRepo(app, alice, { name: "r", visibility: "public" }); | |
| 41 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 42 | runId = createRun(app.db, { repoId: repo.value.id, kind: "check" }).id; | |
| 43 | }); | |
| 44 | afterAll(async () => { | |
| 45 | await servers.stop(); | |
| 46 | app.close(); | |
| 47 | if (cells === undefined) delete process.env["FORGE_CELLS"]; | |
| 48 | else process.env["FORGE_CELLS"] = cells; | |
| 49 | await rm(dataDir, { recursive: true, force: true }); | |
| 50 | }); | |
| 51 | ||
| 52 | async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> { | |
| 53 | const res = await fetch(`${servers.url}${path}`, { headers: { cookie } }); | |
| 54 | const body = await res.text(); | |
| 55 | const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!); | |
| 56 | return { status: res.status, names: names.slice(names.indexOf("app") + 1), body }; | |
| 57 | } | |
| 58 | ||
| 59 | test("a run read names session, run, carried", async () => { | |
| 60 | const got = await phases(`/api/v1/repos/alice/r/runs/${runId}`); | |
| 61 | expect(got.status).toBe(200); | |
| 62 | expect(got.names).toEqual(["session", "run", "carried"]); | |
| 63 | const run = JSON.parse(got.body); | |
| 64 | expect(run.id).toBe(runId); | |
| 65 | expect(run.carries).toEqual([]); | |
| 66 | }); | |
| 67 | ||
| 68 | test("an unknown run is a 404 naming session and run", async () => { | |
| 69 | const got = await phases("/api/v1/repos/alice/r/runs/nope"); | |
| 70 | expect(got.status).toBe(404); | |
| 71 | expect(got.names).toEqual(["session", "run"]); | |
| 72 | }); |
new above
offline