Files
money/internal/parser/traderepublic_test.go
T
nikolaandClaude Opus 5 16c2585637 Drop counterparty, the generic parsers and .money/
counterparty was a structured field only nlb could fill honestly. revolut
and traderepublic invented one by running an IBAN-shaped regex over the
description they had just built, and the two spellings disagreed --
SI56 1234 5678 9012 345 against SI56123456789012345 -- so a literal rule
pattern that worked on one account silently matched nothing on another. It
is gone from the model, the index, the rule keys, ls --wide and the rules
screen. nlb now appends its IBAN column to the end of the description,
where the other two already keep theirs, so match = "*SI56*" works
everywhere. That changes those descriptions and with them their
fingerprints, so a statement overlapping an already-imported period will
re-add rather than dedupe those rows until the index is rebuilt. An index
built by an older binary drops the column when it is opened.

The index itself moves from .money/index.db up to index.db beside
rules.toml. Nothing looks in the old location, so an existing one has to be
moved by hand -- otherwise the tool quietly starts a fresh index and the
manual tags in the old file, the only thing statements cannot reproduce,
stay behind in it.

The csv and cmd parsers are gone along with the [csv] and [cmd] config they
carried. cmd shelled out to the Python extractors, which were ported to Go
and deleted, so it bridged to nothing; csv was a generic column-mapped
fallback that no account used, and between them they were the largest
configuration surface in the tool. A bank is now described in Go, where it
can be tested. The importer tests register their own three-column parser
rather than borrow a bank's, so they stay about the directory walk, dedupe
and per-file error reporting.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-11 22:51:20 +02:00

165 lines
4.8 KiB
Go

package parser
import (
"strings"
"testing"
)
// place builds a fixed-width line by putting each string at a given column,
// which keeps these fixtures honest about the layout the parser measures.
func place(cells map[int]string) string {
width := 0
for col, text := range cells {
if end := col + len(text); end > width {
width = end
}
}
line := []byte(strings.Repeat(" ", width))
for col, text := range cells {
copy(line[col:], text)
}
return string(line)
}
// Column starts, spaced as a real statement is: wide enough that every token
// of "05 Jan 2026" stays closest to DATE rather than drifting into TYPE.
const (
colDate = 2
colType = 20
colDesc = 40
colMoneyIn = 75
colMoneyOut = 90
colBalance = 105
)
func trHeaderLine() string {
return place(map[int]string{
colDate: "DATE", colType: "TYPE", colDesc: "DESCRIPTION",
colMoneyIn: "MONEY IN", colMoneyOut: "MONEY OUT", colBalance: "BALANCE",
})
}
// A statement mixing both layouts: one transaction on a single line, one with
// its fields stacked across three, and one whose description wraps.
func trStatement() string {
lines := []string{
" Trade Republic Bank GmbH",
"",
trHeaderLine(),
"",
// Everything on one line.
place(map[int]string{
colDate: "05 Jan 2026", colType: "Deposit", colDesc: "Payment from ACME",
colMoneyIn: "€2,500.00", colBalance: "€2,500.00",
}),
"",
// Stacked across three lines.
place(map[int]string{
colDate: "06 Jan 2026", colMoneyOut: "€45.20", colBalance: "€2,454.80",
}),
place(map[int]string{colType: "Card"}),
place(map[int]string{colDesc: "LIDL SOFIA 4412"}),
"",
// Wrapped description, hyphen-broken on the first line, plus an IBAN.
place(map[int]string{
colDate: "10 Jan 2026", colType: "Transfer", colDesc: "Standing order to sav-",
colMoneyOut: "€500.00", colBalance: "€1,954.80",
}),
place(map[int]string{colDesc: "ings SI56123456789012345"}),
"",
" Page 1 of 2",
}
return strings.Join(lines, "\n")
}
func TestParseTradeRepublicText(t *testing.T) {
txns, err := parseTradeRepublicText(trStatement(), 2)
if err != nil {
t.Fatal(err)
}
if len(txns) != 3 {
t.Fatalf("got %d transactions, want 3: %+v", len(txns), txns)
}
deposit := txns[0]
if deposit.Date != "2026-01-05" {
t.Errorf("date = %q, want 2026-01-05", deposit.Date)
}
if deposit.AmountMinor != 250000 {
t.Errorf("money in = %d, want 250000", deposit.AmountMinor)
}
if deposit.Type != "Deposit" {
t.Errorf("type = %q, want Deposit", deposit.Type)
}
if deposit.Description != "Payment from ACME" {
t.Errorf("description = %q", deposit.Description)
}
// The stacked layout must produce the same shape as the single-line one.
card := txns[1]
if card.Date != "2026-01-06" {
t.Errorf("date = %q, want 2026-01-06", card.Date)
}
if card.AmountMinor != -4520 {
t.Errorf("money out = %d, want -4520", card.AmountMinor)
}
if card.Type != "Card" {
t.Errorf("type = %q, want Card", card.Type)
}
if card.Description != "LIDL SOFIA 4412" {
t.Errorf("description = %q", card.Description)
}
if card.BalanceMinor == nil || *card.BalanceMinor != 245480 {
t.Errorf("balance = %v, want 245480", card.BalanceMinor)
}
// A hyphen-broken word rejoins without a space. The hyphen itself is kept,
// as the original script does, because descriptions contain real hyphens
// that must not be swallowed.
transfer := txns[2]
if transfer.Description != "Standing order to sav-ings SI56123456789012345" {
t.Errorf("description = %q, want the wrapped fragment joined without a space", transfer.Description)
}
if transfer.AmountMinor != -50000 {
t.Errorf("amount = %d, want -50000", transfer.AmountMinor)
}
}
// Pages with no transaction table (cover pages, disclaimers) are skipped.
func TestParseTradeRepublicSkipsPagesWithoutTable(t *testing.T) {
text := " Some cover page with no table at all\n" + "\f" + trStatement()
txns, err := parseTradeRepublicText(text, 2)
if err != nil {
t.Fatal(err)
}
if len(txns) != 3 {
t.Errorf("got %d transactions, want 3", len(txns))
}
}
// A figure inside the description must not be mistaken for a money column.
func TestParseTradeRepublicIgnoresAmountsInDescription(t *testing.T) {
text := strings.Join([]string{
trHeaderLine(),
"",
place(map[int]string{
colDate: "05 Jan 2026", colType: "Card", colDesc: "Refund of €12.00 order",
colMoneyIn: "€12.00", colBalance: "€100.00",
}),
}, "\n")
txns, err := parseTradeRepublicText(text, 2)
if err != nil {
t.Fatal(err)
}
if len(txns) != 1 {
t.Fatalf("got %d transactions, want 1", len(txns))
}
if txns[0].AmountMinor != 1200 {
t.Errorf("amount = %d, want 1200", txns[0].AmountMinor)
}
if !strings.Contains(txns[0].Description, "€12.00") {
t.Errorf("description = %q, want the inline figure kept", txns[0].Description)
}
}