diff --git a/pm-stack/aggregator/src/charges-match.ts b/pm-stack/aggregator/src/charges-match.ts index fa791b8f5..dc4f678bc 100644 --- a/pm-stack/aggregator/src/charges-match.ts +++ b/pm-stack/aggregator/src/charges-match.ts @@ -99,10 +99,6 @@ export function normalizeMerchantName(raw: string): string { return tokens.join(" "); } -function squash(normalized: string): string { - return normalized.replace(/\s+/g, ""); -} - function daysBetween(a: string, b: string): number { const ms = Math.abs(Date.parse(a) - Date.parse(b)); return Math.floor(ms / 86_400_000); @@ -146,6 +142,10 @@ const SAAS_MARKERS = [ // charges to these are exactly the unmatched SaaS/AI charges the // hub exists to surface. "META", + // Known Meta billing descriptor (METAPAY*...): previously matched via + // the META substring; listed explicitly so token-boundary matching + // keeps surfacing it as unmatched SaaS when Meta is not in the catalog. + "METAPAY", "CLINE", "OPENCODE", "OPENDESIGN", @@ -156,20 +156,17 @@ const SAAS_MARKERS = [ /** Heuristic: does this normalized merchant look like a SaaS/AI charge? */ export function isSaasLooking(normalized: string): boolean { - const flat = squash(normalized); - return SAAS_MARKERS.some((marker) => flat.includes(marker)); + const tokens = new Set(normalized.split(" ").filter(Boolean)); + return SAAS_MARKERS.some((marker) => tokens.has(marker)); } /** The hub service a normalized merchant belongs to, if any. */ export function matchService(normalized: string, services: ChargeServiceRef[]): ChargeServiceRef | undefined { - const flat = squash(normalized); const tokenSet = new Set(normalized.split(" ").filter(Boolean)); for (const service of services) { for (const candidate of [service.name, ...(service.aliases ?? [])]) { const norm = normalizeMerchantName(candidate); if (!norm) continue; - const flatNorm = squash(norm); - if (flatNorm.length >= 4 && flat.includes(flatNorm)) return service; const tokens = norm.split(" ").filter(Boolean); if (tokens.length > 0 && tokens.every((t) => tokenSet.has(t))) return service; } diff --git a/pm-stack/aggregator/test/charges-match.test.ts b/pm-stack/aggregator/test/charges-match.test.ts index d0136e300..1cfa8db61 100644 --- a/pm-stack/aggregator/test/charges-match.test.ts +++ b/pm-stack/aggregator/test/charges-match.test.ts @@ -88,6 +88,40 @@ describe("matchCharges", () => { expect(rows[0]?.source).toBe("Gmail alert"); }); + test("service and SaaS-marker matches require token boundaries", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-15", merchant: "METAMUCIL", amountUsd: 12 }, + { source: "plaid", at: "2026-10-16", merchant: "METAL ROOFING LLC", amountUsd: 34 }, + { source: "plaid", at: "2026-10-17", merchant: "META PAY", amountUsd: 8 }, + ]; + const { rows, unmatchedSaas } = matchCharges(candidates, [{ name: "Meta" }]); + + expect(rows.map((row) => row.label)).toEqual(["METAMUCIL", "METAL ROOFING LLC", "Meta"]); + expect(unmatchedSaas.map((charge) => charge.merchant)).toEqual([]); + expect(isSaasLooking(normalizeMerchantName("METAMUCIL"))).toBe(false); + expect(isSaasLooking(normalizeMerchantName("METAL ROOFING LLC"))).toBe(false); + expect(isSaasLooking(normalizeMerchantName("META PAY"))).toBe(true); + expect(isSaasLooking(normalizeMerchantName("METAPAY*UC5DPA22Y4 US"))).toBe(true); + }); + + test("Cursor.com still matches the Cursor service", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-18", merchant: "CURSOR.COM", amountUsd: 20 }, + ]; + expect(matchCharges(candidates, [{ name: "Cursor" }]).rows[0]?.label).toBe("Cursor"); + }); + + test("multi-word service names retain token-set matching", () => { + const candidates: ChargeCandidate[] = [ + { source: "plaid", at: "2026-10-19", merchant: "NORTH STAR PLAN", amountUsd: 9 }, + { source: "plaid", at: "2026-10-20", merchant: "PLAN NORTH MONTHLY STAR", amountUsd: 10 }, + ]; + expect(matchCharges(candidates, [{ name: "North Star" }]).rows.map((row) => row.label)).toEqual([ + "North Star", + "North Star", + ]); + }); + test("unmatched SaaS-looking charges surface once, deduped", () => { const candidates: ChargeCandidate[] = [ { source: "plaid", at: "2026-10-12", merchant: "CURSOR.COM", amountUsd: 20 },