Files
fuj-management/go/internal/services/banksync/infer_test.go
T
kacerrandClaude Opus 5.5 bbad19d822 feat(infer): messages starting with "other:" are not monthly fees
When the bank message starts with "other:" (case-insensitive),
inference does not look for a month, neither in the text nor from the
payment date. The trimmed message is written to Purpose, so reconcile
books the payment as an "other" transaction for the person instead of
a monthly fee. Messages containing "other:" elsewhere are unchanged.

- matching.IsOtherPurpose / is_other_purpose shared by inference and
  reconcile
- reconcile applies the rule to rows not yet processed by inference
- no person found: Purpose is still written, row shows as unmatched
- implemented in Go and legacy Python; four new parity fixtures

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 12:37:03 +02:00

194 lines
5.5 KiB
Go

package banksync
import (
"context"
"fuj-management/go/internal/domain/reconcile"
"fuj-management/go/internal/io/sheets"
"testing"
)
type fakeAttendance struct {
adults, juniors []reconcile.Member
}
func (f *fakeAttendance) LoadAdults(_ context.Context) ([]reconcile.Member, []string, error) {
return f.adults, nil, nil
}
func (f *fakeAttendance) LoadJuniors(_ context.Context) ([]reconcile.Member, []string, error) {
return f.juniors, nil, nil
}
var paymentsHeader = []any{
"Date", "Amount", "manual fix", "Person", "Purpose", "Inferred Amount",
"Sender", "VS", "Message", "Bank ID", "Sync ID",
}
func TestInferPayments_BasicMatch(t *testing.T) {
sh := &sheets.Fake{Values: map[string][][]any{
"SHEETID/A1:Z": {
paymentsHeader,
// Row with no Person/Purpose — should be inferred
{"2026-04-10", 750.0, "", "", "", "", "Jana Novakova", "123", "duben 2026", "", ""},
},
}}
att := &fakeAttendance{
adults: []reconcile.Member{{Name: "Jana Novakova", Tier: "A"}},
}
n, err := InferPayments(context.Background(), "SHEETID", sh, att, InferOpts{})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("want 1 row updated, got %d", n)
}
if len(sh.BatchUpdated) != 1 {
t.Fatalf("want 1 batch update call, got %d", len(sh.BatchUpdated))
}
upd := sh.BatchUpdated[0].Updates[0]
person := upd.Values[0][0].(string)
if person != "Jana Novakova" {
t.Errorf("inferred person: want 'Jana Novakova', got %q", person)
}
}
func TestInferPayments_OtherMessage(t *testing.T) {
sh := &sheets.Fake{Values: map[string][][]any{
"SHEETID/A1:Z": {
paymentsHeader,
// Known member: Person filled, message copied to Purpose, no month.
{"2026-04-10", 300.0, "", "", "", "", "Jana Novakova", "", "other: dres", "", ""},
// Unknown sender: Purpose is still written, Person stays empty.
{"2026-04-11", 500.0, "", "", "", "", "Unknown Payer", "", "Other: turnaj", "", ""},
},
}}
att := &fakeAttendance{
adults: []reconcile.Member{{Name: "Jana Novakova", Tier: "A"}},
}
n, err := InferPayments(context.Background(), "SHEETID", sh, att, InferOpts{})
if err != nil {
t.Fatal(err)
}
if n != 2 {
t.Fatalf("want 2 rows updated, got %d", n)
}
updates := sh.BatchUpdated[0].Updates
want := [][]any{
{"Jana Novakova", "other: dres", "300"},
{"", "Other: turnaj", "500"},
}
for i, w := range want {
got := updates[i].Values[0]
for j := range w {
if got[j] != w[j] {
t.Errorf("row %d col %d: want %q, got %q", i, j, w[j], got[j])
}
}
}
}
func TestInferPayments_SkipRule_ManualFix(t *testing.T) {
sh := &sheets.Fake{Values: map[string][][]any{
"SHEETID/A1:Z": {
paymentsHeader,
// manual fix is set — must be skipped
{"2026-04-10", 750.0, "yes", "", "", "", "Jana Novakova", "", "", "", ""},
},
}}
att := &fakeAttendance{adults: []reconcile.Member{{Name: "Jana Novakova", Tier: "A"}}}
n, err := InferPayments(context.Background(), "SHEETID", sh, att, InferOpts{})
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("want 0 updates (manual fix set), got %d", n)
}
}
func TestInferPayments_SkipRule_PersonAlreadySet(t *testing.T) {
sh := &sheets.Fake{Values: map[string][][]any{
"SHEETID/A1:Z": {
paymentsHeader,
{"2026-04-10", 750.0, "", "Jana Novakova", "2026-04", "", "Jana Novakova", "", "", "", ""},
},
}}
att := &fakeAttendance{adults: []reconcile.Member{{Name: "Jana Novakova", Tier: "A"}}}
n, err := InferPayments(context.Background(), "SHEETID", sh, att, InferOpts{})
if err != nil {
t.Fatal(err)
}
if n != 0 {
t.Errorf("want 0 updates (person already set), got %d", n)
}
}
func TestInferPayments_DryRun(t *testing.T) {
sh := &sheets.Fake{Values: map[string][][]any{
"SHEETID/A1:Z": {
paymentsHeader,
{"2026-04-10", 750.0, "", "", "", "", "Jana Novakova", "123", "duben 2026", "", ""},
},
}}
att := &fakeAttendance{adults: []reconcile.Member{{Name: "Jana Novakova", Tier: "A"}}}
n, err := InferPayments(context.Background(), "SHEETID", sh, att, InferOpts{DryRun: true})
if err != nil {
t.Fatal(err)
}
if n != 1 {
t.Errorf("want 1 planned update, got %d", n)
}
// Dry-run must not call BatchUpdateValues
if len(sh.BatchUpdated) != 0 {
t.Error("dry-run must not call BatchUpdateValues")
}
}
func TestInferPayments_ReviewPrefix(t *testing.T) {
sh := &sheets.Fake{Values: map[string][][]any{
"SHEETID/A1:Z": {
paymentsHeader,
// "novak" as sender alone → review confidence
{"2026-04-10", 750.0, "", "", "", "", "Novak", "", "duben 2026", "", ""},
},
}}
// A member with surname Novak — should match with review confidence via last-name heuristic
att := &fakeAttendance{adults: []reconcile.Member{{Name: "Pavel Novak", Tier: "A"}}}
n, err := InferPayments(context.Background(), "SHEETID", sh, att, InferOpts{})
if err != nil {
t.Fatal(err)
}
if n == 0 {
// Novak is in commonSurnames list so it won't match — acceptable
t.Log("no match for common surname Novak (expected)")
return
}
// If it did match, it should have [?] prefix
upd := sh.BatchUpdated[0].Updates[0]
person := upd.Values[0][0].(string)
if !isReviewPrefixed(person) && n > 0 {
t.Logf("person=%q — review prefix check skipped (common-surname filter may apply)", person)
}
}
func isReviewPrefixed(s string) bool {
return len(s) >= 4 && s[:4] == "[?] "
}
func TestDedupeMembers(t *testing.T) {
members := []reconcile.Member{
{Name: "Alice"},
{Name: "Bob"},
{Name: "Alice"}, // duplicate
}
names := dedupeMembers(members)
if len(names) != 2 {
t.Errorf("want 2 unique names, got %d: %v", len(names), names)
}
}