Public: anyone can read this; the crew can change it.
An outsider's offer (outside pull) is one row in /next with Adopt and Decline #15888
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15887-an-outsider-s-offer-outside-pull-is-one → main 0 commits fork 5 threads · 5 unresolved
docs/features/next-up.md +1 −1
| @@ -37,4 +37,4 @@ The subscription's weekly reset is the sprint boundary; what the crew may still | ||
| 37 | 37 | - **Terminal:** Next up shows each row's range and a muted "this week ends here" line under the last issue that fits (by pace, "by pace: this week ends here"), with the projection in the header. "The week in a repository" (palette) shows the retrospective; `j`/`k` choose a proposal and `enter` or `a` takes it, through the same `takeProposal`. A reader or an agent sees it and the key does nothing; a new retrospective (`week.retro`) or a proposal taken anywhere updates the open screen. MCP `next_up` rows carry `estimate` and `thisWeek` (above the pace line when the line is by pace), with a `projection` sentence. |
| 38 | 38 | - Sealed issues are left out of history and of the retrospective, so their cost never shapes a visible range. |
| 39 | 39 | |
| 40 | **From outside (#15840):** above the list, a writer sees each waiting report from an outsider as one row: who sent it (`@name`, "claims to be x, unproven" or "anonymous"), the one-line ask (its title, escaped, at most 140 characters) and two plain forms. *Adopt* opens the report as an issue behind the outside marker (`acceptReport`); *Decline* closes it with one line of why (`dismissReport`; empty says "Not something we will take up right now."), which the sender reads as `reason` at their status link. Both post to `POST /:owner/:name/next` (`report`, `decision` = `adopt` or `decline`, `reason`) and come back to the same page. Offers (outside pulls) are not rows here: they stay on the pulls list (a follow-up, not this change). At 360px the reason takes its own line under the buttons. **Terminal:** the reports screen's `d` opens a prompt for one line of why (empty says the same default) and sends it to the sender's status link; the words come from `actions/outsider_decision.ts`. Pinned by `test/next-outsider-report.test.ts`. | |
| 40 | **From outside (#15840):** above the list, a writer sees each waiting report from an outsider as one row: who sent it (`@name`, "claims to be x, unproven" or "anonymous"), the one-line ask (its title, escaped, at most 140 characters) and two plain forms. *Adopt* opens the report as an issue behind the outside marker (`acceptReport`); *Decline* closes it with one line of why (`dismissReport`; empty says "Not something we will take up right now."), which the sender reads as `reason` at their status link. Both post to `POST /:owner/:name/next` (`report`, `decision` = `adopt` or `decline`, `reason`) and come back to the same page. **Offers (#15887):** an outside pull waiting for approval is the same row (sender "anonymous, offered a change", its title as the ask), posted with `offer` = the pull number. *Adopt* is the pull page's guest approval (`approveGuestPull`): the pull then runs its check and a person can still merge it. *Decline* closes the pull with the one line of why (`decideOffer`), kept on the sender's receipt: `GET /api/v1/offers/:receipt` answers it as `why`. Pinned by `test/next-outsider-offer.test.ts`. At most twenty rows show (the oldest); the heading counts all and a line links the rest on the reports page. At 360px the reason takes its own line under the buttons. **Terminal:** the reports screen lists the waiting offers beside the reports (the palette row counts both): `a` adopts an offer, `d` declines it with the typed line, both through `decideOffer`, read again on every draw (`test/tui-offers.test.ts`). The waiting offers are one statement, whatever their number (`waitingOffers`). On a report, `d` opens a prompt for one line of why (empty says the same default) and sends it to the sender's status link; the words come from `actions/outsider_decision.ts`. Pinned by `test/next-outsider-report.test.ts`. |
src/actions/offer.ts +32
| @@ -230,6 +230,38 @@ function openOffers(app: App, repo: Repo): number { | ||
| 230 | 230 | ); |
| 231 | 231 | } |
| 232 | 232 | |
| 233 | /** One waiting offer as the owner's queue lists it. */ | |
| 234 | export interface WaitingOffer { | |
| 235 | readonly number: number; | |
| 236 | readonly title: string; | |
| 237 | readonly createdAt: string; | |
| 238 | } | |
| 239 | ||
| 240 | /** | |
| 241 | * The open offers nobody has approved at their head, oldest first, at most `OFFERS_WAITING_MAX`, | |
| 242 | * and how many wait in all (#15887). One statement whatever the number of offers: the approval | |
| 243 | * test is in the query (`origins/guest.ts` `approvedAtHead`), so adopted offers that stay open | |
| 244 | * never fill the window. An offer's author is the forge's own offer account, which no pull source | |
| 245 | * trusts, so trust (`foldsIn`) never lifts the wait for one. | |
| 246 | */ | |
outdated
MEDIUM: getPull + outsideAgentWaits per candidate row is N+1, up to 20 times on every /next render for a writer. The budget bump to 72 hides that the count now grows with offers, against the pipeline direction of fast pages. Read waiting offers in one query and keep the statement count constant. |
||
| 247 | export function waitingOffers(app: App, repo: Repo): { readonly offers: readonly WaitingOffer[]; readonly total: number } { | |
| 248 | const rows = app.db | |
| 249 | .query<{ number: number; title: string; created_at: string; total: number }, [string, string, string, number]>( | |
| 250 | `SELECT issue.number AS number, issue.title AS title, issue.created_at AS created_at, COUNT(*) OVER () AS total | |
| 251 | FROM pull INDEXED BY pull_head_branch | |
| 252 | JOIN issue ON issue.id = pull.issue_id | |
| 253 | LEFT JOIN guest_approval ga ON ga.pull_id = pull.issue_id | |
| 254 | WHERE pull.head_branch >= ?2 AND pull.head_branch < ?3 AND issue.repo_id = ?1 AND issue.state = 'open' | |
| 255 | AND (ga.pull_id IS NULL OR (ga.head_oid IS NOT NULL AND ga.head_oid <> pull.head_oid)) | |
| 256 | ORDER BY issue.number LIMIT ?4`, | |
| 257 | ) | |
| 258 | .all(repo.id, `${BRANCH_PREFIX}/`, `${BRANCH_PREFIX}0`, OFFERS_WAITING_MAX); | |
| 259 | return { | |
| 260 | offers: rows.map((r) => ({ number: r.number, title: r.title, createdAt: r.created_at })), | |
| 261 | total: rows[0]?.total ?? 0, | |
| 262 | }; | |
| 263 | } | |
| 264 | ||
| 233 | 265 | /** What the pull's description says before the stranger's words: whose they are. */ |
| 234 | 266 | const FROM_OUTSIDE = "Offered from outside, with no account. Everything below is the sender's, unchecked."; |
| 235 | 267 |
src/actions/outsider_decision.ts +73 −11
| @@ -5,10 +5,16 @@ | ||
| 5 | 5 | * their status link. The page and the terminal share these words and this gate. |
| 6 | 6 | */ |
| 7 | 7 | import type { App } from "../app.ts"; |
| 8 | import { countWaiting, filerWords } from "../core/reports.ts"; | |
| 9 | import type { Repo } from "../core/repos.ts"; | |
| 8 | import { noteDecline } from "../core/offer_receipt.ts"; | |
| 9 | import { countWaiting, filerWords, reasonOf } from "../core/reports.ts"; | |
| 10 | import { canWrite, type Repo } from "../core/repos.ts"; | |
| 11 | import { getPull } from "../core/pulls.ts"; | |
| 12 | import { mayApproveGuest, outsideAgentWaits } from "../origins/guest.ts"; | |
| 10 | 13 | import type { User } from "../core/users.ts"; |
| 11 | 14 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 15 | import { waitingOffers } from "./offer.ts"; | |
| 16 | import { setIssueState } from "./issues.ts"; | |
| 17 | import { approveGuestPull } from "./pulls.ts"; | |
| 12 | 18 | import { acceptReport, dismissReport, waitingReports } from "./reports.ts"; |
| 13 | 19 | |
| 14 | 20 | /** What a decline says when the owner types nothing: a tap is enough. */ |
| @@ -18,6 +24,8 @@ export const DEFAULT_DECLINE = "Not something we will take up right now."; | ||
| 18 | 24 | const ASK_MAX = 140; |
| 19 | 25 | |
| 20 | 26 | export interface OutsiderRow { |
| 27 | /** A report is decided by its id; an offer (an outside pull, #15887) by its pull number. */ | |
| 28 | readonly kind: "report" | "offer"; | |
| 21 | 29 | readonly id: string; |
| 22 | 30 | /** `@name`, `claims to be name, unproven` or `anonymous`. */ |
| 23 | 31 | readonly sender: string; |
| @@ -26,33 +34,54 @@ export interface OutsiderRow { | ||
| 26 | 34 | readonly createdAt: string; |
| 27 | 35 | } |
| 28 | 36 | |
| 29 | /** | |
| 30 | * The most rows the queue shows; the rest wait on the reports page, so filing cannot grow /next. | |
| 31 | */ | |
| 37 | /** The most rows the queue shows; the rest wait on their own pages, so filing cannot grow /next. */ | |
| 32 | 38 | export const OUTSIDER_ROWS_MAX = 20; |
| 33 | 39 | |
| 34 | /** The oldest waiting reports as rows, and how many wait in all. */ | |
| 40 | /** The oldest waiting reports and offers as rows, and how many wait in all. */ | |
| 35 | 41 | export interface OutsiderShelf { |
| 36 | 42 | readonly rows: readonly OutsiderRow[]; |
| 37 | 43 | readonly total: number; |
| 38 | 44 | } |
| 39 | 45 | |
| 46 | const oneLine = (text: string): string => (text.length > ASK_MAX ? `${text.slice(0, ASK_MAX - 1)}…` : text); | |
| 47 | ||
| 40 | 48 | /** |
| 41 | * The oldest `OUTSIDER_ROWS_MAX` waiting reports and the total; nothing for anyone who may not | |
| 42 | * decide them. | |
| 49 | * The oldest `OUTSIDER_ROWS_MAX` waiting reports and offers, oldest first, and the total; nothing | |
| 50 | * for anyone who may not decide them. An offer has no sender to name: it is filed with no account, | |
| 51 | * so the row says so. | |
| 43 | 52 | */ |
| 44 | 53 | export function outsiderRows(app: App, actor: User | undefined, repo: Repo): OutsiderShelf { |
| 45 | 54 | const waiting = waitingReports(app, actor, repo, OUTSIDER_ROWS_MAX); |
| 46 | 55 | if (!waiting.ok) return { rows: [], total: 0 }; |
| 47 | const rows = waiting.value.map((r) => ({ | |
| 56 | const shelf = offerRows(app, actor, repo); | |
| 57 | const reports = waiting.value.map((r): OutsiderRow => ({ | |
| 58 | kind: "report", | |
| 48 | 59 | id: r.id, |
| 49 | 60 | sender: filerWords(r.filedBy), |
| 50 | ask: r.title.length > ASK_MAX ? `${r.title.slice(0, ASK_MAX - 1)}…` : r.title, | |
| 61 | ask: oneLine(r.title), | |
| 51 | 62 | createdAt: r.createdAt, |
| 52 | 63 | })); |
| 53 | return { rows, total: rows.length < OUTSIDER_ROWS_MAX ? rows.length : countWaiting(app.db, repo.id) }; | |
| 64 | const reportTotal = reports.length < OUTSIDER_ROWS_MAX ? reports.length : countWaiting(app.db, repo.id); | |
| 65 | const rows = [...reports, ...shelf.rows].sort((x, y) => x.createdAt.localeCompare(y.createdAt)).slice(0, OUTSIDER_ROWS_MAX); | |
| 66 | return { rows, total: reportTotal + shelf.total }; | |
| 54 | 67 | } |
| 55 | 68 | |
| 69 | /** | |
| 70 | * The waiting offers alone, as rows and a count: what the terminal lists beside the reports. | |
| 71 | * Nothing for anyone who may not approve a guest (`mayApproveGuest`). | |
| 72 | */ | |
| 73 | export function offerRows(app: App, actor: User | undefined, repo: Repo): OutsiderShelf { | |
| 74 | if (!mayApproveGuest(app.db, repo, actor)) return { rows: [], total: 0 }; | |
| 75 | const { offers, total } = waitingOffers(app, repo); | |
| 76 | return { | |
| 77 | rows: offers.map((o): OutsiderRow => ({ kind: "offer", id: String(o.number), sender: OFFER_SENDER, ask: oneLine(o.title), createdAt: o.createdAt })), | |
| 78 | total, | |
| 79 | }; | |
| 80 | } | |
| 81 | ||
| 82 | /** An offer is filed with no account, so the row says what it is rather than who. */ | |
| 83 | const OFFER_SENDER = "anonymous, offered a change"; | |
| 84 | ||
| 56 | 85 | export type Decision = "adopt" | "decline"; |
| 57 | 86 | |
| 58 | 87 | /** `adopt` or `decline`, else undefined. */ |
| @@ -86,3 +115,36 @@ export async function decideOutsider( | ||
| 86 | 115 | if (done.error === "decided") return err(refused("Someone already decided that report.")); |
| 87 | 116 | return err(NOT_FOUND); |
| 88 | 117 | } |
| 118 | ||
| 119 | /** | |
| 120 | * An outsider's offer decided from the same queue (#15887). Adopt is the guest approval the pull | |
| 121 | * page has (checks, a preview and a merge are then a person's to take); Decline closes the pull | |
| 122 | * with one line of why, kept on the sender's receipt so the status link says it. The close and | |
| 123 | * the reason are one transaction: a declined receipt never lacks its why. | |
| 124 | */ | |
| 125 | export function decideOffer( | |
outdated
LOW: the pull is closed and then noteDecline runs as a second write, so a failure or a read between them gives a declined receipt with no why. Do both in one transaction. Adopt failure also maps to NOT_FOUND, which hides the real reason. |
||
| 126 | app: App, | |
| 127 | actor: User | undefined, | |
| 128 | repo: Repo, | |
| 129 | number: number, | |
| 130 | decision: Decision, | |
| 131 | reason = "", | |
| 132 | ): Result<string, DecisionError> { | |
| 133 | const pull = Number.isInteger(number) && number > 0 ? getPull(app.db, repo.id, number) : undefined; | |
| 134 | if (pull === undefined || actor === undefined || actor.kind !== "user" || !canWrite(app.db, repo, actor)) return err(NOT_FOUND); | |
| 135 | if (pull.state !== "open" || !outsideAgentWaits(app.db, pull) || !mayApproveGuest(app.db, repo, actor)) | |
| 136 | return err(refused("Someone already decided that offer.")); | |
| 137 | if (decision === "adopt") { | |
| 138 | const done = approveGuestPull(app, actor, repo, pull); | |
| 139 | if (done.ok) return ok(`Adopted: pull #${pull.number} is approved to run its check.`); | |
| 140 | return err(done.error === "not_guest" ? refused("That pull is not an offer from outside.") : NOT_FOUND); | |
| 141 | } | |
| 142 | const why = reasonOf(reason) || DEFAULT_DECLINE; | |
| 143 | const declined = app.db.transaction((): boolean => { | |
| 144 | const closed = setIssueState(app, actor, repo, pull, "closed", { note: why }); | |
| 145 | if (!closed.ok) return false; | |
| 146 | noteDecline(app.db, pull.id, why); | |
| 147 | return true; | |
| 148 | })(); | |
| 149 | return declined ? ok("Declined. The sender can read why at their status link.") : err(NOT_FOUND); | |
| 150 | } |
src/core/migration-pins/20261123120000-offer-receipt-reason.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 081fd660cb94023481bf8e3aae0c43de25fa61a6550e0bb8d2c15b21f22fd1cf |
src/core/migrations/20261123120000-offer-receipt-reason.ts added +5
| @@ -0,0 +1,5 @@ | ||
| 1 | // A person who declines an offer from outside writes one line of why (#15887); the sender reads it | |
| 2 | // at their status link. Null for an offer not declined, or declined without one. | |
| 3 | export default ` | |
| 4 | ALTER TABLE offer_receipt ADD COLUMN reason TEXT; | |
| 5 | `; |
src/core/offer_receipt.ts +10 −2
| @@ -13,6 +13,8 @@ export interface Receipt { | ||
| 13 | 13 | readonly receipt: string; |
| 14 | 14 | readonly state: OfferState; |
| 15 | 15 | readonly reason: string; |
| 16 | /** The one line a person gave when they declined it; absent when none was given. */ | |
| 17 | readonly why?: string; | |
| 16 | 18 | } |
| 17 | 19 | |
| 18 | 20 | /** The words said once for each state. */ |
| @@ -33,10 +35,15 @@ export function issueReceipt(db: Database, issueId: string): string { | ||
| 33 | 35 | return id; |
| 34 | 36 | } |
| 35 | 37 | |
| 38 | /** Keep the one line of why a declined offer was declined, on each receipt it was given. */ | |
| 39 | export function noteDecline(db: Database, issueId: string, why: string): void { | |
| 40 | db.query("UPDATE offer_receipt SET reason = ?2 WHERE issue_id = ?1").run(issueId, why); | |
| 41 | } | |
| 42 | ||
| 36 | 43 | /** The receipt's state now, or undefined for an id that was never given. */ |
| 37 | 44 | export function readReceipt(db: Database, id: string): Receipt | undefined { |
| 38 | 45 | if (!/^[0-9a-f]{32}$/.test(id)) return undefined; |
| 39 | const row = db.query<{ issue_id: string }, [string]>("SELECT issue_id FROM offer_receipt WHERE id = ?1").get(id); | |
| 46 | const row = db.query<{ issue_id: string; reason: string | null }, [string]>("SELECT issue_id, reason FROM offer_receipt WHERE id = ?1").get(id); | |
| 40 | 47 | if (row === null) return undefined; |
| 41 | 48 | const issue = getIssueById(db, row.issue_id); |
| 42 | 49 | const pull = issue === undefined ? undefined : getPull(db, issue.repoId, issue.number); |
| @@ -45,5 +52,6 @@ export function readReceipt(db: Database, id: string): Receipt | undefined { | ||
| 45 | 52 | : pull.state === "closed" ? "declined" |
| 46 | 53 | : listReviews(db, pull.id).length > 0 ? "under review" |
| 47 | 54 | : "received"; |
| 48 | return { receipt: id, state, reason: OFFER_STATE_WORDS[state] }; | |
| 55 | const said = state === "declined" && row.reason !== null && row.reason !== ""; | |
| 56 | return { receipt: id, state, reason: OFFER_STATE_WORDS[state], ...(said ? { why: row.reason! } : {}) }; | |
| 49 | 57 | } |
src/perf/page_budgets/next.ts +1 −1
| @@ -1,3 +1,3 @@ | ||
| 1 | 1 | import type { PageBudget } from "../page_budget.ts"; |
| 2 | 2 | |
| 3 | export const budget: PageBudget = { page: "next up", pattern: "/:owner/:name/next", medianMs: 50, statements: 70 }; | |
| 3 | export const budget: PageBudget = { page: "next up", pattern: "/:owner/:name/next", medianMs: 50, statements: 72 }; |
src/tui/screens/reports.ts +59 −23
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | import { REPORT_ALREADY_DECIDED } from "../../actions/origin_app_words.ts"; |
| 12 | import { DEFAULT_DECLINE } from "../../actions/outsider_decision.ts"; | |
| 12 | import { DEFAULT_DECLINE, decideOffer, offerRows } from "../../actions/outsider_decision.ts"; | |
| 13 | 13 | import { acceptReport, dismissReport, waitingCount, waitingReports } from "../../actions/reports.ts"; |
| 14 | 14 | import { byFiler, filerWords, isOverdue, REASON_MAX, type Report } from "../../core/reports.ts"; |
| 15 | 15 | import type { Repo } from "../../core/repos.ts"; |
| @@ -29,14 +29,14 @@ import { ListState, ago, drawList, errorText, promptScreen, runAsync, type ListR | ||
| 29 | 29 | /** The palette row for `repo`: only while reports wait, only to a decider. */ |
| 30 | 30 | export function reportsCommands(f: Session, ctx: Ctx, repo: Repo | undefined): Command[] { |
| 31 | 31 | if (repo === undefined || f.user === undefined) return []; |
| 32 | const n = waitingCount(f.app, f.user, repo); | |
| 32 | const n = waitingCount(f.app, f.user, repo) + offerRows(f.app, f.user, repo).total; | |
| 33 | 33 | if (n === 0) return securityCommands(f, ctx, repo); |
| 34 | 34 | return [ |
| 35 | 35 | ...securityCommands(f, ctx, repo), |
| 36 | 36 | { |
| 37 | 37 | id: "reports", |
| 38 | 38 | title: `${repo.ownerName}/${repo.name} — Reports (${n})`, |
| 39 | hint: "accept or dismiss what outsiders sent", | |
| 39 | hint: "accept or dismiss what outsiders sent, reports and offers", | |
| 40 | 40 | run: () => ctx.push(reportsScreen(f, ctx, repo)), |
| 41 | 41 | }, |
| 42 | 42 | ]; |
| @@ -152,28 +152,42 @@ function refusal(code: string): string { | ||
| 152 | 152 | return code === "decided" ? REPORT_ALREADY_DECIDED : errorText(code); |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | /** A row of the screen: a report, or an outside offer (a pull) waiting for approval (#15887). */ | |
| 156 | type Item = | |
| 157 | | { readonly kind: "report"; readonly report: Report } | |
| 158 | | { readonly kind: "offer"; readonly number: number; readonly title: string; readonly createdAt: string }; | |
| 159 | ||
| 155 | 160 | export function reportsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { |
| 156 | 161 | const state = new ListState(); |
| 157 | 162 | let note = ""; |
| 158 | 163 | // What the person last saw: a key acts on that row, so a report someone |
| 159 | 164 | // else decided meanwhile is refused in words rather than silently skipped. |
| 160 | let shown: Report[] = []; | |
| 161 | const load = (): Report[] => { | |
| 165 | let shown: Item[] = []; | |
| 166 | const load = (): Item[] => { | |
| 162 | 167 | const r = waitingReports(f.app, f.user, repo); |
| 163 | shown = r.ok ? r.value : []; | |
| 168 | const reports: Item[] = (r.ok ? r.value : []).map((report) => ({ kind: "report", report })); | |
| 169 | const offers: Item[] = offerRows(f.app, f.user, repo).rows.map((o) => ({ kind: "offer", number: Number(o.id), title: o.ask, createdAt: o.createdAt })); | |
| 170 | shown = [...reports, ...offers]; | |
| 164 | 171 | return shown; |
| 165 | 172 | }; |
| 166 | 173 | |
| 167 | const rows = (list: readonly Report[]): ListRow[] => | |
| 168 | list.map((r) => ({ title: r.title, secondary: filerWords(r.filedBy), meta: isOverdue(r.state, r.createdAt) ? `${ago(r.createdAt)} · past its day` : ago(r.createdAt) })); | |
| 174 | const rows = (list: readonly Item[]): ListRow[] => | |
| 175 | list.map((i) => | |
| 176 | i.kind === "offer" | |
| 177 | ? { title: i.title, secondary: "offer", meta: ago(i.createdAt) } | |
| 178 | : { title: i.report.title, secondary: filerWords(i.report.filedBy), meta: isOverdue(i.report.state, i.report.createdAt) ? `${ago(i.report.createdAt)} · past its day` : ago(i.report.createdAt) }, | |
| 179 | ); | |
| 169 | 180 | |
| 170 | 181 | /** The waiting reports' filers with a count each, most first: "2 @muse · 1 anonymous". */ |
| 171 | const counts = (list: readonly Report[]): string => | |
| 172 | byFiler(list).map((g) => `${g.count} ${filerWords(g)}`).join(" · "); | |
| 182 | const counts = (list: readonly Item[]): string => { | |
| 183 | const reports = list.flatMap((i) => (i.kind === "report" ? [i.report] : [])); | |
| 184 | const offers = list.length - reports.length; | |
| 185 | return [...byFiler(reports).map((g) => `${g.count} ${filerWords(g)}`), ...(offers > 0 ? [`${offers} offered a change`] : [])].join(" · "); | |
| 186 | }; | |
| 173 | 187 | |
| 174 | 188 | const screen: Screen = { |
| 175 | 189 | title: `Reports: ${repo.ownerName}/${repo.name}`, |
| 176 | keys: [binding(["j", "k"], "move cursor"), binding(["a"], "accept as an issue"), binding(["d"], "dismiss")], | |
| 190 | keys: [binding(["j", "k"], "move cursor"), binding(["a"], "accept or adopt"), binding(["d"], "dismiss or decline")], | |
| 177 | 191 | render(area, s) { |
| 178 | 192 | const c = ctx.theme.c; |
| 179 | 193 | const list = load(); |
| @@ -182,7 +196,7 @@ export function reportsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 182 | 196 | list.length === 0 |
| 183 | 197 | ? "Nothing waits." |
| 184 | 198 | : note === "" |
| 185 | ? `From ${counts(list)}; unchecked. a accepts as an issue, d dismisses.` | |
| 199 | ? `From ${counts(list)}; unchecked. a accepts or adopts, d dismisses or declines.` | |
| 186 | 200 | : ""; |
| 187 | 201 | const text = [note, status].filter((t) => t !== "").join(" "); |
| 188 | 202 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| @@ -192,12 +206,22 @@ export function reportsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 192 | 206 | if (aside === undefined || picked === undefined) return; |
| 193 | 207 | const x = aside.x + 1; |
| 194 | 208 | const w = Math.max(0, aside.w - 2); |
| 195 | const lines = [ | |
| 196 | `Sent ${ago(picked.createdAt)} by ${filerWords(picked.filedBy)}`, | |
| 197 | ...(picked.page === null ? [] : [`From ${picked.page}`]), | |
| 198 | "", | |
| 199 | ...wrap(picked.body === "" ? "(no words)" : picked.body, w), | |
| 200 | ]; | |
| 209 | const lines = | |
| 210 | picked.kind === "offer" | |
| 211 | ? [ | |
| 212 | `Sent ${ago(picked.createdAt)} with no account`, | |
| 213 | `Pull #${picked.number}`, | |
| 214 | "", | |
| 215 | ...wrap(picked.title, w), | |
| 216 | "", | |
| 217 | ...wrap("a approves it to run its check; a person still merges. d closes it with a reason the sender reads.", w), | |
| 218 | ] | |
| 219 | : [ | |
| 220 | `Sent ${ago(picked.report.createdAt)} by ${filerWords(picked.report.filedBy)}`, | |
| 221 | ...(picked.report.page === null ? [] : [`From ${picked.report.page}`]), | |
| 222 | "", | |
| 223 | ...wrap(picked.report.body === "" ? "(no words)" : picked.report.body, w), | |
| 224 | ]; | |
| 201 | 225 | lines.slice(0, aside.h).forEach((l, i) => |
| 202 | 226 | s.text(x, aside.y + i, truncate(l, w), style(i === 0 ? c.mutedFg : c.fg, c.bg), x + w), |
| 203 | 227 | ); |
| @@ -208,8 +232,14 @@ export function reportsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 208 | 232 | const picked = shown[state.index]; |
| 209 | 233 | if (e.key === "a") { |
| 210 | 234 | if (picked === undefined) return false; |
| 235 | if (picked.kind === "offer") { | |
| 236 | const done = decideOffer(f.app, f.user, repo, picked.number, "adopt"); | |
| 237 | note = done.ok ? done.value : done.error.kind === "refused" ? done.error.message : errorText("not_found"); | |
| 238 | ctx.invalidate(); | |
| 239 | return true; | |
| 240 | } | |
| 211 | 241 | runAsync(ctx, async () => { |
| 212 | const done = await acceptReport(f.app, f.user, repo, picked.id); | |
| 242 | const done = await acceptReport(f.app, f.user, repo, picked.report.id); | |
| 213 | 243 | note = done.ok ? `Accepted as issue #${done.value.number}.` : refusal(done.error); |
| 214 | 244 | ctx.invalidate(); |
| 215 | 245 | }); |
| @@ -219,14 +249,20 @@ export function reportsScreen(f: Session, ctx: Ctx, repo: Repo): Screen { | ||
| 219 | 249 | if (picked === undefined) return false; |
| 220 | 250 | ctx.push( |
| 221 | 251 | promptScreen(ctx, screen, { |
| 222 | title: "Decline this report", | |
| 252 | title: picked.kind === "offer" ? "Decline this offer" : "Decline this report", | |
| 223 | 253 | label: "Why, for the sender (empty says the default)", |
| 224 | 254 | placeholder: DEFAULT_DECLINE, |
| 225 | hint: "The sender reads this at their status link. No issue is opened.", | |
| 255 | hint: picked.kind === "offer" ? "The sender reads this at their status link. The pull is closed." : "The sender reads this at their status link. No issue is opened.", | |
| 226 | 256 | onSubmit: (value) => { |
| 227 | 257 | if (value.length > REASON_MAX) return `at most ${REASON_MAX} characters`; |
| 228 | const done = dismissReport(f.app, f.user, repo, picked.id, value.trim() === "" ? DEFAULT_DECLINE : value); | |
| 229 | note = done.ok ? "Declined. The sender can read why." : refusal(done.error); | |
| 258 | const why = value.trim() === "" ? DEFAULT_DECLINE : value; | |
| 259 | if (picked.kind === "offer") { | |
| 260 | const done = decideOffer(f.app, f.user, repo, picked.number, "decline", why); | |
| 261 | note = done.ok ? done.value : done.error.kind === "refused" ? done.error.message : errorText("not_found"); | |
| 262 | } else { | |
| 263 | const done = dismissReport(f.app, f.user, repo, picked.report.id, why); | |
| 264 | note = done.ok ? "Declined. The sender can read why." : refusal(done.error); | |
| 265 | } | |
| 230 | 266 | ctx.invalidate(); |
| 231 | 267 | return undefined; |
| 232 | 268 | }, |
src/web/pages/next.ts +15 −11
| @@ -7,7 +7,7 @@ | ||
| 7 | 7 | * beside each action (`j`/`k` choose the row), which post the same form. |
| 8 | 8 | */ |
| 9 | 9 | import { keyStep } from "./next_keys.ts"; |
| 10 | import { DEFAULT_DECLINE, decideOutsider, decisionOf, outsiderRows } from "../../actions/outsider_decision.ts"; | |
| 10 | import { DEFAULT_DECLINE, decideOffer, decideOutsider, decisionOf, outsiderRows } from "../../actions/outsider_decision.ts"; | |
| 11 | 11 | import { REASON_MAX } from "../../core/reports.ts"; |
| 12 | 12 | import { placeOf, rankIssue, rankedWords, stepTo, type Place } from "../../actions/next.ts"; |
| 13 | 13 | import { currentByNumber } from "../../core/estimates.ts"; |
| @@ -217,16 +217,17 @@ function walletRow(ctx: Ctx, repo: Repo, base: string): Html | null { | ||
| 217 | 217 | } |
| 218 | 218 | |
| 219 | 219 | /** |
| 220 | * From outside (#15840): each waiting report as one row, who sent it and what it asks, with Adopt | |
| 221 | * and Decline as plain forms that post here and come back here. A decline carries one line of why | |
| 222 | * (the sender reads it at their status link); left empty it says the default. Writers only. | |
| 220 | * From outside (#15840, #15887): each waiting report or offer (an outside pull) as one row, who | |
| 221 | * sent it and what it asks, with Adopt and Decline as plain forms that post here and come back | |
| 222 | * here. A decline carries one line of why (the sender reads it at their status link); left empty it | |
| 223 | * says the default. Writers only. | |
| 223 | 224 | */ |
| 224 | 225 | function outsiderSection(ctx: Ctx, repo: Repo, base: string, back: string): Html | null { |
| 225 | 226 | const { rows, total } = ctx.auth.user === undefined || ctx.auth.user.kind === "agent" ? { rows: [], total: 0 } : outsiderRows(ctx.app, ctx.auth.user, repo); |
| 226 | 227 | if (rows.length === 0) return null; |
| 227 | 228 | const more = total - rows.length; |
| 228 | const form = (id: string, decision: string, extra: Html | null, label: Html): Html => html`<form method="post" action="${base}/next" class="form-actions"> | |
| 229 | <input type="hidden" name="report" value="${id}" /> | |
| 229 | const form = (kind: string, id: string, decision: string, extra: Html | null, label: Html): Html => html`<form method="post" action="${base}/next" class="form-actions"> | |
| 230 | <input type="hidden" name="${kind}" value="${id}" /> | |
| 230 | 231 | <input type="hidden" name="decision" value="${decision}" /> |
| 231 | 232 | <input type="hidden" name="back" value="${back}" /> |
| 232 | 233 | ${extra}${btn(label, { variant: decision === "adopt" ? "primary" : "outline", size: "sm" })} |
| @@ -235,12 +236,13 @@ function outsiderSection(ctx: Ctx, repo: Repo, base: string, back: string): Html | ||
| 235 | 236 | <h2>From outside · ${String(total)}</h2> |
| 236 | 237 | ${listCard(rows.map((r) => |
| 237 | 238 | row({ |
| 238 | href: `${base}/reports`, | |
| 239 | href: r.kind === "offer" ? `${base}/pulls/${r.id}` : `${base}/reports`, | |
| 239 | 240 | title: html`${r.ask}`, |
| 240 | 241 | titleAttr: r.ask, |
| 241 | 242 | meta: html`${r.sender} · ${time(r.createdAt)}`, |
| 242 | data: { report: r.id }, | |
| 243 | after: html`<div class="outsider-acts">${form(r.id, "adopt", null, html`Adopt`)}${form( | |
| 243 | data: { [r.kind]: r.id }, | |
| 244 | after: html`<div class="outsider-acts">${form(r.kind, r.id, "adopt", null, html`Adopt`)}${form( | |
| 245 | r.kind, | |
| 244 | 246 | r.id, |
| 245 | 247 | "decline", |
| 246 | 248 | html`<input class="input" name="reason" type="text" maxlength="${String(REASON_MAX)}" placeholder="${DEFAULT_DECLINE}" aria-label="Why, for the sender" autocomplete="off" />`, |
| @@ -361,10 +363,12 @@ export async function nextSubmit(ctx: Ctx): Promise<Response> { | ||
| 361 | 363 | const form = await readForm(ctx, "rw"); |
| 362 | 364 | if (form instanceof Response) return form; |
| 363 | 365 | const dest = backTo(form, `${repoBase(repo)}/next`); |
| 364 | if (form.has("report")) { | |
| 366 | if (form.has("report") || form.has("offer")) { | |
| 365 | 367 | const decision = decisionOf(field(form, "decision")); |
| 366 | 368 | if (decision === undefined) return flashOrJson(ctx, dest, { tone: "danger", message: "Say adopt or decline." }); |
| 367 | const decided = await decideOutsider(ctx.app, ctx.auth.user, repo, field(form, "report"), decision, field(form, "reason")); | |
| 369 | const decided = form.has("offer") | |
| 370 | ? decideOffer(ctx.app, ctx.auth.user, repo, Number(field(form, "offer")), decision, field(form, "reason")) | |
| 371 | : await decideOutsider(ctx.app, ctx.auth.user, repo, field(form, "report"), decision, field(form, "reason")); | |
| 368 | 372 | if (decided.ok) return flashOrJson(ctx, dest, { message: decided.value }); |
| 369 | 373 | return decided.error.kind === "not_found" ? notFoundPage(ctx) : flashOrJson(ctx, dest, { tone: "danger", message: decided.error.message }); |
| 370 | 374 | } |
test/helpers/offer-post.ts added +17
| @@ -0,0 +1,17 @@ | ||
| 1 | /** | |
| 2 | * File an offer on `owner/name` of the forge at `url` from a fresh source address; the ids it | |
| 3 | * answers. | |
| 4 | */ | |
| 5 | let sources = 0; | |
| 6 | export async function postOffer(url: string, repo: string, title: string): Promise<{ number: number; receipt: string }> { | |
| 7 | sources += 1; | |
| 8 | const patch = `diff --git a/hello.txt b/hello.txt\n--- a/hello.txt\n+++ b/hello.txt\n@@ -1,2 +1,2 @@\n hello\n-world\n+there${sources}\n`; | |
| 9 | const res = await fetch(`${url}/api/v1/repos/${repo}/offers`, { | |
| 10 | method: "POST", | |
| 11 | headers: { "content-type": "application/json", "x-forwarded-for": `10.8.${Math.floor(sources / 250)}.${(sources % 250) + 1}` }, | |
| 12 | body: JSON.stringify({ title, patch }), | |
| 13 | }); | |
| 14 | if (res.status !== 202) throw new Error(`offer refused: ${res.status}`); | |
| 15 | const body = (await res.json()) as { number: number; id?: string; receipt?: string }; | |
| 16 | return { number: body.number, receipt: String(body.receipt ?? body.id) }; | |
| 17 | } |
test/helpers/tui-repo-screen.ts added +31
| @@ -0,0 +1,31 @@ | ||
| 1 | import type { App } from "../../src/app.ts"; | |
| 2 | import type { Repo } from "../../src/core/repos.ts"; | |
| 3 | import type { User } from "../../src/core/users.ts"; | |
| 4 | import { createForgeApp } from "../../src/tui/forge.ts"; | |
| 5 | import { repoScreen } from "../../src/tui/screens/repo.ts"; | |
| 6 | import { local } from "../../src/tui/screens/where.ts"; | |
| 7 | import { headlessTerminal } from "../../src/tui/term/terminal.ts"; | |
| 8 | import { settleApp } from "./tui.ts"; | |
| 9 | ||
| 10 | /** A terminal session of `user` on `repo`'s screen: `run` feeds keys and returns what is drawn. */ | |
| 11 | export async function openRepoScreen(app: App, repo: Repo, user: User) { | |
| 12 | const terminal = headlessTerminal(140, 36); | |
| 13 | const { app: tui, session } = createForgeApp( | |
| 14 | app, | |
| 15 | { sshUsername: user.username, remoteAddress: "127.0.0.1", fingerprint: "SHA256:test", user }, | |
| 16 | { terminal, exitHooks: false }, | |
| 17 | ); | |
| 18 | tui.start(); | |
| 19 | tui.ctx.push(repoScreen(session, tui.ctx, local(session, repo))); | |
| 20 | await settleApp(tui); | |
| 21 | return { | |
| 22 | run: async (...keys: string[]) => { | |
| 23 | for (const k of keys) { | |
| 24 | terminal.feed(k); | |
| 25 | await settleApp(tui); | |
| 26 | } | |
| 27 | return tui.screen.toText(); | |
| 28 | }, | |
| 29 | stop: () => tui.stop?.(), | |
| 30 | }; | |
| 31 | } |
test/next-outsider-offer.test.ts added +159
| @@ -0,0 +1,159 @@ | ||
| 1 | /** | |
| 2 | * An outsider's offer (an outside pull) is one row in /next beside the reports, with Adopt and | |
| 3 | * Decline; a decline's reason is readable at the sender's status link (#15887). | |
| 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 { createApp, type App } from "../src/app.ts"; | |
| 10 | import { editFiles } from "../src/actions/contents.ts"; | |
| 11 | import { createRepo } from "../src/actions/repos.ts"; | |
| 12 | import { decideOffer } from "../src/actions/outsider_decision.ts"; | |
| 13 | import { waitingOffers } from "../src/actions/offer.ts"; | |
| 14 | import { REASON_MAX } from "../src/core/reports.ts"; | |
| 15 | import { getUserByName } from "../src/core/users.ts"; | |
| 16 | import { getPull } from "../src/core/pulls.ts"; | |
| 17 | import { getRepo } from "../src/core/repos.ts"; | |
| 18 | import { createSession } from "../src/core/sessions.ts"; | |
| 19 | import { createUser, type User } from "../src/core/users.ts"; | |
| 20 | import { guestWaits } from "../src/origins/guest.ts"; | |
| 21 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 22 | import { postOffer } from "./helpers/offer-post.ts"; | |
| 23 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 24 | ||
| 25 | ||
| 26 | let dir: string; | |
| 27 | let app: App; | |
| 28 | let servers: Servers; | |
| 29 | let cookie: string; | |
| 30 | ||
| 31 | beforeAll(async () => { | |
| 32 | dir = await mkdtemp(join(tmpdir(), "forge-next-offer-")); | |
| 33 | app = createApp({ dataDir: dir, port: 0, trustedProxies: LOOPBACK_PROXIES }); | |
| 34 | servers = await serve(app); | |
| 35 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 36 | if (!made.ok) throw new Error(made.error); | |
| 37 | const alice: User = made.value; | |
| 38 | cookie = `forge_session=${createSession(app.db, alice.id).token}`; | |
| 39 | const r = await createRepo(app, alice, { name: "game", visibility: "public" }); | |
| 40 | if (!r.ok) throw new Error(JSON.stringify(r.error)); | |
| 41 | const seeded = await editFiles(app, alice, getRepo(app.db, "alice", "game")!, { files: [{ path: "hello.txt", content: "hello\nworld\n" }], message: "first" }); | |
| 42 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 43 | }, 30_000); | |
| 44 | afterAll(async () => { | |
| 45 | await servers.stop(); | |
| 46 | app.close(); | |
| 47 | await rm(dir, { recursive: true, force: true }); | |
| 48 | }); | |
| 49 | ||
| 50 | const offer = (title: string) => postOffer(servers.url, "alice/game", title); | |
| 51 | const nextPage = async (withCookie = true) => | |
| 52 | (await fetch(`${servers.url}/alice/game/next`, { headers: withCookie ? { cookie } : {} })).text(); | |
| 53 | const decide = (number: number, decision: string, reason = "", withCookie = true) => | |
| 54 | fetch(`${servers.url}/alice/game/next`, { | |
| 55 | method: "POST", | |
| 56 | headers: { ...(withCookie ? { cookie } : {}), origin: servers.url, accept: "application/json" }, | |
| 57 | body: new URLSearchParams({ offer: String(number), decision, reason }), | |
| 58 | }); | |
| 59 | const status = async (receipt: string) => | |
| 60 | (await (await fetch(`${servers.url}/api/v1/offers/${receipt}`)).json()) as { state: string; why?: string }; | |
| 61 | const pullOf = (number: number) => getPull(app.db, getRepo(app.db, "alice", "game")!.id, number)!; | |
| 62 | ||
| 63 | test("an outside offer is one row with its ask, for the owner only", async () => { | |
| 64 | const o = await offer("Say <i>there</i>"); | |
| 65 | const page = await nextPage(); | |
| 66 | expect(page).toContain(`data-offer="${o.number}"`); | |
| 67 | expect(page).toContain("Say <i>there</i>"); | |
| 68 | expect(page).not.toContain("<i>there</i>"); | |
| 69 | expect(page).toMatch(/Adopt\s+<\/button>/); | |
| 70 | expect(page).toMatch(/Decline\s+<\/button>/); | |
| 71 | expect(await nextPage(false)).not.toContain(`data-offer="${o.number}"`); | |
| 72 | await decide(o.number, "decline", "", false); | |
| 73 | expect(guestWaits(app.db, pullOf(o.number))).toBe(true); | |
| 74 | expect(pullOf(o.number).state).toBe("open"); | |
| 75 | }); | |
| 76 | ||
| 77 | test("adopt approves the offer, and the row goes", async () => { | |
| 78 | const o = await offer("Adopt this offer"); | |
| 79 | expect(guestWaits(app.db, pullOf(o.number))).toBe(true); | |
| 80 | const res = await decide(o.number, "adopt"); | |
| 81 | expect(res.status).toBe(200); | |
| 82 | expect(((await res.json()) as { flash: string }).flash).toContain(`pull #${o.number}`); | |
| 83 | expect(guestWaits(app.db, pullOf(o.number))).toBe(false); | |
| 84 | expect(pullOf(o.number).state).toBe("open"); | |
| 85 | expect(await nextPage()).not.toContain(`data-offer="${o.number}"`); | |
| 86 | expect((await decide(o.number, "adopt")).status).toBe(422); | |
| 87 | }); | |
| 88 | ||
| 89 | test("decline closes the pull with a reason the sender reads at their status link", async () => { | |
| 90 | const o = await offer("Decline this offer"); | |
| 91 | expect((await decide(o.number, "decline", "Out of scope for this game.")).status).toBe(200); | |
| 92 | expect(pullOf(o.number).state).toBe("closed"); | |
| 93 | expect(await nextPage()).not.toContain(`data-offer="${o.number}"`); | |
| 94 | expect(await status(o.receipt)).toMatchObject({ state: "declined", why: "Out of scope for this game." }); | |
| 95 | const quiet = await offer("Decline quietly"); | |
| 96 | await decide(quiet.number, "decline"); | |
| 97 | expect((await status(quiet.receipt)).why).toContain("Not something we will take up"); | |
| 98 | }); | |
| 99 | ||
| 100 | test("a bad word, an ordinary pull number and a cross-origin post decide nothing", async () => { | |
| 101 | const o = await offer("Leave this offer"); | |
| 102 | expect((await decide(o.number, "maybe")).status).toBe(422); | |
| 103 | expect((await decide(999, "decline")).status).toBe(404); | |
| 104 | const away = await fetch(`${servers.url}/alice/game/next`, { method: "POST", headers: { cookie, origin: "https://evil.example", accept: "application/json" }, body: new URLSearchParams({ offer: String(o.number), decision: "decline" }) }); | |
| 105 | expect(away.status).toBe(403); | |
| 106 | expect(pullOf(o.number).state).toBe("open"); | |
| 107 | expect(guestWaits(app.db, pullOf(o.number))).toBe(true); | |
| 108 | }); | |
| 109 | ||
| 110 | test("a signed-in user who cannot write decides nothing: 404, the pull untouched", async () => { | |
| 111 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 112 | if (!made.ok) throw new Error(made.error); | |
| 113 | const bobCookie = `forge_session=${createSession(app.db, made.value.id).token}`; | |
| 114 | const o = await offer("Not bob's to decide"); | |
| 115 | for (const decision of ["adopt", "decline"]) { | |
| 116 | const res = await fetch(`${servers.url}/alice/game/next`, { | |
| 117 | method: "POST", | |
| 118 | headers: { cookie: bobCookie, origin: servers.url, accept: "application/json" }, | |
| 119 | body: new URLSearchParams({ offer: String(o.number), decision }), | |
outdated
Tests: the signed-out case asserts no status. Add a signed-in user without write access (expect 404 and the pull untouched), and a reason longer than REASON_MAX. Also assert the receipt has no why for an offer that was not declined. |
||
| 120 | }); | |
| 121 | expect(res.status).toBe(404); | |
| 122 | } | |
| 123 | expect(pullOf(o.number).state).toBe("open"); | |
| 124 | expect(guestWaits(app.db, pullOf(o.number))).toBe(true); | |
| 125 | expect(await (await fetch(`${servers.url}/alice/game/next`, { headers: { cookie: bobCookie } })).text()).not.toContain(`data-offer="${o.number}"`); | |
| 126 | }); | |
| 127 | ||
| 128 | test("a reason is cut at REASON_MAX; an offer not declined has no why on its receipt", async () => { | |
| 129 | const o = await offer("Long reason"); | |
| 130 | expect((await status(o.receipt)).why).toBeUndefined(); | |
| 131 | expect((await decide(o.number, "decline", "x".repeat(REASON_MAX + 80))).status).toBe(200); | |
| 132 | const declined = await status(o.receipt); | |
| 133 | expect(declined.state).toBe("declined"); | |
| 134 | expect(declined.why).toBe("x".repeat(REASON_MAX)); | |
| 135 | const adopted = await offer("Adopted has no why"); | |
| 136 | await decide(adopted.number, "adopt"); | |
| 137 | expect((await status(adopted.receipt)).why).toBeUndefined(); | |
| 138 | }); | |
| 139 | ||
| 140 | test("adopted offers that stay open do not count among the waiting, and the read is one statement", async () => { | |
| 141 | const repo = getRepo(app.db, "alice", "game")!; | |
| 142 | const alice = getUserByName(app.db, "alice")!; | |
| 143 | for (const o of waitingOffers(app, repo).offers) expect(decideOffer(app, alice, repo, o.number, "adopt").ok).toBe(true); | |
| 144 | expect(waitingOffers(app, repo)).toEqual({ offers: [], total: 0 }); | |
| 145 | const newer = await offer("Newer than every adopted one"); | |
| 146 | const { offers, total } = waitingOffers(app, repo); | |
| 147 | expect(offers.map((o) => o.number)).toEqual([newer.number]); | |
| 148 | expect(total).toBe(1); | |
| 149 | const seen: string[] = []; | |
| 150 | const db = app.db as unknown as { query: (sql: string) => unknown }; | |
| 151 | const real = db.query.bind(app.db); | |
| 152 | db.query = (sql: string) => (seen.push(sql), real(sql)); | |
| 153 | try { | |
| 154 | waitingOffers(app, repo); | |
| 155 | } finally { | |
| 156 | db.query = real as typeof db.query; | |
| 157 | } | |
| 158 | expect(seen).toHaveLength(1); | |
| 159 | }); |
test/pages-budget.test.ts +1 −1
| @@ -55,7 +55,7 @@ const DEFAULT = { statements: 60, git: 8 }; | ||
| 55 | 55 | /** Pages that cost more today, with the most they may: lower these, never raise them. */ |
| 56 | 56 | const HEAVIER: Record<string, { statements?: number; git?: number }> = { |
| 57 | 57 | "/:owner/:name/issues/:number": { statements: 71 }, // +1: whether the repository is archived, so its pages say noindex (#14305) |
| 58 | "/:owner/:name/next": { statements: 70 }, // to lower: #15940; +1: the size limits of held issues, one read of both (#15845); +2: the waiting outsider reports, the decider check and the list (#15840); +1: whether the repository is archived (#14305); +1: the wallet row, one read of the monthly cap (#13529); +1: the growth line, one read for the week, the open counts and the planner pause (#12290, #12867) | |
| 58 | "/:owner/:name/next": { statements: 72 }, // to lower: #15940; +2: the waiting offers and the decision check beside the reports (#15887); +1: the size limits of held issues, one read of both (#15845); +2: the waiting outsider reports, the decider check and the list (#15840); +1: whether the repository is archived (#14305); +1: the wallet row, one read of the monthly cap (#13529); +1: the growth line, one read for the week, the open counts and the planner pause (#12290, #12867) | |
| 59 | 59 | "/:owner/:name/pulls/:number": { statements: 117 }, // to lower: #15941; +1: what a green pull waits for (#15812); +1: whether the reports door is closed, one primary-key read per request, never from memory (#15053); +1: whether the repository is archived (#14305); +1: the desk hold on the pull, one read of the notes that link it (#12536); +1: whether a repository is the forge's own copy, one read of the source setting for the tell box (#12037); +1: the paused sentence, one read of a queued paused run on the pull (#12010); +1: the header Talk count (#10327); +1: the landing line (#10860); +1: whether a reviewer is seated, read only when Require review is off and the pull would otherwise wait on one; the page loads no crew besides (#11666) |
| 60 | 60 | "/api/v1/asks": { statements: 61 }, // +1: the stopped apps someone asked for, one read of the viewer's archived repositories (#12277) |
| 61 | 61 | "/board": { statements: 72 }, // +1: the forge-copy read for the tell box (#12037); +1: the vitals read the database's growth (#12025); +6: the money line (#510) — one settings read, then the month's and the 30 days' meter, storage and ledger reads; constant in the number of apps |
test/tui-offers.test.ts added +91
| @@ -0,0 +1,91 @@ | ||
| 1 | /** | |
| 2 | * Offers from outside in the terminal (#15887): an outside pull waits under the same "Reports (N)" | |
| 3 | * row as the reports; `a` approves it, `d` closes it with a typed reason the sender reads at their | |
| 4 | * status link (the same `decideOffer` the page uses). A reader has no row. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, 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 { editFiles } from "../src/actions/contents.ts"; | |
| 11 | import { waitingOffers } from "../src/actions/offer.ts"; | |
| 12 | import { createRepo } from "../src/actions/repos.ts"; | |
| 13 | import { createApp, type App } from "../src/app.ts"; | |
| 14 | import { getPull } from "../src/core/pulls.ts"; | |
| 15 | import type { Repo } from "../src/core/repos.ts"; | |
| 16 | import { createUser, type User } from "../src/core/users.ts"; | |
| 17 | import { guestWaits } from "../src/origins/guest.ts"; | |
| 18 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | import { openRepoScreen } from "./helpers/tui-repo-screen.ts"; | |
| 21 | import { postOffer } from "./helpers/offer-post.ts"; | |
| 22 | import { LOOPBACK_PROXIES } from "./helpers/loopback-proxy.ts"; | |
| 23 | ||
| 24 | quietLogs(); | |
| 25 | ||
| 26 | let dir: string; | |
| 27 | let app: App; | |
| 28 | let servers: Servers; | |
| 29 | let alice: User; | |
| 30 | let bob: User; | |
| 31 | let repo: Repo; | |
| 32 | ||
| 33 | const open = (user: User) => openRepoScreen(app, repo, user); | |
| 34 | ||
| 35 | const offer = (title: string) => postOffer(servers.url, "alice/lib", title); | |
| 36 | const pullOf = (number: number) => getPull(app.db, repo.id, number)!; | |
| 37 | ||
| 38 | beforeAll(async () => { | |
| 39 | dir = await mkdtemp(join(tmpdir(), "forge-tui-offers-")); | |
| 40 | app = createApp({ dataDir: dir, port: 0, host: "forge.test", trustedProxies: LOOPBACK_PROXIES }); | |
| 41 | servers = await serve(app); | |
| 42 | const people = ["alice", "bob"].map(async (username) => { | |
| 43 | const r = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: username === "alice" }); | |
| 44 | if (!r.ok) throw new Error(r.error); | |
| 45 | return r.value; | |
| 46 | }); | |
| 47 | [alice, bob] = [await people[0]!, await people[1]!]; | |
| 48 | const made = await createRepo(app, alice, { name: "lib", visibility: "public" }); | |
| 49 | if (!made.ok) throw new Error(String(made.error)); | |
| 50 | repo = made.value; | |
| 51 | const seeded = await editFiles(app, alice, repo, { files: [{ path: "hello.txt", content: "hello\nworld\n" }], message: "first" }); | |
| 52 | if (!seeded.ok) throw new Error(JSON.stringify(seeded.error)); | |
| 53 | }, 30_000); | |
| 54 | afterAll(async () => { | |
| 55 | await servers.stop(); | |
| 56 | app.close(); | |
| 57 | await rm(dir, { recursive: true, force: true }); | |
| 58 | }); | |
| 59 | ||
| 60 | test("two offers wait under Reports (2): the owner adopts one and declines the other with a reason; a reader has no row", async () => { | |
| 61 | const first = await offer("Say there"); | |
| 62 | const second = await offer("Say hello twice"); | |
| 63 | ||
| 64 | const reader = await open(bob); | |
| 65 | expect(await reader.run(":", "Reports (")).not.toContain("lib — Reports ("); | |
| 66 | reader.stop(); | |
| 67 | ||
| 68 | const h = await open(alice); | |
| 69 | let text = await h.run(":", "Reports ("); | |
| 70 | expect(text).toContain("alice/lib — Reports (2)"); | |
| 71 | text = await h.run("\r"); | |
| 72 | expect(text).toContain("Say there"); | |
| 73 | expect(text).toContain("Say hello twice"); | |
| 74 | expect(text).toContain("2 offered a change"); | |
| 75 | ||
| 76 | text = await h.run("a"); | |
| 77 | expect(text).toContain(`pull #${first.number} is approved`); | |
| 78 | expect(guestWaits(app.db, pullOf(first.number))).toBe(false); | |
| 79 | expect(pullOf(first.number).state).toBe("open"); | |
| 80 | expect(waitingOffers(app, repo).offers.map((o) => o.number)).toEqual([second.number]); | |
| 81 | ||
| 82 | await h.run("d"); | |
| 83 | for (const ch of "Not now") await h.run(ch); | |
| 84 | text = await h.run("\r"); | |
| 85 | expect(text).toContain("Declined."); | |
| 86 | expect(text).toContain("Nothing waits."); | |
| 87 | expect(pullOf(second.number).state).toBe("closed"); | |
| 88 | const status = (await (await fetch(`${servers.url}/api/v1/offers/${second.receipt}`)).json()) as { state: string; why?: string }; | |
| 89 | expect(status).toMatchObject({ state: "declined", why: "Not now" }); | |
| 90 | h.stop(); | |
| 91 | }); |
HIGH (correctness/DoS of the queue): LIMIT is applied before the outsideAgentWaits filter, and adopted offers stay open. Once 20 offers are adopted and still open, they fill the oldest-first window and every newer waiting offer is invisible in /next, so the owner cannot adopt or decline it. Filter waiting in SQL (or page past non-waiting ones) so the limit counts only waiting offers. Add a test that adopts more than the cap and then expects a newer offer to show.