Files
money/internal/transfers/transfers_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

356 lines
13 KiB
Go

package transfers
import (
"testing"
"git.petrovv.com/nikola/money/internal/config"
"git.petrovv.com/nikola/money/internal/model"
)
// txn builds a transaction the way the index hands them out.
func txn(id int64, account, date, desc string, amount int64) model.Transaction {
return model.Transaction{
ID: id,
AccountSlug: account,
Currency: "EUR",
MinorDigits: 2,
Date: date,
Description: desc,
AmountMinor: amount,
}
}
func engine(defs ...config.Transfer) *Engine {
return New(&config.Rules{Transfer: defs})
}
var topUp = config.Transfer{
FromAccount: "nlb", FromDesc: "*TO REVOLUT*",
ToAccount: "revolut", ToDesc: "*FROM NLB*",
}
// The two legs of one movement, dated a weekend apart, are one transfer.
func TestPairsTheTwoLegs(t *testing.T) {
res := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-09", "Top-up from NLB", 50000),
txn(3, "nlb", "2026-03-07", "LIDL SOFIA", -2000),
})
if len(res.Pairs) != 1 {
t.Fatalf("pairs = %+v, want exactly one", res.Pairs)
}
if res.Pairs[0].Out.ID != 1 || res.Pairs[0].In.ID != 2 {
t.Errorf("paired %d→%d, want 1→2", res.Pairs[0].Out.ID, res.Pairs[0].In.ID)
}
if len(res.Unmatched) != 0 {
t.Errorf("unmatched = %+v, want none; the shopping matches no side", res.Unmatched)
}
if res.Paired[0] != 1 || res.Orphaned[0] != 0 {
t.Errorf("counts = %d paired, %d orphaned; want 1 and 0", res.Paired[0], res.Orphaned[0])
}
}
// Money that left and never arrived is the case the whole feature exists to
// surface: it is not a transfer, it is one leg on its own.
func TestLegWithoutACounterpartIsUnmatched(t *testing.T) {
res := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
})
if len(res.Pairs) != 0 {
t.Fatalf("pairs = %+v, want none", res.Pairs)
}
if len(res.Unmatched) != 1 || res.Unmatched[0].Txn.ID != 1 || !res.Unmatched[0].Out {
t.Fatalf("unmatched = %+v, want the leaving leg", res.Unmatched)
}
if res.Paired[0] != 0 || res.Orphaned[0] != 1 {
t.Errorf("counts = %d paired, %d orphaned; want 0 and 1", res.Paired[0], res.Orphaned[0])
}
}
// A definition that catches nothing at all is dead, and reports as such
// separately from one that catches legs it cannot pair.
func TestDefinitionMatchingNothing(t *testing.T) {
res := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "LIDL SOFIA", -2000),
})
if res.Paired[0] != 0 || res.Orphaned[0] != 0 {
t.Errorf("counts = %d paired, %d orphaned; want both zero", res.Paired[0], res.Orphaned[0])
}
}
// Within one currency the amount is the evidence, so by default it must agree
// exactly: a movement that arrives short a fee is not the same movement
// leaving, unless the definition says the route charges one.
func TestAmountMustAgreeWithinACurrency(t *testing.T) {
res := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 49500),
})
if len(res.Pairs) != 0 {
t.Errorf("pairs = %+v, want none: the amounts differ", res.Pairs)
}
if len(res.Unmatched) != 2 {
t.Errorf("unmatched = %+v, want both legs reported", res.Unmatched)
}
}
// Across currencies the amounts are unrelated -- there are no exchange rates
// here -- so the dates carry the pairing on their own.
func TestCrossCurrencyPairsOnDateAlone(t *testing.T) {
arrived := txn(2, "revolut", "2026-03-07", "Top-up from NLB", 97790)
arrived.Currency = "BGN"
res := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
arrived,
})
if len(res.Pairs) != 1 {
t.Fatalf("pairs = %+v, want the exchange paired", res.Pairs)
}
if res.Pairs[0].In.Currency == res.Pairs[0].Out.Currency {
t.Error("expected the pair to span two currencies")
}
// The window still bounds it.
late := arrived
late.Date = "2026-03-20"
res = engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
late,
})
if len(res.Pairs) != 0 {
t.Errorf("pairs = %+v, want none beyond the window", res.Pairs)
}
}
// With no amount to go on, the nearest date decides, so two exchanges in flight
// at once pair in order rather than crossing over.
func TestCrossCurrencyPicksTheNearestDate(t *testing.T) {
arriving := func(id int64, date string, amount int64) model.Transaction {
in := txn(id, "revolut", date, "Top-up from NLB", amount)
in.Currency = "BGN"
return in
}
res := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-02", "TRANSFER TO REVOLUT LTD", -50000),
txn(3, "nlb", "2026-03-10", "TRANSFER TO REVOLUT LTD", -20000),
arriving(2, "2026-03-03", 97790),
arriving(4, "2026-03-11", 39116),
})
if len(res.Pairs) != 2 {
t.Fatalf("pairs = %+v, want both exchanges paired", res.Pairs)
}
for _, p := range res.Pairs {
if p.In.ID != p.Out.ID+1 {
t.Errorf("paired %d->%d, want each exchange with its own counterpart",
p.Out.ID, p.In.ID)
}
}
}
// Legs further apart than the window are not the same movement.
func TestWindowBoundsThePairing(t *testing.T) {
inside := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-01", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-06", "Top-up from NLB", 50000),
})
if len(inside.Pairs) != 1 {
t.Errorf("pairs = %+v, want one at exactly the window", inside.Pairs)
}
outside := engine(topUp).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-01", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 50000),
})
if len(outside.Pairs) != 0 {
t.Errorf("pairs = %+v, want none beyond the window", outside.Pairs)
}
}
// Two identical monthly transfers must pair in order rather than crossing over,
// or the dates in the report would be wrong even though the totals were right.
func TestIdenticalTransfersPairInOrder(t *testing.T) {
res := engine(topUp).Analyze([]model.Transaction{
txn(4, "revolut", "2026-04-02", "Top-up from NLB", 50000),
txn(1, "nlb", "2026-03-01", "TRANSFER TO REVOLUT LTD", -50000),
txn(3, "nlb", "2026-04-01", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-02", "Top-up from NLB", 50000),
})
if len(res.Pairs) != 2 {
t.Fatalf("pairs = %+v, want two", res.Pairs)
}
for _, p := range res.Pairs {
if p.In.ID != p.Out.ID+1 {
t.Errorf("paired %d→%d, want each transfer with its own month", p.Out.ID, p.In.ID)
}
}
}
// A transaction belongs to one transfer. The first definition to claim a leg
// keeps it, exactly as the first matching rule keeps a tag.
func TestFirstDefinitionClaimsTheLeg(t *testing.T) {
broad := config.Transfer{
FromAccount: "nlb", FromDesc: "*TRANSFER*",
ToAccount: "revolut", ToDesc: "*NLB*",
}
res := engine(topUp, broad).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 50000),
})
if len(res.Pairs) != 1 || res.Pairs[0].Def != 0 {
t.Fatalf("pairs = %+v, want one claimed by the first definition", res.Pairs)
}
if res.Paired[1] != 0 || res.Orphaned[1] != 0 {
t.Errorf("the shadowed definition reports %d paired, %d orphaned; want zero",
res.Paired[1], res.Orphaned[1])
}
}
// Direction is part of the definition: the arriving leg is an inflow and the
// leaving leg an outflow, so a definition written backwards pairs nothing.
func TestDirectionMatters(t *testing.T) {
backwards := config.Transfer{
FromAccount: "revolut", FromDesc: "*FROM NLB*",
ToAccount: "nlb", ToDesc: "*TO REVOLUT*",
}
res := engine(backwards).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 50000),
})
if len(res.Pairs) != 0 {
t.Errorf("pairs = %+v, want none: neither leg is on the side it was named for", res.Pairs)
}
if len(res.Unmatched) != 0 {
t.Errorf("unmatched = %+v, want none either", res.Unmatched)
}
}
// Money moved inside one account (a savings pocket, say) is still a pair.
func TestSameAccountBothSides(t *testing.T) {
pocket := config.Transfer{
FromAccount: "revolut", FromDesc: "*TO VAULT*",
ToAccount: "revolut", ToDesc: "*FROM VAULT*",
}
res := engine(pocket).Analyze([]model.Transaction{
txn(1, "revolut", "2026-03-06", "Move to Vault", -10000),
txn(2, "revolut", "2026-03-06", "Move from Vault", 10000),
})
if len(res.Pairs) != 1 {
t.Errorf("pairs = %+v, want one within the account", res.Pairs)
}
}
// tolerant is the same route on a bank that takes a fee on the way: 1% of the
// leaving leg, so a 500.00 transfer may arrive as little as 495.00.
var tolerant = config.Transfer{
FromAccount: "nlb", FromDesc: "*TO REVOLUT*",
ToAccount: "revolut", ToDesc: "*FROM NLB*",
TolerancePct: 1,
}
// A route that charges pairs anyway, and says what it cost.
func TestToleranceAdmitsAFee(t *testing.T) {
res := engine(tolerant).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 49500),
})
if len(res.Pairs) != 1 {
t.Fatalf("pairs = %+v, want the fee tolerated", res.Pairs)
}
if fee := res.Pairs[0].Fee(); fee != 500 {
t.Errorf("fee = %d, want 500: the pair leaves the report, so the fee has to be reported", fee)
}
}
// The tolerance is a bound, not an invitation: past it the legs are still two
// separate things.
func TestToleranceStopsAtItsBound(t *testing.T) {
res := engine(tolerant).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 49499),
})
if len(res.Pairs) != 0 {
t.Errorf("pairs = %+v, want none: 5.01 is more than 1%% of 500.00", res.Pairs)
}
}
// The allowance is a share of the amount that left, so the same percentage
// means the same thing on a small transfer as on a large one -- and it is
// rounded, or 1% of 10.00 would admit 0.09 and miss the 0.10 fee it was
// chosen for.
func TestToleranceScalesWithTheAmount(t *testing.T) {
res := engine(tolerant).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -1000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 990),
})
if len(res.Pairs) != 1 || res.Pairs[0].Fee() != 10 {
t.Fatalf("pairs = %+v, want a 0.10 fee on 10.00 tolerated", res.Pairs)
}
res = engine(tolerant).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -1000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 950),
})
if len(res.Pairs) != 0 {
t.Errorf("pairs = %+v, want none: 0.50 is 5%% of 10.00", res.Pairs)
}
}
// A tolerance belongs to the definition that declares it and to no other, so
// one tolerant route cannot loosen a strict one written beside it.
func TestToleranceIsPerDefinition(t *testing.T) {
strict := config.Transfer{
FromAccount: "nlb", FromDesc: "*TO SAVINGS*",
ToAccount: "savings", ToDesc: "*FROM NLB*",
}
res := engine(tolerant, strict).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-07", "Top-up from NLB", 49500),
txn(3, "nlb", "2026-03-06", "TRANSFER TO SAVINGS", -50000),
txn(4, "savings", "2026-03-07", "FROM NLB", 49500),
})
if len(res.Pairs) != 1 || res.Pairs[0].Def != 0 {
t.Fatalf("pairs = %+v, want only the tolerant definition to pair", res.Pairs)
}
if len(res.Unmatched) != 2 {
t.Errorf("unmatched = %+v, want both legs of the strict route reported", res.Unmatched)
}
}
// Across currencies the amounts are in different units, so a fee cannot be
// computed from them -- subtracting one from the other would be a number
// meaning nothing.
func TestNoFeeAcrossCurrencies(t *testing.T) {
arrived := txn(2, "revolut", "2026-03-07", "Top-up from NLB", 97790)
arrived.Currency = "BGN"
res := engine(tolerant).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
arrived,
})
if len(res.Pairs) != 1 {
t.Fatalf("pairs = %+v, want the exchange paired", res.Pairs)
}
if fee := res.Pairs[0].Fee(); fee != 0 {
t.Errorf("fee = %d, want 0 across currencies", fee)
}
}
// With slack in the amount two candidates can sit the same number of days
// away, and then the nearer amount is the better evidence.
func TestEqualGapPrefersTheNearerAmount(t *testing.T) {
res := engine(tolerant).Analyze([]model.Transaction{
txn(1, "nlb", "2026-03-06", "TRANSFER TO REVOLUT LTD", -50000),
txn(2, "revolut", "2026-03-05", "Top-up from NLB", 49600),
txn(3, "revolut", "2026-03-07", "Top-up from NLB", 50000),
})
if len(res.Pairs) != 1 {
t.Fatalf("pairs = %+v, want one", res.Pairs)
}
if res.Pairs[0].In.ID != 3 {
t.Errorf("paired with %d, want 3: same gap, exact amount", res.Pairs[0].In.ID)
}
}