A tag could come from two places: rules.toml, or the TUI's t key, which wrote manual_tag with COALESCE(manual_tag, rule_tag) deciding the winner. That split paid for itself in the first invariant of the codebase, in ClearOverrides and the c key, in the * marker on the tag column, and in the one exception to a disposable index -- a tag set by hand was the only thing in index.db that the statements could not reproduce. Now rules.toml decides every tag. The index is derived entirely from the statements plus that file, so deleting it and re-importing gets back exactly what was there, and retag has nothing to be careful of. Tagging a one-off means writing a narrow rule on screen 4, which previews what the glob catches before it is saved. A manual tag in an existing index is dropped along with the column the first time this build opens it, and those rows read as whatever the rules say, or as untagged. TestManualTagSurvivesRetag guarded the invariant that has just been removed; TestRetagRewritesEveryTag replaces it with the one that took its place, and keeps Retag itself covered. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
216 lines
6.5 KiB
Go
216 lines
6.5 KiB
Go
package rules
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"git.petrovv.com/nikola/money/internal/config"
|
|
"git.petrovv.com/nikola/money/internal/model"
|
|
"git.petrovv.com/nikola/money/internal/store"
|
|
)
|
|
|
|
func testDB(t *testing.T) *store.DB {
|
|
t.Helper()
|
|
db, err := store.Open(filepath.Join(t.TempDir(), "index.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { db.Close() })
|
|
return db
|
|
}
|
|
|
|
// seed inserts transactions with no rule verdict yet.
|
|
func seed(t *testing.T, db *store.DB, descriptions ...string) int64 {
|
|
t.Helper()
|
|
accountID, err := db.UpsertAccount(model.Account{
|
|
Slug: "checking", Name: "Checking", Currency: "EUR", MinorDigits: 2,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sourceID, err := db.SourceFile(accountID, "checking/st.csv", "sha", "2026-01-01T00:00:00Z")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, d := range descriptions {
|
|
added, err := db.InsertTransaction(model.Transaction{
|
|
AccountID: accountID,
|
|
SourceFileID: sourceID,
|
|
Fingerprint: d,
|
|
Date: "2026-01-0" + string(rune('1'+i)),
|
|
Description: d,
|
|
AmountMinor: -1000,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !added {
|
|
t.Fatalf("seed %q was not inserted", d)
|
|
}
|
|
}
|
|
return accountID
|
|
}
|
|
|
|
func byDescription(t *testing.T, db *store.DB, desc string) model.Transaction {
|
|
t.Helper()
|
|
txns, err := db.Transactions(store.Filter{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, x := range txns {
|
|
if x.Description == desc {
|
|
return x
|
|
}
|
|
}
|
|
t.Fatalf("no transaction with description %q", desc)
|
|
return model.Transaction{}
|
|
}
|
|
|
|
// A tag is derived state: rules.toml is the only thing that decides it, so
|
|
// retagging rewrites every tag from the current rules however often it runs,
|
|
// including back to nothing when no rule matches any more.
|
|
func TestRetagRewritesEveryTag(t *testing.T) {
|
|
db := testDB(t)
|
|
seed(t, db, "LIDL SOFIA", "SOME RANDOM SHOP")
|
|
|
|
engine := New(&config.Rules{Rule: []config.Rule{
|
|
{Match: "*LIDL*", Tag: "groceries"},
|
|
{Match: "*SHOP*", Tag: "shopping"},
|
|
}})
|
|
n, err := engine.Retag(db)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n != 2 {
|
|
t.Errorf("retagged %d rows, want 2", n)
|
|
}
|
|
if got := byDescription(t, db, "LIDL SOFIA").RuleTag; got != "groceries" {
|
|
t.Errorf("tag = %q, want groceries", got)
|
|
}
|
|
|
|
// Re-running with the same rules changes nothing and says so.
|
|
if n, err := engine.Retag(db); err != nil || n != 0 {
|
|
t.Errorf("second retag: %d rows, %v; want 0 and no error", n, err)
|
|
}
|
|
|
|
// New rules win, and a row no rule matches loses its tag entirely.
|
|
engine = New(&config.Rules{Rule: []config.Rule{
|
|
{Match: "*LIDL*", Tag: "food"},
|
|
}})
|
|
if _, err := engine.Retag(db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := byDescription(t, db, "LIDL SOFIA").RuleTag; got != "food" {
|
|
t.Errorf("tag = %q, want food", got)
|
|
}
|
|
if got := byDescription(t, db, "SOME RANDOM SHOP").RuleTag; got != "" {
|
|
t.Errorf("tag = %q, want empty once no rule matches", got)
|
|
}
|
|
}
|
|
|
|
func TestFirstMatchWins(t *testing.T) {
|
|
engine := New(&config.Rules{Rule: []config.Rule{
|
|
{Match: "*LIDL EXPRESS*", Tag: "snacks"},
|
|
{Match: "*LIDL*", Tag: "groceries"},
|
|
}})
|
|
if tag := engine.Apply("checking", "CARD LIDL EXPRESS 12"); tag != "snacks" {
|
|
t.Errorf("tag = %q, want snacks (earlier rule must win)", tag)
|
|
}
|
|
if tag := engine.Apply("checking", "CARD LIDL 12"); tag != "groceries" {
|
|
t.Errorf("tag = %q, want groceries", tag)
|
|
}
|
|
if tag := engine.Apply("checking", "SOMETHING ELSE"); tag != "" {
|
|
t.Errorf("tag = %q, want empty for an unmatched description", tag)
|
|
}
|
|
}
|
|
|
|
// A rule can be dead two ways: nothing matches it, or an earlier rule already
|
|
// claimed everything it would have caught. Usage must report both as zero.
|
|
func TestUsageCountsFirstMatchOnly(t *testing.T) {
|
|
engine := New(&config.Rules{Rule: []config.Rule{
|
|
{Match: "*LIDL*", Tag: "groceries"}, // claims both LIDL rows
|
|
{Match: "*LIDL SOFIA*", Tag: "shadowed"}, // fully shadowed by the above
|
|
{Match: "*NOTHING MATCHES ME*", Tag: "no"}, // matches nothing at all
|
|
{Match: "*PAYROLL*", Tag: "salary"}, // claims one row
|
|
}})
|
|
|
|
txns := []model.Transaction{
|
|
{AccountSlug: "checking", Description: "LIDL SOFIA 4412"},
|
|
{AccountSlug: "checking", Description: "LIDL VARNA 9911"},
|
|
{AccountSlug: "checking", Description: "ACME PAYROLL"},
|
|
{AccountSlug: "checking", Description: "UNMATCHED SHOP"},
|
|
}
|
|
|
|
usage := engine.Usage(txns)
|
|
want := []int{2, 0, 0, 1}
|
|
if len(usage) != len(want) {
|
|
t.Fatalf("usage has %d entries, want %d", len(usage), len(want))
|
|
}
|
|
for i := range want {
|
|
if usage[i] != want[i] {
|
|
t.Errorf("rule %d used by %d transactions, want %d", i+1, usage[i], want[i])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMatchIndex(t *testing.T) {
|
|
engine := New(&config.Rules{Rule: []config.Rule{
|
|
{Match: "*LIDL EXPRESS*", Tag: "snacks"},
|
|
{Match: "*LIDL*", Tag: "groceries"},
|
|
}})
|
|
|
|
if got := engine.MatchIndex("checking", model.Transaction{Description: "LIDL EXPRESS 1"}); got != 0 {
|
|
t.Errorf("index = %d, want 0", got)
|
|
}
|
|
if got := engine.MatchIndex("checking", model.Transaction{Description: "LIDL 1"}); got != 1 {
|
|
t.Errorf("index = %d, want 1", got)
|
|
}
|
|
if got := engine.MatchIndex("checking", model.Transaction{Description: "OTHER"}); got != -1 {
|
|
t.Errorf("index = %d, want -1 for no match", got)
|
|
}
|
|
}
|
|
|
|
func TestAccountScopedRule(t *testing.T) {
|
|
engine := New(&config.Rules{Rule: []config.Rule{
|
|
{Match: "*TRANSFER*", Tag: "transfer", Account: "savings"},
|
|
{Match: "*TRANSFER*", Tag: "misc"},
|
|
}})
|
|
|
|
if tag := engine.Apply("savings", "TRANSFER FROM CHECKING"); tag != "transfer" {
|
|
t.Errorf("savings: tag = %q, want transfer", tag)
|
|
}
|
|
if tag := engine.Apply("checking", "TRANSFER FROM CHECKING"); tag != "misc" {
|
|
t.Errorf("checking: tag = %q, want misc", tag)
|
|
}
|
|
}
|
|
|
|
// Besides the description, a rule can match the bank's own classification of
|
|
// the transaction, on its own or alongside a description glob.
|
|
func TestTypeRules(t *testing.T) {
|
|
engine := New(&config.Rules{Rule: []config.Rule{
|
|
{Type: "CARD_PAYMENT", Match: "*LIDL*", Tag: "groceries"},
|
|
{Type: "ATM", Tag: "cash"},
|
|
}})
|
|
|
|
// A rule setting several patterns requires all of them to match.
|
|
tag := engine.ApplyTxn("checking", model.Transaction{
|
|
Description: "LIDL SOFIA", Type: "CARD_PAYMENT",
|
|
})
|
|
if tag != "groceries" {
|
|
t.Errorf("tag = %q, want groceries", tag)
|
|
}
|
|
tag = engine.ApplyTxn("checking", model.Transaction{
|
|
Description: "LIDL SOFIA", Type: "TRANSFER",
|
|
})
|
|
if tag != "" {
|
|
t.Errorf("tag = %q, want empty: the type does not match", tag)
|
|
}
|
|
|
|
// Type alone works too.
|
|
if tag := engine.ApplyTxn("checking", model.Transaction{
|
|
Description: "WITHDRAWAL", Type: "ATM",
|
|
}); tag != "cash" {
|
|
t.Errorf("tag = %q, want cash", tag)
|
|
}
|
|
}
|