Public: anyone can read this; the crew can change it.
The client list shows when each sent forge link was first opened and last seen, in plain words #12832
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/12694-the-client-list-shows-when-each-sent-for → main 5 commits fork 2 threads · 2 unresolved
8 files
changed +268
−10
docs/features/forge-link-states.md +23
| @@ -50,3 +50,26 @@ name. A claimed forge is never marked. | ||
| 50 | 50 | is. A link already used still says so and offers sign-in, since its owner |
| 51 | 51 | needs that. |
| 52 | 52 | - Link validation itself is unchanged. |
| 53 | ||
| 54 | ## Sent, opened, last seen | |
| 55 | ||
| 56 | The sender's `/forges` row and the terminal's forges screen (an admin's; a | |
| 57 | guest sees none of it) carry one line from the same code (`seenOf`): | |
| 58 | "sent 3d ago · opened 1d ago · last seen 2h ago". It replaces the separate | |
| 59 | stage and "a person last did something" words, which said the same thing; a | |
| 60 | resend notice and the last tell keep their own words. | |
| 61 | ||
| 62 | - **Opened** means a browser asked for the claim page, not that a person read | |
| 63 | it. The client forge notes the first such request while the link is open and | |
| 64 | keeps it with the link, so it survives a restart and needs no migration. A | |
| 65 | link preview, unfurl or prefetch does not count: a request that does not ask | |
| 66 | for HTML, or that says `Purpose`, `Sec-Purpose`, `X-Purpose` or `X-Moz` is | |
| 67 | prefetch or preview. Those headers are written by the client, so anyone | |
| 68 | holding the link can mark it opened; one write per link, only while open. | |
| 69 | - The client's own link (`/welcome`) never notes an open. | |
| 70 | - A link spent before opens were noted reads "opened (time not noted)": the | |
| 71 | spend time is not shown as the open time, because the open came earlier. | |
| 72 | - **Not opened yet** is said while no browser has asked. | |
| 73 | - **Last seen** appears once the client signed in: the time of a person's last | |
| 74 | action there. It is blank before, never "now". | |
| 75 | - Nothing is said while the forge is still being made or did not answer. |
src/spawn/claim.ts +30 −1
| @@ -54,6 +54,8 @@ interface Stored { | ||
| 54 | 54 | readonly u: string | null; |
| 55 | 55 | /** When the spawner took it back, ISO; absent while it stands. */ |
| 56 | 56 | readonly r?: string; |
| 57 | /** When a browser first opened the claim page, ISO; absent until one did. */ | |
| 58 | readonly o?: string; | |
| 57 | 59 | } |
| 58 | 60 | |
| 59 | 61 | export type ClaimState = |
| @@ -427,6 +429,8 @@ export interface LinkInfo { | ||
| 427 | 429 | readonly revokedAt: string | null; |
| 428 | 430 | /** When the link was spent (someone opened it and made an account), ISO; absent while unspent. */ |
| 429 | 431 | readonly usedAt?: string | null; |
| 432 | /** When a person's browser first opened the claim page, ISO; null while none did. */ | |
| 433 | readonly openedAt?: string | null; | |
| 430 | 434 | /** When the link stops working, ISO. */ |
| 431 | 435 | readonly expiresAt?: string; |
| 432 | 436 | } |
| @@ -456,7 +460,7 @@ const stateOf = (s: Stored, now: number): LinkState => | ||
| 456 | 460 | /** The claim link in this forge's own database; null when it has none. */ |
| 457 | 461 | export function linkOf(db: Database, now = Date.now()): LinkInfo | null { |
| 458 | 462 | const found = readStored(db); |
| 459 | return found === undefined ? null : { state: stateOf(found.stored, now), revokedAt: found.stored.r ?? null, usedAt: found.stored.u, expiresAt: found.stored.e }; | |
| 463 | return found === undefined ? null : { state: stateOf(found.stored, now), revokedAt: found.stored.r ?? null, usedAt: found.stored.u, openedAt: found.stored.o ?? null, expiresAt: found.stored.e }; | |
| 460 | 464 | } |
| 461 | 465 | |
| 462 | 466 | /** Replace the stored link only if nobody changed it since it was read. */ |
| @@ -468,6 +472,31 @@ function swap(db: Database, orgId: string, from: Found, next: Stored): boolean { | ||
| 468 | 472 | ); |
| 469 | 473 | } |
| 470 | 474 | |
| 475 | /** | |
| 476 | * Whether a request is a person's browser opening a page: a GET that accepts | |
| 477 | * HTML and carries no prefetch purpose. A chat unfurl or a link prefetcher | |
| 478 | * (`Purpose`/`Sec-Purpose: prefetch`, `X-Moz: prefetch`, `X-Purpose: preview`, | |
| 479 | * or an accept header that does not ask for HTML) is not someone looking. | |
| 480 | */ | |
| 481 | export function isBrowserOpen(req: Request): boolean { | |
| 482 | if (req.method !== "GET") return false; | |
| 483 | if (!(req.headers.get("accept") ?? "").toLowerCase().includes("text/html")) return false; | |
| 484 | for (const h of ["purpose", "sec-purpose", "x-purpose", "x-moz"]) | |
| 485 | if (/prefetch|preview|prerender/i.test(req.headers.get(h) ?? "")) return false; | |
| 486 | return true; | |
| 487 | } | |
| 488 | ||
| 489 | /** | |
| 490 | * Remember the first time the open link's page was opened; false when it was not open or | |
| 491 | * already noted. | |
| 492 | */ | |
| 493 | export function noteOpened(db: Database, now = Date.now()): boolean { | |
| 494 | const row = db.query<{ user_id: string }, [string]>("SELECT user_id FROM user_setting WHERE key = ?1").get(KEY); | |
| 495 | const found = readStored(db); | |
| 496 | if (row === null || found === undefined || found.stored.o !== undefined || stateOf(found.stored, now) !== "open") return false; | |
| 497 | return swap(db, row.user_id, found, { ...found.stored, o: new Date(now).toISOString() }); | |
| 498 | } | |
| 499 | ||
| 471 | 500 | /** Take back an open link: from now it answers "ended". False when it was not open. */ |
| 472 | 501 | export function revokeClaim(db: Database, orgId: string, now = Date.now()): boolean { |
| 473 | 502 | const found = readStored(db, orgId); |
src/spawn/clients.ts +35 −1
| @@ -193,7 +193,7 @@ function linkOfBody(v: unknown): LinkInfo | null { | ||
| 193 | 193 | const { state, revokedAt } = v as Record<string, unknown>; |
| 194 | 194 | if (!LINK_STATES.includes(state as LinkState)) return null; |
| 195 | 195 | const iso = (x: unknown): string | null => (typeof x === "string" && !Number.isNaN(Date.parse(x)) ? x : null); |
| 196 | return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso((v as Record<string, unknown>)["usedAt"]) }; | |
| 196 | return { state: state as LinkState, revokedAt: iso(revokedAt), usedAt: iso((v as Record<string, unknown>)["usedAt"]), openedAt: iso((v as Record<string, unknown>)["openedAt"]) }; | |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | async function probe(app: App, child: Child): Promise<ForgeState | undefined> { |
| @@ -542,10 +542,44 @@ export function stageOf(r: Omit<ClientRow, "attention" | "stage">, now: number): | ||
| 542 | 542 | : make("signedin", at(r.link?.usedAt)); |
| 543 | 543 | } |
| 544 | 544 | if (r.link?.state === "used") return make("opened", at(r.link.usedAt)); |
| 545 | if (r.link?.openedAt != null) return make("opened", r.link.openedAt); | |
| 545 | 546 | const sent = r.sentAt === null ? NaN : Date.parse(r.sentAt); |
| 546 | 547 | return make("sent", r.sentAt, !Number.isNaN(sent) && now - sent > UNCLAIMED_MS); |
| 547 | 548 | } |
| 548 | 549 | |
| 550 | /** | |
| 551 | * What the sender is told about a link in plain words: when it was sent, whether and when a | |
| 552 | * browser opened it, when a person was last seen there. "Opened" means a browser asked for the | |
| 553 | * claim page, not that a person read it; a link spent before opens were noted is opened with no | |
| 554 | * time, never a guessed one. | |
| 555 | */ | |
| 556 | export interface Seen { | |
| 557 | /** ISO of the send, or null when the ledger did not say. */ | |
| 558 | readonly sentAt: string | null; | |
| 559 | /** True once a browser opened the page or the link was spent. */ | |
| 560 | readonly opened: boolean; | |
| 561 | /** ISO of the first browser open; null while unopened, or spent before opens were noted. */ | |
| 562 | readonly openedAt: string | null; | |
| 563 | /** ISO of a person's last action there; null until they signed in. */ | |
| 564 | readonly lastSeenAt: string | null; | |
| 565 | } | |
| 566 | ||
| 567 | /** Null while the forge is still being made or did not answer: nothing is claimed unknown. */ | |
| 568 | export function seenOf(r: Pick<ClientRow, "made" | "claimed" | "sentAt" | "link" | "lastActivityAt">): Seen | null { | |
| 569 | if (!r.made || r.claimed === null || r.link === null) return null; | |
| 570 | const openedAt = r.link.openedAt ?? null; | |
| 571 | return { sentAt: r.sentAt, opened: openedAt !== null || r.link.usedAt != null || r.link.state === "used", openedAt, lastSeenAt: r.claimed ? r.lastActivityAt : null }; | |
| 572 | } | |
| 573 | ||
| 574 | /** The one line as parts to join with " · ": the words, and the instant to say beside them. */ | |
| 575 | export function seenParts(s: Seen): { words: string; at: string | null }[] { | |
| 576 | return [ | |
| 577 | ...(s.sentAt === null ? [] : [{ words: "sent", at: s.sentAt }]), | |
| 578 | s.opened ? { words: s.openedAt === null ? "opened (time not noted)" : "opened", at: s.openedAt } : { words: "not opened yet", at: null }, | |
| 579 | ...(s.lastSeenAt === null ? [] : [{ words: "last seen", at: s.lastSeenAt }]), | |
| 580 | ]; | |
| 581 | } | |
| 582 | ||
| 549 | 583 | /** Rows that need the owner first, by reason; then by last activity, newest first. */ |
| 550 | 584 | export function orderRows(rows: readonly Omit<ClientRow, "attention" | "stage" | "archive">[], now: number): ClientRow[] { |
| 551 | 585 | const tier = (a: Attention | null) => (a === null ? ATTENTION_ORDER.length : ATTENTION_ORDER.indexOf(a)); |
src/tui/screens/forges.ts +10 −4
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | |
| 7 | 7 | import type { Ctx, Screen } from "../app/app.ts"; |
| 8 | 8 | import { binding } from "../app/keys.ts"; |
| 9 | import { ATTENTION_WORDS, firstScreenOf, CLAIM_WORDS, claimState, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts"; | |
| 9 | import { ATTENTION_WORDS, firstScreenOf, CLAIM_WORDS, claimState, seenOf, seenParts, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts"; | |
| 10 | 10 | import { useLine } from "../../spawn/usage.ts"; |
| 11 | 11 | import { timeAgo } from "../../web/ui.ts"; |
| 12 | 12 | import { card } from "../ui/widgets.ts"; |
| @@ -26,16 +26,22 @@ import type { Session } from "../session.ts"; | ||
| 26 | 26 | import { appLinkSummary, appLinkWords } from "../../spawn/app_links.ts"; |
| 27 | 27 | |
| 28 | 28 | /** [name, "Not reporting · for Maria · Claimed · 3 apps running"] per row; an empty line when none. */ |
| 29 | export function forgeLines(rows: readonly ClientRow[]): [string, string][] { | |
| 29 | export function forgeLines(rows: readonly ClientRow[], sender = false): [string, string][] { | |
seenLine(r, sender) is computed four times in one array literal; compute it once above. Not blocking. |
||
| 30 | 30 | if (rows.length === 0) return [["", "You have not sent a forge yet"]]; |
| 31 | 31 | return rows.flatMap((r): [string, string][] => [[ |
| 32 | 32 | r.name, |
| 33 | [r.made ? (r.awake === true ? "awake" : "asleep") : null, r.made && r.where.inProcess ? r.where.says : null, r.stopped ?? null, crewHeldWords(r), r.attention === null ? null : ATTENTION_WORDS[r.attention], r.for === null ? null : `for ${r.for}`, CLAIM_WORDS[claimState(r)], linkWords(r.link), runningWords(r), r.stage?.words ?? null, r.nextStep.words, spendWords(r)?.words ?? null, monthWords(r)?.words ?? null, creditWords(r), graceWords(r), useLine(r.size, r.use), r.made ? diskWords(r.disk) : null, r.made ? (r.lastActivityAt === null ? "no person has done anything here" : `a person last did something ${timeAgo(r.lastActivityAt)}`) : null, claimedWords(r), keepWords(r), r.archive === null || r.archive === undefined ? null : `proposed for archive: ${r.archive.why}`, r.lastHeartbeatAt === null ? "no heartbeat yet" : `heartbeat ${timeAgo(r.lastHeartbeatAt)}`] | |
| 33 | [r.made ? (r.awake === true ? "awake" : "asleep") : null, r.made && r.where.inProcess ? r.where.says : null, r.stopped ?? null, crewHeldWords(r), r.attention === null ? null : ATTENTION_WORDS[r.attention], r.for === null ? null : `for ${r.for}`, CLAIM_WORDS[claimState(r)], linkWords(r.link), runningWords(r), seenLine(r, sender) !== null && r.stage?.key !== "tell" && r.stage?.resend !== true ? null : r.stage?.words ?? null, seenLine(r, sender), r.nextStep.words, spendWords(r)?.words ?? null, monthWords(r)?.words ?? null, creditWords(r), graceWords(r), useLine(r.size, r.use), r.made ? diskWords(r.disk) : null, r.made && seenLine(r, sender) === null ? (r.lastActivityAt === null ? "no person has done anything here" : `a person last did something ${timeAgo(r.lastActivityAt)}`) : null, claimedWords(r), keepWords(r), r.archive === null || r.archive === undefined ? null : `proposed for archive: ${r.archive.why}`, r.lastHeartbeatAt === null ? "no heartbeat yet" : `heartbeat ${timeAgo(r.lastHeartbeatAt)}`] | |
| 34 | 34 | .filter((m): m is string => m !== null) |
| 35 | 35 | .join(" · "), |
| 36 | 36 | ], ...firstScreenLines(r), ...appLinkLines(r), ...addressLines(r)]); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | /** "sent 3d ago · opened 1d ago · last seen 2h ago", as /forges says it; for an admin only. */ | |
| 40 | function seenLine(r: ClientRow, sender: boolean): string | null { | |
| 41 | const seen = sender ? seenOf(r) : null; | |
| 42 | return seen === null ? null : seenParts(seen).map((p) => (p.at === null ? p.words : `${p.words} ${timeAgo(p.at)}`)).join(" · "); | |
| 43 | } | |
| 44 | ||
| 39 | 45 | /** "link open", "link ended (undo with u)": what the sender can do about the link, as /forges shows it. */ |
| 40 | 46 | function linkWords(link: LinkInfo | null): string | null { |
| 41 | 47 | if (link === null || link.state === "used") return null; |
| @@ -81,7 +87,7 @@ export function ownCodePicks(app: App, rows: readonly ClientRow[]): { id: string | ||
| 81 | 87 | } |
| 82 | 88 | |
| 83 | 89 | export function forgesScreen(ctx: Ctx, rows: readonly ClientRow[], session?: Session): Screen { |
| 84 | const lines = forgeLines(rows); | |
| 90 | const lines = forgeLines(rows, session?.user?.isAdmin === true); | |
| 85 | 91 | // Stop and Remove are the page's buttons, through the same actions (`spawn/manage.ts`). |
| 86 | 92 | const choose = (title: string, act: (name: string) => void): void => { |
| 87 | 93 | const items = rows.filter((r) => r.made).map((r) => ({ id: r.name, label: r.name, hint: r.awake === true ? "awake" : "asleep" })); |
src/web/pages/claim.ts +3
| @@ -57,6 +57,8 @@ import { | ||
| 57 | 57 | unburnClaim, |
| 58 | 58 | type ClaimState, |
| 59 | 59 | type Spawned, |
| 60 | isBrowserOpen, | |
| 61 | noteOpened, | |
| 60 | 62 | } from "../../spawn/claim.ts"; |
| 61 | 63 | import { pageHead, CAME_FROM_ELSEWHERE } from "../chrome.ts"; |
| 62 | 64 | import { field, readForm } from "../form.ts"; |
| @@ -323,6 +325,7 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> { | ||
| 323 | 325 | // The forge has one link at a time, so its state says nothing about any |
| 324 | 326 | // particular code: a stale or made-up code only ever meets "unknown" on the POST. |
| 325 | 327 | const link = linkOf(ctx.app.db); |
| 328 | if (!own && link?.state === "open" && isBrowserOpen(ctx.request)) noteOpened(ctx.app.db); | |
| 326 | 329 | if (link !== null && link.state !== "open") |
| 327 | 330 | return dead( |
| 328 | 331 | ctx, |
src/web/pages/forges.ts +13 −3
| @@ -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, firstScreenOf, CLAIM_WORDS, claimState, memoryOf, servedRows, dropServed, creditWords, crewHeldWords, graceWords, runningWords, spendWords, monthWords, type ClientRow } from "../../spawn/clients.ts"; | |
| 12 | import { ATTENTION_WORDS, 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"; |
| @@ -66,8 +66,15 @@ function spendNote(r: ClientRow): Html | null { | ||
| 66 | 66 | : html`<span data-spend="ok">${s.words}</span>`; |
| 67 | 67 | } |
| 68 | 68 | |
| 69 | /** "sent 3d ago · opened 1d ago · last seen 2h ago": the one line for where the link stands. */ | |
| 70 | function seenWords(seen: Seen): Html { | |
| 71 | const parts = seenParts(seen).map((p) => html`${p.words}${p.at === null ? null : html` ${time(p.at)}`}`); | |
| 72 | return html`<span data-seen data-opened="${seen.opened ? "yes" : "no"}">${parts.map((p, i) => (i === 0 ? p : html` · ${p}`))}</span>`; | |
| 73 | } | |
| 74 | ||
| 69 | 75 | export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = false): Html { |
| 70 | 76 | const state = claimState(r); |
| 77 | const seen = seenOf(r); | |
| 71 | 78 | const meta = [ |
| 72 | 79 | r.for === null ? null : html`for ${r.for}`, |
| 73 | 80 | r.apps.length === 0 ? null : html`${r.apps.join(", ")}`, |
| @@ -84,10 +91,13 @@ export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = f | ||
| 84 | 91 | monthNote(r), |
| 85 | 92 | creditWords(r) === null ? null : html`<span data-credit>${creditWords(r)}</span>`, |
| 86 | 93 | graceWords(r) === null ? null : html`<span data-grace>${graceWords(r)}</span>`, |
| 87 | r.stage === null | |
| 94 | // With the seen line, the stage and the activity words would say the same thing twice; | |
| 95 | // only what it does not say (a resend, the last tell) keeps its own words. | |
| 96 | r.stage === null || (seen !== null && r.stage.key !== "tell" && !r.stage.resend) | |
| 88 | 97 | ? null |
| 89 | 98 | : html`<span data-stage="${r.stage.key}"${r.stage.resend ? html` data-resend` : null}>${r.stage.words}${r.stage.at === null ? null : html` ${time(r.stage.at)}`}</span>`, |
| 90 | r.made ? (r.lastActivityAt === null ? html`<span data-activity>no one has done anything here yet</span>` : html`<span data-activity>a person last did something ${time(r.lastActivityAt)}</span>`) : null, | |
| 99 | seen === null ? null : seenWords(seen), | |
| 100 | r.made && seen === null ? (r.lastActivityAt === null ? html`<span data-activity>no one has done anything here yet</span>` : html`<span data-activity>a person last did something ${time(r.lastActivityAt)}</span>`) : null, | |
| 91 | 101 | claimedWords(r) === null ? null : html`<span data-claimed-by>${claimedWords(r)}</span>`, |
| 92 | 102 | keepWords(r) === null ? null : html`<span data-keep>${keepWords(r)}</span>`, |
| 93 | 103 | r.noticedAt === null || r.noticedAt === undefined ? null : html`<span data-noticed>owner told ${time(r.noticedAt)}</span>`, |
test/client-link-seen.test.ts added +151
| @@ -0,0 +1,151 @@ | ||
| 1 | /** | |
| 2 | * Whether the client has looked (#12694): the child notes when a person's | |
| 3 | * browser first opened its claim page (a link prefetcher or chat unfurl does | |
| 4 | * not count), and the sender's client list says "sent <day>", "opened <day>" | |
| 5 | * or "not opened yet", and "last seen" once they signed in. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtempSync, rmSync } from "node:fs"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { createOrg } from "../src/core/orgs.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { isBrowserOpen, linkOf, mintClaim } from "../src/spawn/claim.ts"; | |
| 16 | import { seenOf, type ClientRow } from "../src/spawn/clients.ts"; | |
| 17 | import { forgeLines } from "../src/tui/screens/forges.ts"; | |
| 18 | import { forgeRow } from "../src/web/pages/forges.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | const NOW = Date.now(); | |
| 24 | const DAY = 86_400_000; | |
| 25 | const ago = (ms: number) => new Date(NOW - ms).toISOString(); | |
| 26 | const row = (over: Record<string, unknown>) => | |
| 27 | ({ | |
| 28 | name: "acme", url: "https://acme.example", for: "Maria", apps: [], sentAt: ago(3 * DAY), made: true, claimed: false, | |
| 29 | lastActivityAt: null, running: null, reach: null, link: { state: "open", revokedAt: null, usedAt: null, openedAt: null }, | |
| 30 | spendTodayUsd: null, ceilingUsd: null, size: "small", use: { kind: "shared" }, disk: { own: false }, lastHeartbeatAt: null, | |
| 31 | placement: "cell", where: { placement: "cell", inProcess: false, says: "runs in its own sandbox", movable: false, held: false }, | |
| 32 | nextStep: { key: "claim", words: "not claimed yet", lastTellAt: null }, attention: null, stage: null, ...over, | |
| 33 | }) as unknown as ClientRow; | |
| 34 | const html = (r: ClientRow) => String(forgeRow(r, NOW)).replace(/<[^>]*>/g, "").replace(/\s+/g, " ").replace(/ ,/g, ","); | |
| 35 | ||
| 36 | describe("the client list says whether the link was looked at", () => { | |
| 37 | test("unopened", () => { | |
| 38 | const h = html(row({})); | |
| 39 | expect(h).toContain("sent 3d ago"); | |
| 40 | expect(h).toContain("not opened yet"); | |
| 41 | expect(h).not.toContain("last seen"); | |
| 42 | }); | |
| 43 | ||
| 44 | test("opened", () => { | |
| 45 | const h = html(row({ link: { state: "open", revokedAt: null, usedAt: null, openedAt: ago(DAY) } })); | |
| 46 | expect(h).toContain("opened 1d ago"); | |
| 47 | expect(h).not.toContain("not opened yet"); | |
| 48 | expect(h).not.toContain("last seen"); | |
| 49 | }); | |
| 50 | ||
| 51 | test("signed in", () => { | |
| 52 | const h = html(row({ claimed: true, lastActivityAt: ago(2 * 3600_000), link: { state: "used", revokedAt: null, usedAt: ago(DAY), openedAt: ago(DAY) } })); | |
| 53 | expect(h).toContain("opened 1d ago"); | |
| 54 | expect(h).toContain("last seen 2h ago"); | |
| 55 | expect(h).not.toContain("a person last did something"); | |
| 56 | expect(seenOf(row({ claimed: true, lastActivityAt: ago(1), link: { state: "used", usedAt: ago(DAY) } }))!.openedAt).toBeNull(); | |
| 57 | }); | |
| 58 | ||
| 59 | test("a link spent before opens were noted is opened with no day, never a guess", () => { | |
| 60 | const old = row({ claimed: true, lastActivityAt: ago(1), link: { state: "used", revokedAt: null, usedAt: ago(DAY) } }); | |
| 61 | expect(seenOf(old)).toMatchObject({ opened: true, openedAt: null }); | |
| 62 | const h = html(old); | |
| 63 | expect(h).toContain("opened (time not noted)"); | |
| 64 | expect(h).not.toContain("opened 1d ago"); | |
| 65 | }); | |
| 66 | ||
| 67 | test("one line: the stage and the activity words are not said again", () => { | |
| 68 | const h = html(row({ claimed: true, lastActivityAt: ago(2 * 3600_000), link: { state: "used", usedAt: ago(DAY), openedAt: ago(DAY) } })); | |
| 69 | expect(h).not.toContain("a person last did something"); | |
| 70 | expect(h).not.toContain("Signed in"); | |
| 71 | expect(html(row({}))).not.toContain("Sent"); | |
| 72 | }); | |
| 73 | ||
| 74 | test("the terminal says it to the sender, and nothing to a guest", () => { | |
| 75 | const r = row({ claimed: true, lastActivityAt: ago(2 * 3600_000), link: { state: "used", usedAt: ago(DAY), openedAt: ago(DAY) } }); | |
| 76 | const line = forgeLines([r], true)[0]![1]; | |
| 77 | expect(line).toContain("sent 3d ago · opened 1d ago · last seen 2h ago"); | |
| 78 | expect(forgeLines([r], false)[0]![1]).not.toContain("opened"); | |
| 79 | expect(forgeLines([row({})], true)[0]![1]).toContain("sent 3d ago · not opened yet"); | |
| 80 | expect(forgeLines([row({})])[0]![1]).not.toContain("not opened yet"); | |
| 81 | }); | |
| 82 | ||
| 83 | test("nothing is said while the child did not answer", () => { | |
| 84 | expect(seenOf(row({ claimed: null }))).toBeNull(); | |
| 85 | expect(html(row({ claimed: null }))).not.toContain("not opened yet"); | |
| 86 | }); | |
| 87 | }); | |
| 88 | ||
| 89 | describe("only a person's browser counts as opening", () => { | |
| 90 | const get = (headers: Record<string, string>, method = "GET") => new Request("https://x.test/claim", { method, headers }); | |
| 91 | const html5 = { accept: "text/html,application/xhtml+xml;q=0.9,*/*;q=0.8" }; | |
| 92 | ||
| 93 | test("the header rule", () => { | |
| 94 | expect(isBrowserOpen(get(html5))).toBe(true); | |
| 95 | expect(isBrowserOpen(get({}))).toBe(false); | |
| 96 | expect(isBrowserOpen(get({ accept: "*/*" }))).toBe(false); | |
| 97 | expect(isBrowserOpen(get({ accept: "application/json" }))).toBe(false); | |
| 98 | expect(isBrowserOpen(get({ ...html5, purpose: "prefetch" }))).toBe(false); | |
| 99 | expect(isBrowserOpen(get({ ...html5, "sec-purpose": "prefetch;prerender" }))).toBe(false); | |
| 100 | expect(isBrowserOpen(get({ ...html5, "x-moz": "prefetch" }))).toBe(false); | |
| 101 | expect(isBrowserOpen(new Request("https://x.test/claim", { method: "HEAD", headers: html5 }))).toBe(false); | |
| 102 | }); | |
| 103 | ||
| 104 | let root = ""; | |
| 105 | let app: App; | |
| 106 | let orgId = ""; | |
| 107 | let servers: Servers; | |
| 108 | beforeAll(async () => { | |
| 109 | root = mkdtempSync(join(tmpdir(), "forge-seen-")); | |
| 110 | app = createApp({ dataDir: root, port: 0 }); | |
| 111 | const u = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 112 | if (!u.ok) throw new Error(u.error); | |
| 113 | const org = createOrg(app.db, "kin", u.value.id); | |
| 114 | if (!org.ok) throw new Error(org.error); | |
| 115 | const id = org.value.id; | |
| 116 | orgId = id; | |
| 117 | await Bun.write(join(root, "parent.json"), JSON.stringify({ org: "kin" })); | |
| 118 | mintClaim(app.db, id); | |
| 119 | servers = await serve(app); | |
| 120 | }); | |
| 121 | afterAll(() => { | |
| 122 | servers?.stop(); | |
| 123 | app?.close(); | |
| 124 | rmSync(root, { recursive: true, force: true }); | |
| 125 | }); | |
| 126 | ||
| 127 | const visit = (headers: Record<string, string>) => fetch(`${servers.url}/claim`, { headers }); | |
| 128 | ||
| 129 | test("an unfurl leaves the link unopened; a browser opens it, once", async () => { | |
| 130 | await visit({ accept: "*/*" }); | |
| 131 | await visit({ ...html5, purpose: "prefetch" }); | |
| 132 | expect(linkOf(app.db)!.openedAt).toBeNull(); | |
| 133 | expect((await visit(html5)).status).toBe(200); | |
| 134 | const first = linkOf(app.db)!.openedAt; | |
| 135 | expect(first).not.toBeNull(); | |
| 136 | await visit(html5); | |
| 137 | expect(linkOf(app.db)!.openedAt).toBe(first); | |
| 138 | }); | |
| 139 | ||
| 140 | test("the child's own link page (/welcome) notes no open and its pages never say it", async () => { | |
| 141 | // Reset: a new link, unopened. | |
| 142 | mintClaim(app.db, orgId); | |
| 143 | expect(linkOf(app.db)!.openedAt).toBeNull(); | |
| 144 | const res = await fetch(`${servers.url}/welcome`, { headers: html5 }); | |
| 145 | await res.text(); | |
| 146 | expect(linkOf(app.db)!.openedAt).toBeNull(); | |
| 147 | const body = await (await fetch(`${servers.url}/`, { headers: html5 })).text(); | |
| 148 | for (const w of ["not opened yet", "opened (time not noted)", "last seen"]) expect(body).not.toContain(w); | |
| 149 | expect(await (await fetch(`${servers.url}/forges`, { headers: html5, redirect: "manual" })).text()).not.toContain("data-seen"); | |
| 150 | }); | |
| 151 | }); |
test/client-states.test.ts +3 −1
| @@ -67,7 +67,9 @@ describe("client states", () => { | ||
| 67 | 67 | |
| 68 | 68 | test("the page says the same words and offers resend only when stale", () => { |
| 69 | 69 | const fresh = String(forgeRow(stage(), NOW)); |
| 70 | expect(fresh).toContain(`>${STAGE_WORDS.sent} `); | |
| 70 | // The stage's "Sent" is folded into the one seen line (#12694), not said twice. | |
| 71 | expect(fresh).toContain("data-seen"); | |
| 72 | expect(fresh).not.toContain(`>${STAGE_WORDS.sent} `); | |
| 71 | 73 | expect(fresh).not.toContain("Resend"); |
| 72 | 74 | const stale = String(forgeRow(stage({ sentAt: ago(4 * DAY) }), NOW)); |
| 73 | 75 | expect(stale).toContain(STALE_WORDS); |
new above
offline
Heuristic only: unfurl bots that send Accept: text/html with no Purpose header count as opened. Documented, but a user-agent bot check would tighten it.