package inventory import ( "path/filepath" "strings" "testing" "time" "gopkg.in/yaml.v3" "supermarket/internal/db" ) func TestPrices(t *testing.T) { d, err := db.Open(filepath.Join(t.TempDir(), "app.db")) if err != nil { t.Fatal(err) } defer d.Close() e := &Entry{Name: "Zendesk", Relationship: Competitor, Prices: []Price{ {Plan: " Suite Team ", Amount: "$55", Per: "per agent per month", Checked: "2026-11-24", Source: "https://www.zendesk.com/pricing/"}, {Plan: "", Amount: "", Per: "", Checked: "2026-11-24"}, // an empty row, with today's date: dropped {Plan: "Old", Amount: "$49", Per: "per agent per month", Checked: "2026-03-01"}, }} if err := Save(d, e); err != nil { t.Fatal(err) } got, err := Get(d, e.ID) if err != nil || len(got.Prices) != 2 || got.Prices[0].Plan != "Suite Team" { t.Fatalf("saved: %+v %v", got, err) } if got.Prices[0].String() != "Suite Team: $55 per agent per month" || got.Prices[0].CheckedOn() != "24 Nov 2026" { t.Errorf("words: %q %q", got.Prices[0].String(), got.Prices[0].CheckedOn()) } now := time.Date(2026, 11, 24, 0, 0, 0, 0, time.UTC) if st := got.StalePrices(now); len(st) != 1 || st[0].Plan != "Old" { t.Errorf("stale: %+v", st) } prompt := got.PricesPrompt() for _, want := range []string{"the only prices you may state for them", "- Suite Team: $55 per agent per month (checked Nov 2026, from https://www.zendesk.com/pricing/)"} { if !strings.Contains(prompt, want) { t.Errorf("prompt lacks %q:\n%s", want, prompt) } } // Exported and imported with the rest of the entry. raw, _ := yaml.Marshal(got) var back Entry if err := yaml.Unmarshal(raw, &back); err != nil || len(back.Prices) != 2 || back.Prices[1].Checked != "2026-03-01" { t.Errorf("yaml: %v %+v\n%s", err, back.Prices, raw) } for _, bad := range []Price{ {Amount: "$5"}, // no date {Amount: "$5", Checked: "24/11/2026"}, // not a date {Plan: "Pro", Checked: "2026-11-24", Per: "x"}, // a row without its price {Amount: "$5", Checked: "2026-11-24", Source: "zendesk.com/pricing"}, } { if err := Save(d, &Entry{Name: "Bad", Relationship: Competitor, Prices: []Price{bad}}); err == nil { t.Errorf("accepted %+v", bad) } } }