Files
money/internal/parser/nlb_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

115 lines
3.9 KiB
Go

package parser
import "testing"
// A page as pdftotext -layout renders it: a header, transaction lines ending
// in amount and balance, and indented continuation lines carrying wrapped
// descriptions and the other side's IBAN.
// The description column sits at 15, which is what the script's
// CONTINUATION_INDENT tells us about the real layout.
const nlbPage = ` NLB d.d.
IZPISEK 001/2026
DATUM OPIS ZNESEK STANJE
02.01.26 PLACILO S KARTICO -45,20 1.234,56
LIDL SOFIA 4412 SI56 1234 5678 9012 345
05.01.26 PRILIV PLACE +2.500,00 3.734,56
ACME DOO
10.01.26 PRENOS NA VARCEVALNI -500,00 3.234,56
15.01.26 NEGATIVNO STANJE -3.300,00 65,44-
Stran 1
`
func TestParseNLBText(t *testing.T) {
txns, err := parseNLBText(nlbPage, 2)
if err != nil {
t.Fatal(err)
}
if len(txns) != 4 {
t.Fatalf("got %d transactions, want 4: %+v", len(txns), txns)
}
first := txns[0]
if first.Date != "2026-01-02" {
t.Errorf("date = %q, want 2026-01-02", first.Date)
}
// The continuation line is appended to the description, and the IBAN in
// its second column goes last so that it stays contiguous however many
// lines it wrapped over.
if first.Description != "PLACILO S KARTICO LIDL SOFIA 4412 SI56 1234 5678 9012 345" {
t.Errorf("description = %q", first.Description)
}
if first.AmountMinor != -4520 {
t.Errorf("amount = %d, want -4520", first.AmountMinor)
}
if first.BalanceMinor == nil || *first.BalanceMinor != 123456 {
t.Errorf("balance = %v, want 123456", first.BalanceMinor)
}
if txns[1].AmountMinor != 250000 {
t.Errorf("credit amount = %d, want 250000", txns[1].AmountMinor)
}
if txns[1].Description != "PRILIV PLACE ACME DOO" {
t.Errorf("description = %q", txns[1].Description)
}
// A transaction with no continuation line gets nothing appended.
if txns[2].Description != "PRENOS NA VARCEVALNI" {
t.Errorf("description = %q, want no trailing account number", txns[2].Description)
}
// A trailing minus marks a negative balance.
if txns[3].BalanceMinor == nil || *txns[3].BalanceMinor != -6544 {
t.Errorf("balance = %v, want -6544", txns[3].BalanceMinor)
}
}
// A wrapped line at the top of a page must not attach to the last transaction
// of the previous page.
func TestParseNLBPageBoundary(t *testing.T) {
text := "02.01.26 FIRST -10,00 100,00\n" +
"\f" +
" STRAY CONTINUATION AT TOP OF PAGE\n" +
"03.01.26 SECOND -20,00 80,00\n" +
" REAL CONTINUATION\n"
txns, err := parseNLBText(text, 2)
if err != nil {
t.Fatal(err)
}
if len(txns) != 2 {
t.Fatalf("got %d transactions, want 2", len(txns))
}
if txns[0].Description != "FIRST" {
t.Errorf("first description = %q, want FIRST (page furniture must not leak in)", txns[0].Description)
}
if txns[1].Description != "SECOND REAL CONTINUATION" {
t.Errorf("second description = %q", txns[1].Description)
}
}
// Shallowly indented lines are footers, not wrapped descriptions.
func TestParseNLBIgnoresShallowIndent(t *testing.T) {
text := "02.01.26 FIRST -10,00 100,00\n" +
" Stran 1 od 2\n"
txns, err := parseNLBText(text, 2)
if err != nil {
t.Fatal(err)
}
if len(txns) != 1 || txns[0].Description != "FIRST" {
t.Errorf("got %+v, want a single FIRST transaction", txns)
}
}
// A date-led line without an amount and balance is a real problem, not
// something to silently drop.
func TestParseNLBRejectsMalformedLine(t *testing.T) {
text := "02.01.26 NO AMOUNT OR BALANCE HERE\n"
if _, err := parseNLBText(text, 2); err == nil {
t.Error("expected an error for a transaction line with no amount")
}
}