Public: anyone can read this; the crew can change it.
A bounded read lists an app's open pulls a viewer may see, newest first, with each head's latest check #6078
scout/forge:scout/6072-a-bounded-read-lists-an-app-s-open-pulls → main 3 commits fork 1 thread · 1 unresolved
8 files
changed +350
−4
README.md +2
| @@ -276,6 +276,8 @@ An app's repository home wears the pace on its app strip, the terminal's **Deplo | ||
| 276 | 276 | |
| 277 | 277 | ### Every app's visitors |
| 278 | 278 | |
| 279 | An app's door (`/__forge/` on its host, production and previews) lists its five newest open pulls with each head's latest check, and `ssh notes@forge pulls` prints the same list with no pty (`src/apps/door_pulls.ts`): only to someone who can read the repository, and a sealed pull only to its author, a writer or an admin. The MCP does not have it yet; `src/http/mcp.ts` is frozen until open pulls are under 40, and issue #6079 carries it. | |
| 280 | ||
| 279 | 281 | An app made from the template ships the same beacon as `static/beacon.js`, posting to `POST /__forge/measure` on the app's own host. The forge answers that door itself (`src/apps/measure.ts`), before the app is woken: same-origin only, 30 reports a minute per client and 600 per app (429), a body over the cap is a 413, and a private app or a sealed pull's preview refuses anyone its gate refuses. The app is the host's, never the body's. Production reports are kept in `app_vital` (`src/apps/vitals.ts`, TTFB, load, LCP, INP and CLS, a visitor hashed under the day's salt, no address, kept 90 days and removed with the app, at most 20,000 rows per app a day); previews store nothing. Only Do-Not-Track opts a visitor out on an app's host: the `forge_measure=off` cookie is set on the forge's host and never reaches an app's. Read the numbers (visitors today and over 7 days, p75 TTFB, load, LCP, INP and CLS) on the app's settings page under **Visitors and speed**, or as `vitals` in an admin's `GET /api/v1/repos/:owner/:name/app`. Both doors show them only to people who administer the repository (`appVisitors` in `src/actions/apps.ts`), so a reader or an anonymous caller sees no `vitals`. The page also draws p75 load for each of the last 30 days as a sparkline (server-rendered, no script); a day with no reports is a gap in the line, not a zero, and an app with none in 30 days says so in words. The same 30 values, oldest first with `null` for a silent day, are `vitals.loadDays` in the API. In the terminal all five are on the second line of the app's **Deploys** screen over ssh, and a third line there shows the 30 days as block characters with `·` for a silent day, then the range. |
| 280 | 282 | |
| 281 | 283 | The `app_vital` board source (`src/boards/sources.ts`) charts those reports: one row per app per day with `app` (the app's id), `day`, `load_p75`, `inp_p75` (ms) and `reports` (rows kept that day), the percentiles nearest-rank per app per day as `reach` does. `max` or `avg` over several days summarises daily percentiles, not the percentile of the whole range. Its rows belong to the app's repository, so a query sees only the repos in its scope. Today boards query with `repoIds: "all"` (`src/web/pages/boards.ts`) and only a site admin reaches them; no non-admin scope exists, so nothing yet applies the settings page's `canAdmin` gate per repository, and whoever builds one must. Days older than the 90-day trim are gone, not zero. The slug and the visitor hash are not columns. No dependency was added. Left out: a starting board for it; pick `app_vital` in the `/boards` source picker. |
src/apps/door.ts +15 −1
| @@ -6,6 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import { createHash } from "node:crypto"; |
| 8 | 8 | import { escapeAttr, type PillWhere } from "./toolbar.ts"; |
| 9 | import type { OpenPull } from "./door_pulls.ts"; | |
| 9 | 10 | import type { Served, Serving } from "./serving.ts"; |
| 10 | 11 | |
| 11 | 12 | export type DoorFacts = { |
| @@ -22,6 +23,8 @@ export type DoorFacts = { | ||
| 22 | 23 | * `ssh …` command, or null when the terminal is off. Unset shows nothing. |
| 23 | 24 | */ |
| 24 | 25 | ssh?: string | null; |
| 26 | /** The open pulls this viewer may see (`door_pulls.ts`); listed for a reader of the repository. */ | |
| 27 | pulls?: readonly OpenPull[]; | |
| 25 | 28 | }; |
| 26 | 29 | |
| 27 | 30 | const STYLE = |
| @@ -75,6 +78,17 @@ function servingHtml(facts: DoorFacts): string { | ||
| 75 | 78 | return `<p>Serving ${sha(now)} since ${stamp(now.at)}</p>${older === "" ? "" : `<h2>Before it</h2><ul>${older}</ul>`}`; |
| 76 | 79 | } |
| 77 | 80 | |
| 81 | function pullsHtml(facts: DoorFacts): string { | |
| 82 | const { where, pulls } = facts; | |
| 83 | if (where.repo === undefined || !pulls?.length) return ""; | |
| 84 | const items = pulls.map((p) => { | |
| 85 | const href = new URL(`/${where.repo}/pulls/${p.number}`, facts.forgeUrl).href; | |
| 86 | const check = p.run === null ? "" : ` · check ${p.run.state}`; | |
| 87 | return `<li><a href="${escapeAttr(href)}">#${p.number} ${escapeAttr(p.title)}</a>${escapeAttr(check)}</li>`; | |
| 88 | }); | |
| 89 | return `<h2>Open pulls</h2><ul>${items.join("")}</ul>`; | |
| 90 | } | |
| 91 | ||
| 78 | 92 | export function doorPage(facts: DoorFacts, head: boolean): Response { |
| 79 | 93 | const { where, forgeUrl } = facts; |
| 80 | 94 | const links = [link(forgeUrl, "Home")]; |
| @@ -93,7 +107,7 @@ export function doorPage(facts: DoorFacts, head: boolean): Response { | ||
| 93 | 107 | : `<p>Terminal:</p><pre><code>${escapeAttr(facts.ssh)}</code></pre>`; |
| 94 | 108 | const html = `<!doctype html> |
| 95 | 109 | <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><meta name="robots" content="noindex"><title>${escapeAttr(where.app)}, ${escapeAttr(doorWhere(where))}</title><style>${STYLE}</style></head> |
| 96 | <body><main><h1>${escapeAttr(where.app)}</h1><p>${escapeAttr(doorWhere(where))}</p>${servingHtml(facts)}<ul>${links.join("")}</ul>${terminal}</main></body></html> | |
| 110 | <body><main><h1>${escapeAttr(where.app)}</h1><p>${escapeAttr(doorWhere(where))}</p>${servingHtml(facts)}${pullsHtml(facts)}<ul>${links.join("")}</ul>${terminal}</main></body></html> | |
| 97 | 111 | `; |
| 98 | 112 | return new Response(head ? null : html, { |
| 99 | 113 | headers: { |
src/apps/door_pulls.ts added +96
| @@ -0,0 +1,96 @@ | ||
| 1 | /** | |
| 2 | * The open pulls an app's door lists: newest first, each with the latest | |
| 3 | * check at its head, read in one query. Only what the viewer may see: | |
| 4 | * nothing for a repository they may not read (a stranger is never told a | |
| 5 | * private pull exists), and no sealed pull unless it is theirs to see. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { canRead, getRepoById, type Repo } from "../core/repos.ts"; | |
| 10 | import type { RunState } from "../core/runs.ts"; | |
| 11 | import { sealedScope } from "../core/sealed.ts"; | |
| 12 | import { getUserById, isActive, type User } from "../core/users.ts"; | |
| 13 | ||
| 14 | const DOOR_PULLS = 5; | |
| 15 | ||
| 16 | export type OpenPull = { | |
| 17 | number: number; | |
| 18 | title: string; | |
| 19 | headOid: string; | |
| 20 | /** The newest check or deploy at this head; null when none has run. */ | |
| 21 | run: { id: string; state: RunState; summary: string } | null; | |
| 22 | }; | |
| 23 | ||
| 24 | interface Row { | |
| 25 | number: number; | |
| 26 | title: string; | |
| 27 | head: string; | |
| 28 | run_id: string | null; | |
| 29 | run_state: RunState | null; | |
| 30 | run_summary: string | null; | |
| 31 | } | |
| 32 | ||
| 33 | /** The one query: ?1 repo, ?2 sees every sealed pull, ?3 the viewer whose sealed pulls show, ?4 limit. */ | |
| 34 | export const PULLS_SQL = `SELECT issue.number, issue.title, pull.head_oid AS head, | |
| 35 | run.id AS run_id, run.state AS run_state, run.summary AS run_summary | |
| 36 | FROM issue | |
| 37 | JOIN pull ON pull.issue_id = issue.id | |
| 38 | LEFT JOIN run ON run.id = ( | |
| 39 | SELECT r.id FROM run r | |
| 40 | WHERE r.issue_id = issue.id AND r.commit_oid = pull.head_oid | |
| 41 | AND r.kind IN ('deploy','check') | |
| 42 | ORDER BY r.id DESC LIMIT 1) | |
| 43 | WHERE issue.repo_id = ?1 AND issue.kind = 'pull' AND issue.state = 'open' | |
| 44 | AND (?2 = 1 OR issue.sealed = 0 OR issue.author_id = ?3) | |
| 45 | ORDER BY issue.number DESC LIMIT ?4`; | |
| 46 | ||
| 47 | export function openPulls( | |
| 48 | db: Database, | |
| 49 | repo: Repo, | |
| 50 | viewer: User | undefined, | |
| 51 | limit = DOOR_PULLS, | |
| 52 | ): OpenPull[] { | |
| 53 | // A suspended or removed account is a guest. | |
| 54 | const who = viewer !== undefined && isActive(viewer) ? viewer : undefined; | |
| 55 | if (!canRead(db, repo, who)) return []; | |
| 56 | const scope = sealedScope(db, repo, who); | |
| 57 | const n = Number.isInteger(limit) ? Math.min(Math.max(limit, 0), 50) : DOOR_PULLS; | |
| 58 | const rows = db | |
| 59 | .query<Row, [string, number, string | null, number]>( | |
| 60 | PULLS_SQL, | |
| 61 | ) | |
| 62 | .all(repo.id, scope.sealed === true ? 1 : 0, scope.sealedBy ?? null, n); | |
| 63 | return rows.map((r) => ({ | |
| 64 | number: r.number, | |
| 65 | title: r.title, | |
| 66 | headOid: r.head, | |
| 67 | run: | |
| 68 | r.run_id === null || r.run_state === null | |
| 69 | ? null | |
| 70 | : { id: r.run_id, state: r.run_state, summary: r.run_summary ?? "" }, | |
| 71 | })); | |
| 72 | } | |
| 73 | ||
| 74 | /** The pulls the door of the app on `repoId` lists for the signed-in `viewerId` (none: a guest). */ | |
| 75 | export function doorPulls( | |
| 76 | db: Database, | |
| 77 | repoId: string, | |
| 78 | viewerId: string | undefined, | |
| 79 | ): OpenPull[] { | |
| 80 | const repo = getRepoById(db, repoId); | |
| 81 | if (repo === undefined) return []; | |
| 82 | return openPulls(db, repo, viewerId === undefined ? undefined : getUserById(db, viewerId)); | |
| 83 | } | |
| 84 | ||
| 85 | /** `#12 title [check ok]`, one pull a line, for a terminal; control characters in a title are dropped. */ | |
| 86 | export function pullLines(pulls: readonly OpenPull[]): string { | |
| 87 | if (pulls.length === 0) return "no open pulls\n"; | |
| 88 | return pulls | |
| 89 | .map( | |
| 90 | (p) => | |
| 91 | `#${p.number} ${p.title.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")}${ | |
| 92 | p.run === null ? "" : ` [check ${p.run.state}]` | |
| 93 | }\n`, | |
| 94 | ) | |
| 95 | .join(""); | |
| 96 | } |
src/apps/proxy.ts +5
| @@ -40,6 +40,7 @@ import { crewNow, tellLink } from "./crew.ts"; | ||
| 40 | 40 | import { feedback, mintProof } from "./feedback.ts"; |
| 41 | 41 | import { clientBucket } from "../http/peer.ts"; |
| 42 | 42 | import { doorPage } from "./door.ts"; |
| 43 | import { doorPulls } from "./door_pulls.ts"; | |
| 43 | 44 | import { takePhases, timePhase } from "../core/route_timing.ts"; |
| 44 | 45 | import { readServing } from "./serving.ts"; |
| 45 | 46 | import { notFound, securityFloor } from "../http/respond.ts"; |
| @@ -685,10 +686,14 @@ export async function proxyApp( | ||
| 685 | 686 | const serving = timePhase(request, "serving", () => |
| 686 | 687 | readServing(runtime.app.db, target.app.repoId, target.env), |
| 687 | 688 | ); |
| 689 | const pulls = timePhase(request, "pulls", () => | |
| 690 | doorPulls(runtime.app.db, target.app.repoId, signed?.who?.id), | |
| 691 | ); | |
| 688 | 692 | const page = timePhase(request, "render", () => |
| 689 | 693 | doorPage( |
| 690 | 694 | { |
| 691 | 695 | where, |
| 696 | pulls, | |
| 692 | 697 | ...(serving === undefined ? {} : { serving }), |
| 693 | 698 | forgeUrl: runtime.options.forgeUrl, |
| 694 | 699 | ...(ssh === undefined ? {} : { ssh }), |
src/apps/tty_pulls.ts added +24
| @@ -0,0 +1,24 @@ | ||
| 1 | /** | |
| 2 | * `ssh notes@forge pulls`: the app's open pulls as plain lines, no pty. The | |
| 3 | * same gate as the app's terminal (an app you may not see is not there) and | |
| 4 | * the same list as its door page (`door_pulls.ts`). | |
| 5 | */ | |
| 6 | ||
| 7 | import type { App as Forge } from "../app.ts"; | |
| 8 | import type { AppRecord } from "../core/apps.ts"; | |
| 9 | import { canEnterApp, canRead, getRepoById } from "../core/repos.ts"; | |
| 10 | import type { User } from "../core/users.ts"; | |
| 11 | import { openPulls, pullLines } from "./door_pulls.ts"; | |
| 12 | ||
| 13 | /** The text to print, or undefined when the viewer is told there is no such app. */ | |
| 14 | export function appPullsText( | |
| 15 | forge: Forge, | |
| 16 | user: User | undefined, | |
| 17 | app: AppRecord, | |
| 18 | ): string | undefined { | |
| 19 | const repo = getRepoById(forge.db, app.repoId); | |
| 20 | if (repo === undefined || !canRead(forge.db, repo, user)) return undefined; | |
| 21 | if (app.access === "private" && user === undefined) return undefined; | |
| 22 | if (!canEnterApp(forge.db, repo, app, user, { named: false })) return undefined; | |
| 23 | return pullLines(openPulls(forge.db, repo, user)); | |
| 24 | } |
src/tui/ssh.ts +14 −1
| @@ -25,7 +25,8 @@ import { parseGitCommand, runGitService } from "../git/smart-ssh.ts"; | ||
| 25 | 25 | import { serveSsh, type SshServer, type SshSession } from "../ssh/server.ts"; |
| 26 | 26 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 27 | 27 | import { createForgeApp } from "./forge.ts"; |
| 28 | import { appForLogin, attachAppTty } from "../apps/tty.ts"; | |
| 28 | import { appForLogin, attachAppTty, ttyRefusalText } from "../apps/tty.ts"; | |
| 29 | import { appPullsText } from "../apps/tty_pulls.ts"; | |
| 29 | 30 | import type { Terminal } from "./term/terminal.ts"; |
| 30 | 31 | import type { TuiIdentity } from "./session.ts"; |
| 31 | 32 | |
| @@ -210,6 +211,18 @@ export function forgeSshSession(forge: Forge): (session: SshSession) => void { | ||
| 210 | 211 | else attachTui(forge, session, identity); |
| 211 | 212 | return; |
| 212 | 213 | } |
| 214 | const named = appForLogin(forge, session.username); | |
| 215 | if (named !== undefined && session.command?.trim() === "pulls") { | |
| 216 | const text = appPullsText(forge, identity.user, named.app); | |
| 217 | if (text === undefined) { | |
| 218 | session.writeStderr(`forge: ${ttyRefusalText("not_found", session.username)}${CRLF}`); | |
| 219 | session.exit(1); | |
| 220 | } else { | |
| 221 | session.write(text.replace(/\n/g, CRLF)); | |
| 222 | session.exit(0); | |
| 223 | } | |
| 224 | return; | |
| 225 | } | |
| 213 | 226 | void serveGitCommand(forge, session, identity.user).catch( |
| 214 | 227 | (error: unknown) => { |
| 215 | 228 | forge.log.error("git over ssh failed", { error: String(error) }); |
test/apps-door.test.ts +67 −2
| @@ -260,7 +260,7 @@ test("the door and the crew line name the phases they read, signed in, on produc | ||
| 260 | 260 | [`notes--${pull}.${DOMAIN}`, grantFor(`pull-${pull}`)], |
| 261 | 261 | ]; |
| 262 | 262 | const reads: Record<string, string[]> = { |
| 263 | "/__forge/": ["session", "app", "pull", "serving", "render"], | |
| 263 | "/__forge/": ["session", "app", "pull", "serving", "pulls", "render"], | |
| 264 | 264 | "/__forge/crew": ["session", "app", "pull", "working", "render"], |
| 265 | 265 | }; |
| 266 | 266 | for (const [host, cookie] of hosts) |
| @@ -279,6 +279,71 @@ test("a guest names session and render, and the crew line stops before the reads | ||
| 279 | 279 | for (const ms of crew.values()) expect(Number.isFinite(ms)).toBe(true); |
| 280 | 280 | // The door still looks the app and pull up for its words. |
| 281 | 281 | const page = await phasesOf(host, "/__forge/"); |
| 282 | expect([...page.keys()].sort()).toEqual(["app", "pull", "render", "serving", "session"]); | |
| 282 | expect([...page.keys()].sort()).toEqual(["app", "pull", "pulls", "render", "serving", "session"]); | |
| 283 | 283 | } |
| 284 | 284 | }); |
| 285 | ||
| 286 | /** An open pull with a head, as `openPulls` reads it. */ | |
| 287 | function openPullRow(title: string, authorId: string, sealed = false): number { | |
| 288 | const made = createIssue(app.db, { repoId: record.repoId, authorId, title, kind: "pull", sealed }); | |
| 289 | if (!made.ok) throw new Error(made.error); | |
| 290 | app.db.run( | |
| 291 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1,'main','b','0','1')", | |
| 292 | [made.value.id], | |
| 293 | ); | |
| 294 | return made.value.number; | |
| 295 | } | |
| 296 | ||
| 297 | test("production and a preview list the open pulls, escaped, and hides a sealed one from a guest", async () => { | |
| 298 | const open = openPullRow("<i>fix</i>", alice); | |
| 299 | openPullRow("secret hole", alice, true); | |
| 300 | const guest = await (await door(`notes.${DOMAIN}`)).text(); | |
| 301 | expect(guest).toContain("<h2>Open pulls</h2>"); | |
| 302 | expect(guest).toContain(`href="${servers.url}/alice/notes/pulls/${open}"`); | |
| 303 | expect(guest).toContain(`#${open} <i>fix</i>`); | |
| 304 | expect(guest).not.toContain("<i>fix</i>"); | |
| 305 | expect(guest).not.toContain("secret hole"); | |
| 306 | const grant = signGrant(dataDir, { app: record, env: "production" } as AppTarget, 60_000, Date.now(), { | |
| 307 | id: alice, | |
| 308 | name: "alice", | |
| 309 | role: "admin", | |
| 310 | }); | |
| 311 | const mine = await (await door(`notes.${DOMAIN}`, `forge_grant_${record.id.slice(-8)}=${grant}`)).text(); | |
| 312 | expect(mine).toContain("secret hole"); | |
| 313 | const preview = await (await door(`notes--${pull}.${DOMAIN}`)).text(); | |
| 314 | expect(preview).toContain("<h2>Open pulls</h2>"); | |
| 315 | expect(preview).not.toContain("secret hole"); | |
| 316 | }); | |
| 317 | ||
| 318 | test("ssh notes@forge pulls prints the same list, with the same gate", async () => { | |
| 319 | const { forgeSshSession } = await import("../src/tui/ssh.ts"); | |
| 320 | const run = (username: string, command: string) => { | |
| 321 | const out: string[] = []; | |
| 322 | const err: string[] = []; | |
| 323 | let code = -1; | |
| 324 | const session = { | |
| 325 | kind: "exec", | |
| 326 | command, | |
| 327 | username, | |
| 328 | key: { fingerprint: "SHA256:nobody" }, | |
| 329 | write: (d: string) => void out.push(String(d)), | |
| 330 | writeStderr: (d: string) => void err.push(String(d)), | |
| 331 | exit: (c: number) => void (code = c), | |
| 332 | onClose: () => {}, | |
| 333 | }; | |
| 334 | forgeSshSession(app)(session as never); | |
| 335 | return { out: out.join(""), err: err.join(""), code }; | |
| 336 | }; | |
| 337 | const ok = run("notes", "pulls"); | |
| 338 | expect(ok.code).toBe(0); | |
| 339 | expect(ok.out).toContain("fix"); | |
| 340 | expect(ok.out).not.toContain("secret hole"); | |
| 341 | expect(ok.out).not.toContain("\r\r"); | |
| 342 | const none = run("nothing", "pulls"); | |
| 343 | expect(none.code).toBe(1); | |
| 344 | expect(none.err).toContain("only git is available"); | |
| 345 | const priv = run("vault", "pulls"); | |
| 346 | expect(priv.code).toBe(1); | |
| 347 | expect(priv.err).toContain("no app named vault"); | |
| 348 | expect(priv.out).toBe(""); | |
| 349 | }); |
test/door-pulls.test.ts added +127
| @@ -0,0 +1,127 @@ | ||
| 1 | /** The open pulls the door lists: bounded, newest first, and only what the viewer may see. */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createRepo } from "../src/core/repos.ts"; | |
| 9 | import { openPulls, PULLS_SQL } from "../src/apps/door_pulls.ts"; | |
| 10 | import { createIssue } from "../src/core/issues.ts"; | |
| 11 | import { createRun } from "../src/core/runs.ts"; | |
| 12 | import { createUser, type User } from "../src/core/users.ts"; | |
| 13 | import type { Repo } from "../src/core/repos.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | ||
| 18 | let dataDir = ""; | |
| 19 | let app: App; | |
| 20 | let admin: User; | |
| 21 | let alice: User; | |
| 22 | let stranger: User; | |
| 23 | let repo: Repo; | |
| 24 | ||
| 25 | const user = async (name: string, adminIfFirst = false): Promise<User> => { | |
| 26 | const made = await createUser(app.db, { | |
| 27 | origin: "seed", | |
| 28 | trusted: true, | |
| 29 | username: name, | |
| 30 | email: `${name}@forge.test`, | |
| 31 | password: "a-good-passphrase", | |
| 32 | adminIfFirst, | |
| 33 | }); | |
| 34 | if (!made.ok) throw new Error(made.error); | |
| 35 | return made.value; | |
| 36 | }; | |
| 37 | ||
| 38 | const pull = (authorId: string, title: string, sealed = false): { number: number; id: string } => { | |
| 39 | const made = createIssue(app.db, { repoId: repo.id, authorId, title, kind: "pull", sealed }); | |
| 40 | if (!made.ok) throw new Error(made.error); | |
| 41 | app.db.run( | |
| 42 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1,'main',?2,?3,?4)", | |
| 43 | [made.value.id, `b${made.value.number}`, "0".repeat(40), String(made.value.number).padStart(40, "a")], | |
| 44 | ); | |
| 45 | return { number: made.value.number, id: made.value.id }; | |
| 46 | }; | |
| 47 | ||
| 48 | beforeAll(async () => { | |
| 49 | dataDir = await mkdtemp(join(tmpdir(), "forge-door-pulls-")); | |
| 50 | app = createApp({ dataDir, port: 0 }); | |
| 51 | admin = await user("boss", true); | |
| 52 | alice = await user("alice"); | |
| 53 | stranger = await user("stranger"); | |
| 54 | const made = await createRepo(app.db, app.reposRoot, { | |
| 55 | ownerId: admin.id, | |
| 56 | name: "notes", | |
| 57 | visibility: "public", | |
| 58 | }); | |
| 59 | if (!made.ok) throw new Error(made.error); | |
| 60 | repo = made.value; | |
| 61 | }); | |
| 62 | ||
| 63 | afterAll(async () => { | |
| 64 | app.close(); | |
| 65 | await rm(dataDir, { recursive: true, force: true }); | |
| 66 | }); | |
| 67 | ||
| 68 | test("a sealed pull is for its author, a writer and an admin, not a stranger", () => { | |
| 69 | const open = pull(stranger.id, "plain"); | |
| 70 | const sealed = pull(alice.id, "hole", true); | |
| 71 | const numbers = (v: User | undefined) => openPulls(app.db, repo, v).map((p) => p.number); | |
| 72 | expect(numbers(stranger)).toEqual([open.number]); | |
| 73 | expect(numbers(undefined)).toEqual([open.number]); | |
| 74 | expect(numbers(alice)).toEqual([sealed.number, open.number]); | |
| 75 | expect(numbers(admin)).toEqual([sealed.number, open.number]); | |
| 76 | }); | |
| 77 | ||
| 78 | test("a suspended account is a guest", () => { | |
| 79 | const suspended = { ...alice, suspendedAt: "2026-10-01T00:00:00.000Z" }; | |
| 80 | expect(openPulls(app.db, repo, suspended).map((p) => p.title)).toEqual(["plain"]); | |
| 81 | }); | |
| 82 | ||
| 83 | test("a viewer who cannot read the repository gets nothing", async () => { | |
| 84 | const hidden = await createRepo(app.db, app.reposRoot, { | |
| 85 | ownerId: admin.id, | |
| 86 | name: "secret", | |
| 87 | visibility: "private", | |
| 88 | }); | |
| 89 | if (!hidden.ok) throw new Error(hidden.error); | |
| 90 | const made = createIssue(app.db, { repoId: hidden.value.id, authorId: admin.id, title: "x", kind: "pull" }); | |
| 91 | if (!made.ok) throw new Error(made.error); | |
| 92 | app.db.run( | |
| 93 | "INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1,'main','b','0','1')", | |
| 94 | [made.value.id], | |
| 95 | ); | |
| 96 | expect(openPulls(app.db, hidden.value, stranger)).toEqual([]); | |
| 97 | expect(openPulls(app.db, hidden.value, undefined)).toEqual([]); | |
| 98 | expect(openPulls(app.db, hidden.value, admin)).toHaveLength(1); | |
| 99 | }); | |
| 100 | ||
| 101 | test("200 open pulls return the newest 5, with each head's latest run", () => { | |
| 102 | let last = { number: 0, id: "" }; | |
| 103 | for (let i = 0; i < 200; i++) last = pull(alice.id, `p${i}`); | |
| 104 | const head = String(last.number).padStart(40, "a"); | |
| 105 | createRun(app.db, { repoId: repo.id, kind: "check", issueId: last.id, commitOid: head }); | |
| 106 | const newest = createRun(app.db, { repoId: repo.id, kind: "check", issueId: last.id, commitOid: head }); | |
| 107 | app.db.run("UPDATE run SET state = 'failed' WHERE id = ?1", [newest.id]); | |
| 108 | const got = openPulls(app.db, repo, stranger); | |
| 109 | expect(got).toHaveLength(5); | |
| 110 | expect(got[0]).toMatchObject({ number: last.number, title: "p199", headOid: head }); | |
| 111 | expect(got[0]?.run).toMatchObject({ id: newest.id, state: "failed" }); | |
| 112 | expect(got[1]?.run).toBeNull(); | |
| 113 | expect(got.map((p) => p.number)).toEqual([...got.map((p) => p.number)].sort((a, b) => b - a)); | |
| 114 | expect(openPulls(app.db, repo, stranger, 2)).toHaveLength(2); | |
| 115 | }); | |
| 116 | ||
| 117 | test("the query reaches the repository's pulls and a head's runs by index, never by scanning", () => { | |
| 118 | const plan = app.db | |
| 119 | .query<{ detail: string }, [string, number, string | null, number]>(`EXPLAIN QUERY PLAN ${PULLS_SQL}`) | |
| 120 | .all(repo.id, 0, null, 5) | |
| 121 | .map((r) => r.detail); | |
| 122 | const text = plan.join("\n"); | |
| 123 | expect(text).not.toMatch(/SCAN (run|r|pull|issue)\b(?! USING)/); | |
| 124 | expect(plan.some((d) => /SEARCH issue USING (COVERING )?INDEX/.test(d))).toBe(true); | |
| 125 | expect(plan.some((d) => /SEARCH pull USING/.test(d))).toBe(true); | |
| 126 | expect(plan.some((d) => /SEARCH r USING/.test(d))).toBe(true); | |
| 127 | }); |
new above
offline
Nothing imports this module. Wire it into the door page and the tty, or the module is dead code.