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

/next has a test that finds its rows in the dispatcher's order #11737

Merged opened this

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

Merged

scout/forge:scout/11734-next-has-a-test-that-finds-its-rows-in-t → main 1 commit fork 1 thread · 1 unresolved

  • Merged. scout/11734-next-has-a-test-that-finds-its-rows-in-t was merged into main as 9a845f8.

Review

Sign in to review this change.
1 file changed +90 −0
test/next-order-page.test.ts added +90
@@ -0,0 +1,90 @@
1 /** /next lists its numbered rows in the dispatcher's order, with each fold's count right (#11734). */
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 { openIssue } from "../src/actions/issues.ts";
8 import { rankIssue } from "../src/actions/next.ts";
9 import { createRepo } from "../src/actions/repos.ts";
10 import { nextUp, TOP } from "../src/core/next_up.ts";
11 import { sealedScope } from "../src/core/sealed.ts";
12 import { notStartable } from "../src/crew/not_startable.ts";
13 import type { Repo } from "../src/core/repos.ts";
14 import type { User } from "../src/core/users.ts";
15 import { createSession } from "../src/core/sessions.ts";
16 import { createUser } from "../src/core/users.ts";
17 import { serve, type Servers } from "../src/http/server.ts";
18
19 let dir: string;
20 let app: App;
21 let servers: Servers;
22 let cookie: string;
23
24 const made3: number[] = [];
25 let alice: User;

made3 and made2 are names that say nothing; rename (issueNumbers, createdRepo). The Later gate issue opened in beforeAll is never asserted on; say why it is there or drop it.

26 let repo: Repo;
27
28 const COUNT = TOP + 3;
29
30 beforeAll(async () => {
31 dir = await mkdtemp(join(tmpdir(), "forge-next-order-"));
32 app = createApp({ dataDir: dir, port: 0 });
33 servers = await serve(app);
34 const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
35 if (!made.ok) throw new Error(made.error);
36 alice = made.value;
37 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
38 const made2 = await createRepo(app, alice, { name: "order", visibility: "public" });
39 if (!made2.ok) throw new Error(JSON.stringify(made2.error));
40 repo = made2.value;
41 const gate = openIssue(app, alice, repo, { title: "Later gate: not now" });
42 if (!gate.ok) throw new Error(String(gate.error));
43 for (let i = 1; i <= COUNT; i++) {
44 const o = openIssue(app, alice, repo, { title: `Issue ${i}` });
45 if (!o.ok) throw new Error(String(o.error));
46 made3.push(o.value.number);
47 }
48 // A known, non-creation order: 12 and 7 to the top, 2 and 5 into Later.
49 for (const [n, to] of [[7, "top"], [12, "top"], [2, "not-now"], [5, "not-now"]] as const) {
50 const r = rankIssue(app, alice, repo, made3[n - 1]!, to === "top" ? { to: "top" } : { to: "not_now" });
51 if (!r.ok) throw new Error(JSON.stringify(r.error));
52 }
53 });
54
55 afterAll(async () => {
56 await servers.stop();
57 app.close();
58 await rm(dir, { recursive: true, force: true });
59 });
60
61 /** Each fold's heading count and the issue numbers of its rows, in page order. */
62 function folds(page: string): { title: string; count: number; numbers: number[] }[] {
63 return page.split("<details").slice(1).flatMap((chunk) => {
64 const h = /<h2>([^<·]*?) · (\d+)<\/h2>/.exec(chunk);
65 if (!h) return [];
66 const numbers = [...chunk.matchAll(/<li[^>]*data-number="(\d+)"/g)].map((m) => Number(m[1]));
67 return [{ title: h[1]!.trim(), count: Number(h[2]), numbers }];
68 });
69 }
70
71 test("the numbered rows, the folds and Later follow the dispatcher's order", async () => {
72 const page = await (await fetch(`${servers.url}/alice/order/next`, { headers: { cookie } })).text();
73 const api = (await (await fetch(`${servers.url}/api/v1/repos/alice/order/queue`, { headers: { cookie } })).json()) as { queue: { position: number; number: number }[] };
74 const order = api.queue.map((q) => q.number);
75 expect(order.slice(0, 2)).toEqual([made3[11]!, made3[6]!]);
76 expect(order.length).toBe(COUNT - 2);
77
78 const [open, after, later] = folds(page);
79 expect(open).toEqual({ title: "Up next", count: TOP, numbers: order.slice(0, TOP) });
80 expect(after).toEqual({ title: "After", count: order.length - TOP, numbers: order.slice(TOP) });
81
82 const parked = nextUp(app.db, repo, sealedScope(app.db, repo, alice), (ready) => notStartable(app, repo, ready, sealedScope(app.db, repo, alice))).notNow;
83 expect(later!.title).toBe("Later");
84 expect(later!.count).toBe(2);
85 expect(later!.numbers).toEqual(parked.map((r) => r.issue.number));
86
87 // The page numbers rows with the API's position.
88 const lead = [...page.matchAll(/data-number="(\d+)"/g)].map((m) => Number(m[1]));
89 expect(lead.slice(0, order.length)).toEqual(order);
90 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs