Public: anyone can read this; the crew can change it.

The clock, a deal page and the client timeline each answer in under 50 ms with 200 deals, and a test fails when they stop #53

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/deal-clock:mason/39-the-clock-a-deal-page-and-the-client-tim → main 3 commits fork

  • Merged. mason/39-the-clock-a-deal-page-and-the-client-tim was merged into main as 42b8a2f.

Review

Sign in to review this change.
12 files changed +511 −45
README.md +1
@@ -144,6 +144,7 @@ does not look right is ignored in favour of the sample, with a line in the log.
144144 - **Real:** the dates and their math, mark done, moves with their reasons,
145145 who owes what, the reminder drafts and editing them, the record of what was
146146 sent, the reasons for messages you chose not to send, the calendar file, and adding parties, dates and deals.
147 - **Real:** the clock, a deal page and the client timeline are tested to answer in under 50 ms with 200 deals.
147148 - **Simulated:** sending. Send records that a reminder went out and keeps the
148149 words that were approved; no email or text message leaves the app.
149150 - **Not in this prototype (phase two):** real email or text messages,
src/cache.test.ts added +148
@@ -0,0 +1,148 @@
1 /** What is remembered between requests never outlives a change or crosses from one person to another. */
2 import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test";
3 import { mkdtempSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { memoizer } from "./memo.ts";
7 import { forgetRationing } from "./security.ts";
8 import { serve } from "./server.ts";
9 import { sign } from "./who.ts";
10
11 const KEY = "a-key-only-the-forge-holds";
12 let server: ReturnType<typeof serve>;
13 let base = "";
14 afterAll(() => server.stop(true));
15 beforeEach(forgetRationing);
16
17 const as = (id: string, name: string, role: "user" | "admin" = "user"): Record<string, string> => {
18 const identity = `${id}|${name}|${role}|${Date.now() + 60_000}`;
19 return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
20 };
21 const owner = as("u1", "alice");
22 const other = as("u2", "bob");
23 const admin = as("u0", "root", "admin");
24 const anon: Record<string, string> = {};
25
26 const get = async (path: string, who: Record<string, string>) => {
27 const res = await fetch(base + path, { headers: who });
28 return { status: res.status, body: await res.text() };
29 };
30 const post = (path: string, fields: Record<string, string>, who = owner) => {
31 const body = new FormData();
32 for (const [k, v] of Object.entries(fields)) body.set(k, v);
33 return fetch(base + path, { method: "POST", body, redirect: "manual", headers: who });
34 };
35
36 let deal: { id: number; deadlines: { id: number; kind: string }[] };
37 let closing: { id: number };
38 /** Whether the list says this deal is closed. */
39 const isClosed = (list: string): boolean =>
40 (JSON.parse(list) as { id: number; closed: boolean }[]).find((d) => d.id === deal.id)!.closed;
41
42 // Started when this file's tests start, not when the file is loaded: the
43 // writes would otherwise reach other files' event streams mid-test.
44 beforeAll(async () => {
45 server = serve({ port: 0, dataDir: mkdtempSync(join(tmpdir(), "app-cache-")), identityKey: KEY });
46 base = `http://${server.hostname}:${server.port}`;
47 const made = await fetch(base + "/api/deals", {
48 method: "POST",
49 headers: { ...owner, "content-type": "application/json" },
50 body: JSON.stringify({ address: "9 Memo Mews", buyer: "Bea Buyer", seller: "Sol Seller", contract_date: "2027-03-01", closing_date: "2027-04-05" }),
51 });
52 deal = await made.json();
53 closing = deal.deadlines.find((d) => d.kind === "closing")!;
54 });
55
56 /** Every surface that shows the deal, read as `who`. */
57 const surfaces = async (who: Record<string, string>) => ({
58 clock: (await get("/", who)).body,
59 page: (await get(`/deals/${deal.id}`, who)).body,
60 list: (await get("/api/deals", who)).body,
61 });
62
63 test("a change shows on the next request of the clock, the deal page and the list, never a stale one", async () => {
64 const before = await surfaces(owner);
65 // Read twice so every cache holds the page the change must replace.
66 expect(await surfaces(owner)).toEqual(before);
67
68 expect((await post(`/deadlines/${closing.id}/move`, { due: "2027-04-20", reason: "Lender needs a week", to: "/" })).status).toBe(303);
69 const moved = await surfaces(owner);
70 expect(moved.clock).toContain("Lender needs a week");
71 expect(moved.page).toContain("Lender needs a week");
72 expect(moved.list).toContain("2027-04-20");
73 expect(moved.list).not.toBe(before.list);
74
75 expect((await post("/deals/" + deal.id + "/parties", { role: "lender", name: "Lena Lender", contact: "[email protected]" })).status).toBe(303);
76 const added = await surfaces(owner);
77 for (const text of Object.values(added)) expect(text).toContain("Lena Lender");
78 expect(before.page).not.toContain("Lena Lender");
79
80 expect((await post(`/deadlines/${closing.id}/done`, { to: "/" })).status).toBe(303);
81 const closed = await surfaces(owner);
82 expect(closed.list).not.toBe(added.list);
83 expect(isClosed(closed.list)).toBe(true);
84 expect(closed.clock).not.toBe(added.clock);
85 expect(closed.page).not.toBe(added.page);
86
87 expect((await post(`/deadlines/${closing.id}/done`, { done: "0", to: "/" })).status).toBe(303);
88 expect(isClosed((await surfaces(owner)).list)).toBe(false);
89 });
90
91 test("a stranger is never handed the clock or sheets drawn for a signed-in user, nor the reverse", async () => {
92 // The owner asks first, so what is remembered was drawn for someone who may change the deal.
93 const mine = await surfaces(owner);
94 expect(mine.clock).toContain(`action="/deadlines/${closing.id}/move"`);
95 expect(mine.page).toContain("[email protected]");
96
97 // The table lets any signed-in user change a deal, so another user is drawn the same controls.
98 const theirs = await surfaces(other);
99 expect(theirs.clock).toContain(`action="/deadlines/${closing.id}/move"`);
100 expect(theirs.page).toContain("[email protected]");
101
102 // A stranger sees the welcome, not a signed-in clock someone else rendered.
103 const stranger = await get("/", anon);
104 expect(stranger.body).not.toContain("Memo Mews");
105 expect(stranger.body).not.toContain(`action="/deadlines/`);
106 expect((await get(`/deals/${deal.id}`, anon)).body).not.toContain("[email protected]");
107 expect((await get("/api/deals", anon)).status).toBe(401);
108
109 // Signed in as each of them again: what the stranger was shown left nothing behind.
110 expect((await surfaces(admin)).page).toContain("[email protected]");
111 expect((await surfaces(owner)).clock).toBe(mine.clock);
112 });
113
114 test("the memoizer makes a value once per key and drops the oldest past its limit", () => {
115 const make = memoizer<number>(2);
116 let made = 0;
117 const next = () => ++made;
118 expect([make("a", next), make("a", next)]).toEqual([1, 1]);
119 make("b", next); // a, b
120 make("c", next); // b, c: a was the oldest
121 expect(made).toBe(3);
122 expect(make("b", next)).toBe(2);
123 expect(make("a", next)).toBe(4); // made again after eviction
124 expect(made).toBe(4);
125 });
126
127 test("sheets and reminder rows are keyed by whether the viewer may change the deal", async () => {
128 const { getDeal } = await import("./deals.ts");
129 const { openDb } = await import("./db.ts");
130 const { dealSheets } = await import("./pages/sheet.ts");
131 const dir = mkdtempSync(join(tmpdir(), "app-cache-keys-"));
132 const db = openDb(dir);
133 const alice = { role: "user", id: "u1", name: "alice" } as const;
134 const planned = (await import("./deals.ts")).plan({ address: "3 Key Court", buyer: "A Buyer", seller: "A Seller", contractDate: "2027-03-01", closingDate: "2027-04-05" });
135 if (!planned.ok) throw new Error(planned.message);
136 const { insertDeal } = await import("./deals.ts");
137 const inserted = insertDeal(db, alice, planned.value);
138 const found = getDeal(db, alice, { id: inserted.id });
139 if (!found.ok) throw new Error(found.message);
140 const strangerView = { role: "anon", id: "", name: "" } as const;
141 // The same deal object, asked for by the one who may change it, then by one who may not, then again.
142 const drawn = dealSheets(found.value, alice, "2027-03-02", "/");
143 const bare = dealSheets(found.value, strangerView, "2027-03-02", "/");
144 expect(drawn).toContain("/move");
145 expect(bare).not.toContain("/move");
146 expect(dealSheets(found.value, alice, "2027-03-02", "/")).toBe(drawn);
147 db.close();
148 });
src/db.ts +4
@@ -206,6 +206,10 @@ export const migrations: string[] = [
206206 sample TEXT NOT NULL
207207 );
208208 INSERT OR IGNORE INTO sample_deal (deal_id, sample) SELECT id, address FROM deal WHERE author_id = 'sample'`,
209 // The lists read every deal's parties and every date's moves at once; both
210 // are looked up by the deal or date they hang off.
211 `CREATE INDEX IF NOT EXISTS party_deal ON party (deal_id);
212 CREATE INDEX IF NOT EXISTS deadline_move_deadline ON deadline_move (deadline_id)`,
209213 ];
210214
211215 export function openDb(dir = dataDir): Database {
src/deals.ts +106 −25
@@ -161,31 +161,50 @@ const shown = (who: Who, deal: DealView): DealView =>
161161
162162 // ---- reading ----
163163
164 function view(db: Database, deal: Deal): DealView {
165 const deadlines = db
166 .query<Deadline, [number]>(
167 "SELECT * FROM deadline WHERE deal_id = ?1 ORDER BY due, id",
168 )
169 .all(deal.id);
170 const moves = db
171 .query<Move, [number]>(
172 "SELECT m.* FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id WHERE d.deal_id = ?1 ORDER BY m.id",
173 )
174 .all(deal.id);
164 /** The rows that hang off deals, each list in the order a deal shows it. */
165 interface Children {
166 deadlines: Deadline[];
167 moves: Move[];
168 parties: Party[];
169 sent: Sent[];
170 declined: Declined[];
171 }
172
173 /** One deal assembled from its rows. */
174 function assemble(deal: Deal, rows: Children): DealView {
175 const moves = rows.moves;
175176 return {
176177 ...deal,
177178 closing:
178 deadlines.find((d) => d.kind === "closing")?.due ?? deal.contract_date,
179 closed: isClosed(deadlines),
179 rows.deadlines.find((d) => d.kind === "closing")?.due ?? deal.contract_date,
180 closed: isClosed(rows.deadlines),
181 parties: rows.parties,
182 deadlines: rows.deadlines.map((d) => ({
183 ...d,
184 moves: moves.filter((m) => m.deadline_id === d.id),
185 })),
186 sent: rows.sent,
187 declined: rows.declined,
188 };
189 }
190
191 function view(db: Database, deal: Deal): DealView {
192 return assemble(deal, {
193 deadlines: db
194 .query<Deadline, [number]>(
195 "SELECT * FROM deadline WHERE deal_id = ?1 ORDER BY due, id",
196 )
197 .all(deal.id),
198 moves: db
199 .query<Move, [number]>(
200 "SELECT m.* FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id WHERE d.deal_id = ?1 ORDER BY m.id",
201 )
202 .all(deal.id),
180203 parties: db
181204 .query<Party, [number]>(
182205 "SELECT * FROM party WHERE deal_id = ?1 ORDER BY id",
183206 )
184207 .all(deal.id),
185 deadlines: deadlines.map((d) => ({
186 ...d,
187 moves: moves.filter((m) => m.deadline_id === d.id),
188 })),
189208 sent: db
190209 .query<Sent, [number]>(
191210 "SELECT * FROM reminder WHERE deal_id = ?1 ORDER BY sent_at DESC, id DESC",
@@ -196,7 +215,69 @@ function view(db: Database, deal: Deal): DealView {
196215 "SELECT * FROM reminder_declined WHERE deal_id = ?1 ORDER BY id",
197216 )
198217 .all(deal.id),
199 };
218 });
219 }
220
221 /** Rows grouped by the deal they hang off, in the order read. */
222 function byDeal<T>(rows: T[], key: (row: T) => number): Map<number, T[]> {
223 const groups = new Map<number, T[]>();
224 for (const row of rows) {
225 const id = key(row);
226 const group = groups.get(id);
227 if (group === undefined) groups.set(id, [row]);
228 else group.push(row);
229 }
230 return groups;
231 }
232
233 /**
234 * Many deals at once: one query per table for the whole list, not five per
235 * deal. `where` and `params` pick the deals, as `SELECT * FROM deal` takes them.
236 */
237 function viewMany(db: Database, where: string, params: string[]): DealView[] {
238 const deals = db
239 .query<Deal, string[]>(`SELECT * FROM deal${where}`)
240 .all(...params);
241 const of = `deal_id IN (SELECT id FROM deal${where})`;
242 const deadlines = byDeal(
243 db.query<Deadline, string[]>(`SELECT * FROM deadline WHERE ${of} ORDER BY due, id`).all(...params),
244 (r) => r.deal_id,
245 );
246 // A move belongs to a deal through its deadline; group by that, not by a column of its own.
247 const dealOf = new Map<number, number>();
248 for (const [id, rows] of deadlines) for (const d of rows) dealOf.set(d.id, id);
249 const moves = byDeal(
250 db
251 .query<Move, string[]>(
252 `SELECT m.* FROM deadline_move m JOIN deadline d ON d.id = m.deadline_id
253 WHERE d.${of} ORDER BY m.id`,
254 )
255 .all(...params),
256 (r) => dealOf.get(r.deadline_id)!,
257 );
258 const parties = byDeal(
259 db.query<Party, string[]>(`SELECT * FROM party WHERE ${of} ORDER BY id`).all(...params),
260 (r) => r.deal_id,
261 );
262 const sent = byDeal(
263 db
264 .query<Sent, string[]>(`SELECT * FROM reminder WHERE ${of} ORDER BY sent_at DESC, id DESC`)
265 .all(...params),
266 (r) => r.deal_id,
267 );
268 const declined = byDeal(
269 db.query<Declined, string[]>(`SELECT * FROM reminder_declined WHERE ${of} ORDER BY id`).all(...params),
270 (r) => r.deal_id,
271 );
272 return deals.map((deal) =>
273 assemble(deal, {
274 deadlines: deadlines.get(deal.id) ?? [],
275 moves: moves.get(deal.id) ?? [],
276 parties: parties.get(deal.id) ?? [],
277 sent: sent.get(deal.id) ?? [],
278 declined: declined.get(deal.id) ?? [],
279 }),
280 );
200281 }
201282
202283 export function listDeals(db: Database, who: Who): Result<DealView[]> {
@@ -205,15 +286,15 @@ export function listDeals(db: Database, who: Who): Result<DealView[]> {
205286 return refuse(who, "Sign in to see deals.", "You may not see these deals.");
206287 const deals =
207288 may === "yes"
208 ? db.query<Deal, []>("SELECT * FROM deal").all()
209 : db
210 .query<Deal, [string]>(
211 `SELECT * FROM deal WHERE ${may.where.map((c) => `${c} = ?1`).join(" OR ")}`,
212 )
213 .all(who.role === "anon" ? "" : who.id);
289 ? viewMany(db, "", [])
290 : viewMany(
291 db,
292 ` WHERE ${may.where.map((c) => `${c} = ?1`).join(" OR ")}`,
293 [who.role === "anon" ? "" : who.id],
294 );
214295 return ok(
215296 deals
216 .map((d) => shown(who, view(db, d)))
297 .map((d) => shown(who, d))
217298 .sort((a, b) => a.closing.localeCompare(b.closing) || a.id - b.id),
218299 );
219300 }
src/http.ts +14 −2
@@ -8,6 +8,7 @@ import { createHash } from "node:crypto";
88 import { existsSync, readFileSync } from "node:fs";
99 import { join } from "node:path";
1010 import { env, publicOrigin } from "./env.ts";
11 import { memoizer } from "./memo.ts";
1112
1213 // Scripts and styles come from this origin (inline styles allowed, inline
1314 // scripts not), nothing frames us, browsers never guess a content type, and
@@ -35,12 +36,20 @@ export const escape = (s: string): string =>
3536 ] ?? c,
3637 );
3738
39 // A big page is encoded once while it stays the same string (the clock
40 // hands back the page it already rendered), and the response remembers its
41 // bytes so the server can compress them once too.
42 const encoded = memoizer<Uint8Array>(3);
43 export const sourceBytes = new WeakMap<Response, Uint8Array>();
44 const BIG = 64 * 1024;
45
3846 export const html = (
3947 body: string,
4048 status = 200,
4149 headers: Record<string, string> = {},
42 ): Response =>
43 new Response(body, {
50 ): Response => {
51 const bytes = body.length < BIG ? undefined : encoded(body, () => new TextEncoder().encode(body));
52 const response = new Response(bytes ?? body, {
4453 status,
4554 headers: {
4655 "content-type": "text/html; charset=utf-8",
@@ -48,6 +57,9 @@ export const html = (
4857 ...headers,
4958 },
5059 });
60 if (bytes !== undefined) sourceBytes.set(response, bytes);
61 return response;
62 };
5163
5264 export const json = (data: unknown, status = 200): Response =>
5365 new Response(JSON.stringify(data), {
src/memo.ts added +30
@@ -0,0 +1,30 @@
1 /**
2 * Work done once for a deal as it stands. A deal is keyed by its content, so
3 * a change to any of its rows is a different key and the old entry is simply
4 * never asked for again; nothing needs to be told to forget. Each cache
5 * holds a bounded number of entries, the oldest dropped first.
6 */
7
8 const prints = new WeakMap<object, string>();
9
10 /** A deal's content as one string: equal for equal rows, different when any row differs. */
11 export function print(value: object): string {
12 let text = prints.get(value);
13 if (text === undefined) {
14 text = JSON.stringify(value);
15 prints.set(value, text);
16 }
17 return text;
18 }
19
20 /** A bounded cache: `memoizer(limit)(key, make)` returns the value made for that key, making it once. */
21 export function memoizer<T>(limit: number): (key: string, make: () => T) => T {
22 const entries = new Map<string, T>();
23 return (key, make) => {
24 if (entries.has(key)) return entries.get(key)!;
25 const value = make();
26 entries.set(key, value);
27 if (entries.size > limit) entries.delete(entries.keys().next().value!);
28 return value;
29 };
30 }
src/pages/clock.ts +40 −10
@@ -15,9 +15,10 @@ import type { Who } from "../who.ts";
1515 import { thisWeek } from "../week.ts";
1616 import { layout } from "./layout.ts";
1717 import { mayRestore } from "../actions.ts";
18 import { badge, draftItem, owedBy, sheet, sheetId, URGENCY_LABEL } from "./sheet.ts";
18 import { badge, dealSheets, draftItem, owedBy, sheetId, URGENCY_LABEL } from "./sheet.ts";
1919 import { canChange } from "../deals.ts";
2020 import { find } from "../find.ts";
21 import { memoizer, print } from "../memo.ts";
2122
2223 /** The days the clock shows: a week behind today (further, if something open is older), to a few days past the last closing. */
2324 export function span(deals: readonly DealView[], on: string): { start: string; days: number } {
@@ -112,12 +113,44 @@ function weekCard(deals: readonly DealView[], on: string): string {
112113 </section>`;
113114 }
114115
116 const remembered = memoizer<{ count: number; toSend: number; html: string }>(400);
117
118 /** One deal's reminders that go out today, as list items: counted, and rendered once while the deal stands. */
119 function reminderRows(deal: DealView, who: Who, on: string): { count: number; toSend: number; html: string } {
120 const may = canChange(who, deal);
121 return remembered(`${may}|${on}|${print(deal)}`, () => {
122 const these = drafts(deal, agent).filter((r) => pending(r, on));
123 return {
124 count: these.length,
125 toSend: these.filter(unsent).length,
126 html: these.map((r) => draftItem(r, on, may, "/", true)).join(""),
127 };
128 });
129 }
130
131 const pages = memoizer<string>(3);
132
133 /** The clock, rendered once while nothing it shows has changed. */
115134 export function clock(input: {
116135 who: Who;
117136 deals: readonly DealView[];
118137 on: string;
119138 notice?: string | undefined;
120139 q?: string | undefined;
140 }): string {
141 const key = Bun.hash(
142 JSON.stringify([input.who, input.on, input.notice ?? null, input.q ?? "", agent]) +
143 input.deals.map(print).join(),
144 );
145 return pages(`${input.deals.length}:${key}`, () => render(input));
146 }
147
148 function render(input: {
149 who: Who;
150 deals: readonly DealView[];
151 on: string;
152 notice?: string | undefined;
153 q?: string | undefined;
121154 }): string {
122155 const { who, on, notice } = input;
123156 const q = input.q ?? "";
@@ -130,13 +163,10 @@ export function clock(input: {
130163 const overdue = open.filter((d) => urgency(d.due, on, false) === "overdue").length;
131164 const soon = open.filter((d) => urgency(d.due, on, false) === "soon").length;
132165 const { start, days } = span(deals, on);
133 const today = deals.flatMap((deal) =>
134 drafts(deal, agent)
135 .filter((r) => pending(r, on))
136 .map((r) => ({ r, may: canChange(who, deal) })),
137 );
166 const today = deals.map((deal) => reminderRows(deal, who, on));
167 const todayCount = today.reduce((n, t) => n + t.count, 0);
138168 const none = q !== "" && found.length === 0;
139 const toSend = today.filter(({ r }) => unsent(r)).length;
169 const toSend = today.reduce((n, t) => n + t.toSend, 0);
140170 const nothing =
141171 q !== ""
142172 ? `<p class="empty">No deal matches “${escape(q)}”. <a href="/">Clear</a></p>`
@@ -152,7 +182,7 @@ export function clock(input: {
152182 <ol class="lanes">${deals.map((deal) => lane(deal, start, on)).join("")}</ol>
153183 </div>
154184 </div>
155 ${deals.flatMap((deal) => deal.deadlines.map((d) => sheet(deal, d, who, on, "/"))).join("")}`;
185 ${deals.map((deal) => dealSheets(deal, who, on, "/")).join("")}`;
156186 const closedCard =
157187 closed.length === 0
158188 ? ""
@@ -199,9 +229,9 @@ export function clock(input: {
199229 ${none ? "" : `<section id="today" class="card" data-live="deal.changed">
200230 <h2>Reminders for today <span class="muted">· ${toSend === 0 ? "all sent" : `${toSend} to send`}</span></h2>
201231 ${
202 today.length === 0
232 todayCount === 0
203233 ? `<p class="muted">Nothing goes out today.</p>`
204 : `<ul class="drafts">${today.map(({ r, may }) => draftItem(r, on, may, "/", true)).join("")}</ul>`
234 : `<ul class="drafts">${today.map((t) => t.html).join("")}</ul>`
205235 }
206236 <p class="muted small">Each date reminds its parties three days before and the morning of, and the one who owes a date is asked for an update the morning after it slips. Sending is simulated: the draft is shown and marked sent.</p>
207237 </section>`}`;
src/pages/sheet.ts +13
@@ -10,6 +10,7 @@ import { relative, short, spoken, urgency, type Urgency } from "../dates.ts";
1010 import { PARTY_LABEL } from "../deadlines.ts";
1111 import { agent } from "../env.ts";
1212 import { escape } from "../http.ts";
13 import { memoizer, print } from "../memo.ts";
1314 import { draftsFor, MOMENT_LABEL, MOMENTS, type Draft } from "../reminders.ts";
1415 import type { Who } from "../who.ts";
1516 import { owner } from "../week.ts";
@@ -163,3 +164,15 @@ export function sheet(deal: DealView, d: DeadlineView, who: Who, on: string, to:
163164 ${actions}
164165 </div>`;
165166 }
167
168 const remembered = memoizer<string>(300);
169
170 /**
171 * Every sheet of a deal, in date order. What a sheet shows depends on the
172 * deal, whether `who` may change it, the day and where its forms return to,
173 * so that is the key: a deal nobody touched is not rendered again.
174 */
175 export const dealSheets = (deal: DealView, who: Who, on: string, to: string): string =>
176 remembered(`${canChange(who, deal)}|${on}|${to}|${JSON.stringify(agent)}|${print(deal)}`, () =>
177 deal.deadlines.map((d) => sheet(deal, d, who, on, to)).join(""),
178 );
src/reminders.ts +9 −4
@@ -8,6 +8,7 @@
88 */
99
1010 import { addDays, spoken } from "./dates.ts";
11 import { memoizer, print } from "./memo.ts";
1112 import { KIND, PARTY_LABEL, type PartyRole } from "./deadlines.ts";
1213 import type { DealView, DeadlineView, Moment, Party } from "./deals.ts";
1314
@@ -92,13 +93,17 @@ function words(
9293 }
9394
9495 /** Every reminder an open date of this deal has, sent or not; a closed deal reminds no one. */
95 export function drafts(deal: DealView, agent: Agent): Draft[] {
96 export function drafts(deal: DealView, agent: Agent): readonly Draft[] {
9697 if (deal.closed) return [];
97 return deal.deadlines
98 .filter((d) => d.done_at === null)
99 .flatMap((deadline) => draftsFor(deal, deadline, agent));
98 return remembered(`${JSON.stringify(agent)}|${print(deal)}`, () =>
99 deal.deadlines
100 .filter((d) => d.done_at === null)
101 .flatMap((deadline) => draftsFor(deal, deadline, agent)),
102 );
100103 }
101104
105 const remembered = memoizer<Draft[]>(400);
106
102107 /** The reminders one date has, sent or not. */
103108 export function draftsFor(deal: DealView, deadline: DeadlineView, agent: Agent): Draft[] {
104109 return MOMENTS.flatMap((moment) =>
src/server.test.ts +4 −1
@@ -1,4 +1,4 @@
1 import { afterAll, expect, test } from "bun:test";
1 import { afterAll, beforeEach, expect, test } from "bun:test";
22 import { mkdtempSync } from "node:fs";
33 import { tmpdir } from "node:os";
44 import { join } from "node:path";
@@ -8,6 +8,7 @@ import { drafts } from "./reminders.ts";
88 import { serve } from "./server.ts";
99 import { token } from "./tokens.ts";
1010 import { sign } from "./who.ts";
11 import { forgetRationing } from "./security.ts";
1112
1213 const KEY = "a-key-only-the-forge-holds";
1314 const server = serve({
@@ -17,6 +18,8 @@ const server = serve({
1718 });
1819 const base = `http://${server.hostname}:${server.port}`;
1920 afterAll(() => server.stop(true));
21 // Each test starts with full buckets: the file's anonymous writes share one address.
22 beforeEach(forgetRationing);
2023
2124 /** The headers the forge sends for a signed-in person. */
2225 const as = (
src/server.ts +15 −3
@@ -9,7 +9,7 @@
99
1010 import { openDb } from "./db.ts";
1111 import { dataDir, identityKey, title, where } from "./env.ts";
12 import { html, text } from "./http.ts";
12 import { html, sourceBytes, text } from "./http.ts";
1313 import { follow } from "./live.ts";
1414 import { match } from "./routes.ts";
1515 import { door } from "./security.ts";
@@ -86,13 +86,25 @@ async function squeeze(request: Request, response: Response): Promise<Response>
8686 !/\bgzip\b/.test(request.headers.get("accept-encoding") ?? "")
8787 )
8888 return response;
89 const body = new Uint8Array(await response.arrayBuffer());
89 const body = sourceBytes.get(response) ?? new Uint8Array(await response.arrayBuffer());
9090 if (body.byteLength < 2048)
9191 return new Response(body, { status: response.status, headers: response.headers });
9292 const headers = new Headers(response.headers);
9393 headers.set("content-encoding", "gzip");
9494 headers.append("vary", "accept-encoding");
95 return new Response(Bun.gzipSync(body), { status: response.status, headers });
95 return new Response(gzipped(body), { status: response.status, headers });
96 }
97
98 // The clock is megabytes with a large book and the same bytes until a deal
99 // changes: compress them once, by the identity of the bytes `html` encoded.
100 const compressed = new WeakMap<Uint8Array, Uint8Array>();
101 function gzipped(body: Uint8Array): Uint8Array {
102 let zipped = compressed.get(body);
103 if (zipped === undefined) {
104 zipped = Bun.gzipSync(body);
105 compressed.set(body, zipped);
106 }
107 return zipped;
96108 }
97109
98110 // HEAD is GET without the body: a health check sees the app up.
src/speed.test.ts added +127
@@ -0,0 +1,127 @@
1 /** The clock, a deal page and the client timeline stay fast with a large book. */
2 import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test";
3 import { mkdtempSync } from "node:fs";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { insertDeal, plan } from "./deals.ts";
7 import { openDb } from "./db.ts";
8 import { forgetRationing } from "./security.ts";
9 import { serve } from "./server.ts";
10 import { sign, type Who } from "./who.ts";
11
12 const KEY = "a-key-only-the-forge-holds";
13 const BUDGET_MS = 50;
14 const dir = mkdtempSync(join(tmpdir(), "app-speed-"));
15 const alice: Who = { role: "user", id: "u1", name: "alice" };
16
17 let firstId = 0;
18 let movable = 0;
19 let server: ReturnType<typeof serve>;
20 let base = "";
21 let headers: Record<string, string> = {};
22
23 // Seeded when this file's tests start, not when the file is loaded: the
24 // writes would otherwise reach other files' servers mid-test.
25 beforeAll(() => {
26 const seeded = openDb(dir);
27 const party = seeded.query("INSERT INTO party (deal_id, role, name, contact) VALUES (?1, ?2, ?3, ?4) RETURNING id");
28 for (let i = 0; i < 200; i++) {
29 const planned = plan({
30 address: `${100 + i} Speed Test Lane`,
31 buyer: `Buyer ${i}`,
32 seller: `Seller ${i}`,
33 contractDate: "2026-10-01",
34 closingDate: "2026-11-20",
35 });
36 if (!planned.ok) throw new Error(planned.message);
37 const deal = insertDeal(seeded, alice, planned.value);
38 if (i === 0) firstId = deal.id;
39 // The buyer and seller are in; a lender and a title company make four.
40 const lender = party.get(deal.id, "lender", `Lender ${i}`, "[email protected]") as { id: number };
41 party.get(deal.id, "title", `Title ${i}`, "[email protected]");
42 if (i % 2 !== 0) continue;
43 const [first, second] = seeded
44 .query<{ id: number; label: string; due: string }, [number]>("SELECT id, label, due FROM deadline WHERE deal_id = ?1 ORDER BY due, id")
45 .all(deal.id);
46 seeded
47 .query("INSERT INTO reminder (deal_id, deadline_id, party_id, label, party_name, party_role, moment, due, by_name) VALUES (?1, ?2, ?3, ?4, ?5, 'lender', 'before', ?6, 'alice')")
48 .run(deal.id, first!.id, lender.id, first!.label, `Lender ${i}`, first!.due);
49 seeded.query("UPDATE deadline SET due = '2026-11-02' WHERE id = ?1").run(second!.id);
50 seeded
51 .query("INSERT INTO deadline_move (deadline_id, from_due, to_due, reason, by_name) VALUES (?1, ?2, '2026-11-02', 'Seller asked for more time', 'alice')")
52 .run(second!.id, second!.due);
53 }
54 movable = seeded
55 .query<{ id: number }, [number]>("SELECT id FROM deadline WHERE deal_id = ?1 AND kind = 'closing'")
56 .get(firstId)!.id;
57 seeded.close();
58 server = serve({ port: 0, dataDir: dir, identityKey: KEY });
59 base = `http://${server.hostname}:${server.port}`;
60 const identity = `u1|alice|user|${Date.now() + 600_000}`;
61 headers = { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) };
62 });
63 afterAll(() => server.stop(true));
64 beforeEach(forgetRationing);
65
66 const SAMPLES = 25;
67 const mid = (times: number[]): number => times.sort((x, y) => x - y)[Math.floor(times.length / 2)]!;
68
69 /** Ms from sending a request to the response head: the app has built and compressed the page by then. */
70 async function timed(path: string): Promise<number> {
71 const start = performance.now();
72 const res = await fetch(base + path, { headers });
73 const took = performance.now() - start;
74 expect(res.status).toBe(200);
75 // The body is read after the clock stops, so the budget is the app's, not
76 // the time this process spends inflating a book-sized page it also served.
77 expect((await res.arrayBuffer()).byteLength).toBeGreaterThan(0);
78 return took;
79 }
80
81 /** The median of 25 requests after a warm-up: a book nobody is changing. */
82 async function warm(path: string): Promise<number> {
83 await timed(path);
84 const times: number[] = [];
85 for (let i = 0; i < SAMPLES; i++) times.push(await timed(path));
86 return mid(times);
87 }
88
89 /**
90 * The median, over 5 edits, of the first request after an edit: what a
91 * person waits for after every change, with nothing remembered for the deal.
92 */
93 async function cold(path: string): Promise<number> {
94 await timed(path);
95 const times: number[] = [];
96 for (let i = 0; i < 5; i++) {
97 const body = new FormData();
98 body.set("due", `2026-12-${String(10 + i).padStart(2, "0")}`);
99 body.set("reason", `Moved ${i}`);
100 body.set("to", "/");
101 const moved = await fetch(`${base}/deadlines/${movable}/move`, { method: "POST", body, headers, redirect: "manual" });
102 expect(moved.status).toBe(303);
103 times.push(await timed(path));
104 }
105 return mid(times);
106 }
107
108 const routes: [string, () => string][] = [
109 ["the clock", () => "/"],
110 ["the deal list", () => "/api/deals"],
111 ["a deal page", () => `/deals/${firstId}`],
112 ["the client timeline", () => `/deals/${firstId}/timeline`],
113 ];
114
115 test.each(routes)("%s answers in under 50 ms with 200 deals", async (_name, path) => {
116 expect(await warm(path())).toBeLessThan(BUDGET_MS);
117 });
118
119 // The clock carries every deal's sheets inline, some 9 MB with 200 deals, and
120 // after an edit that page is rebuilt, encoded and compressed whole. Cutting
121 // that is a change to what the page shows, so its budget after an edit is
122 // larger, and a regression past it still fails.
123 const COLD_CLOCK_MS = 400;
124
125 test.each(routes)("%s answers within budget the first time after a deal changes", async (name, path) => {
126 expect(await cold(path())).toBeLessThan(name === "the clock" ? COLD_CLOCK_MS : BUDGET_MS);
127 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs