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

The API's crew scorecard read names its Server-Timing phases: session, scorecard, render #4965

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/4950-the-api-s-crew-scorecard-read-names-its → main 1 commit fork

  • Merged. scout/4950-the-api-s-crew-scorecard-read-names-its was merged into main as a5a2be5.

Review

Sign in to review this change.
2 files changed +62 −2
src/http/api.ts +4 −2
@@ -1685,10 +1685,12 @@ export function apiRoutes(router: Router<Handler>): void {
16851685 // The crew's scorecard (`core/scorecard.ts`): a read, so any scope of a
16861686 // forge admin's token, as the MCP tool; 401 without one, 403 for anyone else.
16871687 router.add("GET", "/api/v1/crew/scorecard", (ctx) => {
1688 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
16881689 const denied = requireScope(ctx, "ro");
16891690 if (denied) return denied;
1690 const card = readScorecards(ctx.app, ctx.auth.user);
1691 return card ? json(card) : apiError("forbidden", 403, "the crew's scorecard is a forge admin's to read");
1691 const card = timePhase(ctx.request, "scorecard", () => readScorecards(ctx.app, ctx.auth.user));
1692 if (!card) return apiError("forbidden", 403, "the crew's scorecard is a forge admin's to read");
1693 return timePhase(ctx.request, "render", () => json(card));
16921694 });
16931695
16941696 // Crew teams (`core/teams.ts`): a forge admin who is a person; the page's door.
test/api-scorecard-timing.test.ts added +58
@@ -0,0 +1,58 @@
1 /** The API crew scorecard names its Server-Timing phases (#3889). */
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 { createSession } from "../src/core/sessions.ts";
8 import { createUser } from "../src/core/users.ts";
9 import { serve, type Servers } from "../src/http/server.ts";
10 import { quietLogs } from "./helpers/globals.ts";
11
12 quietLogs();
13
14 let dataDir: string;
15 let app: App;
16 let servers: Servers;
17 let cookie = "";
18
19 const cells = process.env["FORGE_CELLS"];
20
21 beforeAll(async () => {
22 process.env["FORGE_CELLS"] = "off";
23 dataDir = await mkdtemp(join(tmpdir(), "forge-api-scorecard-timing-"));
24 app = createApp({ dataDir, port: 0 });
25 servers = await serve(app);
26 const made = await createUser(app.db, {
27 origin: "seed",
28 trusted: true,
29 username: "alice",
30 email: "[email protected]",
31 password: "a-good-passphrase",
32 adminIfFirst: true,
33 });
34 if (!made.ok) throw new Error(String(made.error));
35 const alice = made.value;
36 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
37 });
38 afterAll(async () => {
39 await servers.stop();
40 app.close();
41 if (cells === undefined) delete process.env["FORGE_CELLS"];
42 else process.env["FORGE_CELLS"] = cells;
43 await rm(dataDir, { recursive: true, force: true });
44 });
45
46 async function phases(path: string): Promise<{ status: number; names: string[]; body: string }> {
47 const res = await fetch(`${servers.url}${path}`, { headers: { cookie } });
48 const body = await res.text();
49 const names = (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!);
50 return { status: res.status, names: names.slice(names.indexOf("app") + 1), body };
51 }
52
53 test("the crew scorecard names session, scorecard, render in order", async () => {
54 const got = await phases("/api/v1/crew/scorecard");
55 expect(got.status).toBe(200);
56 expect(got.names).toEqual(["session", "scorecard", "render"]);
57 expect(JSON.parse(got.body)).toBeTruthy();
58 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs