From 151995f77835bd39878205a0998972ffc35f8c9f Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 1 Nov 2026 03:05:14 -0600 Subject: [PATCH] fix(aggregator): skip invalid Gmail receipt dates without dropping the batch (#2448) --- .../scripts/produce-charge-candidates.ts | 17 ++++++++++- .../test/produce-charge-candidates.test.ts | 29 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/pm-stack/aggregator/scripts/produce-charge-candidates.ts b/pm-stack/aggregator/scripts/produce-charge-candidates.ts index dbb8c98cae..58704085db 100644 --- a/pm-stack/aggregator/scripts/produce-charge-candidates.ts +++ b/pm-stack/aggregator/scripts/produce-charge-candidates.ts @@ -115,6 +115,21 @@ interface TriageMessage { date?: string; } +/** + * The day a Gmail message counts as a charge on (#2448): the triage + * record's local day when present, else the parsed message date. + * A missing or garbled date yields "" (the caller's format check then + * counts the message as skipped) — never a throw: isoDay on an + * invalid Date raises RangeError, and letting that escape here would + * discard the whole Gmail candidate batch for one bad message. + */ +export function gmailMessageDay(m: Pick): string { + const local = (m.message_sent_at?.user_local ?? "").slice(0, 10); + if (local) return local; + const parsed = new Date(m.date ?? ""); + return Number.isNaN(parsed.getTime()) ? "" : isoDay(parsed); +} + const AMOUNT_RE = /\$([\d,]+\.\d{2})/; /** "Oct 25, 2026" / "2026-10-25" as a merchant is a parse failure, not a merchant. */ @@ -218,7 +233,7 @@ async function gmailCandidates( skipped++; continue; } - const at = (m.message_sent_at?.user_local ?? "").slice(0, 10) || isoDay(new Date(m.date ?? "")); + const at = gmailMessageDay(m); if (!/^\d{4}-\d{2}-\d{2}$/.test(at)) { skipped++; continue; diff --git a/pm-stack/aggregator/test/produce-charge-candidates.test.ts b/pm-stack/aggregator/test/produce-charge-candidates.test.ts index c896310acb..d1d5778b57 100644 --- a/pm-stack/aggregator/test/produce-charge-candidates.test.ts +++ b/pm-stack/aggregator/test/produce-charge-candidates.test.ts @@ -2,7 +2,7 @@ // inside "Subtotal" as the charged total, and an explicit paid amount // wins over an earlier bare total. import { describe, expect, test } from "bun:test"; -import { amountFromBody, isNotChargeSubject } from "../scripts/produce-charge-candidates.ts"; +import { amountFromBody, gmailMessageDay, isNotChargeSubject } from "../scripts/produce-charge-candidates.ts"; describe("amountFromBody", () => { test("records the paid total, not the subtotal", () => { @@ -74,3 +74,30 @@ describe("amountFromBody (#3120)", () => { expect(amountFromBody("Receipt\nYou were charged $20.00 for OpenAI")).toBe("20.00"); }); }); + +// #2448: a Gmail message with a missing or garbled date must be +// skippable by the caller (empty day -> format check -> skipped), +// never throw RangeError and discard the whole candidate batch. +describe("gmailMessageDay (#2448)", () => { + test("the triage local day wins when present", () => { + expect(gmailMessageDay({ message_sent_at: { user_local: "2026-10-25T09:30:00" }, date: "garbage" })).toBe("2026-10-25"); + }); + + test("a parseable message date is used when no local day exists", () => { + expect(gmailMessageDay({ date: "Sat, 25 Oct 2026 09:30:00 -0500" })).toBe("2026-10-25"); + }); + + test("a missing date yields an empty day instead of throwing", () => { + expect(gmailMessageDay({})).toBe(""); + }); + + test("a garbled date yields an empty day instead of throwing", () => { + expect(gmailMessageDay({ date: "not a date" })).toBe(""); + expect(gmailMessageDay({ date: "" })).toBe(""); + }); + + test("one bad message does not stop the batch: later messages still resolve", () => { + const days = [{ date: "garbage" }, { date: "Sat, 25 Oct 2026 09:30:00 -0500" }, {}].map(gmailMessageDay); + expect(days).toEqual(["", "2026-10-25", ""]); + }); +});