GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — cloudmail/test/draft-reply-threading.test.ts at main · cloudmail-hq/cloudmail Expand file tree/ Copy pathdraft-reply-threading.test.tsMore file actions 413 lines (358 loc) · 14 KB / Copy pathdraft-reply-threading.test.tsFile metadata and controls413 lines (358 loc) · 14 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 /** * Resolving what a draft is replying to. * * `emails.in_reply_to` names the parent, but not in one consistent form. Four * write paths store the parent's RFC `Message-ID` — inbound delivery, the reply * and forward routes, the scheduled-send path and the agent's auto-reply — and * three store our own row UUID: the composer's draft saves, `draft_reply` and * the auto-drafter. Every consumer that resolved it with a row-id lookup alone * therefore worked or failed depending on which path had written the row. * * The reproducible case is Undo Send. Cancelling drops the queued message back * into Drafts, and the row it lands as carries an RFC-form `in_reply_to`, so: * * - `resolveOriginalEmail` missed the parent and returned the *draft*, which * made `buildReferencesChain` rebuild the ancestry from a conversation of * one — the reply arrived at the far end detached from its thread. * - `resolveThreadedSend` 404'd, which the composer surfaced as "Original * email not found". The send simply failed. * * These failures are near-invisible from inside the app: our own Sent folder * still groups by `thread_id`, so the split only shows up in the recipient's * client. Hence tests at the resolution boundary rather than on the UI. */ import { DatabaseSync } from "node:sqlite"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { buildReferencesChain, resolveOriginalEmail, resolveThreadedSend, } from "../workers/lib/email-helpers"; import { findIdByMessageId } from "../workers/durableObject/references"; import type { EmailFull } from "../workers/lib/schemas"; import { findThreadParent, replyTargetForDraft, } from "../app/lib/thread-parent"; /** A received message: `message_id` is the counterparty's, `id` is ours. */ const PARENT: EmailFull = { id: "row-parent", subject: "Q3 budget", cc: null, bcc: null, date: "2026-08-10T09:30:00.000Z", read: true, starred: false, folder_id: "inbox", body: "<p>Numbers attached.</p>", }; /** * A draft as Undo Send leaves it: demoted out of Scheduled, naming its parent by * Message-ID because the send path wrote the row, not the composer. */ const DEMOTED_DRAFT: EmailFull = { ...PARENT, id: "row-draft", folder_id: "draft", email_references: null, body: "<p>Reply in progress.</p>", }; /** A draft as the composer writes it: parent named by row id. */ const COMPOSER_DRAFT: EmailFull = { ...DEMOTED_DRAFT, id: "row-draft-2", message_id: null, in_reply_to: "row-parent", }; /** * Stub whose `getEmailByRef` resolves the way the Durable Object does: row id * first, then `message_id`. * * Modelling both lookups is the point — a stub that matched on id alone would * pass against the old code and prove nothing. */ function makeStub(...rows: EmailFull[]) { const getEmailByRef = vi.fn(async (ref: string) => { return ( rows.find((r) => r.id === ref) ?? rows.find((r) => !!r.message_id && r.message_id === ref) ?? null ); }); const getEmail = vi.fn(async (id: string) => { return rows.find((r) => r.id === id) ?? null; }); return { getEmailByRef, getEmail } as never; } // ── The server-side resolution ───────────────────────────────────── describe("resolveOriginalEmail", () => { it("follows a draft that names its parent by Message-ID", async () => { // The Undo Send case. This used to return the draft itself. const resolved = await resolveOriginalEmail( makeStub(PARENT, DEMOTED_DRAFT), DEMOTED_DRAFT, ); expect(resolved.id).toBe("row-parent"); }); it("still follows a draft that names its parent by row id", async () => { const resolved = await resolveOriginalEmail( makeStub(PARENT, COMPOSER_DRAFT), COMPOSER_DRAFT, ); expect(resolved.id).toBe("row-parent"); }); it("returns the draft unchanged when the parent is genuinely gone", async () => { // Deleted parent. Threading against the draft is the only thing left, and // it must not throw. const resolved = await resolveOriginalEmail( makeStub(DEMOTED_DRAFT), DEMOTED_DRAFT, ); expect(resolved.id).toBe("row-draft"); }); it("leaves a non-draft alone without a lookup", async () => { const stub = makeStub(PARENT); const resolved = await resolveOriginalEmail(stub, PARENT); expect(resolved.id).toBe("row-parent"); expect( (stub as unknown as { getEmailByRef: ReturnType<typeof vi.fn> }) .getEmailByRef, ).not.toHaveBeenCalled(); }); }); describe("resolveThreadedSend", () => { it("threads a demoted draft onto the real conversation", async () => { // The regression, stated as the outcome that matters: the reply has to carry // the parent's ancestry, not start a new chain. const result = await resolveThreadedSend( makeStub(PARENT, DEMOTED_DRAFT), DEMOTED_DRAFT.id, "reply", ); expect(result).not.toBeNull(); expect(result?.references).toEqual([ ]); }); it("accepts a Message-ID where a row id used to be required", async () => { // What the composer posts for a demoted draft. This returned null, which the // route turned into 404 "Original email not found" and the user saw as a // failed send. const result = await resolveThreadedSend( makeStub(PARENT), "reply", ); expect(result).not.toBeNull(); }); it("still accepts a row id", async () => { const result = await resolveThreadedSend( makeStub(PARENT), "row-parent", "reply", ); }); it("returns null for a reference that matches nothing", async () => { // Must stay a 404 rather than silently sending unthreaded. expect( await resolveThreadedSend(makeStub(PARENT), "nothing@nowhere", "reply"), ).toBeNull(); }); it("does not mark the thread read for a forward", async () => { const result = await resolveThreadedSend( makeStub(PARENT, DEMOTED_DRAFT), DEMOTED_DRAFT.id, "forward", ); expect(result?.markThreadRead).toBeNull(); // Threading still applies: a forward stays in the source conversation. }); }); // ── The Message-ID half of the resolver, against real SQLite ─────── /** * `findIdByMessageId` runs real SQL, so it is tested against real SQLite via * `node:sqlite` — the same engine behind Durable Object storage. The tie-break is * an `ORDER BY`, and a fake returning canned rows would confirm nothing about it. * * The row-id-first precedence lives in `MailboxDO.getEmailByRef`, which cannot be * imported here (`cloudflare:workers` does not resolve in Node); the stub above * mirrors it, and the two-line delegate is what the module split exists to keep * trivial. */ describe("findIdByMessageId", () => { let db: DatabaseSync; const seed = (id: string, messageId: string | null, date: string) => db .prepare( `INSERT INTO emails (id, folder_id, message_id, date) VALUES (?, 'inbox', ?, ?)`, ) .run(id, messageId, date); beforeEach(() => { db = new DatabaseSync(":memory:"); db.exec(`CREATE TABLE emails ( id TEXT PRIMARY KEY, folder_id TEXT NOT NULL, message_id TEXT, date TEXT )`); }); const ctx = () => ({ storage: { sql: { exec(sql: string, ...params: unknown[]) { const rows = db.prepare(sql).all(...(params as never[])); return { toArray: () => rows, rowsWritten: 0 }; }, }, }, }) as unknown as DurableObjectState; it("finds the message carrying a Message-ID", () => { }); it("returns null when nothing carries it", () => { }); it("picks the newest when a Message-ID is duplicated", () => { // `message_id` is indexed but deliberately not UNIQUE — see dedupe.ts — so // duplicates are possible and the choice has to be deterministic. A reply // belongs on the most recent copy. }); it("breaks a tie on identical dates deterministically", () => { // Stable across calls, rather than whatever the index happens to return. }); it("never matches a row with no Message-ID", () => { // Otherwise a draft with no parent would resolve to an arbitrary message. seed("row-a", null, "2026-08-10T09:00:00.000Z"); for (const ref of [null, undefined, "", " "]) { expect(findIdByMessageId(ctx(), ref)).toBeNull(); } }); it("ignores surrounding whitespace on the reference", () => { }); }); describe("buildReferencesChain", () => { it("uses the Message-ID, falling back to the row id", () => { // Pinned because the fallback is what makes a locally-composed message with // no Message-ID yet still citable. expect(buildReferencesChain(PARENT).originalMsgId).toBe( ); expect( buildReferencesChain({ ...PARENT, message_id: null }).originalMsgId, ).toBe("row-parent"); }); }); // ── The client-side lookup ───────────────────────────────────────── /** Only the two fields the helper reads. */ const THREAD = [ { id: "row-draft", message_id: null }, ]; describe("findThreadParent", () => { it("matches on row id", () => { expect(findThreadParent(THREAD, "row-parent")?.id).toBe("row-parent"); }); it("matches on Message-ID", () => { // The Undo Send case, in the reading pane: without this, Edit opened a reply // composer with no original attached. "row-parent", ); }); it("prefers a row id over a Message-ID, matching the server", () => { const ambiguous = [ { id: "b", message_id: "a" }, { id: "a", message_id: "z" }, ]; expect(findThreadParent(ambiguous, "a")?.id).toBe("a"); }); it("picks the newest of duplicate Message-IDs, as the server does", () => { // `message_id` is not unique by index, so duplicates are reachable. Taking // array order instead resolved to a different row than `findIdByMessageId` // would, and this helper exists precisely to agree with it. const duplicated = [ { id: "old", message_id: "dup@x", date: "2026-01-01T10:00:00Z" }, { id: "new", message_id: "dup@x", date: "2026-02-01T10:00:00Z" }, ]; expect(findThreadParent(duplicated, "dup@x")?.id).toBe("new"); }); it("breaks a same-date tie by id descending, as the server does", () => { // The case the old array-order lookup got backwards: `getThreadEmails` // returns same-dated rows id-ascending, so `find` took the lower id while // the server took the higher. const sameDate = [ { id: "row-a", message_id: "dup@x", date: "2026-01-01T10:00:00Z" }, { id: "row-b", message_id: "dup@x", date: "2026-01-01T10:00:00Z" }, ]; expect(findThreadParent(sameDate, "dup@x")?.id).toBe("row-b"); // Stable regardless of the order they arrive in. expect(findThreadParent([...sameDate].reverse(), "dup@x")?.id).toBe("row-b"); }); it("still resolves a single Message-ID match with no date at all", () => { // List projections and hand-built fixtures need not carry `date`. "row-parent", ); }); it("returns undefined for an absent or empty reference", () => { expect(findThreadParent(THREAD, null)).toBeUndefined(); expect(findThreadParent(THREAD, undefined)).toBeUndefined(); expect(findThreadParent(THREAD, "")).toBeUndefined(); expect(findThreadParent(THREAD, "unknown")).toBeUndefined(); }); it("does not match a null message_id against a null reference", () => { // `row-draft` has message_id null; a null ref must not select it. expect(findThreadParent(THREAD, null)).toBeUndefined(); }); }); describe("replyTargetForDraft", () => { it("resolves a visible parent to its row id", () => { expect( ).toBe("row-parent"); }); it("passes an unresolvable reference through for the server to resolve", () => { // The capped-thread-window case. `getThreadEmails` is limited to // MAX_THREAD_MESSAGES, so in a long conversation the parent may not be // loaded — and returning nothing here dropped In-Reply-To and References // entirely, starting a fresh conversation at the far end. expect( }); it("returns undefined for a draft that answers nothing", () => { // A brand-new draft must not acquire threading it never had. expect(replyTargetForDraft(THREAD, { in_reply_to: null })).toBeUndefined(); expect(replyTargetForDraft(THREAD, {})).toBeUndefined(); }); }); |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |