Public: anyone can read this; the crew can change it.
Every drafted message for every sample deal is checked for leaks: no undefined, null, [object], unfilled braces, double spaces or a missing sign-off #274
Check · Review · Land · Live done — Landed and live in production.
mason/deal-clock:mason/270-every-drafted-message-for-every-sample-d → main 1 commit fork
1 file
changed +68
−0
src/leaks.test.ts added +68
| @@ -0,0 +1,68 @@ | ||
| 1 | /** A realtor reading Review must never meet a half-filled message: every draft of every sample deal is checked for leaks. */ | |
| 2 | import { 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 { openDb } from "./db.ts"; | |
| 7 | import { createDeal, listDeals } from "./deals.ts"; | |
| 8 | import { anniversaryDrafts, checkinDrafts, drafts, signOff, statusDrafts, type Draft } from "./reminders.ts"; | |
| 9 | import { seed } from "./sample.ts"; | |
| 10 | import type { Who } from "./who.ts"; | |
| 11 | ||
| 12 | const alice: Who = { role: "user", id: "u1", name: "alice" }; | |
| 13 | const agents = [ | |
| 14 | { name: "Dana Reyes", brokerage: "Harbor & Pine", phone: "(555) 010-0142", email: "[email protected]" }, | |
| 15 | { name: "Dana Reyes", brokerage: "Harbor & Pine", phone: "(555) 010-0142", email: "[email protected]", line: "Thanks for trusting me with this." }, | |
| 16 | ]; | |
| 17 | const LEAKS: readonly [string, RegExp][] = [ | |
| 18 | ["undefined", /undefined/], | |
| 19 | ["null", /\bnull\b/], | |
| 20 | ["NaN", /NaN/], | |
| 21 | ["[object", /\[object/], | |
| 22 | ["{{", /\{\{|\}\}/], | |
| 23 | ["double space", /\S {2,}\S/], | |
| 24 | ]; | |
| 25 | ||
| 26 | function leaks(d: Draft, agent: (typeof agents)[number]): string[] { | |
| 27 | const found: string[] = []; | |
| 28 | for (const [field, text] of [["subject", d.subject], ["body", d.body]] as const) | |
| 29 | for (const [name, re] of LEAKS) if (re.test(text)) found.push(`${d.moment} ${d.label} ${d.party_name}: ${field} has ${name}`); | |
| 30 | if (d.party_name.trim() === "") found.push(`${d.moment} ${d.label}: empty party name`); | |
| 31 | if (d.subject.trim() === "") found.push(`${d.moment} ${d.label}: empty subject`); | |
| 32 | if (!d.body.endsWith(signOff(agent))) found.push(`${d.moment} ${d.label} ${d.party_name}: body does not end with the sign-off`); | |
| 33 | return found; | |
| 34 | } | |
| 35 | ||
| 36 | function everyDraft(db: ReturnType<typeof openDb>, agent: (typeof agents)[number]): Draft[] { | |
| 37 | return listDeals(db, alice).value!.flatMap((deal) => [ | |
| 38 | ...drafts(deal, agent), | |
| 39 | ...statusDrafts(deal, agent), | |
| 40 | ...checkinDrafts(deal, agent), | |
| 41 | ...anniversaryDrafts(deal, agent), | |
| 42 | ]); | |
| 43 | } | |
| 44 | ||
| 45 | test("no draft of any sample deal leaks a placeholder, a double space or a missing sign-off", () => { | |
| 46 | const db = openDb(mkdtempSync(join(tmpdir(), "app-leaks-"))); | |
| 47 | seed(db); | |
| 48 | for (const agent of agents) { | |
| 49 | const all = everyDraft(db, agent); | |
| 50 | expect(all.length).toBeGreaterThan(10); | |
| 51 | const moments = new Set(all.map((d) => d.moment)); | |
| 52 | for (const m of ["before", "day_of", "late", "moved", "checkin", "anniversary"]) expect(moments.has(m as Draft["moment"])).toBe(true); | |
| 53 | expect(all.flatMap((d) => leaks(d, agent))).toEqual([]); | |
| 54 | } | |
| 55 | }); | |
| 56 | ||
| 57 | test("a party with no contact and a date with no time or place leaves no leak", () => { | |
| 58 | const db = openDb(mkdtempSync(join(tmpdir(), "app-leaks-bare-"))); | |
| 59 | const made = createDeal(db, alice, { openAnyway: true, address: "9 Bare Lane", buyer: "Ann Buyer", seller: "Sam Seller", contractDate: "2026-09-01", closingDate: "2026-12-01", side: "both", salePrice: 400000 }); | |
| 60 | if (!made.ok) throw new Error(made.message); | |
| 61 | db.query("UPDATE party SET contact = '', email = '', phone = '' WHERE deal_id = ?1 AND role = 'seller'").run(made.value.id); | |
| 62 | db.query("UPDATE party SET email = '[email protected]', contact = '[email protected]' WHERE deal_id = ?1 AND role = 'buyer'").run(made.value.id); | |
| 63 | for (const agent of agents) { | |
| 64 | const all = everyDraft(db, agent); | |
| 65 | expect(all.some((d) => d.moment === "status")).toBe(true); | |
| 66 | expect(all.flatMap((d) => leaks(d, agent))).toEqual([]); | |
| 67 | } | |
| 68 | }); |
new above
offline