Files
money/internal/importer/importer_test.go
T
nikolaandClaude Opus 5 ebf7770569 Pair transfers from rules.toml
The boolean transfer flag went two commits ago because a one-sided verdict let
half a movement vanish and left the report unbalanced. This is what replaces
it: a [[transfer]] block names both legs, and only a matched pair is dropped
from the report -- both legs together, never one.

Legs pair within five days, nearest date first, and a transaction belongs to at
most one transfer, so the first definition to claim a leg keeps it, exactly as
the first matching rule keeps a tag. The pairing is derived state like the tags:
Engine.Link rewrites the whole transfers table from rules.toml, which is why
retag re-derives both halves of what that file decides, and why it runs over
the whole index rather than a filtered view -- pairing inside one would let a
movement count as a transfer in one report and not in another. An unmatched leg
is not a transfer and keeps counting, surfaced as a warning instead.

Within one currency the amount is the evidence and must be the exact opposite.
Across currencies it is not checked at all: there are no rates here, so the two
numbers are unrelated and the dates carry the pairing alone.

tolerance_pct is the one exception, per definition, for a route where the bank
takes a fee and the two statements genuinely disagree. It defaults to zero and
belongs on the one definition that charges; a global or default tolerance would
loosen every route that does not. The difference it admits is not forgiven --
the pair leaves the report entirely, so a fee hidden inside one would be
spending that appears nowhere. Pair.Fee is what left less what arrived, and
report.Excluded carries it out per currency alongside the legs. It counts only
pairs whose legs are both in view, for the same reason it counts legs and not
transfers: half a pair cannot say what the other half received.

The screens:

- 6 builds a definition against the index as you type, showing the pairs it
  would form and the legs it would catch but leave unpaired. Six fields need
  more room than the rule builder's four, so the form sheds its spacing, then
  its hints, then the borders on unfocused fields.
- 7 lists every definition with what it pairs. Two counts, because they mean
  different things: an unpaired leg is a definition doing something and not
  finishing it, no pairs at all is dead weight. Tol names the tolerance, blank
  where amounts must agree.
- 3 grows a (transfers) row under TOTAL, and a fees row beneath it, or the
  report silently disagrees with the account balances.

Two things that are not part of transfers but are the same day's work:

- ls --uniq lists each account and description once, normalised the way a glob
  sees them, which is the shape of "what still needs a rule?" -- fifty visits
  to one shop are one pattern to write, not fifty rows to read.
- The rule builder's preview now filters to what the glob matches instead of
  marking matches in a full list. The count carries the context the rows no
  longer can: 2 of 7, measured against everything still in view.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 19:19:12 +02:00

260 lines
8.2 KiB
Go

