Files
davidandClaude Opus 5.5 c36f786ae9 Fixes from the first round of TEST.md
SEO: a domain property (sc-domain:) covers every subdomain, so the shop,
Gitea and forum were counted as the website. What ranks, the morning
check, the Monday report, keywords, change candidates and the audit's
traffic now read only the website's own host (with or without www).
The report keeps pages worth a proposal apart from "Waiting for your
yes", explains only moves past the thresholds, and says "places up/down".

Also:
- Feed summaries drop the posts' scripts and styles.
- Spend you log can't be negative.
- Test and check buttons can't be clicked twice while they run.
- Previews show the site's web fonts and icon sprites.
- Backups are read without leaving -wal/-shm files; deleting one removes
  them, and leftovers are swept at start.
- The Overview's setup flags a CRM that Hermes can't reach.
- Import now says what came in; adding a prospect that exists says so;
  prospects can be deleted.
- Signal tags and brief addresses keep accented letters (Ü → u).
- Page errors start with a capital; clearer messages for bad uploads,
  CRM retry and cancel, keywords, and the finished audit.
- The module you're in stays unfolded in the sidebar.
- A picked keyword can be unpicked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 06:31:42 -07:00

113 lines
4.5 KiB
Go

package metrics
import (
"strings"
"testing"
"time"
)
func TestSpendLog(t *testing.T) {
d := openDB(t)
now := time.Date(2026, 11, 22, 0, 0, 0, 0, time.UTC)
if err := AddSpend(d, Spend{Month: "2026-11", Channel: "linkedin", AmountCents: 32000}, now); err != nil {
t.Fatal(err)
}
for _, bad := range []Spend{{Month: "Nov", Channel: "linkedin", AmountCents: 1}, {Month: "2026-11", Channel: "tv", AmountCents: 1}, {Month: "2026-11", Channel: "email"}} {
if err := AddSpend(d, bad, now); err == nil {
t.Errorf("accepted %+v", bad)
}
}
if _, err := ParseAmount("-5"); err == nil {
t.Error("accepted a negative amount")
}
if c, err := ParseAmount("$1,250.50"); err != nil || c != 125050 {
t.Errorf("amount: %d %v", c, err)
}
// A CSV with a bad row adds nothing.
if _, err := ImportSpendCSV(d, strings.NewReader("month,channel,amount,note\n2026-10,LinkedIn,100,ads\n2026-10,Radio,5\n"), now); err == nil || !strings.Contains(err.Error(), "row 3") {
t.Errorf("bad csv: %v", err)
}
n, err := ImportSpendCSV(d, strings.NewReader("month,channel,amount,note\n2026-10-01,LinkedIn,100,ads\n2026-10,general,\"1,000\",tools\n"), now)
if err != nil || n != 2 {
t.Fatalf("csv: %d %v", n, err)
}
log, _ := SpendLog(d, 10)
if len(log) != 3 || log[0].Month != "2026-11" || log[2].Channel != "linkedin" || log[1].AmountCents != 100000 || log[2].Source != "csv" {
t.Errorf("log: %+v", log)
}
DeleteSpend(d, log[0].ID)
if log, _ := SpendLog(d, 10); len(log) != 2 {
t.Errorf("after delete: %+v", log)
}
// Outside campaigns: the whole spend the first time, then the difference.
d.Exec(`INSERT INTO outside_campaigns (source, id, name, spend_cents, synced_at) VALUES ('explee', '7', 'Nonprofits', 20000, ''), ('origami', 'x', 'Zero', 0, '')`)
if n, _ := SyncOutsideSpend(d, now); n != 1 {
t.Errorf("first sync: %d", n)
}
d.Exec(`UPDATE outside_campaigns SET spend_cents = 26500 WHERE id = '7'`)
SyncOutsideSpend(d, now.AddDate(0, 1, 0))
if n, _ := SyncOutsideSpend(d, now.AddDate(0, 1, 0)); n != 0 {
t.Error("synced the same spend twice")
}
var dec int64
var note string
d.QueryRow(`SELECT amount_cents, note FROM spend WHERE source = 'explee' AND month = '2026-12'`).Scan(&dec, &note)
if dec != 6500 || !strings.Contains(note, "Nonprofits") {
t.Errorf("delta: %d %q", dec, note)
}
}
func TestCAC(t *testing.T) {
d := openDB(t)
now := time.Date(2026, 6, 20, 0, 0, 0, 0, time.UTC)
addLines(t, d, monthly("cus_a", 5000, "2026-03", "2026-07")...)
addLines(t, d, monthly("cus_b", 5000, "2026-04", "2026-07")...)
addLines(t, d, monthly("cus_c", 5000, "2026-04", "2026-07")...)
addLines(t, d, monthly("cus_d", 5000, "2026-06", "2026-07")...)
names := map[string]Customer{"cus_a": {Email: "a@x.example"}, "cus_b": {Email: "b@x.example"}, "cus_c": {Email: "c@x.example"}, "cus_d": {Email: "d@x.example"}}
// b came from an outreach email, c from LinkedIn (first touch wins), d's
// only step is its Stripe payment, which doesn't count.
for _, e := range []struct{ email, channel, source, at string }{
{"b@x.example", "email", "supermarket", "2026-03-01"}, {"c@x.example", "linkedin", "manual", "2026-02-01"},
{"c@x.example", "email", "supermarket", "2026-03-01"}, {"d@x.example", "website", "stripe", "2026-06-11"},
} {
d.Exec(`INSERT INTO funnel_events (stage, channel, email, source, at) VALUES ('contacted', ?, ?, ?, ?)`, e.channel, e.email, e.source, e.at+"T00:00:00Z")
}
for _, s := range []Spend{{Month: "2026-04", Channel: "email", AmountCents: 30000}, {Month: "2026-04", Channel: "general", AmountCents: 10000},
{Month: "2026-06", Channel: "linkedin", AmountCents: 20000}} {
if err := AddSpend(d, s, now); err != nil {
t.Fatal(err)
}
}
m, _ := Build(d, now)
c, err := BuildCAC(d, m, names)
if err != nil {
t.Fatal(err)
}
got := map[string]CACMonth{}
for _, cm := range c.Months {
got[cm.Month] = cm
}
if apr := got["2026-04"]; apr.New != 2 || apr.Spend != 40000 || apr.CAC != 20000 {
t.Errorf("april: %+v", apr)
}
if may := got["2026-05"]; may.CAC != -1 {
t.Errorf("may has no new customers: %+v", may)
}
// June, three months: $600 over 3 customers (b, c, d; a was in March).
if jun := got["2026-06"]; jun.Trailing3 != 20000 || jun.CAC != 20000 {
t.Errorf("june: %+v", jun)
}
ch := map[string]CACChannel{}
for _, x := range c.Channels {
ch[x.Channel] = x
}
if ch["email"].New != 1 || ch["email"].CAC != 30000 || ch["linkedin"].New != 1 || ch["linkedin"].CAC != 20000 || ch["general"].CAC != -1 {
t.Errorf("channels: %+v", ch)
}
if c.Unknown != 2 || ch[""].New != 2 {
t.Errorf("unknown: %d %+v", c.Unknown, ch[""])
}
}