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

The boards' sources list names its Server-Timing phases: session, sources, render #5021

Merged opened this

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

Merged

scout/forge:scout/5011-the-boards-sources-list-names-its-server → main 1 commit fork

  • Merged. scout/5011-the-boards-sources-list-names-its-server was merged into main as e4a61a1.

Review

Sign in to review this change.
2 files changed +53 −4
src/http/api.ts +5 −4
@@ -1447,16 +1447,17 @@ export function apiRoutes(router: Router<Handler>): void {
14471447 router.add("GET", "/api/v1/contribute", (ctx) => json(welcome(ctx.app)));
14481448
14491449 router.add("GET", "/api/v1/boards/sources", (ctx) => {
1450 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
14501451 const denied = boardsAdmin(ctx);
14511452 if (denied) return denied;
1452 return json({
1453 sources: Object.entries(SOURCES).map(([name, s]) => ({
1453 const sources = timePhase(ctx.request, "sources", () =>
1454 Object.entries(SOURCES).map(([name, s]) => ({
14541455 name,
14551456 time: s.time,
14561457 columns: s.columns,
14571458 ...(s.docs ? { docs: s.docs } : {}),
1458 })),
1459 });
1459 })));
1460 return timePhase(ctx.request, "render", () => json({ sources }));
14601461 });
14611462
14621463 // A domain drill and its verdict (`kernel/domain_drill.ts`): an admin
test/boards-sources-timing.test.ts added +48
@@ -0,0 +1,48 @@
1 /** GET /api/v1/boards/sources names its phases in Server-Timing. */
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 { serve, type Servers } from "../src/http/server.ts";
8 import { seedAdmin } from "./helpers/admin.ts";
9
10 let dataDir = "";
11 let app: App, servers: Servers, bearer = "";
12
13 beforeAll(async () => {
14 dataDir = await mkdtemp(join(tmpdir(), "forge-bsrc-"));
15 app = createApp({ dataDir, port: 0 });
16 servers = await serve(app);
17 await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" });
18 const minted = await fetch(`${servers.url}/api/v1/tokens`, {
19 method: "POST",
20 headers: { "content-type": "application/json" },
21 body: JSON.stringify({ adminConfirm: true, username: "alice", password: "correct-horse-battery", name: "cli", scope: "admin" }),
22 });
23 bearer = ((await minted.json()) as { token: string }).token;
24 });
25
26 afterAll(async () => {
27 servers.stop();
28 app.close();
29 await rm(dataDir, { recursive: true, force: true });
30 });
31
32 const phases = (res: Response): string[] =>
33 (res.headers.get("server-timing") ?? "").split(", ").map((p) => p.split(";")[0]!).filter(Boolean);
34
35 test("the sources list names session, sources, render in order", async () => {
36 const res = await fetch(`${servers.url}/api/v1/boards/sources`, { headers: { authorization: `Bearer ${bearer}` } });
37 await res.arrayBuffer();
38 expect(res.status).toBe(200);
39 expect(phases(res).filter((n) => ["session", "sources", "render"].includes(n))).toEqual(["session", "sources", "render"]);
40 });
41
42 test("a refused read still names session", async () => {
43 const res = await fetch(`${servers.url}/api/v1/boards/sources`);
44 await res.arrayBuffer();
45 expect([401, 403, 404]).toContain(res.status);
46 expect(phases(res)).toContain("session");
47 expect(phases(res)).not.toContain("sources");
48 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs