package metrics import ( "fmt" "testing" "time" ) func TestCLV(t *testing.T) { d := openDB(t) now := time.Date(2026, 7, 15, 0, 0, 0, 0, time.UTC) // Ten customers at $100 from January; one leaves at the end of March, // one at the end of May. Products: all "prod_core", at a 50% margin. for i := 0; i < 10; i++ { to := "2026-08" switch i { case 0: to = "2026-03" case 1: to = "2026-05" } addLines(t, d, monthly(fmt.Sprintf("cus_%d", i), 10000, "2026-01", to)...) } d.Exec(`UPDATE stripe_invoice_lines SET product = 'prod_core'`) SaveMargins(d, Margins{Default: 60, ByProduct: map[string]float64{"prod_core": 50}}) AddSpend(d, Spend{Month: "2026-01", Channel: "general", AmountCents: 300000}, now) m, _ := Build(d, now) c, err := BuildCLV(d, m, now) if err != nil || c == nil { t.Fatal(err) } // Complete months: Jan–Jun. Churn: 2 lost over 5 month-starts of // 10+10+9+9+8 customers = 2/46. if c.Month != "2026-06" || c.ARPA != 10000 || c.Margin != 50 || c.ChurnMonths != 5 || !near(c.Churn*46, 200) { t.Fatalf("clv: %+v", c) } // CLV = $100 × 50% × 23 months = $1,150; CAC = $3,000 ÷ 10 = $300. if !near(c.Value, 115000) || !near(c.CAC, 30000) || c.Ratio < 3.83 || c.Ratio > 3.84 { t.Errorf("value: %+v", c) } } func TestRevenueByCohort(t *testing.T) { d := openDB(t) now := time.Date(2026, 4, 15, 0, 0, 0, 0, time.UTC) addLines(t, d, monthly("cus_a", 10000, "2026-01", "2026-05")...) addLines(t, d, monthly("cus_b", 10000, "2026-01", "2026-05")...) at := func(s string) int64 { return day(s) } for _, inv := range []struct { id, cust string paid int64 when string }{ {"p1", "cus_a", 10000, "2026-01-10"}, {"p2", "cus_a", 10000, "2026-02-10"}, {"p3", "cus_b", 10000, "2026-01-10"}, {"p4", "cus_b", 10000, "2026-02-10"}, {"p5", "cus_b", 10000, "2026-03-10"}, } { d.Exec(`INSERT INTO stripe_objects (kind, id, customer, created, status, currency, data) VALUES ('invoice', ?, ?, ?, 'paid', 'usd', ?)`, inv.id, inv.cust, at(inv.when), fmt.Sprintf(`{"amount_paid":%d,"status_transitions":{"paid_at":%d}}`, inv.paid, at(inv.when))) } // cus_b gets $40 back in February; a $20 post-payment credit in March, // $5 of it refunded (counted as a refund); and a pre-payment note, // which the amount paid already reflects. d.Exec(`INSERT INTO stripe_objects (kind, id, customer, created, status, currency, amount, data) VALUES ('refund', 'r1', 'cus_b', ?, 'succeeded', 'usd', 4000, '{}'), ('refund', 'r2', 'cus_b', ?, 'canceled', 'usd', 9999, '{}'), ('credit_note', 'cn1', 'cus_b', ?, 'issued', 'usd', 2000, '{"type":"post_payment","post_payment_amount":2000,"refunds":[{"amount_refunded":500}]}'), ('credit_note', 'cn2', 'cus_a', ?, 'issued', 'usd', 3000, '{"type":"pre_payment","pre_payment_amount":3000,"post_payment_amount":0}')`, at("2026-02-20"), at("2026-02-21"), at("2026-03-20"), at("2026-02-01")) m, _ := Build(d, now) cos, err := RevenueByCohort(d, m, 5, 4, now) if err != nil || len(cos) != 1 { t.Fatalf("cohorts: %+v %v", cos, err) } co := cos[0] // Two customers, cumulative: Jan $200 → $100 each; Feb +$200 −$40 = // $360 → $180; Mar +$100 −$15 = $445 → $222.50; Apr nothing → same. want := []float64{10000, 18000, 22250, 22250} for i, w := range want { if c := co.PerCustomer[i]; !c.Known || !near(c.V, w) { t.Errorf("month %d: %+v, want %.0f", i, c, w) } } if co.PerCustomer[4].Known { t.Error("a month not reached yet is known") } if !near(co.Margin[1].V, 18000*0.6) { t.Errorf("gross profit: %v", co.Margin[1]) } } func TestNegativeCohortRevenueShows(t *testing.T) { d := openDB(t) now := time.Date(2026, 3, 15, 0, 0, 0, 0, time.UTC) addLines(t, d, monthly("cus_r", 10000, "2026-01", "2026-02")...) d.Exec(`INSERT INTO stripe_objects (kind, id, customer, created, status, currency, data) VALUES ('invoice', 'pr', 'cus_r', ?, 'paid', 'usd', '{"amount_paid":10000}')`, day("2026-01-10")) d.Exec(`INSERT INTO stripe_objects (kind, id, customer, created, status, currency, amount, data) VALUES ('refund', 'rr', 'cus_r', ?, 'succeeded', 'usd', 15000, '{}')`, day("2026-02-01")) m, _ := Build(d, now) cos, _ := RevenueByCohort(d, m, 5, 2, now) if len(cos) != 1 || !cos[0].PerCustomer[1].Known || cos[0].PerCustomer[1].V != -5000 { t.Errorf("negative revenue: %+v", cos) } }