package metrics import ( "bytes" "encoding/csv" "strings" "testing" "time" ) func TestExportsAndPublicPage(t *testing.T) { d := openDB(t) now := time.Date(2027, 2, 15, 0, 0, 0, 0, time.UTC) addLines(t, d, monthly("cus_a", 10000, "2026-01", "2027-03")...) addLines(t, d, monthly("cus_b", 5000, "2026-06", "2027-03")...) d.Exec(`INSERT INTO stripe_objects (kind, id, customer, data) VALUES ('customer', 'cus_a', 'cus_a', '{"name":"Secret Co","email":"ceo@secret.example"}')`) d.Exec(`INSERT INTO stripe_objects (kind, id, customer, status, currency, data) VALUES ('invoice', 'pa', 'cus_a', 'paid', 'usd', '{"amount_paid":10000}')`) AddSpend(d, Spend{Month: "2026-06", Channel: "linkedin", AmountCents: 50000}, now) for _, e := range Exports { var buf bytes.Buffer if err := Export(&buf, d, e.Name, now); err != nil { t.Errorf("%s: %v", e.Name, err) continue } rows, err := csv.NewReader(&buf).ReadAll() if err != nil || len(rows) < 2 { t.Errorf("%s: %d rows %v", e.Name, len(rows), err) } } var buf bytes.Buffer Export(&buf, d, "mrr", now) if !strings.Contains(buf.String(), "2026-06,false,150.00,2,75.00,50.00,1,") { t.Errorf("mrr csv:\n%s", buf.String()) } buf.Reset() Export(&buf, d, "customers", now) if !strings.Contains(buf.String(), "cus_a,Secret Co,ceo@secret.example,100.00,2026-01") { t.Errorf("customers csv:\n%s", buf.String()) } if err := Export(&buf, d, "nope", now); err == nil { t.Error("an unknown table exported") } m, _ := Build(d, now) clv, _ := BuildCLV(d, m, now) page := PublicPage(m, clv, map[string]bool{"mrr": true, "customers": true, "growth": true, "nrr": true, "churn": true, "table": true}, now) for _, want := range []string{"end of January 2027", "**Monthly recurring revenue:** $150 ($1,800 a year)", "**Paying customers:** 2", "**Growth over 12 months:** +50%", "| January 2027 | $150 | 2 |", "| June 2026 | $150 | 2 | +$50 |"} { if !strings.Contains(page, want) { t.Errorf("page lacks %q:\n%s", want, page) } } for _, never := range []string{"Secret", "secret.example", "cus_"} { if strings.Contains(page, never) { t.Errorf("the public page names a customer: %q", never) } } if p := PublicPage(m, clv, map[string]bool{"mrr": true}, now); strings.Contains(p, "|") || strings.Contains(p, "customers") { t.Errorf("unchosen parts shown:\n%s", p) } }