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

The changelog groups landed pulls under the chapter they served, newest chapter first #12720

Merged opened this

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

Merged

scout/forge:scout/12695-the-changelog-groups-landed-pulls-under → main 7 commits fork

  • Merged. scout/12695-the-changelog-groups-landed-pulls-under was merged into main as 8edc90f.

Review

Sign in to review this change.
10 files changed +448 −236
docs/features/changelog.md +8 −4
@@ -26,7 +26,7 @@ A squash keeps the people behind its commits: the landed message carries every d
2626
2727 ## For agents
2828
29 `GET /api/v1/changelog` is the page's list as JSON, public, cached five seconds: `{version, commit, live, onItsWay}`, each landing `{repo, number, title, at, live[, with]}`. `?repo=all` or `?repo=owner/name` filters; the default is the forge's own repository, as on the page. MCP `releases` with `{shipped: true, repo?}` and `GET /api/v1/releases?shipped=true[&repo=]` answer the same rows with the same default. Only public repositories are read; concurrent requests share one read of git and the merged rows, so an anonymous caller costs no more than a page view. The page links the JSON with `rel="alternate"`. The terminal's Changelog screen is the release-commit view (`release/changelog.ts`); it does not show the landings list.
29 `GET /api/v1/changelog` is the page's list as JSON, public, cached five seconds: `{version, commit, live, onItsWay}`, each landing `{repo, number, title, at, live[, with]}`. `?repo=all` or `?repo=owner/name` filters; the default is the forge's own repository, as on the page. MCP `releases` with `{shipped: true, repo?}` and `GET /api/v1/releases?shipped=true[&repo=]` answer the same rows with the same default. Only public repositories are read; concurrent requests share one read of git and the merged rows, so an anonymous caller costs no more than a page view. The page links the JSON with `rel="alternate"`. The terminal's Changelog screen is the release-commit view (`release/changelog.ts`), told in chapters when there are any (see Chapters).
3030
3131 ## As a feed (#8213)
3232
@@ -34,8 +34,12 @@ A squash keeps the people behind its commits: the landed message carries every d
3434
3535 An agent reads the same data as JSON, not Atom: MCP `releases` with `{shipped: true}` or `GET /api/v1/changelog` (see above). The terminal has no screen for the feed, since a feed is for a reader program; its Changelog screen is the release-commit view.
3636
37 ## Chapters (#11868)
37 ## Chapters (#12695)
3838
39 A pull that landed while a chapter was open, and that chapter has since closed (tagged, `closedAt` passed), is drawn under that chapter on `/changelog`: one row (the chapter's name linked to its page, the first sentence of its closing note in plain words, "Means" and "Pulls" as facts, the close date), then its pulls under the usual day headings, so every pull keeps its date. Pulls outside any closed chapter stay flat in runs, in order; a chapter with no pull in the list, one still open, and one in a repository the reader may not read are not drawn. The terminal's Changelog screen groups the same way: a `Chapter v1` line (release count and the note's first sentence) over the releases that fell in its window, indented. Both read `release/chapter_words.ts`.
39 When a repository has chapters (open or closed, never a draft), `/changelog` tells its landed pulls under them, newest chapter first, the open one before the closed. Each chapter is a row of the page's usual list: its name linked to its page, and its closing day, or "open", as the trail; below it, one line per pull that served it, in the list's order. A chapter nothing landed in is kept and says "Nothing landed yet." on its row. Pulls that served no chapter come last under a "Between chapters" row of the same shape. With no chapter to show (none, or none the reader may read) the list is the flat days as before.
4040
41 Membership is by time, not by recorded tag: a landing belongs to the chapter open when it merged (opened < merged ≤ closed). A landing at the edge of a window can be placed in a neighbouring chapter; the chapter's own page, which reads its issues, is the exact record. The page's `ETag` holds one aggregate query over closed chapters (`chapterStamp`: count, newest close, repository visibility); 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.
41 Which chapter a pull served is read from the chapter items, never from text or time (`chapterOfPulls` in `core/chapters.ts`): the chapter holding the pull's own issue, else the first chapter holding an issue its body closes (`Closes #N`, in the order written). A pull that closes issues in two chapters is therefore placed in the first match only, silently. Chapters of a repository the reader may not read are not drawn.
42
43 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.
44
45 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.
src/core/chapters.ts +28
@@ -406,3 +406,31 @@ export function inheritChapter(db: Database, parent: Issue, children: readonly I
406406 if (chapter === undefined || chapter.state === "closed") return;
407407 for (const child of children) putIssue(db, chapter, child.id, byId);
408408 }
409
410 /**
411 * Which chapter each merged pull served, by pull number: read from the chapter items, never from
412 * text or time. A pull serves the chapter holding its own issue, else the chapter holding an issue
413 * it closes (`Closes #N`). One query over the items and one over the merged pulls.
414 */
415 export function chapterOfPulls(db: Database, repoId: string): Map<number, string> {
416 const items = db
417 .query<{ number: number; chapter: string }, [string]>(
418 `SELECT i.number AS number, c.chapter_id AS chapter FROM chapter_item c
419 JOIN issue i ON i.id = c.issue_id JOIN chapter ch ON ch.id = c.chapter_id WHERE ch.repo_id = ?1`,
420 )
421 .all(repoId);
422 const out = new Map<number, string>();
423 if (items.length === 0) return out;
424 const held = new Map(items.map((r) => [r.number, r.chapter]));
425 const merged = db
426 .query<{ number: number; body: string }, [string]>(
427 `SELECT issue.number AS number, issue.body AS body FROM pull JOIN issue ON issue.id = pull.issue_id
428 WHERE issue.repo_id = ?1 AND pull.merged_at IS NOT NULL`,
429 )
430 .all(repoId);
431 for (const p of merged) {
432 const chapter = held.get(p.number) ?? closesFromBody(p.body).map((n) => held.get(n)).find((c) => c !== undefined);
433 if (chapter !== undefined) out.set(p.number, chapter);
434 }
435 return out;
436 }
src/release/chapter_groups.ts added +53
@@ -0,0 +1,53 @@
1 /**
2 * The changelog told in chapters, shared by the page and the terminal so they cannot disagree
3 * (#12695): each chapter newest first, open before closed, with the landed pulls that served it,
4 * then the pulls that served none under "Between chapters". Which chapter a pull served is read
5 * from the chapter items (`chapterOfPulls`), handed in as numbers; nothing here reads text or time.
6 */
7
8 import type { Chapter } from "../core/chapters.ts";
9 import type { Landing } from "./landings.ts";
10
11 /**
12 * A repository's chapter, whether the reader may see that repository, and the pull numbers that
13 * served it.
14 */
15 export interface ChapterEntry {
16 readonly repo: string;
17 readonly chapter: Chapter;
18 readonly canSee: boolean;
19 readonly pulls: readonly number[];
20 }
21
22 export type Group =
23 | { readonly kind: "chapter"; readonly repo: string; readonly chapter: Chapter; readonly landings: readonly Landing[] }
24 | { readonly kind: "between"; readonly landings: readonly Landing[] };
25
26 /** Newest chapter first: the open one, then by closing time, later position breaking a tie. */
27 function newest(a: ChapterEntry, b: ChapterEntry): number {
28 const at = (e: ChapterEntry): number => (e.chapter.closedAt === null ? Infinity : Date.parse(e.chapter.closedAt));
29 return at(b) === at(a) ? b.chapter.position - a.chapter.position : at(b) < at(a) ? -1 : 1;
30 }
31
32 /**
33 * Landings (newest first) under the chapters of the repositories they came from. A chapter that
34 * is drafted, or of a repository the reader may not see, or of a repository none of the landings
35 * came from, is left out; one with no landing yet is kept, empty. The between group is last and
36 * absent when empty. With no chapter to show there is no grouping at all: the list is empty.
37 */
38 export function groupByChapter(landings: readonly Landing[], entries: readonly ChapterEntry[]): Group[] {
39 const repos = new Set(landings.map((l) => l.repo.toLowerCase()));
40 const usable = entries.filter((e) => e.canSee && e.chapter.state !== "draft" && repos.has(e.repo.toLowerCase())).sort(newest);
41 if (usable.length === 0) return [];
42 const served = new Map<ChapterEntry, Landing[]>(usable.map((e) => [e, []]));
43 const between: Landing[] = [];
44 const owner = new Map<string, ChapterEntry>();
45 for (const e of [...usable].reverse()) for (const n of e.pulls) owner.set(`${e.repo.toLowerCase()}#${n}`, e);
46 for (const l of landings) {
47 const e = owner.get(`${l.repo.toLowerCase()}#${l.number}`);
48 (e === undefined ? between : served.get(e)!).push(l);
49 }
50 const out: Group[] = usable.map((e) => ({ kind: "chapter", repo: e.repo, chapter: e.chapter, landings: served.get(e)! }));
51 if (between.length > 0) out.push({ kind: "between", landings: between });
52 return out;
53 }
src/release/chapter_words.ts deleted −48
@@ -1,48 +0,0 @@
1 /**
2 * What the changelog says about a closed chapter, shared by the page and the
3 * terminal so they cannot disagree (#11868): which moments belong to it, and
4 * its closing note in plain words.
5 *
6 * Membership is by time, not by tag: a pull or release belongs to the chapter
7 * that was open when it landed (opened < at <= closed, the tag standing at the
8 * default branch's tip at the close). A landing at the very edge of a window
9 * can be placed in the neighbouring chapter; the chapter's own page, which
10 * reads its issues, is the exact record.
11 */
12
13 import type { Chapter } from "../core/chapters.ts";
14
15 /** A chapter that may head a group: closed, tagged and closed no later than `now`. */
16 export function isDrawable(c: Chapter, now: Date): boolean {
17 return c.state === "closed" && c.tagOid !== null && c.closedAt !== null && Date.parse(c.closedAt) <= now.getTime();
18 }
19
20 /** Whether `at` (an ISO time) falls inside the chapter's window. */
21 export function inChapter(c: Chapter, at: string): boolean {
22 const t = Date.parse(at);
23 return c.closedAt !== null && Date.parse(c.openedAt ?? c.createdAt) < t && t <= Date.parse(c.closedAt);
24 }
25
26 /** Longest the first sentence of a note is shown. */
27 const MOST = 200;
28
29 /**
30 * The first sentence of a closing note as plain words: markdown headings, emphasis,
31 * code marks, list bullets and link syntax are dropped (a link keeps its text).
32 * Empty when the note says nothing in words.
33 */
34 export function noteWords(notes: string): string {
35 const body = notes
36 .split("\n")
37 .filter((line) => !/^\s*#{1,6}\s/.test(line) && !/^\s*(```|~~~)/.test(line))
38 .map((line) => line.replace(/^\s*(?:[-*+]|\d+\.)\s+/, "").replace(/^\s*>\s?/, ""))
39 .join(" ")
40 .replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
41 .replace(/[`*_~]/g, "")
42 .replace(/<[^>]*>/g, "")
43 .replace(/\s+/g, " ")
44 .trim();
45 const stop = body.search(/[.!?](\s|$)/);
46 const sentence = stop === -1 ? body : body.slice(0, stop + 1);
47 return sentence.length > MOST ? `${sentence.slice(0, MOST - 1).trimEnd()}…` : sentence;
48 }
src/tui/screens/changelog.ts +54 −35
@@ -5,11 +5,11 @@
55 * pull title; Enter lists a release's pulls, and Enter on a pull opens it.
66 */
77
8 import { listChapters, type Chapter } from "../../core/chapters.ts";
9 import { inChapter, isDrawable, noteWords } from "../../release/chapter_words.ts";
8 import { chapterOfPulls, listChapters } from "../../core/chapters.ts";
9 import { groupByChapter, type ChapterEntry, type Group } from "../../release/chapter_groups.ts";
1010 import { gather, plainTitle } from "../../story/gather.ts";
1111 import { forgeChangelog, type ReleaseEntry } from "../../release/changelog.ts";
12 import { readLandings } from "../../release/landings.ts";
12 import { readLandings, type Landing } from "../../release/landings.ts";
1313 import { nearMilestones, nearWords, type Near } from "../../web/pages/milestones_near.ts";
1414 import { sourceFor } from "../../view/source.ts";
1515 import type { Ctx, Screen } from "../app/app.ts";
@@ -38,36 +38,36 @@ export function changelogRows(entries: readonly ReleaseEntry[]): ListRow[] {
3838 }));
3939 }
4040
41 /** One line of the list: a release, or the chapter heading over the releases it closed. */
41 /** One line of the list: a release, a chapter heading, or a pull under one. */
4242 export interface Line {
4343 readonly row: ListRow;
4444 readonly entry?: ReleaseEntry;
45 readonly pull?: number;
4546 }
4647
4748 /**
48 * The releases (newest first) as lines, each run that fell in a closed chapter under a heading line
49 * with the chapter's name, its closing note's first sentence in plain words and its count, as the
50 * page groups them (`release/chapter_words.ts` decides membership). Releases outside any chapter
51 * stay flat. A chapter with none of the shown releases draws no heading.
49 * The landed pulls told in chapters, as the page tells them (`release/chapter_groups.ts` decides
50 * membership): a heading line per chapter, newest first, with its closing day or "open", then its
51 * pulls, one line each; pulls that served none sit under "Between chapters". `keep` narrows the pulls.
5252 */
53 export function changelogLines(entries: readonly ReleaseEntry[], chapters: readonly Chapter[], now: Date = new Date()): Line[] {
54 const usable = chapters.filter((c) => isDrawable(c, now));
53 export function chapterLines(groups: readonly Group[], keep: (l: Landing) => boolean = () => true, filtering = false): Line[] {
5554 const out: Line[] = [];
56 let at: Chapter | undefined;
57 for (const e of entries) {
58 const c = usable.find((u) => inChapter(u, e.date));
59 if (c !== undefined && c !== at) {
60 const n = entries.filter((x) => inChapter(c, x.date)).length;
61 const said = noteWords(c.notes);
62 out.push({ row: { title: `Chapter ${c.name}`, meta: metaOf(`${n} ${n === 1 ? "release" : "releases"}`, said) } });
63 }
64 at = c;
65 const [row] = changelogRows([e]);
66 out.push({ row: c === undefined ? row! : { ...row!, title: ` ${row!.title}` }, entry: e });
55 for (const g of groups) {
56 const pulls = g.landings.filter(keep);
57 if (filtering && pulls.length === 0) continue;
58 const title = g.kind === "chapter" ? `Chapter ${g.chapter.name}` : "Between chapters";
59 const when = g.kind === "chapter" ? (g.chapter.closedAt === null ? "open" : `closed ${g.chapter.closedAt.slice(0, 10)}`) : "";
60 out.push({ row: { title, meta: metaOf(when, `${pulls.length} ${pulls.length === 1 ? "pull" : "pulls"}`) } });
61 for (const l of pulls) out.push({ row: { title: ` #${l.number} ${plainTitle(l.title)}`.trimEnd(), meta: ago(l.at) }, pull: l.number });
6762 }
6863 return out;
6964 }
7065
66 /** The releases as lines, for a repository with no chapter to tell them in. */
67 export function changelogLines(entries: readonly ReleaseEntry[]): Line[] {
68 return entries.map((e) => ({ row: changelogRows([e])[0]!, entry: e }));
69 }
70
7171 /** The open milestones as lines above the releases: "name N of M done", at most `MOST` of them. */
7272 const MOST = 5;
7373 export function nearLines(near: readonly Near[]): string[] {
@@ -129,23 +129,24 @@ export function changelogScreen(f: Session, ctx: Ctx): Screen {
129129 let repoRef: Repo | undefined;
130130 let credits = new Map<number, string>();
131131 let near: string[] = [];
132 let chapters: Chapter[] = [];
133 const lines = (): Line[] => changelogLines(shown(), chapters);
132 let groups: Group[] = [];
133 const lines = (): Line[] =>
134 groups.length > 0 ? chapterLines(groups, (l) => filter.matches(`${plainTitle(l.title)}\n#${l.number}`), filter.query !== "") : changelogLines(shown());
134135 const shown = (): ReleaseEntry[] => entries.filter((e) => releaseMatches(filter, e));
135136 return {
136137 title: "Changelog",
137138 keys: [
138139 binding(["j", "k"], "move"),
139140 binding(["/"], "find"),
140 binding(["enter"], "list pulls"),
141 binding(["enter"], "open"),
141142 binding(["esc", "q"], "close"),
142143 ],
143144 async load() {
144145 const read = await forgeChangelog(f.app);
145146 entries = read?.entries ?? [];
146147 repoRef = read === undefined ? undefined : { owner: read.repo.ownerName, name: read.repo.name };
148 const landings = read === undefined ? undefined : await readLandings(f.app).catch(() => undefined);
147149 if (read !== undefined) {
148 const landings = await readLandings(f.app).catch(() => undefined);
149150 const repoName = `${read.repo.ownerName}/${read.repo.name}`.toLowerCase();
150151 credits = new Map();
151152 for (const l of [...(landings?.live ?? []), ...(landings?.onItsWay ?? [])]) {
@@ -153,7 +154,21 @@ export function changelogScreen(f: Session, ctx: Ctx): Screen {
153154 if (words !== "" && l.repo.toLowerCase() === repoName) credits.set(l.number, words);
154155 }
155156 }
156 chapters = read === undefined ? [] : listChapters(f.app.db, read.repo.id);
157 groups = [];
158 if (read !== undefined) {
159 const name = `${read.repo.ownerName}/${read.repo.name}`;
160 // Whether this reader may see the repository goes through the same door as opening a pull.
161 const canSee = sourceFor(f.app, f.user, { owner: read.repo.ownerName, name: read.repo.name }).ok;
162 const served = chapterOfPulls(f.app.db, read.repo.id);
163 const entries: ChapterEntry[] = listChapters(f.app.db, read.repo.id).map((chapter) => ({
164 repo: name,
165 chapter,
166 canSee,
167 pulls: [...served].filter(([, id]) => id === chapter.id).map(([n]) => n),
168 }));
169 const live = (landings?.live ?? []).filter((l) => l.repo.toLowerCase() === name.toLowerCase());
170 groups = groupByChapter(live, entries);
171 }
157172 near = nearLines(nearMilestones(f.app, f.app.config.forgeRepo, f.user)?.near ?? []);
158173 state.clamp(entries.length);
159174 loaded = true;
@@ -161,25 +176,27 @@ export function changelogScreen(f: Session, ctx: Ctx): Screen {
161176 },
162177 render(area, s) {
163178 const c = ctx.theme.c;
164 if (entries.length === 0) {
179 if (entries.length === 0 && groups.length === 0) {
165180 emptyState(s, area, c, {
166181 title: loaded ? "No releases yet" : "Reading…",
167182 body: "Each release lands here with its pulls.",
168183 });
169184 return;
170185 }
171 const list = shown();
186 const list = lines();
172187 const [head, ms, box, body] = split(area, "v", [len(1), len(near.length), len(filter.on ? 1 : 0), fill(1)]);
173188 near.forEach((line, i) => s.text(ms!.x, ms!.y + i, truncate(line, ms!.w), style(c.fg, c.bg), ms!.x + ms!.w));
174 const count = filter.query === "" ? `${entries.length} releases` : `${list.length} of ${entries.length} releases match`;
189 const noun = groups.length > 0 ? "pulls" : "releases";
190 const total = groups.length > 0 ? groups.reduce((n, g) => n + g.landings.length, 0) : entries.length;
191 const matching = list.filter((l) => (groups.length > 0 ? l.pull !== undefined : true)).length;
192 const count = filter.query === "" ? `${total} ${noun}` : `${matching} of ${total} ${noun} match`;
175193 s.text(head!.x, head!.y, truncate(count, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w);
176 ctx.setCursor(filter.on ? filter.render(s, box!, c, "find a version or pull") : null);
194 ctx.setCursor(filter.on ? filter.render(s, box!, c, groups.length > 0 ? "find a pull" : "find a version or pull") : null);
177195 if (list.length === 0) {
178 emptyState(s, body!, c, { title: "No release matches", body: "Esc clears the word." });
196 emptyState(s, body!, c, { title: groups.length > 0 ? "No pull matches" : "No release matches", body: "Esc clears the word." });
179197 return;
180198 }
181 const shownLines = lines();
182 drawList(s, body!, c, state, shownLines.map((l) => l.row));
199 drawList(s, body!, c, state, list.map((l) => l.row));
183200 },
184201 key(e) {
185202 if (filter.key(e)) {
@@ -191,8 +208,10 @@ export function changelogScreen(f: Session, ctx: Ctx): Screen {
191208 state.clamp(lines().length);
192209 } else if (e.key === "esc" || e.key === "q") ctx.pop();
193210 else if (e.key === "enter") {
194 const entry = lines()[state.index]?.entry;
195 if (entry !== undefined && repoRef !== undefined) ctx.push(releasePulls(f, ctx, repoRef, entry, credits));
211 const line = lines()[state.index];
212 const opened = repoRef === undefined ? undefined : sourceFor(f.app, f.user, repoRef);
213 if (line?.entry !== undefined && repoRef !== undefined) ctx.push(releasePulls(f, ctx, repoRef, line.entry, credits));
214 else if (line?.pull !== undefined && opened?.ok) ctx.push(pullScreen(f, ctx, opened.value, line.pull));
196215 } else state.key(e, lines().length);
197216 return true;
198217 },
src/web/pages/changelog.ts +36 −74
@@ -11,8 +11,8 @@ import type { Ctx } from "../../http/context.ts";
1111 import { betNamesOfPulls } from "../../core/bets.ts";
1212 import { sourcesOfPulls } from "../../core/trusted_sources.ts";
1313 import { canRead, getRepo, type Repo } from "../../core/repos.ts";
14 import { listChapters, type Chapter } from "../../core/chapters.ts";
15 import { inChapter, isDrawable, noteWords } from "../../release/chapter_words.ts";
14 import { chapterOfPulls, listChapters } from "../../core/chapters.ts";
15 import { groupByChapter, type ChapterEntry, type Group } from "../../release/chapter_groups.ts";
1616 import type { Database } from "bun:sqlite";
1717 import type { User } from "../../core/users.ts";
1818 import { newestLive, secondsWords } from "../../keeper/landed.ts";
@@ -75,73 +75,27 @@ function gatheredRow(g: Gathered<Landing>, own: string | undefined, showRepo: bo
7575 });
7676 }
7777
78 /** A repository's chapter, with whether the reader may see that repository. */
79 export interface ChapterEntry {
80 readonly repo: string;
81 readonly chapter: Chapter;
82 readonly canSee: boolean;
83 }
84
85 export type Group =
86 | { readonly kind: "chapter"; readonly repo: string; readonly chapter: Chapter; readonly landings: readonly Landing[] }
87 | { readonly kind: "loose"; readonly landings: readonly Landing[] };
88
8978 /**
90 * Landings (newest first) grouped under the chapter they closed (#11868). A landing belongs to a
91 * closed chapter of its repository when it merged after the chapter opened and no later than it
92 * closed (the tag stands at the default branch's tip at that close). Everything else is loose, in
93 * runs. A chapter is placed at its newest landing; one with no landing is not drawn; one the
94 * reader may not see, or that closes after `now`, is ignored. The words are the chapter's own
95 * (`chapter_notes.ts` wrote them at close); nothing is recomputed here.
96 */
97 export function groupLandings(landings: readonly Landing[], entries: readonly ChapterEntry[], now: Date): Group[] {
98 const usable = entries.filter((e) => e.canSee && isDrawable(e.chapter, now));
99 const closes = (e: ChapterEntry): number => Date.parse(e.chapter.closedAt!);
100 const owner = (l: Landing): ChapterEntry | undefined => {
101 return usable
102 .filter((e) => e.repo.toLowerCase() === l.repo.toLowerCase() && inChapter(e.chapter, l.at))
103 .sort((a, b) => closes(a) - closes(b))[0];
104 };
105 const out: Group[] = [];
106 const placed = new Map<ChapterEntry, Landing[]>();
107 let run: Landing[] | undefined;
108 for (const l of landings) {
109 const e = owner(l);
110 if (e === undefined) {
111 if (run === undefined) {
112 run = [];
113 out.push({ kind: "loose", landings: run });
114 }
115 run.push(l);
116 continue;
117 }
118 run = undefined;
119 const members = placed.get(e);
120 if (members !== undefined) members.push(l);
121 else {
122 const first = [l];
123 placed.set(e, first);
124 out.push({ kind: "chapter", repo: e.repo, chapter: e.chapter, landings: first });
125 }
126 }
127 return out;
128 }
129
130 /**
131 * One query that moves whenever a chapter closes, is reopened or deleted, or a repository with
132 * one changes visibility: what the page's tag holds in place of the chapters themselves.
79 * One query that moves whenever a chapter opens, closes, is reopened or deleted, an item joins, leaves
80 * or moves between them, or a repository with one changes visibility: what the page's tag holds in place of
81 * the chapters themselves.
13382 */
13483 export function chapterStamp(db: Database): string {
13584 const r = db
136 .query<{ n: number; last: string | null; seen: string | null }, []>(
137 `SELECT count(*) AS n, max(chapter.closed_at) AS last, group_concat(DISTINCT repo.id || ':' || repo.visibility) AS seen
138 FROM chapter JOIN repo ON repo.id = chapter.repo_id WHERE chapter.state = 'closed'`,
85 .query<{ n: number; last: string | null; seen: string | null; items: number; placed: string | null }, []>(
86 `SELECT count(*) AS n, max(chapter.closed_at) AS last, group_concat(DISTINCT repo.id || ':' || repo.visibility) AS seen,
87 (SELECT count(*) || ':' || ifnull(max(at), '') FROM chapter_item) AS items,
88 (SELECT group_concat(issue_id || '>' || chapter_id) FROM (SELECT issue_id, chapter_id FROM chapter_item ORDER BY issue_id)) AS placed
89 FROM chapter JOIN repo ON repo.id = chapter.repo_id WHERE chapter.state != 'draft'`,
13990 )
14091 .get();
141 return `${r?.n ?? 0}|${r?.last ?? ""}|${r?.seen ?? ""}`;
92 return `${r?.n ?? 0}|${r?.last ?? ""}|${r?.seen ?? ""}|${r?.items ?? ""}|${Bun.hash(r?.placed ?? "").toString(36)}`;
14293 }
14394
144 /** The closed chapters of the repositories the live landings came from, each marked with whether `user` may read its repository. */
95 /**
96 * The chapters (open and closed) of the repositories the live landings came from, each marked with
97 * whether `user` may read its repository and which pulls served it.
98 */
14599 export function chapterEntries(db: Database, live: readonly Landing[], user: User | undefined): ChapterEntry[] {
146100 const out: ChapterEntry[] = [];
147101 for (const name of new Set(live.map((l) => l.repo))) {
@@ -149,28 +103,36 @@ export function chapterEntries(db: Database, live: readonly Landing[], user: Use
149103 const repo = getRepo(db, o, n);
150104 if (repo === undefined) continue;
151105 const canSee = canRead(db, repo, user);
152 for (const chapter of listChapters(db, repo.id)) if (chapter.state === "closed") out.push({ repo: name, chapter, canSee });
106 if (!canSee) continue;
107 const served = chapterOfPulls(db, repo.id);
108 for (const chapter of listChapters(db, repo.id)) {
109 if (chapter.state === "draft") continue;
110 const pulls = [...served].filter(([, id]) => id === chapter.id).map(([number]) => number);
111 out.push({ repo: name, chapter, canSee, pulls });
112 }
153113 }
154114 return out;
155115 }
156116
157117 /**
158 * A chapter's releases: the chapter is one row (its name linked to its page, the first sentence of
159 * its closing note in plain words, what it means and how many pulls it carried as facts), then the
160 * pulls under day headings as everywhere else, so each keeps its date.
118 * A chapter: a nav row (its name linked to its page, its closing day or "open" as the trail), then
119 * its landed pulls, one line each; a chapter nothing landed in says so on its row.
161120 */
162 function chapterSection(g: Extract<Group, { kind: "chapter" }>, own: string | undefined, showRepo: boolean, zone: string, today: string): Html {
121 function chapterSection(g: Extract<Group, { kind: "chapter" }>, own: string | undefined, showRepo: boolean): Html {
163122 const c = g.chapter;
164 const said = noteWords(c.notes);
165123 const head = row({
166124 href: `/${g.repo}/chapters/${c.name}`,
167125 nav: true,
168126 title: c.name,
169 ...(said === "" ? {} : { meta: html`${said}` }),
170 facts: [...(c.means === "" ? [] : [{ label: "Means", value: c.means }]), { label: "Pulls", value: String(g.landings.length) }],
171 ...(c.closedAt === null ? {} : { trail: time(c.closedAt) }),
127 ...(g.landings.length === 0 ? { meta: html`Nothing landed yet.` } : {}),
128 trail: c.closedAt === null ? html`open` : time(c.closedAt, { date: true }),
172129 });
173 return html`<section class="mt" data-chapter="${c.name}"><ul class="list setting-list">${head}</ul>${byDay(g.landings, own, showRepo, zone, today)}</section>`;
130 return html`<section class="mt" data-chapter="${c.name}"><ul class="list setting-list">${head}${g.landings.map((l) => landingRow(l, own, showRepo))}</ul></section>`;
131 }
132
133 /** The pulls that served no chapter, under a row of the same shape as a chapter's. */
134 function betweenSection(g: Extract<Group, { kind: "between" }>, own: string | undefined, showRepo: boolean): Html {
135 return html`<section class="mt" data-between><ul class="list setting-list">${row({ title: "Between chapters" })}${g.landings.map((l) => landingRow(l, own, showRepo))}</ul></section>`;
174136 }
175137
176138 /** Day sections in `zone`; the browser regroups them in its own (see `localDaysJs`), so the zone is named while it has not. */
@@ -299,14 +261,14 @@ function renderRegion(
299261 onItsWay.length === 0
300262 ? null
301263 : html`<h2 class="mt">On its way</h2><ul class="list setting-list">${onItsWay.map((l) => landingRow(l, read.own, choice === "all"))}</ul>`;
302 const groups = groupLandings(live, chapters, now);
303 const grouped = groups.some((g) => g.kind === "chapter");
264 const groups = groupByChapter(live, chapters);
265 const grouped = groups.length > 0;
304266 const gone =
305267 live.length === 0
306268 ? html`<p class="mt muted">Nothing has shipped yet.</p>`
307269 : !grouped
308270 ? html`${byDay(live, read.own, choice === "all", zone, todayIs)}`
309 : html`${groups.map((g) => (g.kind === "chapter" ? chapterSection(g, read.own, choice === "all", zone, todayIs) : byDay(g.landings, read.own, choice === "all", zone, todayIs)))}`;
271 : html`${groups.map((g) => (g.kind === "chapter" ? chapterSection(g, read.own, choice === "all") : betweenSection(g, read.own, choice === "all")))}`;
310272 return html`${top}${way}${gone}`;
311273 }
312274
test/changelog-chapters.test.ts +116 −60
@@ -1,9 +1,10 @@
1 /** /changelog groups releases under the chapter they closed (#11868); loose ones stay flat. */
1 /** /changelog tells its pulls in chapters, newest first, read from the chapter items (#12695). */
22 import { expect, test } from "bun:test";
33 import type { Chapter } from "../src/core/chapters.ts";
44 import type { Landing, Landings } from "../src/release/landings.ts";
5 import { chapterEntries, chapterStamp, groupLandings, shippedRegion, type ChapterEntry } from "../src/web/pages/changelog.ts";
6 import { noteWords } from "../src/release/chapter_words.ts";
5 import { chapterEntries, chapterStamp, shippedRegion } from "../src/web/pages/changelog.ts";
6 import { groupByChapter, type ChapterEntry } from "../src/release/chapter_groups.ts";
7 import { chapterOfPulls } from "../src/core/chapters.ts";
78 import { createRepo } from "../src/actions/repos.ts";
89 import { getRepo } from "../src/core/repos.ts";
910 import { createUser } from "../src/core/users.ts";
@@ -15,61 +16,67 @@ import { join } from "node:path";
1516 const NOW = new Date("2026-11-12T12:00:00Z");
1617 const day = (d: number) => `2026-10-${String(d).padStart(2, "0")}T10:00:00Z`;
1718 const land = (number: number, at: string): Landing => ({ repo: "a/b", number, title: `Pull ${number}`, at, live: true });
18 const chapter = (name: string, opened: string, closed: string | null, over: Partial<Chapter> = {}): Chapter => ({
19 id: name, repoId: "r", name, means: `${name} means done`, state: closed === null ? "open" : "closed", position: 1, patchOf: null,
20 createdAt: opened, openedAt: opened, closedAt: closed, tagOid: closed === null ? null : "abc123", notes: `# ${name}\n\nShipped.\n`, costUsd: 1, ...over,
19 const chapter = (name: string, closed: string | null, position = 1): Chapter => ({
20 id: name, repoId: "r", name, means: `${name} means done`, state: closed === null ? "open" : "closed", position, patchOf: null,
21 createdAt: day(1), openedAt: day(1), closedAt: closed, tagOid: closed === null ? null : "abc123", notes: "", costUsd: 1,
2122 });
22 const entry = (c: Chapter, canSee = true): ChapterEntry => ({ repo: "a/b", chapter: c, canSee });
23 const entry = (c: Chapter, pulls: number[], canSee = true): ChapterEntry => ({ repo: "a/b", chapter: c, canSee, pulls });
24 const names = (gs: ReturnType<typeof groupByChapter>) => gs.map((g) => (g.kind === "chapter" ? g.chapter.name : "between"));
2325
24 // Newest first: 30, 25 loose; 20, 15 in v2; 10 loose between; 5, 3 in v1; 1 before v1 opened.
25 const landings = [land(30, day(30)), land(25, day(25)), land(20, day(20)), land(15, day(15)), land(10, day(10)), land(5, day(5)), land(3, day(3)), land(1, day(1))];
26 const v1 = chapter("v1.0.0", day(2), day(6));
27 const v2 = chapter("v2.0.0", day(12), day(21));
26 const landings = [land(30, day(30)), land(25, day(25)), land(20, day(20)), land(10, day(10)), land(5, day(5))];
2827
29 test("two chapters and loose releases come out newest first with the right members", () => {
30 const groups = groupLandings(landings, [entry(v1), entry(v2)], NOW);
31 expect(groups.map((g) => (g.kind === "chapter" ? g.chapter.name : "loose"))).toEqual(["loose", "v2.0.0", "loose", "v1.0.0", "loose"]);
32 expect(groups.map((g) => g.landings.map((l) => l.number))).toEqual([[30, 25], [20, 15], [10], [5, 3], [1]]);
28 test("chapters come out newest first, each with its pulls; those that served none are between chapters, last", () => {
29 const groups = groupByChapter(landings, [entry(chapter("v1", day(6), 1), [5]), entry(chapter("v2", day(21), 2), [20, 10]), entry(chapter("v3", null, 3), [30])]);
30 expect(names(groups)).toEqual(["v3", "v2", "v1", "between"]);
31 expect(groups.map((g) => g.landings.map((l) => l.number))).toEqual([[30], [20, 10], [5], [25]]);
3332 });
3433
35 test("a chapter with no release yet is not drawn, nor is one still open or closing after now", () => {
36 const empty = chapter("v3.0.0", day(31), "2026-10-31T23:00:00Z");
37 const later = chapter("v9.0.0", day(1), "2027-01-01T00:00:00Z");
38 const groups = groupLandings(landings, [entry(empty), entry(later), entry(chapter("v4", day(1), null))], NOW);
39 expect(groups).toEqual([{ kind: "loose", landings }]);
34 test("membership is the items' and not the dates': a pull merged inside a chapter's window but not in it is between", () => {
35 const groups = groupByChapter([land(7, day(3))], [entry(chapter("v1", day(6)), [])]);
36 expect(names(groups)).toEqual(["v1", "between"]);
37 expect(groups[0]!.landings).toEqual([]);
4038 });
4139
42 test("a chapter of a repository the reader may not see is not shown", () => {
43 const groups = groupLandings(landings, [entry(v1, false), entry(v2)], NOW);
44 expect(groups.flatMap((g) => (g.kind === "chapter" ? [g.chapter.name] : []))).toEqual(["v2.0.0"]);
40 test("an empty chapter is kept; a draft, a hidden repository's and a stranger repository's chapter are not; none at all is no grouping", () => {
41 const draft = { ...chapter("d", null), state: "draft" as const };
42 const groups = groupByChapter(landings, [entry(chapter("v1", day(6)), []), entry(draft, []), entry(chapter("hid", day(7)), [], false), { ...entry(chapter("x", day(8)), []), repo: "z/z" }]);
43 expect(names(groups)).toEqual(["v1", "between"]);
44 expect(groupByChapter(landings, [])).toEqual([]);
4545 });
4646
4747 const read = (live: Landing[]): Landings => ({ version: "1.0.0", commit: null, own: "a/b", live, onItsWay: [] });
4848
49 test("the region draws the chapter as a row: plain words from the note, no markdown, escaped, each pull under its day", () => {
50 const html = String(shippedRegion(read(landings), "a/b", NOW, "UTC", undefined, true, [entry(chapter("v2.0.0", day(12), day(21), { notes: "# v2\n\n**Shipped** the <b>x</b> thing. More later.\n" }))]));
51 expect(html).toContain('data-chapter="v2.0.0"');
52 expect(html).toContain("v2.0.0 means done");
53 expect(html).toContain("Shipped the x thing.");
54 expect(html).not.toContain("More later");
55 expect(html).not.toContain("# v2");
56 expect(html).not.toContain("**");
57 expect(html).not.toContain("<pre");
58 expect(html).not.toContain("<b>");
59 // The dates stay: a day heading inside the chapter for each day its pulls landed.
60 const inside = html.slice(html.indexOf('data-chapter="v2.0.0"'));
61 expect(inside).toContain('data-day="2026-10-20"');
62 expect(inside).toContain('data-day="2026-10-15"');
63 expect(String(shippedRegion(read(landings), "a/b", NOW))).not.toContain("data-chapter");
49 test("the page draws a heading with the closing day or open, one line per pull, escaped, and Between chapters", () => {
50 const evil = chapter("v<1>", day(21));
51 const html = String(shippedRegion(read(landings), "a/b", NOW, "UTC", undefined, true, [entry(evil, [20, 10]), entry(chapter("v3", null, 3), [])]));
52 expect(html).toContain('data-chapter="v3"');
53 expect(html).toContain("open");
54 expect(html).toContain("Nothing landed yet.");
55 expect(html).toContain("2026-10-21");
56 expect(html).toContain("Between chapters");
57 expect(html).not.toContain("v<1>");
58 expect(html.indexOf('data-chapter="v3"')).toBeLessThan(html.indexOf("Between chapters"));
59 expect(html.indexOf("Pull 20")).toBeLessThan(html.indexOf("Between chapters"));
60 expect(html.indexOf("Pull 25")).toBeGreaterThan(html.indexOf("Between chapters"));
61 // No chapters: the flat list as before.
62 expect(String(shippedRegion(read(landings), "a/b", NOW))).not.toContain("Between chapters");
6463 });
6564
66 test("noteWords keeps the first sentence as plain words", () => {
67 expect(noteWords("# Title\n\n- **Fast** [docs](http://x) and `code`. Second.")).toBe("Fast docs and code.");
68 expect(noteWords("# only a heading\n")).toBe("");
69 expect(noteWords("x".repeat(500)).length).toBeLessThanOrEqual(200);
70 });
65 type Made = ReturnType<typeof createApp>;
66 function chapterRow(app: Made, repoId: string, name: string, state: string, closed: string | null): string {
67 const id = `c-${name}-${repoId}`;
68 app.db
69 .query(`INSERT INTO chapter (id, repo_id, name, means, state, position, created_at, opened_at, closed_at, tag_oid, notes) VALUES (?1, ?2, ?3, 'm', ?4, 1, ?5, ?5, ?6, ?7, 'Done.')`)
70 .run(id, repoId, name, state, day(1), closed, closed === null ? null : "abc");
71 return id;
72 }
73 function mergedPull(app: Made, repoId: string, number: number, body: string): string {
74 const issue = `i-${number}-${repoId}`;
75 app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, state, author_id) VALUES (?1, ?2, ?3, 'pull', ?4, ?5, 'closed', (SELECT id FROM user LIMIT 1))`).run(issue, repoId, number, `Pull ${number}`, body);
76 return issue;
77 }
7178
72 test("chapterStamp moves when a chapter closes and is the same otherwise", async () => {
79 test("which chapter a pull served is read from the items; chapterStamp moves with them", async () => {
7380 const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-chap-")), port: 0 });
7481 try {
7582 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "own", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
@@ -78,20 +85,37 @@ test("chapterStamp moves when a chapter closes and is the same otherwise", async
7885 if (!made.ok) throw new Error("repo");
7986 const repo = getRepo(app.db, "own", "r")!;
8087 const before = chapterStamp(app.db);
81 expect(chapterStamp(app.db)).toBe(before);
82 closed(app, repo.id, "v1");
83 expect(chapterStamp(app.db)).not.toBe(before);
88 const c1 = chapterRow(app, repo.id, "v1", "closed", day(6));
89 const afterChapter = chapterStamp(app.db);
90 expect(afterChapter).not.toBe(before);
91 expect(chapterStamp(app.db)).toBe(afterChapter);
92 // Issue #1 is an item of v1; pull #2 closes it; pull #3 says nothing.
93 app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, state, author_id) VALUES ('it', ?1, 1, 'issue', 'Item', '', 'closed', (SELECT id FROM user LIMIT 1))`).run(repo.id);
94 const item = "it";
95 app.db.query("INSERT INTO chapter_item (issue_id, chapter_id) VALUES (?1, ?2)").run(item, c1);
96 const withItem = chapterStamp(app.db);
97 expect(withItem).not.toBe(afterChapter);
98 // Moving the item to another chapter changes neither the count nor the newest `at`:
99 // the tag must still move.
100 const c2 = chapterRow(app, repo.id, "v2", "closed", day(12));
101 const withTwo = chapterStamp(app.db);
102 app.db.query("UPDATE chapter_item SET chapter_id = ?1 WHERE issue_id = ?2").run(c2, item);
103 expect(chapterStamp(app.db)).not.toBe(withTwo);
104 app.db.query("UPDATE chapter_item SET chapter_id = ?1 WHERE issue_id = ?2").run(c1, item);
105 expect(chapterStamp(app.db)).toBe(withTwo);
106 for (const [n, body] of [[2, "Closes #1"], [3, "Mentions nothing"]] as const) {
107 const id = mergedPull(app, repo.id, n, body);
108 app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', ?2, 'a', 'b', ?3)").run(id, `p${n}`, day(5));
109 }
110 expect([...chapterOfPulls(app.db, repo.id)]).toEqual([[2, c1]]);
111 const live = [land(3, day(5)), land(2, day(5))].map((l) => ({ ...l, repo: "own/r" }));
112 const groups = groupByChapter(live, chapterEntries(app.db, live, undefined));
113 expect(groups.map((g) => [g.kind, g.landings.map((l) => l.number)])).toEqual([["chapter", []], ["chapter", [2]], ["between", [3]]]);
84114 } finally {
85115 app.close();
86116 }
87117 });
88118
89 function closed(app: ReturnType<typeof createApp>, repoId: string, name: string): void {
90 app.db
91 .query(`INSERT INTO chapter (id, repo_id, name, means, state, position, created_at, opened_at, closed_at, tag_oid, notes) VALUES (?1, ?2, ?3, 'm', 'closed', 1, ?4, ?4, ?5, 'abc', 'Done.')`)
92 .run(`c-${name}-${repoId}`, repoId, name, day(1), day(6));
93 }
94
95119 test("a private repository's chapter is read by its owner and by no outsider", async () => {
96120 const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-chap-")), port: 0 });
97121 try {
@@ -105,16 +129,48 @@ test("a private repository's chapter is read by its owner and by no outsider", a
105129 const made = await createRepo(app, owner, { name: "secret", visibility: "private" });
106130 if (!made.ok) throw new Error(JSON.stringify(made.error));
107131 const repo = getRepo(app.db, "own", "secret")!;
108 closed(app, repo.id, "v1");
132 chapterRow(app, repo.id, "v1", "closed", day(6));
109133 const mine = [{ repo: "own/secret", number: 3, title: "Pull 3", at: day(4), live: true }];
110 const seen = (u: typeof owner | undefined) => groupLandings(mine, chapterEntries(app.db, mine, u), NOW).filter((g) => g.kind === "chapter");
111 expect(seen(owner).map((g) => (g.kind === "chapter" ? g.chapter.name : ""))).toEqual(["v1"]);
134 const seen = (u: typeof owner | undefined) => groupByChapter(mine, chapterEntries(app.db, mine, u)).filter((g) => g.kind === "chapter");
135 expect(seen(owner)).toHaveLength(1);
112136 expect(seen(outsider)).toEqual([]);
113137 expect(seen(undefined)).toEqual([]);
114 // And in the page's words: the outsider's region has no chapter row.
115 const region = (u: typeof owner | undefined) => String(shippedRegion({ version: "1", commit: null, own: "own/secret", live: mine, onItsWay: [] }, "own/secret", NOW, "UTC", undefined, true, chapterEntries(app.db, mine, u)));
116 expect(region(owner)).toContain('data-chapter="v1"');
117 expect(region(outsider)).not.toContain("data-chapter");
138 } finally {
139 app.close();
140 }
141 });
142
143 test("500 pulls in chapters render well inside the page's budget", () => {
144 const many = Array.from({ length: 500 }, (_, i) => land(i + 1, day(1 + (i % 28))));
145 const entries = [entry(chapter("v1", day(6), 1), many.slice(0, 200).map((l) => l.number)), entry(chapter("v2", null, 2), many.slice(200, 400).map((l) => l.number))];
146 const start = performance.now();
147 const html = String(shippedRegion(read(many), "a/b", NOW, "UTC", undefined, true, entries));
148 expect(performance.now() - start).toBeLessThan(250);
149 expect(html).toContain("Between chapters");
150 });
151
152 test("a pull that closes issues in two chapters is placed in the first match, in the order of its Closes", async () => {
153 const app = createApp({ dataDir: mkdtempSync(join(tmpdir(), "forge-chap-")), port: 0 });
154 try {
155 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "own", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
156 if (!u.ok) throw new Error(u.error);
157 const made = await createRepo(app, u.value, { name: "r", visibility: "public" });
158 if (!made.ok) throw new Error("repo");
159 const repo = getRepo(app.db, "own", "r")!;
160 const c1 = chapterRow(app, repo.id, "v1", "closed", day(6));
161 const c2 = chapterRow(app, repo.id, "v2", "closed", day(12));
162 for (const [n, chapterId] of [[1, c1], [2, c2]] as const) {
163 app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, body, state, author_id) VALUES (?1, ?2, ?3, 'issue', 'Item', '', 'closed', (SELECT id FROM user LIMIT 1))`).run(`it${n}`, repo.id, n);
164 app.db.query("INSERT INTO chapter_item (issue_id, chapter_id) VALUES (?1, ?2)").run(`it${n}`, chapterId);
165 }
166 const put = (n: number, body: string) => {
167 const id = mergedPull(app, repo.id, n, body);
168 app.db.query("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at) VALUES (?1, 'main', ?2, 'a', 'b', ?3)").run(id, `p${n}`, day(5));
169 };
170 put(3, "Closes #2, closes #1");
171 put(4, "Closes #1 and closes #2");
172 expect(chapterOfPulls(app.db, repo.id).get(3)).toBe(c2);
173 expect(chapterOfPulls(app.db, repo.id).get(4)).toBe(c1);
118174 } finally {
119175 app.close();
120176 }
test/changelog-timing.test.ts +29
@@ -75,3 +75,32 @@ test(`${ENTRIES} entries: GET /changelog answers in under ${BUDGET_MS} ms (media
7575 console.log(`changelog median ${m.toFixed(1)} ms over ${ENTRIES} entries (${runs.map((r) => r.toFixed(1)).join(", ")})`);
7676 expect(m).toBeLessThan(BUDGET_MS);
7777 }, 60_000);
78
79 test(`${ENTRIES} entries in two chapters: GET /changelog still answers in under ${BUDGET_MS} ms`, async () => {
80 const repoId = app.db.query<{ id: string }, []>("SELECT id FROM repo WHERE name = 'forge'").get()!.id;
81 const chapter = app.db.query(
82 `INSERT INTO chapter (id, repo_id, name, means, state, position, created_at, opened_at, closed_at, tag_oid, notes) VALUES (?1, ?2, ?3, 'm', ?4, ?5, '2026-01-01T00:00:00Z', '2026-01-01T00:00:00Z', ?6, ?7, '')`,
83 );
84 chapter.run("c1", repoId, "v1", "closed", 1, "2026-02-01T00:00:00Z", "abc");
85 chapter.run("c2", repoId, "v2", "open", 2, null, null);
86 const item = app.db.query("INSERT INTO chapter_item (issue_id, chapter_id) VALUES (?1, ?2)");
87 app.db.transaction(() => {
88 for (let n = 1; n <= 400; n++) item.run(`i${n}`, n <= 200 ? "c1" : "c2");
89 })();
90 const warm = await fetch(`${servers.url}/changelog`);
91 expect(warm.status).toBe(200);
92 const body = await warm.text();
93 expect(body).toContain('data-chapter="v1"');
94 expect(body).toContain("Between chapters");
95 const runs: number[] = [];
96 for (let i = 0; i < RUNS; i++) {
97 const t = performance.now();
98 const res = await fetch(`${servers.url}/changelog`);
99 await res.arrayBuffer();
100 runs.push(performance.now() - t);
101 expect(res.status).toBe(200);
102 }
103 const m = median(runs);
104 console.log(`changelog with chapters median ${m.toFixed(1)} ms over ${ENTRIES} entries (${runs.map((r) => r.toFixed(1)).join(", ")})`);
105 expect(m).toBeLessThan(BUDGET_MS);
106 }, 60_000);
test/tui-changelog-chapters.test.ts added +102
@@ -0,0 +1,102 @@
1 /**
2 * The terminal's changelog in chapters: Enter on a pull line opens it, a private repository's
3 * chapters stay hidden, wording follows the grouping (#12695).
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createRepo } from "../src/actions/repos.ts";
10 import { createApp, type App } from "../src/app.ts";
11 import { createUser } from "../src/core/users.ts";
12 import { forgetLive, keepLive } from "../src/release/landings.ts";
13 import { createForgeApp } from "../src/tui/forge.ts";
14 import { changelogScreen } from "../src/tui/screens/changelog.ts";
15 import { headlessTerminal } from "../src/tui/term/terminal.ts";
16 import { quietLogs } from "./helpers/globals.ts";
17 import { settleApp } from "./helpers/tui.ts";
18
19 quietLogs();
20
21 let dir: string;
22 let app: App;
23 const guest = { sshUsername: "git", remoteAddress: "127.0.0.1", fingerprint: "SHA256:test" };
24 const saved = process.env["FORGE_COMMIT"];
25
26 beforeAll(async () => {
27 process.env["FORGE_COMMIT"] = "a".repeat(40);
28 dir = await mkdtemp(join(tmpdir(), "forge-tui-chapters-"));
29 app = createApp({ dataDir: dir, port: 0, forgeRepo: "alice/forge" });
30 const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true });
31 if (!u.ok) throw new Error(u.error);
32 const made = await createRepo(app, u.value, { name: "forge", visibility: "public" });
33 if (!made.ok) throw new Error(JSON.stringify(made.error));
34 const issue = app.db.query(`INSERT INTO issue (id, repo_id, number, kind, title, author_id, state) VALUES (?1, ?2, ?3, 'pull', ?4, ?5, 'closed')`);
35 const pull = app.db.query(`INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid, merged_at, merged_oid) VALUES (?1, ?2, 'b', 'base', '', ?3, ?4)`);
36 const oids: string[] = [];
37 for (const n of [1, 2]) {
38 issue.run(`i${n}`, made.value.id, n, `Landed pull ${n}`, u.value.id);
39 pull.run(`i${n}`, made.value.defaultBranch, `2026-10-0${n}T10:00:00.000Z`, `oid-${n}`);
40 oids.push(`oid-${n}`);
41 }
42 keepLive(app.db, "alice/forge", oids, 0);
43 app.db
44 .query(`INSERT INTO chapter (id, repo_id, name, means, state, position, created_at, opened_at, closed_at, tag_oid, notes) VALUES ('c1', ?1, 'v1', 'm', 'closed', 1, '2026-09-01T00:00:00Z', '2026-09-01T00:00:00Z', '2026-10-05T00:00:00Z', 'abc', '')`)
45 .run(made.value.id);
46 app.db.query("INSERT INTO chapter_item (issue_id, chapter_id) VALUES ('i2', 'c1')").run();
47 }, 60_000);
48
49 afterAll(async () => {
50 forgetLive(app.db);
51 if (saved === undefined) delete process.env["FORGE_COMMIT"];
52 else process.env["FORGE_COMMIT"] = saved;
53 app.close();
54 await rm(dir, { recursive: true, force: true });
55 });
56
57 async function open() {
58 const terminal = headlessTerminal(120, 30);
59 const { app: tui, session } = createForgeApp(app, guest, { terminal, exitHooks: false });
60 tui.start();
61 tui.ctx.push(changelogScreen(session, tui.ctx));
62 await settleApp(tui);
63 return { terminal, tui };
64 }
65
66 test("Enter on a pull line under a chapter opens that pull; the filter and its empty state speak of pulls", async () => {
67 const { terminal, tui } = await open();
68 const text = tui.screen.toText();
69 expect(text).toContain("Chapter v1");
70 expect(text).toContain("#2 Landed pull 2");
71 expect(text).toContain("Between chapters");
72 // Down past the heading to the pull line, then Enter.
73 terminal.feed("j");
74 await settleApp(tui);
75 terminal.feed("\r");
76 await settleApp(tui);
77 expect(tui.screen.toText()).toContain("Changelog › #2");
78 terminal.feed("q");
79 await settleApp(tui);
80 terminal.feed("/");
81 await settleApp(tui);
82 expect(tui.screen.toText()).toContain("find a pull");
83 terminal.feed("zzzz");
84 await settleApp(tui);
85 const none = tui.screen.toText();
86 tui.stop?.();
87 expect(none).toContain("No pull matches");
88 expect(none).not.toContain("No release matches");
89 });
90
91 test("a private repository's chapters are not shown to a guest in the terminal", async () => {
92 app.db.run("UPDATE repo SET visibility = 'private' WHERE name = 'forge'");
93 try {
94 const { tui } = await open();
95 const text = tui.screen.toText();
96 tui.stop?.();
97 expect(text).not.toContain("Chapter v1");
98 expect(text).not.toContain("Landed pull");
99 } finally {
100 app.db.run("UPDATE repo SET visibility = 'public' WHERE name = 'forge'");
101 }
102 });
test/tui-changelog.test.ts +22 −15
@@ -9,7 +9,8 @@ import { createApp, type App } from "../src/app.ts";
99 import { createUser } from "../src/core/users.ts";
1010 import { createForgeApp, forgeCommands, Session } from "../src/tui/forge.ts";
1111 import type { Ctx } from "../src/tui/app/app.ts";
12 import { changelogLines, changelogScreen, creditWords, pullLines } from "../src/tui/screens/changelog.ts";
12 import { groupByChapter } from "../src/release/chapter_groups.ts";
13 import { chapterLines, changelogLines, changelogScreen, creditWords, pullLines } from "../src/tui/screens/changelog.ts";
1314 import { headlessTerminal } from "../src/tui/term/terminal.ts";
1415 import { quietLogs } from "./helpers/globals.ts";
1516 import { settleApp } from "./helpers/tui.ts";
@@ -116,20 +117,26 @@ test("a release's pulls read plain, and pulls of one shape gather into one line"
116117 expect(lines[0]!.row.meta).toContain("3 pulls");
117118 });
118119
119 test("the terminal groups releases under the chapter that closed them, as the page does", () => {
120 const ch = (name: string, opened: string, closedAt: string | null) => ({
121 id: name, repoId: "r", name, means: "m", state: closedAt === null ? "open" : "closed", position: 1, patchOf: null,
122 createdAt: opened, openedAt: opened, closedAt, tagOid: closedAt === null ? null : "abc", notes: "# v1\n\n**Shipped** it. More.", costUsd: null,
120 test("the terminal tells pulls in chapters, open first, then between chapters; with no chapter the releases stay", () => {
121 const ch = (name: string, closedAt: string | null, position: number) => ({
122 id: name, repoId: "r", name, means: "m", state: closedAt === null ? "open" : "closed", position, patchOf: null,
123 createdAt: "2026-10-01T00:00:00Z", openedAt: "2026-10-01T00:00:00Z", closedAt, tagOid: null, notes: "", costUsd: null,
123124 }) as const;
124 const rel = (version: string, date: string) => ({ version, date, pulls: [] });
125 const lines = changelogLines(
126 [rel("0.4.0", "2026-10-30T00:00:00Z"), rel("0.3.0", "2026-10-20T00:00:00Z"), rel("0.2.0", "2026-10-10T00:00:00Z"), rel("0.1.0", "2026-10-01T00:00:00Z")],
127 [ch("v1", "2026-10-05T00:00:00Z", "2026-10-25T00:00:00Z"), ch("v2", "2026-10-26T00:00:00Z", null)],
128 new Date("2026-11-12T00:00:00Z"),
125 const land = (number: number) => ({ repo: "a/b", number, title: `Pull ${number}`, at: "2026-10-20T00:00:00Z", live: true });
126 const groups = groupByChapter(
127 [land(4), land(3), land(2), land(1)],
128 [
129 { repo: "a/b", chapter: ch("v1", "2026-10-25T00:00:00Z", 1), canSee: true, pulls: [1] },
130 { repo: "a/b", chapter: ch("v2", null, 2), canSee: true, pulls: [3, 4] },
131 { repo: "a/b", chapter: ch("v3", null, 3), canSee: true, pulls: [] },
132 ],
129133 );
130 expect(lines.map((l) => l.row.title)).toEqual(["0.4.0", "Chapter v1", " 0.3.0", " 0.2.0", "0.1.0"]);
131 expect(lines[1]!.row.meta).toContain("2 releases");
132 expect(lines[1]!.row.meta).toContain("Shipped it.");
133 expect(lines[1]!.entry).toBeUndefined();
134 expect(changelogLines([rel("0.4.0", "2026-10-30T00:00:00Z")], [], new Date()).map((l) => l.row.title)).toEqual(["0.4.0"]);
134 const lines = chapterLines(groups);
135 expect(lines.map((l) => l.row.title)).toEqual(["Chapter v3", "Chapter v2", " #4 Pull 4", " #3 Pull 3", "Chapter v1", " #1 Pull 1", "Between chapters", " #2 Pull 2"]);
136 expect(lines[1]!.row.meta).toContain("open");
137 expect(lines[4]!.row.meta).toContain("closed 2026-10-25");
138 expect(lines.filter((l) => l.pull !== undefined).map((l) => l.pull)).toEqual([4, 3, 1, 2]);
139 // Narrowed, a heading with no pull left goes.
140 expect(chapterLines(groups, (l) => l.number === 1, true).map((l) => l.row.title)).toEqual(["Chapter v1", " #1 Pull 1"]);
141 expect(changelogLines([{ version: "0.4.0", date: "2026-10-30T00:00:00Z", pulls: [] }]).map((l) => l.row.title)).toEqual(["0.4.0"]);
135142 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs