All checks were successful
PR Check — Frontend / frontend (pull_request) Successful in 1m56s
Detection reasoned on the shape of the data alone, so it could not tell a debit column from a credit one: a file laid out `Date;Description;Credit;Debit` was mapped by position and every sign of the import came out inverted — silently, since the total is merely negated and no aggregate check notices. A new `headerDictionary.ts` carries the FR/EN transaction dictionary (date, description, amount, debit, credit, balance) plus the two matching helpers, moved out of `csvAutoDetect.ts` whose holdings tables stay untouched: `montant` is an exclusion token there and the primary amount keyword here, so the two tables cannot be merged. Moving the generic helpers rather than exporting them keeps the dependency one-way. `csvAutoDetect` puts that layer in front of the shape heuristics. Labels resolve the debit/credit order, the date, description and single-amount columns, and give `detectHeader` a second signal for a header row carrying a bare number. Every hint is a preference the data can veto — a labelled date column must still parse, a labelled balance column is never excluded if it would leave nothing to map — and a mute file (no header row, unknown labels) falls back to the shape heuristics unchanged. Files pairing unsigned amounts with an adjacent D/C indicator column are now detected and REFUSED with a dedicated message, instead of being configured as `positive_expense` and importing every deposit as an expense. Detection reports that reason through `detectImportFormat`; `autoDetectConfig` keeps its previous shape for the callers that only need the configuration. Resolves #327
160 lines
5.2 KiB
TypeScript
160 lines
5.2 KiB
TypeScript
// headerDictionary — the lexical layer of transaction-CSV detection (#327).
|
|
//
|
|
// `csvAutoDetect.test.ts` covers what the dictionary DOES to a detected
|
|
// configuration; this file covers the dictionary itself: every keyword it
|
|
// claims to know, the exclusions that keep a label from claiming the wrong
|
|
// column, and the mute row that hands control back to the shape heuristics.
|
|
|
|
import { describe, it, expect } from "vitest";
|
|
import {
|
|
matchHeaderColumn,
|
|
matchTransactionHeaders,
|
|
normalizeHeaderCell,
|
|
MIN_HEADER_ROLE_MATCHES,
|
|
} from "./headerDictionary";
|
|
|
|
describe("normalizeHeaderCell (#245, shared by #327)", () => {
|
|
it("strips accents, case and punctuation", () => {
|
|
expect(normalizeHeaderCell("Débit")).toBe("debit");
|
|
expect(normalizeHeaderCell("Libellé")).toBe("libelle");
|
|
expect(normalizeHeaderCell("Date de l'opération")).toBe("datedeloperation");
|
|
expect(normalizeHeaderCell("Montant ($)")).toBe("montant");
|
|
});
|
|
|
|
it("keeps digits and tolerates an empty or missing cell", () => {
|
|
expect(normalizeHeaderCell("Solde 2024")).toBe("solde2024");
|
|
expect(normalizeHeaderCell("")).toBe("");
|
|
expect(normalizeHeaderCell(undefined as unknown as string)).toBe("");
|
|
});
|
|
});
|
|
|
|
describe("matchHeaderColumn (#245, shared by #327)", () => {
|
|
const headers = ["date", "libelle", "montant", "solde"];
|
|
|
|
it("returns the first unused column containing a keyword", () => {
|
|
expect(matchHeaderColumn(headers, ["montant"], new Set())).toBe(2);
|
|
});
|
|
|
|
it("honours keyword priority order over column order", () => {
|
|
expect(matchHeaderColumn(headers, ["solde", "date"], new Set())).toBe(3);
|
|
});
|
|
|
|
it("skips used and excluded columns, and returns null on no match", () => {
|
|
expect(matchHeaderColumn(headers, ["date"], new Set([0]))).toBeNull();
|
|
expect(matchHeaderColumn(headers, ["mont"], new Set(), ["solde"])).toBe(2);
|
|
expect(matchHeaderColumn(headers, ["quantite"], new Set())).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("matchTransactionHeaders — the FR/EN dictionary (#327)", () => {
|
|
it("reads a French signed-amount header", () => {
|
|
const m = matchTransactionHeaders(["Date", "Description", "Montant", "Solde"]);
|
|
expect(m).toEqual({
|
|
date: 0,
|
|
description: 1,
|
|
amount: 2,
|
|
debit: null,
|
|
credit: null,
|
|
balance: 3,
|
|
roleCount: 4,
|
|
});
|
|
});
|
|
|
|
it("reads an English signed-amount header", () => {
|
|
const m = matchTransactionHeaders(["Date", "Description", "Amount", "Balance"]);
|
|
expect(m.amount).toBe(2);
|
|
expect(m.balance).toBe(3);
|
|
});
|
|
|
|
it.each([
|
|
["Débit", "debit"],
|
|
["Retrait", "debit"],
|
|
["Déboursé", "debit"],
|
|
["Withdrawal", "debit"],
|
|
["Crédit", "credit"],
|
|
["Dépôt", "credit"],
|
|
["Encaissement", "credit"],
|
|
["Deposit", "credit"],
|
|
] as const)("knows %s as the %s column", (label, role) => {
|
|
const m = matchTransactionHeaders(["Date", "Description", label]);
|
|
expect(m[role]).toBe(2);
|
|
});
|
|
|
|
it.each([
|
|
["Libellé", 1],
|
|
["Détail", 1],
|
|
["Transaction", 1],
|
|
["Description", 1],
|
|
] as const)("knows %s as the description column", (label, col) => {
|
|
expect(matchTransactionHeaders(["Date", label, "Montant"]).description).toBe(
|
|
col
|
|
);
|
|
});
|
|
|
|
it("resolves a reversed pair by label, not by position", () => {
|
|
const m = matchTransactionHeaders(["Date", "Description", "Credit", "Debit"]);
|
|
expect(m.debit).toBe(3);
|
|
expect(m.credit).toBe(2);
|
|
});
|
|
|
|
it("claims a column once, so debit/credit beat the amount keyword", () => {
|
|
// Both columns contain `montant`. Resolving debit and credit first is what
|
|
// stops `Montant débit` from being read as THE amount column.
|
|
const m = matchTransactionHeaders([
|
|
"Date",
|
|
"Libelle",
|
|
"Montant debit",
|
|
"Montant credit",
|
|
]);
|
|
expect(m.debit).toBe(2);
|
|
expect(m.credit).toBe(3);
|
|
expect(m.amount).toBeNull();
|
|
});
|
|
|
|
it("never reads a Solde column as the amount", () => {
|
|
const m = matchTransactionHeaders(["Date", "Libelle", "Solde du compte"]);
|
|
expect(m.balance).toBe(2);
|
|
expect(m.amount).toBeNull();
|
|
});
|
|
|
|
it("never reads a date column as the description", () => {
|
|
// `Date de transaction` contains the description keyword `transaction`.
|
|
const m = matchTransactionHeaders([
|
|
"Date de transaction",
|
|
"Date comptable",
|
|
"Montant",
|
|
]);
|
|
expect(m.date).toBe(0);
|
|
expect(m.description).toBeNull();
|
|
});
|
|
|
|
it("returns a map of nulls for labels it does not know", () => {
|
|
const m = matchTransactionHeaders(["Col A", "Col B", "Col C"]);
|
|
expect(m).toEqual({
|
|
date: null,
|
|
description: null,
|
|
amount: null,
|
|
debit: null,
|
|
credit: null,
|
|
balance: null,
|
|
roleCount: 0,
|
|
});
|
|
});
|
|
|
|
it("scores a data row below the header threshold", () => {
|
|
// `DEPOT PAIE EMPLOYEUR` contains the credit keyword `depot` — one role,
|
|
// which is exactly why one is not enough to call a row a header.
|
|
const m = matchTransactionHeaders([
|
|
"05/01/2025",
|
|
"DEPOT PAIE EMPLOYEUR",
|
|
"-84,32",
|
|
]);
|
|
expect(m.credit).toBe(1);
|
|
expect(m.roleCount).toBeLessThan(MIN_HEADER_ROLE_MATCHES);
|
|
});
|
|
|
|
it("tolerates empty cells and a ragged row", () => {
|
|
expect(matchTransactionHeaders([]).roleCount).toBe(0);
|
|
expect(matchTransactionHeaders(["", "Date", ""]).date).toBe(1);
|
|
});
|
|
});
|