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

/changelog shows the latest releases first and reads older ones on demand: under 40 KB and 40 ms #12994

Merged opened this

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

Merged

scout/forge:scout/12987-changelog-shows-the-latest-releases-firs → main 9 commits fork

  • Merged. scout/12987-changelog-shows-the-latest-releases-firs was merged into main as bb46e35.

Review

Sign in to review this change.
4 files changed +321 −35
docs/features/changelog.md +6
@@ -43,3 +43,9 @@ Which chapter a pull served is read from the chapter items, never from text or t
4343 The terminal's Changelog screen tells the same story from the same code (`release/chapter_groups.ts`): a `Chapter v1` line (`open` or `closed 2026-10-25`, and its pull count) with each pull indented under it as `#N title`, then `Between chapters`. Enter on a pull line opens that pull; `/` narrows by title or number, headings with no pull left drop away, and the empty state says "No pull matches". Whether the reader may see the repository goes through `sourceFor`, the door opening a pull uses. With no chapter the screen keeps its release rows.
4444
4545 The page's `ETag` holds one aggregate query (`chapterStamp`: chapter count, newest close, repository visibility, the item count and newest item, and a hash of which chapter holds each item, so moving an item between chapters moves the tag); the per-repository chapter reads run only after the tag has missed, so a 304 costs one query. The browser's day regrouping (`localDaysJs`) leaves a page with chapters alone. `test/changelog-timing.test.ts` holds the page to its budget with 500 pulls in chapters.
46
47 ## Older releases (#12987)
48
49 `/changelog` reads the newest 30 live releases, so the first page stays small however many have shipped (under 40 KB with 500 pulls; the first page answered in about 11 ms median in `test/changelog-window.test.ts`). Below them, inside the live region, a plain link reads "Older releases, before 12 Mar" (the UTC day of the last release shown): it goes to `?before=owner/name#number`, the last pull shown, and that page holds the next 30 older ones. Follow it until it is gone; the last page has no link. Nothing is on its way past the first page.
50
51 The repository and bet filters apply while the page is read, so `?repo=owner/name` or `?bet=name` gives 30 matching releases a page, and the link is there only when more matching ones remain; a filter matching a few shows them on one page with no link. The filters ride along in the link. A cursor that names no public pull is the first page, never an error. The page's `ETag` holds the cursor. The feeds, `/api/v1/changelog` and MCP `releases` are not paged.
src/release/landings.ts +112 −12
@@ -47,10 +47,29 @@ export interface Landings {
4747 readonly live: readonly Landing[];
4848 /** Merged, not yet in the running commit, newest first. */
4949 readonly onItsWay: readonly Landing[];
50 /**
51 * Set when a window was asked and older live landings remain: the cursor
52 * (`owner/name#number`) of the last one shown.
53 */
54 readonly more?: string;
5055 }
5156
57 /** A window of the live landings: how many, after the cursor (`owner/name#number`). */
58 export interface Window {
59 readonly limit: number;
60 readonly before?: string | undefined;
61 /**
62 * Which of a batch of merged rows the reader wants (a repository, a bet), given the forge's own
63 * repository once known. It runs inside the read, so a page holds `limit` matching rows
64 * and `more` is set only when matching ones remain.
65 */
66 readonly keep?: ((rows: readonly MergedRow[], own: string | undefined) => readonly MergedRow[]) | undefined;
67 }
68
69 const CURSOR = /^[A-Za-z0-9_.-]{1,64}\/[A-Za-z0-9_.-]{1,100}#[0-9]{1,9}$/;
70
5271 /** Which repository the reader asked for: "" for this forge's own, "all", or one named in the list; anything else is the default. */
53 export function chosenRepo(asked: string | null, all: readonly Landing[], own: string | undefined): string {
72 export function chosenRepo(asked: string | null, all: readonly { readonly repo: string }[], own: string | undefined): string {
5473 if (asked === "all") return "all";
5574 if (asked !== null && all.some((l) => l.repo === asked)) return asked;
5675 return own === undefined ? "all" : own;
@@ -70,18 +89,48 @@ export interface MergedRow {
7089 }
7190
7291 /** The merged pulls of public repositories, newest first. */
73 export function mergedRows(db: Database, limit = ROWS): MergedRow[] {
92 export function mergedRows(db: Database, limit = ROWS, before?: string): MergedRow[] {
93 const after = before === undefined ? undefined : cursorPoint(db, before);
7494 return db
75 .query<{ repo: string; number: number; title: string; at: string; oid: string | null }, [number]>(
95 .query<{ repo: string; number: number; title: string; at: string; oid: string | null }, [number, string | null, string | null]>(
7696 `SELECT u.username || '/' || r.name AS repo, i.number, i.title, p.merged_at AS at, p.merged_oid AS oid
7797 FROM pull p JOIN issue i ON i.id = p.issue_id JOIN repo r ON r.id = i.repo_id JOIN user u ON u.id = r.owner_id
7898 WHERE p.merged_at IS NOT NULL AND p.base_branch = r.default_branch AND r.visibility = 'public'
79 ORDER BY p.merged_at DESC LIMIT ?1`,
99 AND (?2 IS NULL OR p.merged_at < ?2 OR (p.merged_at = ?2 AND i.id < ?3))
100 ORDER BY p.merged_at DESC, i.id DESC LIMIT ?1`,
80101 )
81 .all(limit)
102 .all(limit, after?.at ?? null, after?.id ?? null)
82103 .map((r) => ({ ...r, oid: r.oid ?? "" }));
83104 }
84105
106 /** Whether a public repository (`owner/name`) has a merged pull. */
107 export function hasMerged(db: Database, named: string): boolean {
108 const [owner = "", name = ""] = named.split("/");
109 return (
110 db
111 .query<{ n: number }, [string, string]>(
112 `SELECT 1 AS n FROM pull p JOIN issue i ON i.id = p.issue_id JOIN repo r ON r.id = i.repo_id JOIN user u ON u.id = r.owner_id
113 WHERE u.username = ?1 AND r.name = ?2 AND p.merged_at IS NOT NULL AND p.base_branch = r.default_branch AND r.visibility = 'public' LIMIT 1`,
114 )
115 .get(owner, name) !== null
116 );
117 }
118
119 /** Where a cursor stands (when merged, which issue); undefined when it names no public pull. */
120 function cursorPoint(db: Database, cursor: string): { at: string; id: string } | undefined {
121 if (!CURSOR.test(cursor)) return undefined;
122 const [named = "", num = ""] = cursor.split("#");
123 const [owner = "", name = ""] = named.split("/");
124 return (
125 db
126 .query<{ at: string; id: string }, [string, string, number]>(
127 `SELECT p.merged_at AS at, i.id AS id FROM pull p JOIN issue i ON i.id = p.issue_id JOIN repo r ON r.id = i.repo_id JOIN user u ON u.id = r.owner_id
128 WHERE u.username = ?1 AND r.name = ?2 AND i.number = ?3 AND p.merged_at IS NOT NULL AND r.visibility = 'public'`,
129 )
130 .get(owner, name, Number(num)) ?? undefined
131 );
132 }
133
85134 /** What the running commit contains, as the last read found it. */
86135 interface LiveState {
87136 readonly own: string;
@@ -289,19 +338,70 @@ async function buildersOfRepo(app: App, own: string, rows: readonly MergedRow[])
289338 return out;
290339 }
291340
292 /** Every landing, live or on its way, as of the running commit. */
293 export async function readLandings(app: App): Promise<Landings> {
294 const rows = mergedRows(app.db);
295 await askLive(app, rows).catch(() => undefined);
341 /** Merged rows read per round while a window fills. */
342 const BATCH = 300;
343
344 /**
345 * Every landing, live or on its way, as of the running commit. With a
346 * window, only that many live landings after the cursor that `keep` accepts
347 * (the newest page also carries what is on its way) and `more` when older
348 * ones remain; rows are read in batches until the page is full or the merged
349 * pulls run out. A cursor that names nothing is the first page. Without a
350 * window, the full read.
351 */
352 export async function readLandings(app: App, window?: Window): Promise<Landings> {
353 let rows: readonly MergedRow[];
354 let point: string | undefined;
355 if (window === undefined) {
356 rows = mergedRows(app.db);
357 await askLive(app, rows).catch(() => undefined);
358 } else {
359 point = window.before !== undefined && cursorPoint(app.db, window.before) !== undefined ? window.before : undefined;
360 // The forge's own repository is found from the newest rows, so `keep` can name it.
361 let batch = mergedRows(app.db, BATCH, point);
362 const key = (r: readonly MergedRow[]) => r.slice(0, window.limit + ASKED).map((m) => `${m.repo}#${m.number}`).join();
363 let asked = key(batch);
364 await askLive(app, batch.slice(0, window.limit + ASKED)).catch(() => undefined);
365 const own = keeps.get(app.db)?.own;
366 const wanted = window.keep ?? ((r: readonly MergedRow[]) => r);
367 const got: MergedRow[] = [];
368 let after = point;
369 for (let first = true; ; first = false) {
370 if (!first) batch = mergedRows(app.db, BATCH, after);
371 got.push(...wanted(batch, own));
372 const last = batch[batch.length - 1];
373 if (last !== undefined) after = `${last.repo}#${last.number}`;
374 if (batch.length < BATCH) break;
375 asked = key(got);
376 await askLive(app, got.slice(0, window.limit + ASKED)).catch(() => undefined);
377 if (split(got, keeps.get(app.db)).live.length > window.limit) break;
378 }
379 rows = got;
380 // Asked once already when the page's rows are the ones the first read found.
381 if (key(got) !== asked) await askLive(app, got.slice(0, window.limit + ASKED)).catch(() => undefined);
382 }
296383 const kept = keeps.get(app.db);
297 const { live, onItsWay } = split(rows, kept);
384 const parts = split(rows, kept);
385 let { live } = parts;
386 let more: string | undefined;
387 if (window !== undefined && live.length > window.limit) {
388 live = live.slice(0, window.limit);
389 const last = live[live.length - 1]!;
390 more = `${last.repo}#${last.number}`;
391 }
392 // Past the first page nothing is on its way.
393 const onItsWay = point === undefined ? parts.onItsWay : [];
298394 const own = kept?.own;
299 const credits = own === undefined ? new Map<string, Builders>() : await buildersOfRepo(app, own, rows).catch(() => new Map<string, Builders>());
395 const shown = new Set([...live, ...onItsWay].map((l) => `${l.repo}#${l.number}`));
396 const credits =
397 own === undefined
398 ? new Map<string, Builders>()
399 : await buildersOfRepo(app, own, window === undefined ? rows : rows.filter((r) => shown.has(`${r.repo}#${r.number}`))).catch(() => new Map<string, Builders>());
300400 const named = (l: Landing): Landing => {
301401 const b = credits.get(`${l.repo}#${l.number}`);
302402 if (b === undefined) return l;
303403 return { ...l, ...(b.by === "" ? {} : { by: b.by }), ...(b.with.length === 0 ? {} : { with: b.with }) };
304404 };
305405 const { version, commit } = running();
306 return { version, commit, own, live: live.map(named), onItsWay: onItsWay.map(named) };
406 return { version, commit, own, live: live.map(named), onItsWay: onItsWay.map(named), ...(more === undefined ? {} : { more }) };
307407 }
src/web/pages/changelog.ts +68 −23
@@ -17,7 +17,7 @@ import type { Database } from "bun:sqlite";
1717 import type { User } from "../../core/users.ts";
1818 import { newestLive, secondsWords } from "../../keeper/landed.ts";
1919 import { dayHeading, dayOf, daysJs, shippedToday } from "../../release/days.ts";
20 import { chosenRepo, readLandings, type Landing, type Landings } from "../../release/landings.ts";
20 import { hasMerged, mergedRows, readLandings, type Landing, type Landings, type MergedRow } from "../../release/landings.ts";
2121 import { gather, plainTitle, type Gathered } from "../../story/gather.ts";
2222 import { milestoneHref, progress } from "./milestones.ts";
2323 import { nearMilestones, nearWords, type Near } from "./milestones_near.ts";
@@ -47,6 +47,9 @@ function nearBlock(base: string, near: readonly Near[]): Html | null {
4747 )}</ul>`;
4848 }
4949
50 /** Releases on a page: the rest are read on demand with `?before=`. */
51 export const PAGE = 30;
52
5053 /** Other repositories offered as filters: more than this is a search, not a chip. */
5154 const FILTERS = 6;
5255
@@ -223,11 +226,12 @@ export function shippedRegion(
223226 toLive?: { version: string; seconds: number },
224227 readable = true,
225228 chapters: readonly ChapterEntry[] = [],
229 bet = "",
226230 ): Html {
227 const key = `${Bun.hash(JSON.stringify([read, choice, zone, toLive ?? null, readable, chapters])).toString(36)}:${Math.floor(now.getTime() / 60_000)}`;
231 const key = `${Bun.hash(JSON.stringify([read, choice, zone, toLive ?? null, readable, chapters, bet])).toString(36)}:${Math.floor(now.getTime() / 60_000)}`;
228232 const kept = rendered.get(key);
229233 if (kept !== undefined) return kept;
230 const made = renderRegion(read, choice, now, zone, toLive, readable, chapters);
234 const made = renderRegion(read, choice, now, zone, toLive, readable, chapters, bet);
231235 if (rendered.size >= REGIONS) rendered.delete(rendered.keys().next().value as string);
232236 rendered.set(key, made);
233237 return made;
@@ -242,6 +246,7 @@ function renderRegion(
242246 toLive?: { version: string; seconds: number },
243247 readable = true,
244248 chapters: readonly ChapterEntry[] = [],
249 bet = "",
245250 ): Html {
246251 const keep = (l: Landing) => choice === "all" || l.repo === choice;
247252 const live = read.live.filter(keep);
@@ -269,29 +274,27 @@ function renderRegion(
269274 : !grouped
270275 ? html`${byDay(live, read.own, choice === "all", zone, todayIs)}`
271276 : html`${groups.map((g) => (g.kind === "chapter" ? chapterSection(g, read.own, choice === "all") : betweenSection(g, read.own, choice === "all")))}`;
272 return html`${top}${way}${gone}`;
277 return html`${top}${way}${gone}${older(read.more, read.live[read.live.length - 1]?.at, choice, read.own, bet)}`;
273278 }
274279
275280 // GET /changelog?repo=all|owner/name
276281 export async function changelogPage(ctx: Ctx): Promise<Response> {
277282 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
278 const landed = await timePhaseAsync(ctx.request, "releases", () => readLandings(ctx.app));
279 const ownRepo = landed.own === undefined ? undefined : getRepo(ctx.app.db, ...(landed.own.split("/") as [string, string]));
280 const readable = ownRepo !== undefined && canRead(ctx.app.db, ownRepo, ctx.auth.user);
281 const { read, bets, bet } = withBets(ctx, landed, readable ? ownRepo : undefined);
283 const view = viewOf(ctx);
284 const landed = await timePhaseAsync(ctx.request, "releases", () => readLandings(ctx.app, { limit: PAGE, before: ctx.url.searchParams.get("before") ?? undefined, keep: view.keep }));
285 const { choice, bet, bets, others, repo: ownRepo, readable } = view.of(landed.own);
286 const read = withBets(ctx, landed, readable ? ownRepo : undefined);
282287 // The forge's own repository, even before anything has landed in it.
283288 const planned = nearMilestones(ctx.app, landed.own ?? ctx.app.config.forgeRepo, ctx.auth.user);
284289 const near = planned?.near ?? [];
285290 const everything = [...read.onItsWay, ...read.live];
286291 // One aggregate read stands for the chapters in the tag; the per-repository reads wait until the tag has missed.
287292 const stamp = chapterStamp(ctx.app.db);
288 const choice = chosenRepo(new URL(ctx.request.url).searchParams.get("repo"), everything, read.own);
289 const others = [...new Set(everything.map((l) => l.repo))].filter((r) => r !== read.own).slice(0, FILTERS);
290293 // The tag comes from what the page would say, before any of it is rendered:
291294 // a match answers 304 without building the page.
292295 const toLive = newestLive(ctx.app.db);
293296 const flashed = readFlash(ctx.request) !== undefined;
294 const tag = flashed ? undefined : changelogTag(read.version, everything, [choice, bet, read.commit, read.live.map((l) => `${l.repo}#${l.number}`), readable, toLive ?? null, everything.map((l) => timeAgo(l.at)), new Date().toISOString().slice(0, 10), ctx.auth.user?.id ?? null, ctx.auth.user?.isAdmin === true, ctx.theme ?? null, near, stamp]);
297 const tag = flashed ? undefined : changelogTag(read.version, everything, [choice, bet, read.commit, read.live.map((l) => `${l.repo}#${l.number}`), readable, toLive ?? null, everything.map((l) => timeAgo(l.at)), new Date().toISOString().slice(0, 10), ctx.auth.user?.id ?? null, ctx.auth.user?.isAdmin === true, ctx.theme ?? null, near, stamp, read.more ?? null, ctx.url.searchParams.get("before") === null ? 0 : 1]);
295298 const keep = tag === undefined ? undefined : cacheHeaders(ctx, tag);
296299 if (keep !== undefined && keep.matched) return keep.response;
297300 const chapters = chapterEntries(ctx.app.db, read.live, ctx.auth.user);
@@ -309,7 +312,7 @@ export async function changelogPage(ctx: Ctx): Promise<Response> {
309312 ...bets.map((b) => ({ label: b, href: `/changelog?bet=${encodeURIComponent(b)}`, current: bet === b })),
310313 ]);
311314 const body = html`${pageHead("Changelog", { sub: "What went live, as it happens." })}${nearBlock(planned === undefined ? "" : repoBase(planned.repo), near)}${filters}${betFilters}${read.own !== undefined && readable ? html`<p class="small"><a href="/${read.own}/waves" data-waves-link>Read it by wave</a></p>` : null}
312 <div id="shipped" data-live="${LIVE}" aria-live="polite">${shippedRegion(read, choice, new Date(), "UTC", toLive, readable, chapters)}</div>`;
315 <div id="shipped" data-live="${LIVE}" aria-live="polite">${shippedRegion(read, choice, new Date(), "UTC", toLive, readable, chapters, bet)}</div>`;
313316 const response = page({ ctx, title: "Changelog", body, feed: { href: "/changelog.atom", title: "Changelog" }, json: { href: `/api/v1/changelog${choice === read.own ? "" : `?repo=${encodeURIComponent(choice)}`}`, title: "Changelog (JSON)" }, scriptJs: `${liveRegionsJs}\n${localDaysJs}` });
314317 notePhase(ctx.request, "render", performance.now() - rendering);
315318 if (keep === undefined) return response;
@@ -318,6 +321,25 @@ export async function changelogPage(ctx: Ctx): Promise<Response> {
318321 return new Response(response.body, { status: response.status, headers });
319322 }
320323
324 const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
325
326 /** "12 Mar" (UTC, as the page groups days), with the year when it is not this one. */
327 function shortDate(at: string, now = new Date()): string {
328 const d = new Date(at);
329 const year = d.getUTCFullYear() === now.getUTCFullYear() ? "" : ` ${d.getUTCFullYear()}`;
330 return `${d.getUTCDate()} ${MONTHS[d.getUTCMonth()]}${year}`;
331 }
332
333 /**
334 * The plain link to the next, older page, inside the live region so a refresh
335 * redraws or drops it; it says where the reader is, and the filters ride along.
336 */
337 function older(more: string | undefined, last: string | undefined, choice: string, own: string | undefined, bet: string): Html | null {
338 if (more === undefined || last === undefined) return null;
339 const q = [...(choice === own ? [] : [`repo=${encodeURIComponent(choice)}`]), ...(bet === "" ? [] : [`bet=${encodeURIComponent(bet)}`]), `before=${encodeURIComponent(more)}`];
340 return html`<p class="mt"><a href="/changelog?${q.join("&")}" rel="next" data-older>Older releases, before ${shortDate(last)}</a></p>`;
341 }
342
321343 /**
322344 * What a tagged answer says about caching, and whether the reader already
323345 * holds it. The tag is of what the page shows (release, rows, filter, each
@@ -354,11 +376,40 @@ export async function changelogJson(ctx: Ctx): Promise<Response> {
354376 }
355377
356378 /**
357 * The landings with the bet each built (#8713), and `?bet=` narrowing them to
358 * one bet's. Only the forge's own repository, and only for a reader of it.
379 * What the reader asked to see, settled once the forge's own repository is known: the repository and
380 * bet filters, the chips offered (from the newest merged pulls, not from the page), and the filter
381 * the read applies to each batch of merged rows, so a page is full of matches.
359382 */
360 function withBets(ctx: Ctx, read: Landings, repo: Repo | undefined): { read: Landings; bets: string[]; bet: string } {
361 if (repo === undefined) return { read, bets: [], bet: "" };
383 function viewOf(ctx: Ctx) {
384 let settled: { own: string | undefined; choice: string; bet: string; bets: string[]; others: string[]; repo: Repo | undefined; readable: boolean } | undefined;
385 let repoId: string | undefined;
386 const of = (own: string | undefined, newest?: readonly MergedRow[]) => {
387 if (settled !== undefined && settled.own === own) return settled;
388 const wide = newest ?? mergedRows(ctx.app.db);
389 const asked = ctx.url.searchParams.get("repo");
390 const choice = asked === "all" ? "all" : asked !== null && asked.length <= 170 && hasMerged(ctx.app.db, asked) ? asked : (own ?? "all");
391 const others = [...new Set(wide.map((r) => r.repo))].filter((r) => r !== own).slice(0, FILTERS);
392 const repo = own === undefined ? undefined : getRepo(ctx.app.db, ...(own.split("/") as [string, string]));
393 const readable = repo !== undefined && canRead(ctx.app.db, repo, ctx.auth.user);
394 repoId = readable ? repo.id : undefined;
395 const names = repoId === undefined ? new Map<number, string>() : betNamesOfPulls(ctx.app.db, repoId, wide.filter((r) => r.repo === own).map((r) => r.number));
396 const bets = [...new Set(names.values())].sort();
397 const askedBet = ctx.url.searchParams.get("bet") ?? "";
398 return (settled = { own, choice, bet: bets.includes(askedBet) ? askedBet : "", bets, others, repo, readable });
399 };
400 const keep = (rows: readonly MergedRow[], own: string | undefined): readonly MergedRow[] => {
401 const { choice, bet } = of(own, ctx.url.searchParams.get("before") === null ? rows : undefined);
402 const byRepo = rows.filter((r) => choice === "all" || r.repo === choice);
403 if (bet === "" || repoId === undefined) return byRepo;
404 const names = betNamesOfPulls(ctx.app.db, repoId, byRepo.filter((r) => r.repo === own).map((r) => r.number));
405 return byRepo.filter((r) => r.repo === own && names.get(r.number) === bet);
406 };
407 return { of, keep };
408 }
409
410 /** The landings marked with their bet (#8713) and folded-in source, for readers of the own repo. */
411 function withBets(ctx: Ctx, read: Landings, repo: Repo | undefined): Landings {
412 if (repo === undefined) return read;
362413 const numbers = [...read.live, ...read.onItsWay].filter((l) => l.repo === read.own).map((l) => l.number);
363414 const names = betNamesOfPulls(ctx.app.db, repo.id, numbers);
364415 const froms = sourcesOfPulls(ctx.app.db, repo.id, numbers);
@@ -368,11 +419,5 @@ function withBets(ctx: Ctx, read: Landings, repo: Repo | undefined): { read: Lan
368419 const from = froms.get(l.number);
369420 return { ...l, ...(bet === undefined ? {} : { bet }), ...(from === undefined ? {} : { from }) };
370421 };
371 const live = read.live.map(mark);
372 const onItsWay = read.onItsWay.map(mark);
373 const bets = [...new Set([...live, ...onItsWay].flatMap((l) => (l.bet === undefined ? [] : [l.bet])))].sort();
374 const asked = ctx.url.searchParams.get("bet") ?? "";
375 const bet = bets.includes(asked) ? asked : "";
376 const keep = (l: Landing) => bet === "" || l.bet === bet;
377 return { read: { ...read, live: live.filter(keep), onItsWay: onItsWay.filter(keep) }, bets, bet };
422 return { ...read, live: read.live.map(mark), onItsWay: read.onItsWay.map(mark) };
378423 }
test/changelog-window.test.ts added +135
@@ -0,0 +1,135 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdtemp, rm } from "node:fs/promises";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { createRepo } from "../src/actions/repos.ts";
6 import { createApp, type App } from "../src/app.ts";
7 import { createUser } from "../src/core/users.ts";
8 import { serve, type Servers } from "../src/http/server.ts";
9 import { expectWork, requestStatements } from "./helpers/work.ts";
10 import { readLandings } from "../src/release/landings.ts";
11
12 const TOTAL = 500;
13
14 describe("changelog reads a window", () => {
15 let dir: string;
16 let app: App;
17 let servers: Servers;
18
19 beforeAll(async () => {
20 dir = await mkdtemp(join(tmpdir(), "forge-changelog-window-"));
21 app = createApp({ dataDir: dir, port: 0 });
22 servers = await serve(app);
23 const r = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
24 if (!r.ok) throw new Error("user");
25 const made = await createRepo(app, r.value, { name: "forge", visibility: "public" });
26 if (!made.ok) throw new Error("repo");
27 const repoId = app.db.query<{ id: string }, []>("SELECT id FROM repo LIMIT 1").get()!.id;
28 for (const name of ["other", "rare"]) if (!(await createRepo(app, r.value, { name, visibility: "public" })).ok) throw new Error(name);
29 const idOf = (name: string) => app.db.query<{ id: string }, [string]>("SELECT id FROM repo WHERE name = ?1").get(name)!.id;
30 const merge = (repo: string, id: string, n: number, at: string) => {
31 const rid = idOf(repo);
32 app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at) VALUES (?1, ?2, ?3, 'pull', ?4, '', ?5, 'closed', ?6)`).run(id, rid, n, `${repo} change ${n} with a plain title`, r.value.id, at);
33 app.db.query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at, merged_oid) VALUES (?1, 'main', ?2, '', '', ?3, ?4, '')`).run(id, `b/${n}`, rid, at);
34 };
35 app.db.transaction(() => {
36 // 45 of "other" spread through the newest hours, 3 of "rare" older than all the forge's.
37 for (let k = 0; k < 45; k++) merge("other", `o${k}`, k + 1, new Date(Date.UTC(2026, 0, 1) + k * 3500).toISOString());
38 for (let k = 0; k < 3; k++) merge("rare", `r${k}`, k + 1, new Date(Date.UTC(2025, 11, 1) + k * 1000).toISOString());
39 for (let i = 1; i <= TOTAL; i++) {
40 // Several share a second, so the cursor must break ties.
41 const at = new Date(Date.UTC(2026, 0, 1) + Math.floor(i / 3) * 1000).toISOString();
42 app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, author_id, state, created_at) VALUES (?1, ?2, ?3, 'pull', ?4, '', ?5, 'closed', ?6)`).run(`w${String(i).padStart(4, "0")}`, repoId, i, `Release number ${i} with a plain title`, r.value.id, at);
43 app.db.query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, head_repo_id, merged_at, merged_oid) VALUES (?1, 'main', ?2, '', '', ?3, ?4, '')`).run(`w${String(i).padStart(4, "0")}`, `b/${i}`, repoId, at);
44 }
45 })();
46 });
47 afterAll(async () => {
48 await servers.stop?.();
49 await rm(dir, { recursive: true, force: true });
50 });
51
52 const get = async (q = "") => {
53 const res = await fetch(`${servers.url}/changelog${q}`);
54 return { res, text: await res.text() };
55 };
56 const numbers = (html: string) => [...html.matchAll(/href="\/alice\/forge\/pulls\/(\d+)"/g)].map((m) => Number(m[1]));
57 const next = (html: string) => /href="([^"]*)" rel="next"/.exec(html)?.[1]?.replaceAll("&amp;", "&");
58
59 test("the first page lists 30 releases in under 40 KB with a plain link on", async () => {
60 const { res, text } = await get();
61 expect(res.status).toBe(200);
62 expect(numbers(text).length).toBe(30);
63 expect(new TextEncoder().encode(text).length).toBeLessThan(40_000);
64 expect(next(text)).toContain("before=");
65 });
66
67 test("following the link yields every release exactly once", async () => {
68 const seen: number[] = [];
69 let url: string | undefined = "/changelog";
70 let pages = 0;
71 while (url !== undefined && pages++ < 30) {
72 const { res, text } = await get(url.slice("/changelog".length));
73 expect(res.status).toBe(200);
74 const got = numbers(text);
75 expect(got.length).toBeLessThanOrEqual(30);
76 seen.push(...got);
77 url = next(text);
78 }
79 expect(seen.length).toBe(TOTAL);
80 expect(new Set(seen).size).toBe(TOTAL);
81 expect(url).toBeUndefined();
82 });
83
84 test("a junk cursor is the first page, never an error", async () => {
85 const first = numbers((await get()).text);
86 for (const junk of ["x", "<script>", "a/b#1", "alice/forge#999999", "alice/forge#", "%00"]) {
87 const { res, text } = await get(`?before=${encodeURIComponent(junk)}`);
88 expect(res.status).toBe(200);
89 expect(numbers(text)).toEqual(first);
90 }
91 });
92
93 const pulls = (html: string, repo: string) => [...html.matchAll(new RegExp(`href="/alice/${repo}/pulls/(\\d+)"`, "g"))].map((m) => Number(m[1]));
94
95 test("a filter that matches a few still fills the page and offers no empty next", async () => {
96 // "rare" is older than 500 other merges: a window read first and filtered after shows nothing.
97 const { res, text } = await get("?repo=alice/rare");
98 expect(res.status).toBe(200);
99 expect(pulls(text, "rare").sort()).toEqual([1, 2, 3]);
100 expect(next(text)).toBeUndefined();
101 });
102
103 test("a filtered page is full while matches remain, and the link ends with them", async () => {
104 const seen: number[] = [];
105 const sizes: number[] = [];
106 let url: string | undefined = "?repo=alice/other";
107 while (url !== undefined && sizes.length < 5) {
108 const { text } = await get(url);
109 const got = pulls(text, "other");
110 sizes.push(got.length);
111 seen.push(...got);
112 const n = next(text);
113 url = n === undefined ? undefined : n.slice("/changelog".length);
114 }
115 expect(sizes).toEqual([30, 15]);
116 expect(new Set(seen).size).toBe(45);
117 });
118
119 test("the link says where the reader is and lives inside the refreshed region", async () => {
120 const { text } = await get();
121 expect(text).toMatch(/Older releases, before \d{1,2} [A-Z][a-z]{2}( \d{4})?<\/a><\/p><\/div>/);
122 });
123
124 test("the first page of 500 costs a handful of statements, counted warm", async () => {
125 await get();
126 const n = await requestStatements(app.db, `${servers.url}/changelog?x=1`);
127 expectWork(n, { max: 30 });
128 });
129
130 test("the full read stays whole for the feeds", async () => {
131 const all = await readLandings(app);
132 expect(all.live.length).toBe(300);
133 expect(all.more).toBeUndefined();
134 });
135 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs