Add a rules screen that finds and removes dead rules

Nothing showed whether a rule was still earning its place. The new screen, on
5, lists every rule in file order with the number of transactions it claims,
marking those that claim none.

The count comes from Engine.Usage, which counts by first match, so a rule
shadowed by an earlier one reports zero even though its glob matches. That is
the case worth catching: such a rule looks correct in isolation and can never
fire.

d removes the selected rule and p removes every unused one, each behind a y/n
confirmation since this rewrites a hand-maintained file. config.DeleteRules
edits rules.toml textually rather than re-serialising the parsed rules, so
comments and layout survive; a comment directly above a rule goes with it,
while one separated by a blank line is left as a heading. The result is
re-parsed before it replaces the file.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-09 02:02:01 +02:00
co-authored by Claude Opus 5
parent aeca203855
commit c2eba53fd4
8 changed files with 831 additions and 9 deletions
+186
View File
@@ -654,6 +654,192 @@ func TestRuleBuilderTabCyclesFieldsAndEscLeaves(t *testing.T) {
}
}
// newRuleListModel seeds a data root whose rules.toml holds one useful rule,
// one shadowed rule and one that matches nothing.
func newRuleListModel(t *testing.T) (*Model, string) {
t.Helper()
m, _, root := newRuleModel(t)
rulesTOML := `# My rules.
# The weekly shop.
[[rule]]
match = "*LIDL*"
tag = "groceries"
[[rule]]
match = "*LIDL SOFIA*"
tag = "shadowed"
[[rule]]
match = "*NEVER MATCHES ANYTHING*"
tag = "dead"
[[rule]]
match = "*ZARA*"
tag = "clothes"
`
if err := os.WriteFile(filepath.Join(root, config.RulesFile), []byte(rulesTOML), 0o644); err != nil {
t.Fatal(err)
}
loaded, err := config.LoadRules(root)
if err != nil {
t.Fatal(err)
}
m.engine = rules.New(loaded)
if _, err := m.engine.Retag(m.db); err != nil {
t.Fatal(err)
}
key(t, m, "5")
return m, root
}
func TestRuleListCountsUsage(t *testing.T) {
m, _ := newRuleListModel(t)
if m.view != viewRuleList {
t.Fatal("expected 5 to open the rule list")
}
// LIDL SOFIA x2 plus LIDL VARNA; the shadowed and dead rules claim nothing.
want := []int{3, 0, 0, 0}
if len(m.ruleUsage) != len(want) {
t.Fatalf("usage = %v, want %d entries", m.ruleUsage, len(want))
}
for i := range want {
if m.ruleUsage[i] != want[i] {
t.Errorf("rule %d used by %d, want %d", i+1, m.ruleUsage[i], want[i])
}
}
// Unused rules are marked so they can be picked out at a glance.
rows := m.ruleListTable.Rows()
if rows[0][1] != " " {
t.Errorf("the used rule is marked %q, want no marker", rows[0][1])
}
for _, i := range []int{1, 2, 3} {
if rows[i][1] != "✗" {
t.Errorf("rule %d marker = %q, want ✗", i+1, rows[i][1])
}
}
if !strings.Contains(m.View(), "3 match nothing") {
t.Errorf("expected the title to count the dead rules:\n%s", m.View())
}
}
// Deleting rewrites a hand-maintained file, so it takes a confirmation.
func TestRuleListDeleteNeedsConfirmation(t *testing.T) {
m, root := newRuleListModel(t)
m.ruleListTable.SetCursor(2) // the dead rule
key(t, m, "d")
if m.confirm != confirmDeleteRule {
t.Fatal("expected d to ask for confirmation")
}
if !strings.Contains(m.status, "delete rule 3") {
t.Errorf("status = %q, want the rule identified", m.status)
}
key(t, m, "n") // anything but y cancels
if m.confirm != confirmNone {
t.Error("expected the confirmation to clear")
}
loaded, _ := config.LoadRules(root)
if len(loaded.Rule) != 4 {
t.Errorf("cancelling deleted something: %d rules left", len(loaded.Rule))
}
}
func TestRuleListDeleteSelected(t *testing.T) {
m, root := newRuleListModel(t)
m.ruleListTable.SetCursor(1) // the shadowed rule
key(t, m, "d")
key(t, m, "y")
if m.err != nil {
t.Fatalf("delete failed: %v", m.err)
}
loaded, err := config.LoadRules(root)
if err != nil {
t.Fatal(err)
}
if len(loaded.Rule) != 3 {
t.Fatalf("got %d rules, want 3: %+v", len(loaded.Rule), loaded.Rule)
}
for _, r := range loaded.Rule {
if r.Tag == "shadowed" {
t.Error("the selected rule is still in rules.toml")
}
}
// The on-screen counts are rebuilt from the new file.
if len(m.ruleUsage) != 3 {
t.Errorf("usage still has %d entries, want 3", len(m.ruleUsage))
}
}
func TestRuleListPruneUnused(t *testing.T) {
m, root := newRuleListModel(t)
key(t, m, "p")
if m.confirm != confirmPruneRules {
t.Fatal("expected p to ask for confirmation")
}
if !strings.Contains(m.status, "all 3 rules") {
t.Errorf("status = %q, want the count of dead rules", m.status)
}
key(t, m, "y")
if m.err != nil {
t.Fatalf("prune failed: %v", m.err)
}
loaded, err := config.LoadRules(root)
if err != nil {
t.Fatal(err)
}
if len(loaded.Rule) != 1 || loaded.Rule[0].Tag != "groceries" {
t.Fatalf("remaining rules = %+v, want only the groceries rule", loaded.Rule)
}
// The rule that was doing work still tags what it did before.
txns, err := m.db.Transactions(store.Filter{})
if err != nil {
t.Fatal(err)
}
tagged := 0
for _, txn := range txns {
if txn.Tag() == "groceries" {
tagged++
}
}
if tagged != 3 {
t.Errorf("%d transactions tagged after pruning, want 3", tagged)
}
if !strings.Contains(m.View(), "all in use") {
t.Errorf("expected the title to report no dead rules left:\n%s", m.View())
}
}
func TestRuleListPruneWithNothingToDo(t *testing.T) {
m, root := newRuleListModel(t)
key(t, m, "p")
key(t, m, "y")
// Second prune: nothing left to remove.
key(t, m, "p")
if m.confirm != confirmNone {
t.Error("expected no confirmation when there is nothing to prune")
}
if !strings.Contains(m.status, "no unused rules") {
t.Errorf("status = %q, want a note that there is nothing to prune", m.status)
}
loaded, _ := config.LoadRules(root)
if len(loaded.Rule) != 1 {
t.Errorf("got %d rules, want the 1 survivor untouched", len(loaded.Rule))
}
}
// newEmptyModel builds a model over an index with nothing in it.
func newEmptyModel(t *testing.T, accounts []*config.Account) *Model {
t.Helper()