Public: anyone can read this; the crew can change it.
The owner resends a client's forge link from the client list: the old link stops working and a receipt names the new one's expiry #15706
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15655-the-owner-resends-a-client-s-forge-link → main 7 commits fork 2 threads · 2 unresolved
13 files
changed +155
−26
docs/features/client-resend-link.md added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | --- | |
| 2 | topic: Hosting forges for others, and getting paid | |
| 3 | row: The owner resends a client's forge link from the client list; the old link answers "replaced" and a receipt names the new expiry. | |
| 4 | --- | |
| 5 | # Resend a client's link | |
| 6 | ||
| 7 | On `/forges`, each client that has not claimed its forge has a **Resend link** button. It opens a page that says who the link is for and that the earlier link stops working; nothing changes until you confirm. The new link is shown once. | |
| 8 | ||
| 9 | - The old link answers "This link has been replaced" (410), never a secret. | |
| 10 | - The row's receipt reads "link replaced <when> · new link works until <when>", from the `forge.claim_reissued` event. | |
| 11 | - MCP: `forges` with `action: "resend"` and `forge`; a dry run (the default) previews, `dryRun: false` replaces. | |
| 12 | - Terminal: the claim-link screen (`src/tui/screens/claim_link.ts`) does the same, behind the same confirm. | |
| 13 | - The POST is same-origin only; the link token is never logged. | |
| 14 | - A link unopened for three days says so in the button's title ("Not opened in 3 days: send it again"). | |
| 15 | - Only the one link before the current one is remembered (its hash, field `p`). A link replaced twice answers the same plain 404 as a made-up link, not "replaced"; a short list was left out to keep one hash. |
src/http/mcp/tools/forges.ts +13 −2
| @@ -10,13 +10,14 @@ import { CHILD_NAME } from "../../../spawn/placement.ts"; | ||
| 10 | 10 | import { checkMove } from "../../../spawn/move_check.ts"; |
| 11 | 11 | import { moveToCell } from "../../../spawn/move_cell.ts"; |
| 12 | 12 | import { resizeForge } from "../../../spawn/manage.ts"; |
| 13 | import { good, bad, type Tool } from "../shared.ts"; | |
| 13 | import { previewDoor, reissueDoor } from "../../../spawn/door.ts"; | |
| 14 | import { good, bad, type Tool, str, LINK_REVEALED } from "../shared.ts"; | |
| 14 | 15 | |
| 15 | 16 | const sentSays = (n: number) => (n === 0 ? "This forge has sent no forges yet." : `${n} forge${n === 1 ? "" : "s"} sent from here.`); |
| 16 | 17 | |
| 17 | 18 | export const tool: Tool = { |
| 18 | 19 | name: "forges", |
| 19 | doc: "an admin's list of the forges sent from here: name, address, whom for, apps, claimed, stage (sent/opened/signed in/last tell; resend true when unopened 3 days: claim_link), last activity (a person or the crew, never a heartbeat), who claimed it and when, kept (its newest backup's age, or never kept), keep cost, archive reason, reachable, size, use, madeFrom; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large) or, with `plan`, grants it by hand; `size` workshop raises a forge; `{ forge, sandbox: true }` reads what moving a forge out of this forge's own process into a sandbox would meet (hooks, command-running keys, size) and with `dryRun: false` moves it, proven and put back on any failure (`acknowledge: true` when it was held for what its repositories hold); each forge carries `placement` and `where`; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false; `archive` a name or names stops each, writes its data and ledger as one file to the backup destination, then removes it; `restore` a name brings an archived one back; `kind` refused lists the last 20 refused first asks; `kind` clients lists each client in the page's words", | |
| 20 | doc: "an admin's list of the forges sent from here: name, address, whom for, apps, claimed, stage (sent/opened/signed in/last tell; resend true when unopened 3 days: claim_link), last activity (a person or the crew, never a heartbeat), who claimed it and when, kept (its newest backup's age, or never kept), keep cost, archive reason, reachable, size, use, madeFrom; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large) or, with `plan`, grants it by hand; `size` workshop raises a forge; `{ forge, sandbox: true }` reads what moving a forge out of this forge's own process into a sandbox would meet (hooks, command-running keys, size) and with `dryRun: false` moves it, proven and put back on any failure (`acknowledge: true` when it was held for what its repositories hold); each forge carries `placement` and `where`; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false; `archive` a name or names stops each, writes its data and ledger as one file to the backup destination, then removes it; `restore` a name brings an archived one back; `kind` refused lists the last 20 refused first asks; `kind` clients lists each client in the page's words; `action` resend + `forge`: previews, with `dryRun: false` replaces its link", | |
| 20 | 21 | description: |
| 21 | 22 | "Forges sent from here (admin), clients, a forge plan or size.", |
| 22 | 23 | inputSchema: { type: "object", properties: { kind: { type: "string", enum: ["refused", "clients"] }, forge: { type: "string" }, plan: { type: "string" }, codes: { type: "boolean" }, code: { type: "object" }, revoke: { type: "string" }, archive: { type: ["string", "array"], items: { type: "string" } }, restore: { type: "string" }, sandbox: { type: "boolean" }, size: { type: "string" }, acknowledge: { type: "boolean" }, dryRun: { type: "boolean" } } }, |
| @@ -26,6 +27,16 @@ export const tool: Tool = { | ||
| 26 | 27 | async run(ctx, user, args) { |
| 27 | 28 | if (!user.isAdmin) |
| 28 | 29 | return bad("forbidden", "only an admin of this forge sees the forges it sent"); |
| 30 | if (args["action"] !== undefined) { | |
| 31 | if (args["action"] !== "resend") return bad("invalid", "action is resend"); | |
| 32 | const name = str(args["forge"]); | |
| 33 | if (args["dryRun"] !== false) { | |
| 34 | const seen = await previewDoor(ctx, user, name); | |
| 35 | return seen.status < 400 ? good({ ...seen.body, says: `${String(seen.body["says"])} Nothing changed yet. Send dryRun: false to do it.`, reissued: false }) : bad(String(seen.body["error"]), String(seen.body["message"])); | |
| 36 | } | |
| 37 | const door = await reissueDoor(ctx, user, name); | |
| 38 | return door.status >= 400 ? bad(String(door.body["error"]), String(door.body["message"])) : good({ says: LINK_REVEALED, ...door.body }); | |
| 39 | } | |
| 29 | 40 | if (args["kind"] !== undefined) { |
| 30 | 41 | if (args["kind"] === "clients") { |
| 31 | 42 | const lines = clientLines(await clientRows(ctx.app)); |
src/spawn/claim.ts +20 −2
| @@ -56,11 +56,15 @@ interface Stored { | ||
| 56 | 56 | readonly r?: string; |
| 57 | 57 | /** When a browser first opened the claim page, ISO; absent until one did. */ |
| 58 | 58 | readonly o?: string; |
| 59 | /** | |
| 60 | * sha256 of the link this one replaced (sent again), so the old link can say so; never the token. | |
| 61 | */ | |
| 62 | readonly p?: string; | |
| 59 | 63 | } |
| 60 | 64 | |
| 61 | 65 | export type ClaimState = |
| 62 | 66 | | { readonly state: "ok"; readonly orgId: string } |
| 63 | | { readonly state: "used" | "expired" | "revoked" | "unknown" | "owned" }; | |
| 67 | | { readonly state: "used" | "expired" | "revoked" | "unknown" | "owned" | "replaced" }; | |
| 64 | 68 | |
| 65 | 69 | /** What a spawned child's `parent.json` says, when it has one. */ |
| 66 | 70 | export interface Spawned { |
| @@ -159,10 +163,12 @@ export function mintClaim( | ||
| 159 | 163 | now = Date.now(), |
| 160 | 164 | ): string { |
| 161 | 165 | const token = newToken(); |
| 166 | const earlier = readStored(db, orgId)?.stored; | |
| 162 | 167 | const value: Stored = { |
| 163 | 168 | h: sha256(token), |
| 164 | 169 | e: new Date(now + CLAIM_TTL_MS).toISOString(), |
| 165 | 170 | u: null, |
| 171 | ...(earlier === undefined ? {} : { p: earlier.h }), | |
| 166 | 172 | }; |
| 167 | 173 | db.query( |
| 168 | 174 | `INSERT INTO user_setting (user_id, key, value) VALUES (?1, ?2, ?3) |
| @@ -201,6 +207,18 @@ function find( | ||
| 201 | 207 | return stored.h === sha256(token) ? { raw: row.value, stored } : undefined; |
| 202 | 208 | } |
| 203 | 209 | |
| 210 | /** Whether `token` is the link the current one replaced. */ | |
| 211 | function replacedBy(db: Database, orgId: string, token: string): boolean { | |
| 212 | if (!TOKEN.test(token)) return false; | |
| 213 | const now = db.query<{ value: string }, [string, string]>("SELECT value FROM user_setting WHERE user_id = ?1 AND key = ?2").get(orgId, KEY); | |
| 214 | if (!now) return false; | |
| 215 | try { | |
| 216 | return (JSON.parse(now.value) as Stored).p === sha256(token); | |
| 217 | } catch { | |
| 218 | return false; | |
| 219 | } | |
| 220 | } | |
| 221 | ||
| 204 | 222 | function judge( |
| 205 | 223 | db: Database, |
| 206 | 224 | orgId: string, |
| @@ -208,7 +226,7 @@ function judge( | ||
| 208 | 226 | now: number, |
| 209 | 227 | ): { state: ClaimState; raw?: string; stored?: Stored } { |
| 210 | 228 | const found = find(db, orgId, token); |
| 211 | if (found === undefined) return { state: { state: "unknown" } }; | |
| 229 | if (found === undefined) return { state: { state: replacedBy(db, orgId, token) ? "replaced" : "unknown" } }; | |
| 212 | 230 | if (found.stored.u !== null) return { state: { state: "used" } }; |
| 213 | 231 | if (found.stored.r !== undefined) return { state: { state: "revoked" } }; |
| 214 | 232 | if (Date.parse(found.stored.e) <= now) return { state: { state: "expired" } }; |
src/spawn/clients.ts +4 −1
| @@ -737,13 +737,15 @@ export interface Seen { | ||
| 737 | 737 | * ISO of the last "send again", which ended the earlier link; null once claimed or when never. |
| 738 | 738 | */ |
| 739 | 739 | readonly replacedAt?: string | null; |
| 740 | /** When the link now standing stops working, ISO, said beside a replaced link; null otherwise. */ | |
| 741 | readonly expiresAt?: string | null; | |
| 740 | 742 | } |
| 741 | 743 | |
| 742 | 744 | /** Null while the forge is still being made or did not answer: nothing is claimed unknown. */ |
| 743 | 745 | export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "firstTellAt" | "link" | "lastActivityAt" | "replacedAt">): Seen | null { |
| 744 | 746 | if (!r.made || r.claimed === null || r.link === null) return null; |
| 745 | 747 | const openedAt = r.link.openedAt ?? null; |
| 746 | return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null, firstTellAt: r.firstTellAt, replacedAt: r.claimed ? null : r.replacedAt ?? null }; | |
| 748 | return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null, firstTellAt: r.firstTellAt, replacedAt: r.claimed ? null : r.replacedAt ?? null, expiresAt: !r.claimed && r.replacedAt != null && r.link.state === "open" ? r.link.expiresAt ?? null : null }; | |
| 747 | 749 | } |
| 748 | 750 | |
| 749 | 751 | /** The one line as parts to join with " · ": the words, and the instant to say beside them. */ |
| @@ -754,6 +756,7 @@ export function seenParts(s: Seen): { words: string; at: string | null }[] { | ||
| 754 | 756 | // Nothing while unknown (undefined); "not yet" only once the child has said there is none. |
| 755 | 757 | ...(s.firstTellAt === undefined ? [] : [s.firstTellAt === null ? { words: "first tell: not yet", at: null } : { words: "first tell", at: s.firstTellAt }]), |
| 756 | 758 | ...(s.replacedAt == null ? [] : [{ words: "link replaced", at: s.replacedAt }]), |
| 759 | ...(s.expiresAt == null ? [] : [{ words: "new link works until", at: s.expiresAt }]), | |
| 757 | 760 | ...(s.lastSeenAt === null ? [] : [{ words: "last seen", at: s.lastSeenAt }]), |
| 758 | 761 | ]; |
| 759 | 762 | } |
src/web/pages/claim.ts +6
| @@ -169,6 +169,11 @@ function dead( | ||
| 169 | 169 | title: "This link was turned off", |
| 170 | 170 | text: LINK_TURNED_OFF, |
| 171 | 171 | }, |
| 172 | replaced: { | |
| 173 | status: 410, | |
| 174 | title: "This link has been replaced", | |
| 175 | text: `A newer link was sent for this forge. Use the newest one, or ask ${ask} to send it again.`, | |
| 176 | }, | |
| 172 | 177 | owned: { |
| 173 | 178 | status: 410, |
| 174 | 179 | title: "This forge already has an owner", |
| @@ -492,6 +497,7 @@ const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = { | ||
| 492 | 497 | expired: "This link has expired.", |
| 493 | 498 | revoked: LINK_TURNED_OFF, |
| 494 | 499 | owned: "This forge already has an owner.", |
| 500 | replaced: "This link has been replaced by a newer one.", | |
| 495 | 501 | }; |
| 496 | 502 | |
| 497 | 503 | // POST /claim/options, /welcome/options — a passkey challenge, for a live link and a fit name. |
src/web/pages/forges.ts +2 −4
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | import type { Ctx } from "../../http/context.ts"; |
| 10 | 10 | import { claimsBill } from "../../actions/forge_bill.ts"; |
| 11 | 11 | import { billRow } from "./bills.ts"; |
| 12 | import { ATTENTION_WORDS, daysLeftWords, firstScreenOf, CLAIM_WORDS, claimState, memoryOf, seenOf, seenParts, type Seen, servedRows, dropServed, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts"; | |
| 12 | import { ATTENTION_WORDS, STALE_WORDS, daysLeftWords, firstScreenOf, CLAIM_WORDS, claimState, memoryOf, seenOf, seenParts, type Seen, servedRows, dropServed, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts"; | |
| 13 | 13 | import { notePhase, takeSessionTime, timePhase, timePhaseAsync } from "../../core/route_timing.ts"; |
| 14 | 14 | import { notFoundPage, pageHead } from "../chrome.ts"; |
| 15 | 15 | import { html, type Html } from "../html.ts"; |
| @@ -157,9 +157,7 @@ export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = f | ||
| 157 | 157 | : html`<span data-attention="${r.attention}">${badge(ATTENTION_WORDS[r.attention], { tone: "attn" })}</span>`}${badge(CLAIM_WORDS[state], { |
| 158 | 158 | ...(state === "yes" ? { tone: "success" as const } : { variant: "outline" as const }), |
| 159 | 159 | })}${state === "no" |
| 160 | ? daysLeft !== null && r.link?.state === "open" | |
| 161 | ? html`<form method="post" action="/forges/${r.name}/claim-link" class="inline">${btn("Renew link", { size: "sm", title: "Issue a fresh link now and end the old one; shown once, on the next page" })}</form>` | |
| 162 | : btn(r.stage?.resend === true ? "Resend" : "Send a new link", { size: "sm", href: `/forges/${r.name}/claim-link`, title: "See who it is for and what stops working, then confirm" }) | |
| 160 | ? btn("Resend link", { size: "sm", href: `/forges/${r.name}/claim-link`, title: r.stage?.resend ? `${STALE_WORDS}: send it again. See who it is for and what stops working, then confirm` : "See who it is for and what stops working, then confirm" }) | |
| 163 | 161 | : null}${state === "yes" && r.made |
| 164 | 162 | ? btn("Recovery link", { size: "sm", href: `/forges/${r.name}/recovery-link`, title: "For an owner who lost their passkey: see what it does, then confirm" }) |
| 165 | 163 | : null}${r.link === null |
test/claim-link-regenerate.test.ts +1 −1
| @@ -78,7 +78,7 @@ describe("regenerating a claim link", () => { | ||
| 78 | 78 | expect(shown).toMatch(/Works once, until [A-Z][a-z]{2} \d{1,2}, \d\d:\d\d UTC\./); |
| 79 | 79 | expect(shown).not.toMatch(/20\d\d-\d\d-\d\dT/); |
| 80 | 80 | |
| 81 | expect((await claim(tokenOf(first))).status).toBe(404); | |
| 81 | expect((await claim(tokenOf(first))).status).toBe(410); | |
| 82 | 82 | |
| 83 | 83 | for (const path of ["/forges", "/forges/rosa/claim-link"]) { |
| 84 | 84 | const again = await (await fetch(`${servers.url}${path}`, { headers: { cookie } })).text(); |
test/claim-link.test.ts +1 −1
| @@ -209,7 +209,7 @@ describe("the claim link in the fragment, and its re-issue", () => { | ||
| 209 | 209 | username: "old", |
| 210 | 210 | email: "[email protected]", |
| 211 | 211 | }); |
| 212 | expect(old.status).toBe(404); | |
| 212 | expect(old.status).toBe(410); | |
| 213 | 213 | expect(getUserByName(child.db, "old")).toBeUndefined(); |
| 214 | 214 | // A reserved name is refused for what it is (#720), and the link is |
| 215 | 215 | // not spent by the refusal. |
test/client-link-replace.test.ts +83 −5
| @@ -16,7 +16,7 @@ import { serve, type Servers } from "../src/http/server.ts"; | ||
| 16 | 16 | import { firstBoot } from "../src/spawn/first-boot.ts"; |
| 17 | 17 | import { forgeRow } from "../src/web/pages/forges.ts"; |
| 18 | 18 | import { forgeLines } from "../src/tui/screens/forges.ts"; |
| 19 | import { clientRows, seenOf, seenParts } from "../src/spawn/clients.ts"; | |
| 19 | import { clientRows, seenOf, seenParts, useChildFetch } from "../src/spawn/clients.ts"; | |
| 20 | 20 | import { quietLogs } from "./helpers/globals.ts"; |
| 21 | 21 | |
| 22 | 22 | quietLogs(); |
| @@ -34,6 +34,12 @@ describe("client list: link replaced", () => { | ||
| 34 | 34 | const claim = (t: string) => |
| 35 | 35 | fetch(`${kid.url}/claim`, { method: "POST", headers: { origin: kid.url }, body: new URLSearchParams({ token: t, username: "someone", email: "[email protected]" }) }); |
| 36 | 36 | |
| 37 | // Whether a link is live, without spending it: the passkey challenge answers 200 for a live one. | |
| 38 | const works = (t: string) => | |
| 39 | fetch(`${kid.url}/claim/options`, { method: "POST", headers: { origin: kid.url, "content-type": "application/json" }, body: JSON.stringify({ token: t, username: "someone", email: "[email protected]" }) }); | |
| 40 | const reissued = () => (parent.db.query("SELECT count(*) AS n FROM event WHERE kind = 'forge.claim_reissued'").get() as { n: number }).n; | |
| 41 | let current = ""; | |
| 42 | ||
| 37 | 43 | beforeAll(async () => { |
| 38 | 44 | dir = await mkdtemp(join(tmpdir(), "client-link-replace-")); |
| 39 | 45 | parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); |
| @@ -50,6 +56,7 @@ describe("client list: link replaced", () => { | ||
| 50 | 56 | if (res.status !== 201) throw new Error(`spawn ${res.status}`); |
| 51 | 57 | const body = (await res.json()) as { claimUrl: string; bodyId: string }; |
| 52 | 58 | first = body.claimUrl; |
| 59 | current = tokenOf(first); | |
| 53 | 60 | child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 }); |
| 54 | 61 | await firstBoot(child); |
| 55 | 62 | kid = await serve(child); |
| @@ -72,8 +79,12 @@ describe("client list: link replaced", () => { | ||
| 72 | 79 | const res = await fetch(`${servers.url}/forges/maria/claim-link`, { method: "POST", headers: { cookie, origin: "https://forge.test" } }); |
| 73 | 80 | expect(res.status).toBe(201); |
| 74 | 81 | const fresh = tokenOf(/https?:\/\/[^"<\s]*\/claim#[A-Za-z0-9_-]+/.exec(await res.text())?.[0] ?? ""); |
| 75 | expect((await claim(tokenOf(first))).status).toBe(404); | |
| 76 | expect((await claim(fresh)).status).not.toBe(404); | |
| 82 | const old = await claim(tokenOf(first)); | |
| 83 | expect(old.status).toBe(410); | |
| 84 | expect(await old.text()).toContain("This link has been replaced"); | |
| 85 | expect((await works(fresh)).status).toBe(200); | |
| 86 | expect((await works(tokenOf(first))).status).toBe(410); | |
| 87 | current = fresh; | |
| 77 | 88 | const rows = await clientRows(parent, { probe: false }); |
| 78 | 89 | expect(rows[0]!.replacedAt).not.toBeNull(); |
| 79 | 90 | const seen = seenOf({ made: true, claimed: false, sentAt: rows[0]!.sentAt, link: { state: "open", revokedAt: null }, lastActivityAt: null, replacedAt: rows[0]!.replacedAt ?? null })!; |
| @@ -81,9 +92,76 @@ describe("client list: link replaced", () => { | ||
| 81 | 92 | // The doors: the /forges row and the terminal row, for a forge that answered unclaimed. |
| 82 | 93 | const row = { ...rows[0]!, made: true, claimed: false, link: { state: "open", revokedAt: null } } as typeof rows[0]; |
| 83 | 94 | expect(String(forgeRow(row, Date.now()))).toContain("link replaced"); |
| 95 | const open = { ...row, link: { state: "open", revokedAt: null, expiresAt: "2030-01-01T00:00:00Z" } } as typeof row; | |
| 96 | expect(seenParts(seenOf(open)!).map((p) => p.words)).toContain("new link works until"); | |
| 84 | 97 | expect(forgeLines([row], true)[0]![1]).toContain("link replaced"); |
| 85 | 98 | }); |
| 86 | 99 | |
| 100 | test("the row after a real resend carries the new expiry and says when the new link works until", async () => { | |
| 101 | // Ask the real child (reached on its loopback port) what its link now is: no fabricated link. | |
| 102 | useChildFetch(parent, (input, init) => fetch(String(input).replace(/^https?:\/\/[^/]+/, kid.url), init)); | |
| 103 | const rows = await clientRows(parent); | |
| 104 | const link = rows[0]!.link; | |
| 105 | expect(link?.state).toBe("open"); | |
| 106 | expect(link?.expiresAt).toBeTruthy(); | |
| 107 | expect(seenParts(seenOf(rows[0]!)!).map((p) => p.words)).toContain("new link works until"); | |
| 108 | }); | |
| 109 | ||
| 110 | test("the row's action is Resend link, behind a confirm page that previews and changes nothing", async () => { | |
| 111 | const row = { ...(await clientRows(parent, { probe: false }))[0]!, made: true, claimed: false, link: { state: "open", revokedAt: null } } as unknown as Parameters<typeof forgeRow>[0]; | |
| 112 | const html = String(forgeRow(row, Date.now())); | |
| 113 | expect(html).toContain("Resend link"); | |
| 114 | expect(html).toContain('href="/forges/maria/claim-link"'); | |
| 115 | const before = (parent.db.query("SELECT count(*) AS n FROM event WHERE kind = 'forge.claim_reissued'").get() as { n: number }).n; | |
| 116 | const page = await fetch(`${servers.url}/forges/maria/claim-link`, { headers: { cookie } }); | |
| 117 | expect(await page.text()).toContain("stops working"); | |
| 118 | expect((parent.db.query("SELECT count(*) AS n FROM event WHERE kind = 'forge.claim_reissued'").get() as { n: number }).n).toBe(before); | |
| 119 | }); | |
| 120 | ||
| 121 | test("a link unopened for three days is told in the Resend button's title", async () => { | |
| 122 | const base = { ...(await clientRows(parent, { probe: false }))[0]!, made: true, claimed: false, link: { state: "open", revokedAt: null } } as unknown as Parameters<typeof forgeRow>[0]; | |
| 123 | const stale = { ...base, stage: { key: "sent", words: "x", at: null, resend: true } } as unknown as typeof base; | |
| 124 | const fresh = { ...base, stage: { key: "sent", words: "x", at: null, resend: false } } as unknown as typeof base; | |
| 125 | expect(String(forgeRow(stale, Date.now()))).toContain("Not opened in 3 days: send it again"); | |
| 126 | expect(String(forgeRow(fresh, Date.now()))).not.toContain("Not opened in 3 days: send it again"); | |
| 127 | }); | |
| 128 | ||
| 129 | test("a POST from another origin is refused and replaces nothing", async () => { | |
| 130 | const before = reissued(); | |
| 131 | const res = await fetch(`${servers.url}/forges/maria/claim-link`, { method: "POST", headers: { cookie, origin: "https://evil.test" } }); | |
| 132 | expect(res.status).toBe(403); | |
| 133 | expect(reissued()).toBe(before); | |
| 134 | expect((await works(current)).status).toBe(200); | |
| 135 | }); | |
| 136 | ||
| 137 | test("MCP forges action resend: dry run previews and keeps the link, live replaces it", async () => { | |
| 138 | const call = async (args: object) => { | |
| 139 | const r = await fetch(`${servers.url}/mcp`, { | |
| 140 | method: "POST", | |
| 141 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" }, | |
| 142 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "forges", arguments: args } }), | |
| 143 | }); | |
| 144 | const j = (await r.json()) as { result: { content: { text: string }[] } }; | |
| 145 | return JSON.parse(j.result.content[0]!.text) as Record<string, unknown>; | |
| 146 | }; | |
| 147 | const before = reissued(); | |
| 148 | const dry = await call({ action: "resend", forge: "maria" }); | |
| 149 | expect(dry["reissued"]).toBe(false); | |
| 150 | expect(String(dry["says"])).toContain("Nothing changed yet"); | |
| 151 | expect(dry["claimUrl"]).toBeUndefined(); | |
| 152 | expect(reissued()).toBe(before); | |
| 153 | expect((await works(current)).status).toBe(200); | |
| 154 | const old = current; | |
| 155 | const live = await call({ action: "resend", forge: "maria", dryRun: false }); | |
| 156 | const fresh = tokenOf(String(live["claimUrl"])); | |
| 157 | expect(fresh).not.toBe(""); | |
| 158 | expect(typeof live["expiresAt"]).toBe("string"); | |
| 159 | expect((await works(old)).status).toBe(410); | |
| 160 | expect((await works(fresh)).status).toBe(200); | |
| 161 | expect(reissued()).toBe(before + 1); | |
| 162 | current = fresh; | |
| 163 | }); | |
| 164 | ||
| 87 | 165 | test("once claimed the part is dropped", () => { |
| 88 | 166 | const row = { made: true, claimed: true, sentAt: "2026-01-01T00:00:00Z", link: { state: "used", revokedAt: null }, lastActivityAt: "2026-01-02T00:00:00Z", replacedAt: "2026-01-01T01:00:00Z" } as const; |
| 89 | 167 | const words = seenParts(seenOf(row)!).map((p) => p.words); |
| @@ -94,9 +172,9 @@ describe("client list: link replaced", () => { | ||
| 94 | 172 | test("a claimed forge's link is not replaced", async () => { |
| 95 | 173 | const made = await createUser(child.db, { origin: "seed", trusted: true, username: "claimer", email: "[email protected]", password: "a-long-claim-passphrase", isAdmin: true }); |
| 96 | 174 | if (!made.ok) throw new Error("claim"); |
| 175 | const before = reissued(); | |
| 97 | 176 | const res = await fetch(`${servers.url}/forges/maria/claim-link`, { method: "POST", headers: { cookie, origin: "https://forge.test" } }); |
| 98 | 177 | expect(res.status).toBeGreaterThanOrEqual(400); |
| 99 | const n = (parent.db.query("SELECT count(*) AS n FROM event WHERE kind = 'forge.claim_reissued'").get() as { n: number }).n; | |
| 100 | expect(n).toBe(1); | |
| 178 | expect(reissued()).toBe(before); | |
| 101 | 179 | }); |
| 102 | 180 | }); |
test/client-link-revoke.test.ts +1 −1
| @@ -108,7 +108,7 @@ test("the row offers a new link; cross-origin is refused; the new link works and | ||
| 108 | 108 | expect(fresh).not.toBe(""); |
| 109 | 109 | expect(fresh).not.toBe(old); |
| 110 | 110 | expect((await options(fresh)).status).toBe(200); |
| 111 | expect((await options(old)).status).toBe(404); | |
| 111 | expect((await options(old)).status).toBe(410); | |
| 112 | 112 | // The token is not kept in the forge's list page afterwards. |
| 113 | 113 | const after = await (await fetch(`${servers.url}/forges`, { headers: { cookie } })).text(); |
| 114 | 114 | expect(after).not.toContain(fresh); |
test/client-list.test.ts +3 −3
| @@ -214,14 +214,14 @@ describe("the client list", () => { | ||
| 214 | 214 | expect(maria).toContain('href="https://maria--forge.test"'); |
| 215 | 215 | expect(maria).toMatch(/data-claimed="no"/); |
| 216 | 216 | // The unclaimed link says how long it has left (#14155). |
| 217 | expect(maria).toMatch(/<form method="post" action="\/forges\/maria\/claim-link"[^>]*>[\s\S]*?<button/); | |
| 217 | expect(maria).toMatch(/href="\/forges\/maria\/claim-link"[^>]*>[\s\S]*?Resend link/); | |
| 218 | 218 | expect(maria).toMatch(/data-days-left[^>]*>(\d+ days? left|expires today)</); |
| 219 | 219 | expect(html).not.toContain("forges-sent"); |
| 220 | 220 | const lou = html.slice(html.indexOf('data-forge="lou"')); |
| 221 | 221 | expect(lou).toMatch(/data-claimed="unknown"/); |
| 222 | 222 | expect(html).not.toContain("claim-link-box"); |
| 223 | 223 | expect(maria).toContain('class="row"'); |
| 224 | expect(maria).toContain("Renew link"); | |
| 224 | expect(maria).toContain("Resend link"); | |
| 225 | 225 | expect(lou).toContain("it did not answer just now"); |
| 226 | 226 | // It did not answer, so it carries the one attention badge and leads the list. |
| 227 | 227 | expect(lou).toContain('data-attention="silent"'); |
| @@ -267,7 +267,7 @@ describe("the client list", () => { | ||
| 267 | 267 | test("an unclaimed row offers a fresh link: it replaces the old one, shown once; only an admin may ask", async () => { |
| 268 | 268 | const html = (await (await list(admin)).text()).replace(/\s+/g, " "); |
| 269 | 269 | const maria = html.slice(html.indexOf('data-forge="maria"'), html.indexOf("</li>", html.indexOf('data-forge="maria"'))); |
| 270 | expect(maria).toContain('action="/forges/maria/claim-link"'); | |
| 270 | expect(maria).toContain('href="/forges/maria/claim-link"'); | |
| 271 | 271 | const lou = html.slice(html.indexOf('data-forge="lou"'), html.indexOf("</li>", html.indexOf('data-forge="lou"'))); |
| 272 | 272 | // Whether lou was claimed is unknown, so it offers nothing to send. |
| 273 | 273 | expect(lou).not.toContain("claim-link"); |
test/client-states.test.ts +2 −2
| @@ -70,14 +70,14 @@ describe("client states", () => { | ||
| 70 | 70 | // The stage's "Sent" is folded into the one seen line (#12694), not said twice. |
| 71 | 71 | expect(fresh).toContain("data-seen"); |
| 72 | 72 | expect(fresh).not.toContain(`>${STAGE_WORDS.sent} `); |
| 73 | expect(fresh).not.toContain("Resend"); | |
| 73 | expect(fresh).toContain("Resend link"); | |
| 74 | 74 | const stale = String(forgeRow(stage({ sentAt: ago(4 * DAY) }), NOW)); |
| 75 | 75 | expect(stale).toContain(STALE_WORDS); |
| 76 | 76 | expect(stale).toContain("Resend"); |
| 77 | 77 | expect(stale).toContain("/forges/acme/claim-link"); |
| 78 | 78 | // The row's button opens the preview (a link), not a form that issues at once (#13166). |
| 79 | 79 | expect(stale).not.toContain('method="post" action="/forges/acme/claim-link"'); |
| 80 | expect(String(forgeRow(stage(), NOW))).toContain("Send a new link"); | |
| 80 | expect(String(forgeRow(stage(), NOW))).toContain("Resend link"); | |
| 81 | 81 | }); |
| 82 | 82 | |
| 83 | 83 | test("the terminal line carries the same words, and resend", () => { |
test/forge-link-reissue.test.ts +4 −4
| @@ -114,8 +114,8 @@ describe("sending a forge's link again", () => { | ||
| 114 | 114 | const html = await res.text(); |
| 115 | 115 | expect(html).toContain("/claim#"); |
| 116 | 116 | const old = await claim(tokenOf(first)); |
| 117 | expect(old.status).toBe(404); | |
| 118 | expect(await old.text()).toContain("this link does not work any more"); | |
| 117 | expect(old.status).toBe(410); | |
| 118 | expect(await old.text()).toContain("This link has been replaced"); | |
| 119 | 119 | const rows = parent.db.query("SELECT payload FROM event WHERE kind = 'forge.claim_reissued'").all() as { payload: string }[]; |
| 120 | 120 | expect(rows.length).toBe(1); |
| 121 | 121 | expect(JSON.parse(rows[0]!.payload)).toMatchObject({ name: "maria", for: "Maria at the bakery" }); |
| @@ -127,7 +127,7 @@ describe("sending a forge's link again", () => { | ||
| 127 | 127 | const second = ((await res.json()) as { claimUrl: string }).claimUrl; |
| 128 | 128 | const viaMcp = JSON.parse(await mcp({ reveal: true })) as { claimUrl: string }; |
| 129 | 129 | expect(tokenOf(viaMcp.claimUrl)).not.toBe(tokenOf(second)); |
| 130 | expect((await claim(tokenOf(second))).status).toBe(404); | |
| 130 | expect((await claim(tokenOf(second))).status).toBe(410); | |
| 131 | 131 | const evil = await fetch(`${servers.url}/forges/maria/claim-link`, { method: "POST", headers: { cookie, origin: "https://evil.test" } }); |
| 132 | 132 | expect(evil.status).toBeGreaterThanOrEqual(400); |
| 133 | 133 | const n = (parent.db.query("SELECT count(*) AS n FROM event WHERE kind = 'forge.claim_reissued'").get() as { n: number }).n; |
| @@ -157,7 +157,7 @@ describe("sending a forge's link again", () => { | ||
| 157 | 157 | await Bun.sleep(200); // real-timer: the terminal app settles on its own render loop |
| 158 | 158 | await settleApp(app); |
| 159 | 159 | expect(app.screen.toText()).toContain("/claim#"); |
| 160 | expect((await claim(before)).status).toBe(404); | |
| 160 | expect((await claim(before)).status).toBe(410); | |
| 161 | 161 | }); |
| 162 | 162 | |
| 163 | 163 | test("a person who is neither admin nor the sender is refused the preview everywhere, and learns no name", async () => { |
new above
offline
This count only becomes 2 because the MCP test above runs a live resend first. The test now depends on test order. Compare against a count taken before the cross-origin request instead.