Public: anyone can read this; the crew can change it.
Cut a wave: an admin opens a draft waves/<next>.md as a pull listing every pull merged since the last wave #1531
scout/forge:scout/1508-cut-a-wave-an-admin-opens-a-draft-waves → main 2 commits fork 4 threads · 4 unresolved
README.md +8
| @@ -601,3 +601,11 @@ Invariants are proven by faults, not by hope. No dependencies were added. | ||
| 601 | 601 | `forge.json`'s `check` may declare `fast` beside `run` (the full gate; `full` names it explicitly). `bun scripts/test-shards.ts --tier fast --diff <base>..<head>` runs the tests that touch the diff (changed, named for a changed source file, or importing one within two hops), every test the times table records under 1 s, and any named by `--learned a,b`. `src/checks/tiers.ts` decides; it adds no dependency. |
| 602 | 602 | |
| 603 | 603 | In the forge itself: a pull's check runs `fast` when the manifest has one, in a clone holding the fork point and the head as `FORGE_CHECK_BASE` and `HEAD` (`origin/main` is not a ref there), with `FORGE_CHECK_LEARNED` naming tests earlier land attempts taught the pull (`src/checks/history.ts`, `learned.ts`). Landing (`landIfGreen`) runs `full` once on the merge with its base before merging (`src/checks/landing.ts`); red comments on the pull with the tests named, is remembered per head and base, and the files ride the pull's next fast run. The pull page's checks line shows the tier words. |
| 604 | ||
| 605 | ## Cut a wave | |
| 606 | ||
| 607 | A repository admin opens **Settings → Cut a wave** to open a pull that adds | |
| 608 | `waves/<next>.md` (format: `src/story/wave.ts`) listing every pull merged since | |
| 609 | the last wave. The headline and highlights are `TODO` placeholders to write | |
| 610 | before it lands. A wave that does not parse stops the cut and is named; no | |
| 611 | merged pulls since the last wave is refused. |
scripts/gate.ts +1
| @@ -56,6 +56,7 @@ export const GATE: readonly string[] = [ | ||
| 56 | 56 | "test/review-from-green.test.ts", |
| 57 | 57 | "test/themes.test.ts", |
| 58 | 58 | "test/waves.test.ts", |
| 59 | "test/wave-cut.test.ts", | |
| 59 | 60 | "test/crew-wrapup.test.ts", |
| 60 | 61 | "test/crew-limits.test.ts", |
| 61 | 62 | "test/crew-parks.test.ts", |
src/actions/wave.ts added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | /** | |
| 2 | * Cut a wave: open a pull that adds `waves/<next>.md`, listing every pull | |
| 3 | * merged since the last wave. Admin only; the file is made by | |
| 4 | * `story/cut.ts` and committed on a fresh branch, never on the default one. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App } from "../app.ts"; | |
| 8 | import { listOpenPullsFromHead, mergedPulls } from "../core/pulls.ts"; | |
| 9 | import type { Repo } from "../core/repos.ts"; | |
| 10 | import type { User } from "../core/users.ts"; | |
| 11 | import { readerFor, listPaths } from "../git/read.ts"; | |
| 12 | import { repoDir } from "../git/repos.ts"; | |
| 13 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 14 | import { cutWave, WAVE_BRANCH, type CutError, type WaveFile } from "../story/cut.ts"; | |
| 15 | import { requireRepoAdmin } from "./access.ts"; | |
| 16 | import { deleteBranch } from "./branches.ts"; | |
| 17 | import { editFiles } from "./contents.ts"; | |
| 18 | ||
| 19 | export type CutWaveError = | |
| 20 | | "not_found" | |
| 21 | | CutError | |
| 22 | | { kind: "failed"; reason: string }; | |
| 23 | ||
| 24 | const WAVE_FILE = /^waves\/[^/]+\.md$/; | |
| 25 | ||
| 26 | export async function cutWaveFor( | |
| 27 | app: App, | |
| 28 | actor: User | undefined, | |
| 29 | repo: Repo, | |
| 30 | version?: string, | |
| 31 | ): Promise<Result<{ number: number; version: string; pulls: number }, CutWaveError>> { | |
| 32 | const admin = requireRepoAdmin(app, actor, repo); | |
| 33 | if (!admin.ok) return admin; | |
| 34 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 35 | const tip = `refs/heads/${repo.defaultBranch}`; | |
| 36 | const listed = await listPaths(dir, tip); | |
| 37 | const files: WaveFile[] = []; | |
| 38 | for (const path of listed.ok ? listed.value.paths.filter((p) => WAVE_FILE.test(p)) : []) { | |
| 39 | const blob = await readerFor(dir).object(`${tip}:${path}`); | |
| 40 | files.push({ | |
| 41 | path, | |
| 42 | text: blob.ok ? new TextDecoder().decode(blob.value.body) : "", | |
| 43 | }); | |
| 44 | } | |
| 45 | const base = app.config.url ?? `http://localhost:${app.config.port}`; | |
| 46 | // Every merged pull; `cutWave` keeps those since the last wave's day and | |
| 47 | // drops what a wave already lists and the wave pulls themselves. | |
| 48 | const merged = mergedPulls(app.db, repo.id).map((p) => ({ | |
| 49 | headBranch: p.headBranch, | |
| 50 | number: p.number, | |
| 51 | title: p.title, | |
| 52 | url: new URL(`/${repo.ownerName}/${repo.name}/pulls/${p.number}`, base).toString(), | |
| 53 | mergedAt: p.mergedAt ?? "", | |
| 54 | })); | |
| 55 | const cut = cutWave(files, merged, new Date().toISOString().slice(0, 10), version); | |
| 56 | if (!cut.ok) return cut; | |
outdated
|
||
| 57 | const { path, text, version: v, pulls } = cut.value; | |
| 58 | const branch = `${WAVE_BRANCH}${v}`; | |
| 59 | // One open wave pull per version; a branch left by a closed one is replaced. | |
| 60 | const open = listOpenPullsFromHead(app.db, repo.id, branch)[0]; | |
| 61 | if (open) return err({ kind: "already_open", version: v, number: open.number }); | |
| 62 | await deleteBranch(app, admin.value, repo, branch); | |
| 63 | const edit = await editFiles(app, admin.value, repo, { | |
| 64 | newBranch: branch, | |
| 65 | message: `Cut wave ${v}`, | |
| 66 | files: [{ path, content: text }], | |
| 67 | openPull: { | |
| 68 | title: `Wave ${v}`, | |
| 69 | body: `Lists ${pulls} merged pull${pulls === 1 ? "" : "s"}. The headline and highlights are placeholders: write them before this lands.`, | |
| 70 | }, | |
| 71 | }); | |
| 72 | if (!edit.ok || !edit.value.pull) | |
| 73 | return err({ | |
| 74 | kind: "failed", | |
| 75 | reason: edit.ok ? "no pull opened" : typeof edit.error === "string" ? edit.error : edit.error.kind, | |
| 76 | }); | |
| 77 | return ok({ number: edit.value.pull.number, version: v, pulls }); | |
| 78 | } |
src/core/pulls.ts +26
| @@ -827,3 +827,29 @@ export function landingTrailers( | ||
| 827 | 827 | line("Co-authored-by", lander), |
| 828 | 828 | ].join("\n"); |
| 829 | 829 | } |
| 830 | ||
| 831 | /** | |
| 832 | * Every pull of a repository that has merged, oldest merge first. Capped at | |
| 833 | * the newest `cap`, so a busy repository cannot mean an unbounded page. | |
| 834 | * Which of them a wave lists is `story/cut.ts`'s call, not this query's. | |
| 835 | */ | |
| 836 | export function mergedPulls( | |
outdated
Nothing calls this with a value, so the parameter and its SQL clause are dead code. Use it or remove it. |
||
| 837 | db: Database, | |
| 838 | repoId: string, | |
| 839 | cap = 2_000, | |
| 840 | ): Pull[] { | |
| 841 | const ids = db | |
| 842 | .query<{ issue_id: string }, [string, number]>( | |
| 843 | `SELECT pull.issue_id FROM pull JOIN issue ON issue.id = pull.issue_id | |
| 844 | WHERE issue.repo_id = ?1 AND pull.merged_at IS NOT NULL | |
| 845 | ORDER BY pull.merged_at DESC LIMIT ?2`, | |
| 846 | ) | |
| 847 | .all(repoId, cap) | |
| 848 | .map((r) => r.issue_id) | |
| 849 | .reverse(); | |
| 850 | const issues = getIssuesByIds(db, ids); | |
| 851 | return joinMany( | |
| 852 | db, | |
| 853 | ids.map((id) => issues.get(id)).filter((i): i is Issue => i !== undefined), | |
| 854 | ); | |
| 855 | } |
src/http/server.ts +2
| @@ -298,6 +298,7 @@ import { | ||
| 298 | 298 | repoDelete, |
| 299 | 299 | settingsPage, |
| 300 | 300 | settingsUpdate, |
| 301 | waveCut, | |
| 301 | 302 | webhookCreate, |
| 302 | 303 | webhookDelete, |
| 303 | 304 | } from "../web/pages/settings.ts"; |
| @@ -1058,6 +1059,7 @@ export function buildRouter(): Router<Handler> { | ||
| 1058 | 1059 | // Settings (owner-gated inside the handlers). |
| 1059 | 1060 | router.add("GET", "/:owner/:name/settings", settingsPage); |
| 1060 | 1061 | router.add("POST", "/:owner/:name/settings", settingsUpdate); |
| 1062 | router.add("POST", "/:owner/:name/settings/wave", waveCut); | |
| 1061 | 1063 | router.add("GET", "/:owner/:name/settings/app", appSettingsPage); |
| 1062 | 1064 | router.add("POST", "/:owner/:name/settings/app", appSettingsUpdate); |
| 1063 | 1065 | router.add("POST", "/:owner/:name/settings/app/disable", appDisable); |
src/story/cut.ts added +122
| @@ -0,0 +1,122 @@ | ||
| 1 | /** | |
| 2 | * Cutting a wave: from the waves already written and the pulls merged, the | |
| 3 | * text of the next `waves/<version>.md`. Pure — files and pulls in, one | |
| 4 | * file out. The headline and highlights are marked placeholders; a person | |
| 5 | * or the crew writes the words before the pull lands. | |
| 6 | */ | |
| 7 | ||
| 8 | import { | |
| 9 | MIN_HIGHLIGHTS, | |
| 10 | parseWave, | |
| 11 | wavePath, | |
| 12 | writeWave, | |
| 13 | type Wave, | |
| 14 | type WavePull, | |
| 15 | } from "./wave.ts"; | |
| 16 | ||
| 17 | export interface WaveFile { | |
| 18 | path: string; | |
| 19 | text: string; | |
| 20 | } | |
| 21 | export interface MergedPull { | |
| 22 | number: number; | |
| 23 | title: string; | |
| 24 | url: string; | |
| 25 | /** ISO instant the pull merged. */ | |
| 26 | mergedAt: string; | |
| 27 | /** The branch it merged from; a wave's own pull comes from `wave-*`. */ | |
| 28 | headBranch: string; | |
| 29 | } | |
| 30 | export interface Cut { | |
| 31 | path: string; | |
| 32 | text: string; | |
| 33 | version: string; | |
| 34 | pulls: number; | |
| 35 | } | |
| 36 | export type CutError = | |
| 37 | | { kind: "malformed"; path: string; reason: string } | |
| 38 | | { kind: "nothing_new" } | |
| 39 | | { kind: "bad_version"; version: string } | |
| 40 | | { kind: "exists"; path: string } | |
| 41 | | { kind: "already_open"; version: string; number: number }; | |
| 42 | ||
| 43 | export const PLACEHOLDER = "TODO"; | |
| 44 | ||
| 45 | /** The branch prefix a cut opens its pull from. */ | |
| 46 | export const WAVE_BRANCH = "wave-"; | |
| 47 | ||
| 48 | const num = (v: string): number[] => | |
| 49 | v.split("-")[0]!.split(".").map(Number); | |
| 50 | const newer = (a: string, b: string): boolean => { | |
| 51 | const x = num(a); | |
| 52 | const y = num(b); | |
| 53 | for (let i = 0; i < 3; i++) if (x[i]! !== y[i]!) return x[i]! > y[i]!; | |
| 54 | return false; | |
| 55 | }; | |
| 56 | ||
| 57 | /** The next patch version after `v`, or 0.1.0 when there is no wave yet. */ | |
| 58 | export const nextVersion = (v: string | undefined): string => { | |
| 59 | if (v === undefined) return "0.1.0"; | |
| 60 | const [a, b, c] = num(v); | |
| 61 | return `${a}.${b}.${c! + 1}`; | |
| 62 | }; | |
| 63 | ||
| 64 | /** One pull as a wave line: no brackets, one line. */ | |
| 65 | const line = (p: MergedPull): WavePull => ({ | |
| 66 | title: `#${p.number} ${p.title.replace(/[\[\]]/g, "").replace(/\s+/g, " ").trim()}`.trimEnd(), | |
| 67 | url: p.url, | |
| 68 | }); | |
| 69 | ||
| 70 | export function cutWave( | |
| 71 | files: readonly WaveFile[], | |
| 72 | merged: readonly MergedPull[], | |
| 73 | today: string, | |
| 74 | version?: string, | |
| 75 | ): { ok: true; value: Cut } | { ok: false; error: CutError } { | |
| 76 | const waves: Wave[] = []; | |
| 77 | for (const f of files) { | |
| 78 | const r = parseWave(f.path, f.text); | |
| 79 | if (!r.ok) | |
| 80 | return { | |
| 81 | ok: false, | |
| 82 | error: { | |
| 83 | kind: "malformed", | |
| 84 | path: f.path, | |
| 85 | reason: r.error.message.slice(f.path.length + 2), | |
| 86 | }, | |
| 87 | }; | |
| 88 | waves.push(r.value); | |
| 89 | } | |
After a wave pull merges, the next cut lists it ("#N Wave 0.1.0"). Leave out the wave pulls themselves, for example by passing the head branch in |
||
| 90 | let last: Wave | undefined; | |
| 91 | let top: string | undefined; | |
| 92 | for (const w of waves) { | |
| 93 | if (!last || w.date > last.date) last = w; | |
| 94 | if (top === undefined || newer(w.version, top)) top = w.version; | |
| 95 | } | |
| 96 | const listed = new Set(waves.flatMap((w) => w.pulls.map((p) => p.url))); | |
| 97 | const fresh = merged.filter( | |
| 98 | (p) => | |
| 99 | !listed.has(p.url) && | |
| 100 | !p.headBranch.startsWith(WAVE_BRANCH) && | |
| 101 | (last === undefined || p.mergedAt.slice(0, 10) >= last.date), | |
| 102 | ); | |
| 103 | if (fresh.length === 0) return { ok: false, error: { kind: "nothing_new" } }; | |
| 104 | ||
| 105 | const v = version ?? nextVersion(top); | |
| 106 | const path = wavePath(v); | |
| 107 | if (waves.some((w) => w.version === v) || files.some((f) => f.path === path)) | |
| 108 | return { ok: false, error: { kind: "exists", path } }; | |
| 109 | const wave: Wave = { | |
| 110 | version: v, | |
| 111 | date: today, | |
| 112 | headline: `${PLACEHOLDER}: write the headline`, | |
| 113 | highlights: Array.from({ length: MIN_HIGHLIGHTS }, (_, i) => ({ | |
| 114 | title: `${PLACEHOLDER}: highlight ${i + 1}`, | |
| 115 | why: `${PLACEHOLDER}: why it matters`, | |
| 116 | })), | |
| 117 | pulls: fresh.map(line), | |
| 118 | }; | |
| 119 | const text = writeWave(wave); | |
| 120 | if (!text.ok) return { ok: false, error: { kind: "bad_version", version: v } }; | |
| 121 | return { ok: true, value: { path, text: text.value, version: v, pulls: fresh.length } }; | |
| 122 | } |
src/web/pages/settings.ts +54 −1
| @@ -37,6 +37,7 @@ import { | ||
| 37 | 37 | type AppEnvName, |
| 38 | 38 | type AppEnvScope, |
| 39 | 39 | } from "../../core/app_env.ts"; |
| 40 | import { cutWaveFor, type CutWaveError } from "../../actions/wave.ts"; | |
| 40 | 41 | import { addToCrew, listCrew, removeFromCrew } from "../../actions/crew.ts"; |
| 41 | 42 | import { getAgentByName, isCrewRole, setCrewRole } from "../../core/agents.ts"; |
| 42 | 43 | import { isAutonomy } from "../../core/repos.ts"; |
| @@ -307,13 +308,24 @@ export async function settingsPage(ctx: Ctx): Promise<Response> { | ||
| 307 | 308 | </form>`, |
| 308 | 309 | { head: html`${icon("branch")}<h2>Branch and gates</h2>` }, |
| 309 | 310 | ); |
| 311 | const wave = card( | |
| 312 | html`<form method="post" action="${base}/settings/wave"> | |
| 313 | <p class="muted"> | |
| 314 | Opens a pull that adds <code>waves/<next>.md</code>, listing every | |
| 315 | pull merged since the last wave. The headline and highlights are left | |
| 316 | as placeholders for you to write. | |
| 317 | </p> | |
| 318 | <div class="form-actions">${btn("Cut a wave", { variant: "primary" })}</div> | |
| 319 | </form>`, | |
| 320 | { head: html`${icon("tag")}<h2>Cut a wave</h2>` }, | |
| 321 | ); | |
| 310 | 322 | const secrets = secretsSection(ctx, "repo", `${repo.ownerName}/${repo.name}`); |
| 311 | 323 | return settingsPageResponse( |
| 312 | 324 | ctx, |
| 313 | 325 | repo, |
| 314 | 326 | "general", |
| 315 | 327 | "Settings", |
| 316 | html`${lead}<div class="mt">${body}</div>${secrets | |
| 328 | html`${lead}<div class="mt">${body}</div><div class="mt">${wave}</div>${secrets | |
| 317 | 329 | ? html`<div class="mt">${secrets}</div>` |
| 318 | 330 | : null}`, |
| 319 | 331 | ); |
| @@ -1782,3 +1794,44 @@ export async function autonomySettingsUpdate(ctx: Ctx): Promise<Response> { | ||
| 1782 | 1794 | }), |
| 1783 | 1795 | }); |
| 1784 | 1796 | } |
| 1797 | ||
| 1798 | /** The plain reason a cut was refused. */ | |
| 1799 | function cutWords(e: CutWaveError): string { | |
| 1800 | if (typeof e === "string") return "That is not yours to do."; | |
| 1801 | switch (e.kind) { | |
| 1802 | case "malformed": | |
| 1803 | return `${e.path} does not parse (${e.reason}); fix it first. Nothing was written.`; | |
| 1804 | case "nothing_new": | |
| 1805 | return "No pull has merged since the last wave, so there is nothing to cut."; | |
| 1806 | case "exists": | |
| 1807 | return `${e.path} already exists.`; | |
| 1808 | case "already_open": | |
| 1809 | return `Wave ${e.version} is already open as #${e.number}.`; | |
| 1810 | case "bad_version": | |
| 1811 | return `“${e.version}” is not a version.`; | |
| 1812 | case "failed": | |
| 1813 | return `The pull could not be opened (${e.reason}).`; | |
| 1814 | } | |
| 1815 | } | |
| 1816 | ||
| 1817 | /** POST /:owner/:name/settings/wave — open the pull that adds the next wave. */ | |
| 1818 | export async function waveCut(ctx: Ctx): Promise<Response> { | |
| 1819 | const gate = await adminForm(ctx); | |
| 1820 | if (gate instanceof Response) return gate; | |
| 1821 | const { repo } = gate; | |
| 1822 | const base = repoBase(repo); | |
| 1823 | const cut = await cutWaveFor(ctx.app, ctx.auth.user, repo); | |
| 1824 | if (!cut.ok) { | |
| 1825 | const e = cut.error; | |
| 1826 | return flashTo( | |
| 1827 | ctx, | |
| 1828 | typeof e === "object" && e.kind === "already_open" | |
| 1829 | ? `${base}/pulls/${e.number}` | |
| 1830 | : `${base}/settings`, | |
| 1831 | { tone: "danger", message: cutWords(e) }, | |
| 1832 | ); | |
| 1833 | } | |
| 1834 | return flashTo(ctx, `${base}/pulls/${cut.value.number}`, { | |
| 1835 | message: `Opened wave ${cut.value.version} with ${cut.value.pulls} pull${cut.value.pulls === 1 ? "" : "s"}. Write the headline and highlights, then ship it.`, | |
| 1836 | }); | |
| 1837 | } |
test/wave-cut.test.ts added +244
| @@ -0,0 +1,244 @@ | ||
| 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 { createApp, type App } from "../src/app.ts"; | |
| 6 | import { mergePull, openPull } from "../src/actions/pulls.ts"; | |
| 7 | import { setIssueStateThrough } from "../src/actions/issues.ts"; | |
| 8 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 9 | import { getPull, type Pull } from "../src/core/pulls.ts"; | |
| 10 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 11 | import { getUserByName, type User } from "../src/core/users.ts"; | |
| 12 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 13 | import { readerFor } from "../src/git/read.ts"; | |
| 14 | import { repoDir } from "../src/git/repos.ts"; | |
| 15 | import { cutWave } from "../src/story/cut.ts"; | |
| 16 | import { parseWave } from "../src/story/wave.ts"; | |
| 17 | ||
| 18 | describe("cut a wave", () => { | |
| 19 | let dataDir: string; | |
| 20 | let app: App; | |
| 21 | let servers: Servers; | |
| 22 | let base: string; | |
| 23 | let owner: User; | |
| 24 | let repo: Repo; | |
| 25 | const shipped: Pull[] = []; | |
| 26 | let ownerCookie = ""; | |
| 27 | let strangerCookie = ""; | |
| 28 | ||
| 29 | const signup = async (username: string): Promise<string> => { | |
| 30 | const res = await fetch(`${base}/signup`, { | |
| 31 | method: "POST", | |
| 32 | body: new URLSearchParams({ | |
| 33 | username, | |
| 34 | email: `${username}@f.dev`, | |
| 35 | password: "wave-password-1", | |
| 36 | }), | |
| 37 | redirect: "manual", | |
| 38 | }); | |
| 39 | return (res.headers.get("set-cookie") ?? "").split(";")[0]!; | |
| 40 | }; | |
| 41 | const cut = (cookie: string, headers: Record<string, string> = {}) => | |
| 42 | fetch(`${base}/olive/proj/settings/wave`, { | |
| 43 | method: "POST", | |
| 44 | headers: { cookie, ...headers }, | |
| 45 | redirect: "manual", | |
| 46 | }); | |
| 47 | const ship = async (branch: string, title: string): Promise<Pull> => { | |
| 48 | await commitToBranch(app, owner, repo, { | |
| 49 | branch, | |
| 50 | base: "main", | |
| 51 | message: title, | |
| 52 | files: [{ path: `${branch}.txt`, content: title }], | |
| 53 | }); | |
| 54 | const opened = await openPull(app, owner, repo, { | |
| 55 | title, | |
| 56 | baseBranch: "main", | |
| 57 | headBranch: branch, | |
| 58 | }); | |
| 59 | if (!opened.ok) throw new Error(JSON.stringify(opened.error)); | |
| 60 | const merged = await mergePull(app, owner, repo, opened.value, "squash"); | |
| 61 | if (!merged.ok) throw new Error(JSON.stringify(merged.error)); | |
| 62 | return getPull(app.db, repo.id, opened.value.number)!; | |
| 63 | }; | |
| 64 | const branchFile = async (branch: string, path: string) => { | |
| 65 | const o = await readerFor( | |
| 66 | repoDir(app.reposRoot, repo.ownerName, repo.name), | |
| 67 | ).object(`refs/heads/${branch}:${path}`); | |
| 68 | return o.ok ? new TextDecoder().decode(o.value.body) : undefined; | |
| 69 | }; | |
| 70 | const wavePulls = () => | |
| 71 | app.db | |
| 72 | .query<{ n: number }, [string]>( | |
| 73 | "SELECT count(*) n FROM issue WHERE repo_id = ?1 AND title LIKE 'Wave %'", | |
| 74 | ) | |
| 75 | .get(repo.id)!.n; | |
| 76 | ||
| 77 | beforeAll(async () => { | |
| 78 | dataDir = await mkdtemp(join(tmpdir(), "forge-wave-cut-")); | |
| 79 | app = createApp({ dataDir, port: 0 }); | |
| 80 | servers = await serve(app); | |
| 81 | base = servers.url; | |
| 82 | ownerCookie = await signup("olive"); | |
| 83 | strangerCookie = await signup("stranger"); | |
| 84 | await fetch(`${base}/new`, { | |
| 85 | method: "POST", | |
| 86 | headers: { cookie: ownerCookie }, | |
| 87 | body: new URLSearchParams({ name: "proj", visibility: "public" }), | |
| 88 | redirect: "manual", | |
| 89 | }); | |
| 90 | owner = getUserByName(app.db, "olive")!; | |
| 91 | repo = getRepo(app.db, "olive", "proj")!; | |
| 92 | await commitToBranch(app, owner, repo, { | |
| 93 | branch: "main", | |
| 94 | message: "base", | |
| 95 | files: [{ path: "a.txt", content: "a" }], | |
| 96 | }); | |
| 97 | }); | |
| 98 | ||
| 99 | afterAll(async () => { | |
| 100 | servers.stop(); | |
| 101 | app.close(); | |
| 102 | await rm(dataDir, { recursive: true, force: true }); | |
| 103 | }); | |
| 104 | ||
| 105 | test("a cut with nothing merged is refused with a reason", async () => { | |
| 106 | const res = await cut(ownerCookie); | |
| 107 | expect(res.status).toBe(303); | |
| 108 | expect(wavePulls()).toBe(0); | |
| 109 | const page = await fetch(`${base}/olive/proj/settings`, { | |
| 110 | headers: { cookie: ownerCookie }, | |
| 111 | }); | |
| 112 | expect(await page.text()).toContain("Cut a wave"); | |
| 113 | }); | |
| 114 | ||
| 115 | test("a non-admin and a cross-origin POST are refused; nothing opens", async () => { | |
| 116 | shipped.push(await ship("one", "First [thing]")); | |
| 117 | shipped.push(await ship("two", "Second thing")); | |
| 118 | expect((await cut(strangerCookie)).status).toBe(404); | |
| 119 | expect((await cut("")).status).toBe(404); | |
| 120 | const cross = await cut(ownerCookie, { origin: "http://evil.example" }); | |
| 121 | expect(cross.status).toBeGreaterThanOrEqual(400); | |
| 122 | expect(wavePulls()).toBe(0); | |
| 123 | }); | |
| 124 | ||
| 125 | test("an admin's cut opens a pull whose file lists exactly the merged pulls", async () => { | |
| 126 | const res = await cut(ownerCookie); | |
| 127 | expect(res.status).toBe(303); | |
| 128 | expect(wavePulls()).toBe(1); | |
| 129 | const text = await branchFile("wave-0.1.0", "waves/0.1.0.md"); | |
| 130 | expect(text).toBeDefined(); | |
| 131 | const wave = parseWave("waves/0.1.0.md", text!); | |
| 132 | expect(wave.ok).toBe(true); | |
| 133 | if (!wave.ok) return; | |
| 134 | const titles = wave.value.pulls.map((p) => p.title); | |
| 135 | expect(titles).toEqual([ | |
| 136 | `#${shipped[0]!.number} First thing`, | |
| 137 | `#${shipped[1]!.number} Second thing`, | |
| 138 | ]); | |
| 139 | expect(text).toContain("TODO"); | |
| 140 | }); | |
| 141 | ||
| 142 | test("a second cut while the wave pull is open is refused and points at it", async () => { | |
| 143 | const open = app.db | |
| 144 | .query<{ number: number }, [string]>( | |
| 145 | "SELECT number FROM issue WHERE repo_id = ?1 AND title = 'Wave 0.1.0'", | |
| 146 | ) | |
| 147 | .get(repo.id)!; | |
| 148 | const res = await cut(ownerCookie); | |
| 149 | expect(res.status).toBe(303); | |
| 150 | expect(res.headers.get("location")).toContain(`/olive/proj/pulls/${open.number}`); | |
| 151 | expect(wavePulls()).toBe(1); | |
| 152 | }); | |
| 153 | ||
| 154 | test("a closed, unmerged wave pull leaves a branch the next cut replaces", async () => { | |
| 155 | const first = app.db | |
| 156 | .query<{ number: number }, [string]>( | |
| 157 | "SELECT number FROM issue WHERE repo_id = ?1 AND title = 'Wave 0.1.0'", | |
| 158 | ) | |
| 159 | .get(repo.id)!; | |
| 160 | const closed = await setIssueStateThrough( | |
| 161 | app, | |
| 162 | owner, | |
| 163 | repo, | |
| 164 | getPull(app.db, repo.id, first.number)!, | |
| 165 | "closed", | |
| 166 | ); | |
| 167 | expect(closed.ok).toBe(true); | |
| 168 | const res = await cut(ownerCookie); | |
| 169 | expect(res.status).toBe(303); | |
| 170 | expect(res.headers.get("location")).not.toContain(`/pulls/${first.number}`); | |
| 171 | expect(res.headers.get("location")).toContain("/pulls/"); | |
| 172 | expect(wavePulls()).toBe(2); | |
| 173 | expect(await branchFile("wave-0.1.0", "waves/0.1.0.md")).toBeDefined(); | |
| 174 | }); | |
| 175 | }); | |
| 176 | ||
| 177 | describe("cutWave (pure)", () => { | |
| 178 | const good = `--- | |
| 179 | version: 0.2.0 | |
| 180 | date: 2026-10-01 | |
| 181 | headline: h | |
| 182 | --- | |
| 183 | ||
| 184 | ## Highlights | |
| 185 | ||
| 186 | - **A** — a | |
| 187 | - **B** — b | |
| 188 | - **C** — c | |
| 189 | ||
| 190 | ## Pulls | |
| 191 | ||
| 192 | - [#1 Old](http://x/p/1) | |
| 193 | `; | |
| 194 | const pull = (n: number, at: string) => ({ | |
| 195 | number: n, | |
| 196 | title: `T${n}`, | |
| 197 | url: `http://x/p/${n}`, | |
| 198 | mergedAt: at, | |
| 199 | headBranch: `b${n}`, | |
| 200 | }); | |
| 201 | ||
| 202 | test("lists pulls from the last wave's day on, skipping those already listed", () => { | |
| 203 | const r = cutWave( | |
| 204 | [{ path: "waves/0.2.0.md", text: good }], | |
| 205 | [pull(1, "2026-10-01T05:00:00Z"), pull(2, "2026-09-30T05:00:00Z"), pull(3, "2026-10-03T05:00:00Z")], | |
| 206 | "2026-10-05", | |
| 207 | ); | |
| 208 | expect(r.ok).toBe(true); | |
| 209 | if (!r.ok) return; | |
| 210 | expect(r.value.path).toBe("waves/0.2.1.md"); | |
| 211 | expect(r.value.pulls).toBe(1); | |
| 212 | expect(r.value.text).toContain("[#3 T3](http://x/p/3)"); | |
| 213 | expect(r.value.text).toContain("TODO"); | |
| 214 | }); | |
| 215 | ||
| 216 | test("a malformed earlier wave stops the cut and is named", () => { | |
| 217 | const r = cutWave( | |
| 218 | [ | |
| 219 | { path: "waves/0.2.0.md", text: good }, | |
| 220 | { path: "waves/0.1.0.md", text: "no front matter" }, | |
| 221 | ], | |
| 222 | [pull(3, "2026-10-03T05:00:00Z")], | |
| 223 | "2026-10-05", | |
| 224 | ); | |
| 225 | expect(r).toMatchObject({ ok: false, error: { kind: "malformed", path: "waves/0.1.0.md" } }); | |
| 226 | }); | |
| 227 | ||
| 228 | test("a wave's own pull is not listed in the next wave", () => { | |
| 229 | const r = cutWave( | |
| 230 | [{ path: "waves/0.2.0.md", text: good }], | |
| 231 | [pull(3, "2026-10-03T05:00:00Z"), { ...pull(4, "2026-10-04T05:00:00Z"), headBranch: "wave-0.2.0" }], | |
| 232 | "2026-10-05", | |
| 233 | ); | |
| 234 | expect(r.ok).toBe(true); | |
| 235 | if (!r.ok) return; | |
| 236 | expect(r.value.pulls).toBe(1); | |
| 237 | expect(r.value.text).not.toContain("T4"); | |
| 238 | }); | |
| 239 | ||
| 240 | test("nothing new is refused", () => { | |
| 241 | const r = cutWave([{ path: "waves/0.2.0.md", text: good }], [pull(1, "2026-10-01T05:00:00Z")], "2026-10-05"); | |
| 242 | expect(r).toEqual({ ok: false, error: { kind: "nothing_new" } }); | |
| 243 | }); | |
| 244 | }); |
sinceis alwaysundefined, so the comment above ("Merged since the last wave's day") describes filtering that happens incutWave, not here. Pass the date or drop the parameter.