package importer
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"git.petrovv.com/nikola/money/internal/config"
"git.petrovv.com/nikola/money/internal/parser"
"git.petrovv.com/nikola/money/internal/rules"
"git.petrovv.com/nikola/money/internal/store"
"git.petrovv.com/nikola/money/internal/transfers"
)
const accountTOML = `
name = "Checking"
currency = "EUR"
parser = "test"
`
// These tests are about the directory walk, the checksum skip, dedupe and
// per-file error reporting -- not about any bank's layout. Driving them with a
// real bank parser would drag that bank's quirks (Revolut's fee folding and
// COMPLETED filter, the PDF parsers' dependency on pdftotext) into every
// fixture, so they register the smallest parser that will do instead.
func init() {
parser.Register("test", func(acc *config.Account) (parser.Parser, error) {
return testParser{digits: acc.Digits()}, nil
})
}
// testParser reads "date,description,amount" with one header row.
type testParser struct{ digits int }
func (p testParser) Parse(path string, acc *config.Account) ([]parser.RawTxn, error) {
body, err := os.ReadFile(path)
if err != nil {
return nil, err
}
var txns []parser.RawTxn
for i, line := range strings.Split(strings.TrimSpace(string(body)), "\n") {
if i == 0 || strings.TrimSpace(line) == "" {
continue // header
}
fields := strings.Split(line, ",")
if len(fields) != 3 {
return nil, fmt.Errorf("row %d: got %d fields, want 3", i+1, len(fields))
}
if _, err := time.Parse("2006-01-02", fields[0]); err != nil {
return nil, fmt.Errorf("row %d: %w", i+1, err)
}
amount, err := parser.ParseAmount(fields[2], ".", "", p.digits)
if err != nil {
return nil, fmt.Errorf("row %d: %w", i+1, err)
}
txns = append(txns, parser.RawTxn{Date: fields[0], Description: fields[1], AmountMinor: amount})
}
return txns, nil
}
// newRoot builds a data root with one account and the given statement files.
func newRoot(t *testing.T, statements map[string]string) (string, *store.DB, []*config.Account, *rules.Engine) {
t.Helper()
root := t.TempDir()
dir := filepath.Join(root, "checking")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
write(t, filepath.Join(dir, config.AccountFile), accountTOML)
for name, body := range statements {
write(t, filepath.Join(dir, name), body)
}
db, err := store.Open(config.IndexPath(root))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { db.Close() })
accounts, err := config.LoadAccounts(root)
if err != nil {
t.Fatal(err)
}
engine := rules.New(&config.Rules{Rule: []config.Rule{{Match: "*LIDL*", Tag: "groceries"}}})
return root, db, accounts, engine
}
func write(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
func mustRun(t *testing.T, root string, db *store.DB, accounts []*config.Account, e *rules.Engine, opts Options) Result {
t.Helper()
res, err := Run(root, db, accounts, e, transfers.New(&config.Rules{}), opts)
if err != nil {
t.Fatal(err)
}
for _, f := range res.Errs() {
t.Fatalf("import of %s failed: %v", f.Path, f.Err)
}
return res
}
// Identical lines inside one statement are distinct transactions; the same
// line seen again in an overlapping statement is not.
func TestDedupe(t *testing.T) {
root, db, accounts, engine := newRoot(t, map[string]string{
"2026-01.csv": `date,description,amount
2026-01-06,LIDL SOFIA,-45.20
2026-01-06,LIDL SOFIA,-45.20
2026-01-10,RENT,-800.00
`,
})
res := mustRun(t, root, db, accounts, engine, Options{})
if _, added, _ := res.Total(); added != 3 {
t.Fatalf("first import added %d rows, want 3 (identical same-day lines must both survive)", added)
}
// Unchanged file: skipped without even parsing.
res = mustRun(t, root, db, accounts, engine, Options{})
if parsed, added, _ := res.Total(); parsed != 0 || added != 0 {
t.Errorf("re-import parsed %d and added %d, want 0 and 0", parsed, added)
}
// Same file, parsed again: every row is recognised as a duplicate.
res = mustRun(t, root, db, accounts, engine, Options{Force: true})
if _, added, skipped := res.Total(); added != 0 || skipped != 3 {
t.Errorf("forced re-import added %d, skipped %d; want 0 and 3", added, skipped)
}
// An overlapping statement contributes only its genuinely new rows.
write(t, filepath.Join(root, "checking", "2026-02.csv"), `date,description,amount
2026-01-10,RENT,-800.00
2026-02-10,RENT,-800.00
`)
accounts, err := config.LoadAccounts(root)
if err != nil {
t.Fatal(err)
}
res = mustRun(t, root, db, accounts, engine, Options{})
if _, added, skipped := res.Total(); added != 1 || skipped != 1 {
t.Errorf("overlapping import added %d, skipped %d; want 1 and 1", added, skipped)
}
txns, err := db.Transactions(store.Filter{})
if err != nil {
t.Fatal(err)
}
if len(txns) != 4 {
t.Errorf("index holds %d transactions, want 4", len(txns))
}
}
// Rules are applied as rows are inserted, so a fresh import is already tagged.
func TestImportAppliesRules(t *testing.T) {
root, db, accounts, engine := newRoot(t, map[string]string{
"2026-01.csv": `date,description,amount
2026-01-06,LIDL SOFIA,-45.20
2026-01-10,RENT,-800.00
`,
})
res := mustRun(t, root, db, accounts, engine, Options{})
if res.Retagged != 0 {
t.Errorf("retag changed %d rows after import, want 0: rules should already be applied", res.Retagged)
}
untagged, err := db.Transactions(store.Filter{Untagged: true})
if err != nil {
t.Fatal(err)
}
if len(untagged) != 1 || untagged[0].Description != "RENT" {
t.Errorf("untagged = %+v, want only RENT", untagged)
}
}
// A broken statement must be reported without aborting the rest of the run.
func TestBadFileIsReportedNotFatal(t *testing.T) {
root, db, accounts, engine := newRoot(t, map[string]string{
"good.csv": `date,description,amount
2026-01-06,LIDL SOFIA,-45.20
`,
"bad.csv": `date,description,amount
not-a-date,BROKEN,-1.00
`,
})
res, err := Run(root, db, accounts, engine, transfers.New(&config.Rules{}), Options{})
if err != nil {
t.Fatalf("Run returned a fatal error, want a per-file report: %v", err)
}
failures := res.Errs()
if len(failures) != 1 || filepath.Base(failures[0].Path) != "bad.csv" {
t.Fatalf("failures = %+v, want exactly bad.csv", failures)
}
if _, added, _ := res.Total(); added != 1 {
t.Errorf("added %d rows, want 1 from good.csv", added)
}
}
func TestCheckBalances(t *testing.T) {
balance := func(v int64) *int64 { return &v }
good := []parser.RawTxn{
{Date: "2026-01-01", Description: "A", AmountMinor: -1000, BalanceMinor: balance(9000)},
{Date: "2026-01-02", Description: "B", AmountMinor: -500, BalanceMinor: balance(8500)},
{Date: "2026-01-03", Description: "C", AmountMinor: 2000, BalanceMinor: balance(10500)},
}
if w := checkBalances(good, 2); len(w) != 0 {
t.Errorf("a consistent chain produced warnings: %v", w)
}
// A missed row shows up as a break at the row after it.
broken := []parser.RawTxn{
{Date: "2026-01-01", Description: "A", AmountMinor: -1000, BalanceMinor: balance(9000)},
{Date: "2026-01-02", Description: "B", AmountMinor: -500, BalanceMinor: balance(7000)},
}
w := checkBalances(broken, 2)
if len(w) != 1 {
t.Fatalf("got %d warnings, want 1: %v", len(w), w)
}
if !strings.Contains(w[0], "70.00") || !strings.Contains(w[0], "85.00") {
t.Errorf("warning = %q, want both the reported and the expected balance", w[0])
}
// Statements that report no balances are not checked.
none := []parser.RawTxn{
{Date: "2026-01-01", Description: "A", AmountMinor: -1000},
{Date: "2026-01-02", Description: "B", AmountMinor: -500},
}
if w := checkBalances(none, 2); len(w) != 0 {
t.Errorf("statements without balances produced warnings: %v", w)
}
}
// Only files matching include globs are treated as statements.
func TestIncludeGlobs(t *testing.T) {
root, db, accounts, engine := newRoot(t, map[string]string{
"2026-01.csv": `date,description,amount
2026-01-06,LIDL SOFIA,-45.20
`,
"notes.txt": "not a statement",
})
accounts[0].Include = []string{"*.csv"}
res := mustRun(t, root, db, accounts, engine, Options{})
if len(res.Files) != 1 {
t.Fatalf("processed %d files, want 1: %+v", len(res.Files), res.Files)
}
if _, added, _ := res.Total(); added != 1 {
t.Errorf("added %d rows, want 1", added)
}
}