Public: anyone can read this; the crew can change it.
The queue page shows how fast the open chapter is filling or draining, in one line #15601
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/15594-the-queue-page-shows-how-fast-the-open-c → main 0 commits fork 4 threads · 4 unresolved
docs/features/chapters.md +4
| @@ -40,3 +40,7 @@ Try it: open `/<owner>/<name>/chapters/<open chapter>` and read the line under t | ||
| 40 | 40 | A sealed security report added to a chapter is listed only to someone who may see it (the reporter and the repository's writers). For anyone else the chapter's page, `/next`'s Chapters section, the chapter bar, the API and the MCP `repo` tool leave it out of the list and out of the counts (`itemsOf` takes the viewer, `sealedScope`; there is no default). The bar counts what the page lists, so a reporter's bar counts their own report too. The changelog's grouping by chapter (`chapterOfPulls`) leaves a sealed pull out the same way. A closed chapter's list is kept for two views only, everything and the public one; a reporter who filed a sealed issue in the repository gets theirs read fresh. The terminal's chapters screen and its /next screen read through the same function with the signed-in person, so they list and count exactly what the page does. |
| 41 | 41 | |
| 42 | 42 | Try it: file a sealed issue, add it to the open chapter, then open `/<owner>/<name>/chapters/<name>` signed out: the report is not listed and the count leaves it out; signed in as a writer, or on the terminal's Chapters screen as a writer, it is there. |
| 43 | ||
| 44 | ## How fast the book fills | |
| 45 | ||
| 46 | The chapter line on `/next`, the open chapter's page, the terminal's Queue screen and the MCP `repo` tool (`view: chapters` with `chapter`, field `flow`) carry one sentence: `opened 12 this week, landed 9: the book is growing by 3` (or `draining by N`, or `flat`). Opened is items put in the chapter in the last seven days, landed is items done (live and proven) in them; a sealed item the viewer may not see counts in neither. Read by `flowOf` in `src/core/chapters.ts`; the terminal door is `queueLines` in `src/tui/screens/queuegrowth.ts`. It is the signal for pausing the planner. |
src/core/chapters.ts +48
| @@ -448,6 +448,54 @@ export const nextParts = (later: Chapter | undefined): Parts => | ||
| 448 | 448 | later === undefined ? { lead: NO_NEXT_WORDS, name: null, tail: "", chapter: null } : { lead: "Next: ", name: later.name, tail: "", chapter: later }; |
| 449 | 449 | export const nextWords = (later: Chapter | undefined): string => joined(nextParts(later)); |
| 450 | 450 | |
| 451 | /** The window the flow line counts: seven days, as /next's week line does. */ | |
| 452 | export const FLOW_DAYS = 7; | |
| 453 | ||
| 454 | export interface Flow { | |
| 455 | /** Items put in the chapter in the window. */ | |
| 456 | readonly opened: number; | |
| 457 | /** Items that went live and proven in the window. */ | |
| 458 | readonly landed: number; | |
| 459 | /** opened minus landed: positive while the book grows. */ | |
| 460 | readonly net: number; | |
| 461 | } | |
| 462 | ||
| 463 | /** | |
| 464 | * How fast the open chapter fills or drains (#15594): items put in over the last seven days | |
| 465 | * against items done in them, over the list as `view` may see it. A sealed item the viewer may not | |
| 466 | * see counts in neither. Pure over its inputs; `flowOf` reads the dates. | |
| 467 | */ | |
| 468 | export function flowIn(addedAt: readonly string[], items: readonly Item[], now: Date): Flow { | |
| 469 | const from = now.getTime() - FLOW_DAYS * 86_400_000; | |
| 470 | const within = (iso: string | null): boolean => iso !== null && Date.parse(iso) > from && Date.parse(iso) <= now.getTime(); | |
| 471 | const opened = addedAt.filter(within).length; | |
| 472 | const landed = items.filter((i) => i.status === "done" && within(i.doneAt)).length; | |
| 473 | return { opened, landed, net: opened - landed }; | |
| 474 | } | |
| 475 | ||
| 476 | /** The flow of a chapter's list: `items` is what `itemsOf` answered for the same viewer. */ | |
| 477 | export function flowOf(db: Database, chapter: Chapter, items: readonly Item[], now: Date): Flow { | |
| 478 | const ids = new Set(items.map((i) => i.issueId)); | |
| 479 | const added = db | |
| 480 | .query<{ issueId: string; at: string }, [string]>("SELECT issue_id AS issueId, at FROM chapter_item WHERE chapter_id = ?1") | |
| 481 | .all(chapter.id) | |
| 482 | .filter((r) => ids.has(r.issueId)) | |
| 483 | .map((r) => r.at); | |
| 484 | return flowIn(added, items, now); | |
| 485 | } | |
| 486 | ||
| 487 | /** "opened 12 this week, landed 9: the book is growing by 3": the same words on every door. */ | |
| 488 | export function flowSentence(f: Flow): string { | |
| 489 | const verdict = f.net > 0 ? `growing by ${f.net}` : f.net < 0 ? `draining by ${-f.net}` : "flat"; | |
| 490 | return `opened ${f.opened} this week, landed ${f.landed}: the book is ${verdict}`; | |
| 491 | } | |
| 492 | ||
| 493 | /** The open chapter's flow sentence, or null with none open. */ | |
| 494 | export function openFlowWords(db: Database, repoId: string, view: ItemView, now: Date): string | null { | |
| 495 | const open = openChapterOf(db, repoId); | |
| 496 | return open === undefined ? null : flowSentence(flowOf(db, open, itemsOf(db, open, view), now)); | |
| 497 | } | |
| 498 | ||
| 451 | 499 | /** |
| 452 | 500 | * The line under /next's heading; null when there are no chapters. With none open, `name` is "Draft |
| 453 | 501 | * one". |
src/http/mcp/tools/repo.ts +2 −1
| @@ -5,7 +5,7 @@ import { repoFor } from "../../../core/repo_fields.ts"; | ||
| 5 | 5 | import { upstreamFacts } from "../../../actions/upstream_follow.ts"; |
| 6 | 6 | import { creditFacts } from "../../../actions/credit_facts.ts"; |
| 7 | 7 | import { type ChapterActError, close, fill, open, preview, refusalWords, type Refused } from "../../../actions/chapters.ts"; |
| 8 | import { costSentence, getChapter, itemsOf, spentOf, listChapters, progressOf, progressSentence } from "../../../core/chapters.ts"; | |
| 8 | import { costSentence, flowOf, flowSentence, getChapter, itemsOf, spentOf, listChapters, progressOf, progressSentence } from "../../../core/chapters.ts"; | |
| 9 | 9 | import { err } from "../../../kernel/result.ts"; |
| 10 | 10 | import { sealedScope } from "../../../core/sealed.ts"; |
| 11 | 11 | import { good, str, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; |
| @@ -59,6 +59,7 @@ export const tool: Tool = { | ||
| 59 | 59 | const p = progressOf(c, items, now); |
| 60 | 60 | return good({ |
| 61 | 61 | name: c.name, means: c.means, state: c.state, progress: progressSentence(p), cost: costSentence(items), spentUsd: spentOf(items), finishesOn: p.finishesOn, |
| 62 | ...(c.state === "open" ? { flow: flowSentence(flowOf(ctx.app.db, c, items, now)) } : {}), | |
| 62 | 63 | items: items.map((i) => ({ number: i.number, title: i.title, status: i.status, doneAt: i.doneAt })), |
| 63 | 64 | }); |
| 64 | 65 | } |
src/tui/screens/queuegrowth.ts +3
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | |
| 12 | 12 | import { DEFER_TOO_LATE, deferOldest, hasGate, recentDefer, undoDefer } from "../../actions/defer_aged.ts"; |
| 13 | 13 | import { setCeiling, setPlannerSwitch, SWITCH_REFUSED, switchInput, type SwitchInput } from "../../actions/planner_switch.ts"; |
| 14 | import { openFlowWords } from "../../core/chapters.ts"; | |
| 14 | 15 | import { growth, oldest, WHO } from "../../core/growth.ts"; |
| 15 | 16 | import { growthPanelFor } from "../../core/growth_panel.ts"; |
| 16 | 17 | import { ceilingHold, CEILING_REASON, openCeiling, PAUSE_STOPS, pauseSentence, pausedUntil } from "../../core/planner_pause.ts"; |
| @@ -38,6 +39,7 @@ export function queueLines(f: Session, repo: Repo, now = new Date()): string[] { | ||
| 38 | 39 | const scope = sealedScope(f.db, repo, f.user); |
| 39 | 40 | const g = growth(f.db, repo, DAYS, now, scope); |
| 40 | 41 | const age = oldest(f.db, repo.id, scope); |
| 42 | const flow = openFlowWords(f.db, repo.id, scope, now); | |
| 41 | 43 | const until = pausedUntil(f.db, repo.id, now); |
| 42 | 44 | const sum = (pick: (d: (typeof g.days)[number]) => number) => g.days.reduce((s, d) => s + pick(d), 0); |
| 43 | 45 | const opened = sum((d) => d.issuesOpened); |
| @@ -51,6 +53,7 @@ export function queueLines(f: Session, repo: Repo, now = new Date()): string[] { | ||
| 51 | 53 | ...(ceilingHold(f.db, repo.id) === null ? [] : [`${ceilingHold(f.db, repo.id)}.`]), |
| 52 | 54 | "", |
| 53 | 55 | ...g.sentences, |
| 56 | ...(flow === null ? [] : [`Chapter: ${flow}.`]), | |
| 54 | 57 | ...(g.suggest === null ? [] : [g.suggest]), |
| 55 | 58 | "", |
| 56 | 59 | `Issues opened ${opened} · closed ${closed} · net ${opened - closed > 0 ? "+" : ""}${opened - closed} · open now ${g.open.issues}`, |
src/web/pages/chapters.ts +2 −2
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | import { CHAPTER_WORDS, close, draft, fill, open, patch, reopen, refusalWords } from "../../actions/chapters.ts"; |
| 10 | import { costSentence, nextParts, getChapter, itemsOf, nextChapterOf, listChapters, progressOf, progressSentence, type Chapter, type Item } from "../../core/chapters.ts"; | |
| 10 | import { costSentence, flowOf, flowSentence, nextParts, getChapter, itemsOf, nextChapterOf, listChapters, progressOf, progressSentence, type Chapter, type Item } from "../../core/chapters.ts"; | |
| 11 | 11 | import { canAdmin, type Repo } from "../../core/repos.ts"; |
| 12 | 12 | import { sealedScope } from "../../core/sealed.ts"; |
| 13 | 13 | import { notePhase, takeSessionTime } from "../../core/route_timing.ts"; |
| @@ -120,7 +120,7 @@ export function chapterPage(ctx: Ctx): Response { | ||
| 120 | 120 | const left = items.filter((i) => i.status === "left"); |
| 121 | 121 | const done = items.filter((i) => i.status === "done"); |
| 122 | 122 | main = html`<p>${c.means}</p> |
| 123 | <p data-progress>${progressSentence(p)}</p> | |
| 123 | <p data-progress>${progressSentence(p)}${c.state === "open" ? html` <span data-chapter-flow>${flowSentence(flowOf(ctx.app.db, c, items, new Date()))}.</span>` : null}</p> | |
| 124 | 124 | <p data-cost>${costSentence(items)}</p> |
outdated
Principle 1: the page now says progress, flow and cost in three lines of prose. Put flow in the progress line or a dl row, and add a web test for it (none exists). |
||
| 125 | 125 | ${items.length === 0 ? html`<p>No issues in this chapter yet.</p>` : null} |
| 126 | 126 | <ul class="list setting-list">${[...done, ...building, ...left].map((i) => itemRow(repo, i, may, c))}</ul> |
src/web/pages/next_chapters.ts +6 −3
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | * `actions/chapters.ts`. |
| 6 | 6 | */ |
| 7 | 7 | import { CLOSE_UNDO_MS, stopsClose, undoableClose } from "../../actions/chapters.ts"; |
| 8 | import { chapterLineParts, itemsOf, progressOf, progressSentence, type Chapter } from "../../core/chapters.ts"; | |
| 8 | import { chapterLineParts, flowOf, flowSentence, itemsOf, progressOf, progressSentence, type Chapter, type Item } from "../../core/chapters.ts"; | |
| 9 | 9 | import { canAdmin, type Repo } from "../../core/repos.ts"; |
| 10 | 10 | import { sealedScope } from "../../core/sealed.ts"; |
| 11 | 11 | import type { Ctx } from "../../http/context.ts"; |
| @@ -69,8 +69,11 @@ export function chaptersSection(ctx: Ctx, repo: Repo, back: string, all: readonl | ||
| 69 | 69 | export function chapterLine(ctx: Ctx, repo: Repo, all: readonly Chapter[]): Html | null { |
| 70 | 70 | const base = repoBase(repo); |
| 71 | 71 | const view = sealedScope(ctx.app.db, repo, ctx.auth.user); |
| 72 | const p = chapterLineParts(all, (c) => itemsOf(ctx.app.db, c, view), new Date()); | |
| 72 | const now = new Date(); | |
| 73 | let listed: readonly Item[] = []; | |
| 74 | const p = chapterLineParts(all, (c) => (listed = itemsOf(ctx.app.db, c, view)), now); | |
| 73 | 75 | if (p === null) return null; |
outdated
Principle 1 (and speed): a second paragraph that restates the chapter line, and itemsOf is read a second time for the same chapter and view. Reuse the list from the first read and fold the sentence into the existing line. |
||
| 74 | 76 | if (p.chapter === null) return html`<p class="muted" data-chapter-line>${p.lead}<a href="${base}/chapters">${p.name}</a>${p.tail}</p>`; |
| 75 | return html`<p data-chapter-line>${p.lead}<a href="${base}/chapters/${encodeURIComponent(p.chapter.name)}">${p.name}</a>${p.tail}</p>`; | |
| 77 | const flow = flowSentence(flowOf(ctx.app.db, p.chapter, listed, now)); | |
| 78 | return html`<p data-chapter-line>${p.lead}<a href="${base}/chapters/${encodeURIComponent(p.chapter.name)}">${p.name}</a>${p.tail}; <span data-chapter-flow>${flow}</span></p>`; | |
| 76 | 79 | } |
src/web/pages/queue.ts +4 −3
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | |
| 8 | 8 | import { deferOldest, hasGate, recentDefer, undoDefer } from "../../actions/defer_aged.ts"; |
| 9 | 9 | import { setCeiling, setPlannerSwitch, SWITCH_REFUSED, switchInput } from "../../actions/planner_switch.ts"; |
| 10 | import { openFlowWords } from "../../core/chapters.ts"; | |
| 10 | 11 | import { growth, oldest, WHO, type Growth } from "../../core/growth.ts"; |
| 11 | 12 | import { ceilingHold, CEILING_HELD_NOTE, CEILING_REASON, openCeiling, PAUSE_STOPS, pauseSentence, pausedUntil } from "../../core/planner_pause.ts"; |
| 12 | 13 | import { canAdmin, type Repo } from "../../core/repos.ts"; |
| @@ -24,14 +25,14 @@ export const DAYS = 14; | ||
| 24 | 25 | const sum = (xs: readonly number[]) => xs.reduce((s, x) => s + x, 0); |
| 25 | 26 | |
| 26 | 27 | /** The totals as rows, the sentences, the suggestion when there is one, and a row for each day. */ |
| 27 | export function growthRows(g: Growth): Html { | |
| 28 | export function growthRows(g: Growth, flow: string | null = null): Html { | |
| 28 | 29 | const d = g.days; |
| 29 | 30 | const opened = sum(d.map((x) => x.issuesOpened)); |
| 30 | 31 | const closed = sum(d.map((x) => x.issuesClosed)); |
| 31 | 32 | const net = opened - closed; |
| 32 | 33 | const fact = (title: string, value: string) => row({ title, trail: badge(value) }); |
| 33 | 34 | const by = WHO.map((w) => ({ w, n: sum(d.map((x) => x.openedBy[w])) })).filter((x) => x.n > 0); |
| 34 | return html`<p>${g.sentences.join(" ")}</p> | |
| 35 | return html`<p>${g.sentences.join(" ")}${flow === null ? null : html` <span data-chapter-flow>Chapter: ${flow}.</span>`}</p> | |
| 35 | 36 | ${g.suggest === null ? null : html`<p role="note">${g.suggest}</p>`} |
| 36 | 37 | <ul class="list setting-list" data-growth> |
| 37 | 38 | ${fact("Issues opened", String(opened))} |
| @@ -115,7 +116,7 @@ export function queuePage(ctx: Ctx): Response { | ||
| 115 | 116 | ${ceilingRow(repo, openCeiling(ctx.app.db, repo.id), ceilingHold(ctx.app.db, repo.id), owner)} |
| 116 | 117 | <p><a href="${base}/chapters">Chapters</a></p> |
| 117 | 118 | ${aged.length === 0 ? null : html`<ul class="list setting-list" data-aged>${aged}</ul>`} |
| 118 | ${growthRows(g)}`; | |
| 119 | ${growthRows(g, openFlowWords(ctx.app.db, repo.id, scope, now))}`; | |
| 119 | 120 | const response = page({ ctx, title: `Queue · ${repo.name}`, crumbs: crumbsFor(repo), palette: repoPalette(ctx, repo), body }); |
| 120 | 121 | notePhase(ctx.request, "render", performance.now() - rendering); |
| 121 | 122 | return response; |
test/queuegrowth-chapter.test.ts added +156
| @@ -0,0 +1,156 @@ | ||
| 1 | /** | |
| 2 | * How fast the open chapter fills or drains (#15594): items put in over seven days against items | |
| 3 | * done in them, as one sentence in the chapter line, the chapter page, the terminal's queue and the | |
| 4 | * MCP's chapter read. Growing, draining and flat give the three phrasings, checked by hand count. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { newApp } from "../src/actions/apps.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { draftChapter, flowIn, flowOf, flowSentence, forgetFrozenItems, itemsOf, openFlowWords, putIssue, startChapter, EVERY_ITEM, type Item } from "../src/core/chapters.ts"; | |
| 13 | import { createIssue } from "../src/core/issues.ts"; | |
| 14 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 15 | import { createToken } from "../src/core/tokens.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { queueLines } from "../src/tui/screens/queuegrowth.ts"; | |
| 19 | import type { Session } from "../src/tui/session.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | const NOW = new Date("2026-11-22T12:00:00.000Z"); | |
| 25 | const done = (doneAt: string): Item => ({ issueId: "x", number: 1, title: "t", status: "done", doneAt, pull: 1, builder: null, costUsd: 0 }); | |
outdated
Missing: tests for the /next line, the chapter page and the MCP field, and for a sealed item the viewer may not see being left out of both counts (the docs claim it). Also add the /queue page, which the issue asks for. |
||
| 26 | ||
| 27 | describe("the open chapter's flow", () => { | |
| 28 | test("the three phrasings", () => { | |
| 29 | expect(flowSentence({ opened: 12, landed: 9, net: 3 })).toBe("opened 12 this week, landed 9: the book is growing by 3"); | |
| 30 | expect(flowSentence({ opened: 2, landed: 5, net: -3 })).toBe("opened 2 this week, landed 5: the book is draining by 3"); | |
| 31 | expect(flowSentence({ opened: 4, landed: 4, net: 0 })).toBe("opened 4 this week, landed 4: the book is flat"); | |
| 32 | }); | |
| 33 | ||
| 34 | test("only the last seven days count, by hand", () => { | |
| 35 | const added = ["2026-11-21T00:00:00.000Z", "2026-11-16T00:00:00.000Z", "2026-11-15T11:00:00.000Z"]; | |
| 36 | const items = [done("2026-11-20T00:00:00.000Z"), done("2026-11-01T00:00:00.000Z")]; | |
| 37 | expect(flowIn(added, items, NOW)).toEqual({ opened: 2, landed: 1, net: 1 }); | |
| 38 | }); | |
| 39 | ||
| 40 | describe("from a database", () => { | |
| 41 | let dataDir = ""; | |
| 42 | let app: App; | |
| 43 | let owner: User; | |
| 44 | let repo: Repo; | |
| 45 | beforeAll(async () => { | |
| 46 | dataDir = await mkdtemp(join(tmpdir(), "forge-flow-")); | |
| 47 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 48 | const made = await createUser(app.db, { username: "alice", email: "[email protected]", password: "a-good-passphrase", origin: "seed", trusted: true, adminIfFirst: true }); | |
| 49 | if (!made.ok) throw new Error(String(made.error)); | |
| 50 | owner = made.value; | |
| 51 | const r = await newApp(app, owner, { name: "shop", description: "Words", visibility: "public" }); | |
| 52 | if (!r.ok) throw new Error(String(r.error)); | |
| 53 | repo = getRepo(app.db, "alice", "shop")!; | |
| 54 | }); | |
| 55 | afterAll(async () => { | |
| 56 | app.db.close(); | |
| 57 | await rm(dataDir, { recursive: true, force: true }); | |
| 58 | }); | |
| 59 | ||
| 60 | test("an open chapter reads growing, and the terminal says it", () => { | |
| 61 | expect(openFlowWords(app.db, repo.id, EVERY_ITEM, NOW)).toBeNull(); | |
| 62 | const c = draftChapter(app.db, repo.id, { name: "v2.0.0" }, owner.id); | |
| 63 | if (!c.ok) throw new Error(c.error); | |
| 64 | const opened = startChapter(app.db, c.value, NOW); | |
| 65 | if (!opened.ok) throw new Error(opened.error); | |
| 66 | for (const t of ["a", "b", "c"]) { | |
| 67 | const i = createIssue(app.db, { repoId: repo.id, authorId: owner.id, title: t, body: "" }); | |
| 68 | if (!i.ok) throw new Error(String(i.error)); | |
| 69 | putIssue(app.db, opened.value, i.value.id, owner.id); | |
| 70 | } | |
| 71 | const old = createIssue(app.db, { repoId: repo.id, authorId: owner.id, title: "old", body: "" }); | |
| 72 | if (!old.ok) throw new Error(String(old.error)); | |
| 73 | putIssue(app.db, opened.value, old.value.id, owner.id); | |
| 74 | app.db.query("UPDATE chapter_item SET at = '2026-10-01T00:00:00.000Z' WHERE issue_id = ?1").run(old.value.id); | |
| 75 | const now = new Date(); | |
| 76 | expect(flowOf(app.db, opened.value, itemsOf(app.db, opened.value, EVERY_ITEM), now)).toEqual({ opened: 3, landed: 0, net: 3 }); | |
| 77 | expect(openFlowWords(app.db, repo.id, EVERY_ITEM, now)).toBe("opened 3 this week, landed 0: the book is growing by 3"); | |
| 78 | const f = { db: app.db, app, user: owner } as unknown as Session; | |
| 79 | expect(queueLines(f, repo, now)).toContain("Chapter: opened 3 this week, landed 0: the book is growing by 3."); | |
| 80 | }); | |
| 81 | }); | |
| 82 | }); | |
| 83 | ||
| 84 | describe("the flow on the web pages and the MCP", () => { | |
| 85 | let dataDir = ""; | |
| 86 | let app: App; | |
| 87 | let servers: Servers; | |
| 88 | let owner: User; | |
| 89 | let reader: User; | |
| 90 | const tokens = new Map<User, string>(); | |
| 91 | ||
| 92 | const person = async (name: string, first = false): Promise<User> => { | |
| 93 | const made = await createUser(app.db, { username: name, email: `${name}@forge.test`, password: "a-good-passphrase", origin: "seed", trusted: true, adminIfFirst: first }); | |
| 94 | if (!made.ok) throw new Error(String(made.error)); | |
| 95 | tokens.set(made.value, createToken(app.db, made.value.id, "t", "admin").token); | |
| 96 | return made.value; | |
| 97 | }; | |
| 98 | const text = async (path: string, who: User) => (await fetch(`${servers.url}${path}`, { headers: { authorization: `Bearer ${tokens.get(who)}` } })).text(); | |
| 99 | const flowOnPage = (html: string): string | null => /data-chapter-flow>([^<]*)</.exec(html)?.[1] ?? null; | |
| 100 | const mcp = async (who: User, chapter: string): Promise<Record<string, unknown>> => { | |
| 101 | const res = await fetch(`${servers.url}/mcp`, { | |
| 102 | method: "POST", | |
| 103 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${tokens.get(who)}` }, | |
| 104 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "repo", arguments: { repo: "alice/shop", view: "chapters", chapter } } }), | |
| 105 | }); | |
| 106 | const body = (await res.json()) as { result: { content: { text: string }[] } }; | |
| 107 | return JSON.parse(body.result.content[0]!.text) as Record<string, unknown>; | |
| 108 | }; | |
| 109 | ||
| 110 | beforeAll(async () => { | |
| 111 | dataDir = await mkdtemp(join(tmpdir(), "forge-flow-web-")); | |
| 112 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 113 | servers = await serve(app); | |
| 114 | owner = await person("alice", true); | |
| 115 | reader = await person("bob"); | |
| 116 | const r = await newApp(app, owner, { name: "shop", description: "Words", visibility: "public" }); | |
| 117 | if (!r.ok) throw new Error(String(r.error)); | |
| 118 | const repo = getRepo(app.db, "alice", "shop")!; | |
| 119 | const c = draftChapter(app.db, repo.id, { name: "v2" }, owner.id); | |
| 120 | if (!c.ok) throw new Error(c.error); | |
| 121 | const opened = startChapter(app.db, c.value, new Date()); | |
| 122 | if (!opened.ok) throw new Error(opened.error); | |
| 123 | for (const [title, sealed] of [["plain", false], ["secret", true]] as const) { | |
| 124 | const i = createIssue(app.db, { repoId: repo.id, authorId: owner.id, title, body: "", sealed }); | |
| 125 | if (!i.ok) throw new Error(String(i.error)); | |
| 126 | putIssue(app.db, opened.value, i.value.id, owner.id); | |
| 127 | } | |
| 128 | }); | |
| 129 | afterAll(async () => { | |
| 130 | servers.stop(); | |
| 131 | app.db.close(); | |
| 132 | await rm(dataDir, { recursive: true, force: true, maxRetries: 20, retryDelay: 100 }); | |
| 133 | }); | |
| 134 | ||
| 135 | test("/next, the chapter page and /queue carry the line, and a sealed item a reader may not see is in neither count", async () => { | |
| 136 | const writer = "opened 2 this week, landed 0: the book is growing by 2"; | |
| 137 | const seen = "opened 1 this week, landed 0: the book is growing by 1"; | |
| 138 | for (const [who, words] of [[owner, writer], [reader, seen]] as const) { | |
| 139 | expect(flowOnPage(await text("/alice/shop/next", who))).toBe(words); | |
| 140 | expect(flowOnPage(await text("/alice/shop/chapters/v2", who))).toBe(`${words}.`); | |
| 141 | expect(flowOnPage(await text("/alice/shop/queue", who))).toBe(`Chapter: ${words}.`); | |
| 142 | } | |
| 143 | }); | |
| 144 | ||
| 145 | test("the MCP chapter read carries flow for the open chapter only", async () => { | |
| 146 | expect((await mcp(owner, "v2")).flow).toBe("opened 2 this week, landed 0: the book is growing by 2"); | |
| 147 | expect((await mcp(reader, "v2")).flow).toBe("opened 1 this week, landed 0: the book is growing by 1"); | |
| 148 | forgetFrozenItems(); | |
| 149 | app.db.query("UPDATE chapter SET state = 'closed' WHERE name = 'v2'").run(); | |
| 150 | expect("flow" in (await mcp(owner, "v2"))).toBe(false); | |
| 151 | }); | |
| 152 | ||
| 153 | test("a closed chapter shows no flow anywhere", async () => { | |
| 154 | for (const path of ["/alice/shop/next", "/alice/shop/chapters/v2", "/alice/shop/queue"]) expect(flowOnPage(await text(path, owner))).toBeNull(); | |
| 155 | }); | |
| 156 | }); |
No test covers the MCP
flowfield being present for an open chapter and absent for a closed one.