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>
This commit is contained in:
@@ -455,3 +455,221 @@ func TestLoadUserConfigMalformed(t *testing.T) {
|
||||
t.Error("expected an error for a malformed config file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendTransfer(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
existing := "[[rule]]\nmatch = \"*LIDL*\"\ntag = \"groceries\"\n"
|
||||
if err := os.WriteFile(filepath.Join(root, RulesFile), []byte(existing), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tr := Transfer{
|
||||
FromAccount: "nlb", FromDesc: "*TO REVOLUT*",
|
||||
ToAccount: "revolut", ToDesc: "*FROM NLB*",
|
||||
Note: `the monthly "top-up"`,
|
||||
}
|
||||
if err := AppendTransfer(root, tr); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loaded, err := LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatalf("the file no longer parses after appending: %v", err)
|
||||
}
|
||||
if len(loaded.Rule) != 1 {
|
||||
t.Errorf("got %d rules, want the existing one untouched", len(loaded.Rule))
|
||||
}
|
||||
if len(loaded.Transfer) != 1 || loaded.Transfer[0] != tr {
|
||||
t.Errorf("transfers = %+v, want %+v", loaded.Transfer, tr)
|
||||
}
|
||||
}
|
||||
|
||||
// Both sides are required: a one-sided definition can never pair anything, so
|
||||
// it is refused rather than written and silently ignored.
|
||||
func TestTransferNeedsBothSides(t *testing.T) {
|
||||
full := Transfer{FromAccount: "a", FromDesc: "*OUT*", ToAccount: "b", ToDesc: "*IN*"}
|
||||
cases := map[string]func(*Transfer){
|
||||
"no from_account": func(t *Transfer) { t.FromAccount = "" },
|
||||
"no from_desc": func(t *Transfer) { t.FromDesc = "" },
|
||||
"no to_account": func(t *Transfer) { t.ToAccount = "" },
|
||||
"no to_desc": func(t *Transfer) { t.ToDesc = "" },
|
||||
}
|
||||
for name, break_ := range cases {
|
||||
root := t.TempDir()
|
||||
tr := full
|
||||
break_(&tr)
|
||||
if err := AppendTransfer(root, tr); err == nil {
|
||||
t.Errorf("%s: expected an error", name)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, RulesFile)); !os.IsNotExist(err) {
|
||||
t.Errorf("%s: a rejected transfer must not create the file", name)
|
||||
}
|
||||
}
|
||||
|
||||
// The same check applies to a file written by hand.
|
||||
root := t.TempDir()
|
||||
body := "[[transfer]]\nfrom_account = \"nlb\"\nfrom_desc = \"*OUT*\"\nto_account = \"revolut\"\n"
|
||||
if err := os.WriteFile(filepath.Join(root, RulesFile), []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := LoadRules(root); err == nil {
|
||||
t.Error("expected a half-written transfer to be refused on load")
|
||||
}
|
||||
}
|
||||
|
||||
// Rules and transfers share a file, so deleting one kind must not take a
|
||||
// neighbouring block of the other kind with it.
|
||||
func TestDeleteLeavesTheOtherKindAlone(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
path := filepath.Join(root, RulesFile)
|
||||
original := `[[rule]]
|
||||
match = "*LIDL*"
|
||||
tag = "groceries"
|
||||
|
||||
# Moving money to the broker.
|
||||
[[transfer]]
|
||||
from_account = "nlb"
|
||||
from_desc = "*TO TRADEREPUBLIC*"
|
||||
to_account = "traderepublic"
|
||||
to_desc = "*FROM NLB*"
|
||||
|
||||
[[rule]]
|
||||
match = "*ZARA*"
|
||||
tag = "clothes"
|
||||
`
|
||||
if err := os.WriteFile(path, []byte(original), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Deleting the first rule must stop at the transfer that follows it.
|
||||
if _, err := DeleteRules(root, []int{0}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(loaded.Rule) != 1 || loaded.Rule[0].Tag != "clothes" {
|
||||
t.Errorf("rules = %+v, want only the clothes rule left", loaded.Rule)
|
||||
}
|
||||
if len(loaded.Transfer) != 1 {
|
||||
t.Fatalf("transfers = %+v, want the transfer untouched", loaded.Transfer)
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(raw), "# Moving money to the broker.") {
|
||||
t.Errorf("rules.toml = %q, want the transfer's own comment kept", raw)
|
||||
}
|
||||
|
||||
// And deleting the transfer leaves the remaining rule alone.
|
||||
if _, err := DeleteTransfers(root, []int{0}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err = LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(loaded.Transfer) != 0 || len(loaded.Rule) != 1 {
|
||||
t.Errorf("after deleting the transfer: %d rules, %d transfers; want 1 and 0",
|
||||
len(loaded.Rule), len(loaded.Transfer))
|
||||
}
|
||||
if raw, err = os.ReadFile(path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(raw), "Moving money to the broker") {
|
||||
t.Errorf("rules.toml = %q, want the deleted transfer's comment gone with it", raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteTransfersOutOfRange(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := AppendTransfer(root, Transfer{
|
||||
FromAccount: "a", FromDesc: "*OUT*", ToAccount: "b", ToDesc: "*IN*",
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := DeleteTransfers(root, []int{3}); err == nil {
|
||||
t.Error("expected an out-of-range position to be refused")
|
||||
}
|
||||
}
|
||||
|
||||
// A tolerance survives the round trip through the file, and a definition
|
||||
// without one keeps meaning what it always meant: exact amounts.
|
||||
func TestAppendTransferWithTolerance(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
tr := Transfer{
|
||||
FromAccount: "nlb", FromDesc: "*TO REVOLUT*",
|
||||
ToAccount: "revolut", ToDesc: "*FROM NLB*",
|
||||
TolerancePct: 1.5,
|
||||
Note: "NLB takes a wire fee",
|
||||
}
|
||||
if err := AppendTransfer(root, tr); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
body, err := os.ReadFile(filepath.Join(root, RulesFile))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.Contains(string(body), "tolerance_pct = 1.5") {
|
||||
t.Errorf("file = %q, want the tolerance written unquoted", body)
|
||||
}
|
||||
|
||||
loaded, err := LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(loaded.Transfer) != 1 || loaded.Transfer[0] != tr {
|
||||
t.Errorf("transfers = %+v, want %+v", loaded.Transfer, tr)
|
||||
}
|
||||
}
|
||||
|
||||
// The default is exact, so an unset tolerance must not be written out as a key
|
||||
// suggesting the definition decided something.
|
||||
func TestAppendTransferOmitsAZeroTolerance(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
tr := Transfer{
|
||||
FromAccount: "nlb", FromDesc: "*TO REVOLUT*",
|
||||
ToAccount: "revolut", ToDesc: "*FROM NLB*",
|
||||
}
|
||||
if err := AppendTransfer(root, tr); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body, err := os.ReadFile(filepath.Join(root, RulesFile))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if strings.Contains(string(body), "tolerance_pct") {
|
||||
t.Errorf("file = %q, want no tolerance key", body)
|
||||
}
|
||||
}
|
||||
|
||||
// A negative tolerance is a typo, and one at 100% or beyond would let any
|
||||
// amount pair with any other, leaving the dates to decide alone.
|
||||
func TestTransferToleranceIsBounded(t *testing.T) {
|
||||
full := Transfer{FromAccount: "a", FromDesc: "*OUT*", ToAccount: "b", ToDesc: "*IN*"}
|
||||
for _, pct := range []float64{-1, 100, 250} {
|
||||
root := t.TempDir()
|
||||
tr := full
|
||||
tr.TolerancePct = pct
|
||||
if err := AppendTransfer(root, tr); err == nil {
|
||||
t.Errorf("tolerance_pct %g: expected an error", pct)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, RulesFile)); !os.IsNotExist(err) {
|
||||
t.Errorf("tolerance_pct %g: a rejected transfer must not create the file", pct)
|
||||
}
|
||||
}
|
||||
|
||||
// The same check applies to a file written by hand.
|
||||
root := t.TempDir()
|
||||
body := "[[transfer]]\nfrom_account = \"a\"\nfrom_desc = \"*OUT*\"\n" +
|
||||
"to_account = \"b\"\nto_desc = \"*IN*\"\ntolerance_pct = 150\n"
|
||||
if err := os.WriteFile(filepath.Join(root, RulesFile), []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := LoadRules(root); err == nil {
|
||||
t.Error("expected an out-of-range tolerance to be refused on load")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user