diff --git a/src/utils/csvAutoDetect.test.ts b/src/utils/csvAutoDetect.test.ts index 02f4bb6..59a3b6b 100644 --- a/src/utils/csvAutoDetect.test.ts +++ b/src/utils/csvAutoDetect.test.ts @@ -190,20 +190,29 @@ function mapCorpusRow( /** The shape `autoDetectConfig` returns: a format minus the encoding. */ type DetectedConfig = NonNullable>; +/** `parseContentEndToEnd` on a corpus fixture. */ +function parseFixtureEndToEnd( + name: CsvFixtureName, + configure?: (config: DetectedConfig) => DetectedConfig +) { + return parseContentEndToEnd(readCsvFixture(name), name, configure); +} + /** * Full pipeline: raw file text -> signed amounts, as the wizard runs it. * * `configure` rewrites the detected configuration before anything is read, so a * correction the user makes in the preview — the sign flip (#329) — can be - * replayed over the whole file exactly as the wizard replays it. + * replayed over the whole file exactly as the wizard replays it. `label` only + * names the file in a failure. */ -function parseFixtureEndToEnd( - name: CsvFixtureName, +function parseContentEndToEnd( + rawContent: string, + label: string, configure?: (config: DetectedConfig) => DetectedConfig ) { - const rawContent = readCsvFixture(name); const detected = autoDetectConfig(rawContent); - if (!detected) throw new Error(`autoDetectConfig returned null for ${name}`); + if (!detected) throw new Error(`autoDetectConfig returned null for ${label}`); const cfg = configure ? configure(detected) : detected; const format = { ...cfg, encoding: "utf-8" }; @@ -239,7 +248,11 @@ function recapOf( name: CsvFixtureName, configure?: (config: DetectedConfig) => DetectedConfig ) { - const { rows } = parseFixtureEndToEnd(name, configure); + return recapOfRows(parseFixtureEndToEnd(name, configure).rows); +} + +/** The preview recap of rows mapped by `parseContentEndToEnd`. */ +function recapOfRows(rows: ReturnType["rows"]) { return summarizeParsedRows( rows.map((r, rowIndex) => "error" in r @@ -921,6 +934,139 @@ describe("autoDetectConfig — description column read from the labels (#327)", }); }); +// ============================================================================= +// Issue #371 — the BMO Mastercard export, read by the generic dictionary +// ============================================================================= +// +// The user guide says generic detection reads BMO exports and keeps the +// transaction date, not the posting date, for the Mastercard; Max confirmed it +// on real exports on 2026-10-22. BMO has no bank signature yet (#488), so these +// cases pin the claim on the real header row of the Mastercard export, labels +// only. Every row value below is invented, the date format included. + +const BMO_MASTERCARD_HEADER = + "Article no,Carte no,Date de la transaction,Date de l'inscription au relevé,Montant de la transaction,Description"; + +/** One invented row: transaction date, posting date, amount as written, merchant. */ +type BmoMastercardRow = readonly [string, string, string, string]; + +/** + * A Mastercard export laid out like BMO's. Two invented card numbers alternate, + * so the card column is a numeric candidate the amount label has to beat. + */ +function bmoMastercard(rows: readonly BmoMastercardRow[]): string { + return withHeader( + BMO_MASTERCARD_HEADER, + rows.map(([date, posted, amount, merchant], i) => { + const card = i % 2 === 0 ? "1111" : "2222"; + return `${i + 1},${card},${date},${posted},${amount},${merchant}`; + }) + ); +} + +/** Purchases written as positive amounts, the card payment written negative. */ +const BMO_PURCHASES_POSITIVE: readonly BmoMastercardRow[] = [ + ["2026-10-01", "2026-10-02", "45.67", "EPICERIE FICTIVE"], + ["2026-10-03", "2026-10-06", "12.50", "CAFE FICTIF"], + ["2026-10-05", "2026-10-07", "-200.00", "PAIEMENT FICTIF"], + ["2026-10-08", "2026-10-09", "89.99", "PHARMACIE FICTIVE"], + ["2026-10-12", "2026-10-13", "23.10", "LIBRAIRIE FICTIVE"], +]; + +/** The same statement, every sign written the other way round. */ +const BMO_PURCHASES_NEGATIVE: readonly BmoMastercardRow[] = + BMO_PURCHASES_POSITIVE.map( + ([date, posted, amount, merchant]): BmoMastercardRow => [ + date, + posted, + amount.startsWith("-") ? amount.slice(1) : `-${amount}`, + merchant, + ] + ); + +/** What reaches the database either way: purchases out, the payment in. */ +const BMO_SIGNED_AMOUNTS = [-45.67, -12.5, 200, -89.99, -23.1]; + +function signedAmounts(rows: ReturnType["rows"]) { + return rows.map((r) => ("error" in r ? r.error : r.amount)); +} + +describe("detectImportFormat — BMO Mastercard header, no bank signature yet (#371)", () => { + it("maps the transaction date, the amount and the description by their labels", () => { + const content = bmoMastercard(BMO_PURCHASES_POSITIVE); + const outcome = detectImportFormat(content); + if (outcome.status !== "ok") { + throw new Error(`bmo-mastercard: detection returned ${outcome.status}`); + } + + // Read by the generic dictionary; the expectation becomes "bmo" with #488. + expect(outcome.bank).toBeNull(); + expect(outcome.config.hasHeader).toBe(true); + expect(outcome.config.skipLines).toBe(0); + expect(outcome.config.amountMode).toBe("single"); + expect(outcome.config.columnMapping).toEqual({ + date: 2, + amount: 4, + description: 5, + }); + expect(outcome.score.totalRows).toBe(5); + expect(outcome.score.readRows).toBe(outcome.score.totalRows); + + const { rows } = parseContentEndToEnd(content, "bmo-mastercard"); + // The transaction date reaches the row, never the posting date. + expect(rows.map((r) => ("error" in r ? r.error : r.date))).toEqual( + BMO_PURCHASES_POSITIVE.map(([date]) => date) + ); + expect(rows.map((r) => ("error" in r ? r.error : r.description))).toEqual( + BMO_PURCHASES_POSITIVE.map(([, , , merchant]) => merchant) + ); + }); + + it("reads purchases as outflows and the payment as an inflow, whichever way the export signs them", () => { + expect( + autoDetectConfig(bmoMastercard(BMO_PURCHASES_POSITIVE))!.signConvention + ).toBe("positive_expense"); + expect( + autoDetectConfig(bmoMastercard(BMO_PURCHASES_NEGATIVE))!.signConvention + ).toBe("negative_expense"); + + for (const statement of [BMO_PURCHASES_POSITIVE, BMO_PURCHASES_NEGATIVE]) { + const { rows } = parseContentEndToEnd( + bmoMastercard(statement), + "bmo-mastercard" + ); + expect(signedAmounts(rows)).toEqual(BMO_SIGNED_AMOUNTS); + const recap = recapOfRows(rows); + expect(recap.outflowCount).toBe(4); + expect(recap.inflowCount).toBe(1); + expect(recap.errorCount).toBe(0); + } + }); + + it("reads a statement with more payments than purchases backwards, as the guide warns, until the flip", () => { + // `detectSingleAmount` guesses the convention from the majority of rows: + // three payments and refunds against one purchase tip it the wrong way. + const content = bmoMastercard([ + ["2026-10-01", "2026-10-02", "-200.00", "PAIEMENT FICTIF"], + ["2026-10-03", "2026-10-06", "-15.00", "REMBOURSEMENT FICTIF"], + ["2026-10-05", "2026-10-07", "-50.00", "SECOND PAIEMENT FICTIF"], + ["2026-10-08", "2026-10-09", "45.67", "EPICERIE FICTIVE"], + ]); + expect(autoDetectConfig(content)!.signConvention).toBe("negative_expense"); + + const backwards = parseContentEndToEnd(content, "bmo-mostly-payments"); + expect(signedAmounts(backwards.rows)).toEqual([-200, -15, -50, 45.67]); + expect(recapOfRows(backwards.rows).inflowCount).toBe(1); + + const repaired = parseContentEndToEnd( + content, + "bmo-mostly-payments", + flipped + ); + expect(signedAmounts(repaired.rows)).toEqual([200, 15, 50, -45.67]); + }); +}); + describe("autoDetectConfig — degenerate input (#326)", () => { it("returns null on content with fewer than two non-empty lines", () => { expect(autoDetectConfig("")).toBeNull();