Files
supermarket/internal/signals/signals_test.go
davidandClaude Opus 5.5 b025a97e4a Phase 2 part 3: buying signals, the funnel and the scorecard
Schema 7 adds signals (hypothesis, how to spot it, angle, status, utm
slug), funnel_events (stage, channel, subject, campaign, source, deduped
by external ID) and outside_campaigns. Prospects -> Signals is the
scorecard: each signal's funnel from found to paid, people once per
stage, with revenue and outside spend, by period and channel. Each
signal's page shows its funnel by channel, its campaigns and its latest
steps. Prospects carry a signal (filter, change, "found" recorded) and
outcomes can be recorded by hand.

Settings -> Lead sources holds origami.chat and explee.com API keys. Every
6 hours (or Import now) Supermarket reads their campaigns, numbers and
spend, contacted people, replies and hot leads, read-only: interested
people and repliers become prospects, unsubscribes are suppressed, and
each campaign is assigned to a signal. The explee.com client follows its
OpenAPI schema; origami.chat's responses are undocumented, so its client
reads fields loosely and its Test shows the real response.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:06:58 -07:00

151 lines
5.4 KiB
Go

package signals
import (
"path/filepath"
"testing"
"time"
"supermarket/internal/db"
)
func openDB(t *testing.T) *db.DB {
t.Helper()
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { d.Close() })
return d
}
func TestSaveSlugsAndList(t *testing.T) {
d := openDB(t)
a := &Signal{Name: "Leaving Microsoft 365!", Hypothesis: "Price rise", Spot: "m365 alternative\n\n leaving microsoft 365 "}
if err := Save(d, a); err != nil {
t.Fatal(err)
}
if a.Slug != "leaving-microsoft-365" || a.Status != StatusIdea || len(a.SpotLines()) != 2 {
t.Errorf("saved: %+v %q", a, a.SpotLines())
}
b := &Signal{Name: "Leaving Microsoft 365", Status: StatusTesting}
if err := Save(d, b); err != nil {
t.Fatal(err)
}
if b.Slug != "leaving-microsoft-365-2" {
t.Errorf("taken slug: %q", b.Slug)
}
a.Name = "Leaving M365"
if err := Save(d, a); err != nil || a.Slug != "leaving-microsoft-365" {
t.Errorf("an update keeps its own slug: %q %v", a.Slug, err)
}
if err := Save(d, &Signal{Name: "x", Status: "maybe"}); err == nil {
t.Error("a bad status was accepted")
}
if err := Save(d, &Signal{Name: "!!!"}); err == nil {
t.Error("a name with no letters was accepted")
}
list, _ := List(d)
if len(list) != 2 || list[0].ID != b.ID {
t.Errorf("testing first: %+v", list)
}
if found, _ := Search(d, []string{"m365", "alternative"}, 10); len(found) != 1 {
t.Errorf("search: %+v", found)
}
}
func TestScorecard(t *testing.T) {
d := openDB(t)
sig := &Signal{Name: "Price rise", Status: StatusTesting}
Save(d, sig)
now := time.Now()
rec := func(e Event) {
t.Helper()
if _, err := Record(d, e); err != nil {
t.Fatal(err)
}
}
for _, who := range []string{"contact:1", "contact:2", "contact:3", "contact:4"} {
rec(Event{SignalID: sig.ID, Stage: StageContacted, Channel: "email", Subject: who, Source: SourceManual})
}
// Two replies from the same person count once.
rec(Event{SignalID: sig.ID, Stage: StageReplied, Channel: "email", Subject: "contact:1", Source: SourceManual})
rec(Event{SignalID: sig.ID, Stage: StageReplied, Channel: "email", Subject: "contact:1", Source: SourceManual})
rec(Event{SignalID: sig.ID, Stage: StagePaid, Channel: "email", Subject: "contact:1", Source: SourceManual, ValueCents: 7000})
// Aggregate visits add their count.
rec(Event{SignalID: sig.ID, Stage: StageVisited, Channel: "website", Source: SourceSupermarket, ExternalID: "v1", Count: 30})
// An import seen twice is recorded once.
if added, _ := Record(d, Event{Stage: StageContacted, Channel: "explee", Subject: "explee:p1", Source: SourceExplee, ExternalID: "x"}); !added {
t.Error("first import not added")
}
if added, _ := Record(d, Event{Stage: StageContacted, Channel: "explee", Subject: "explee:p1", Source: SourceExplee, ExternalID: "x"}); added {
t.Error("a repeated import was added")
}
if _, err := Record(d, Event{Stage: "won", Channel: "email", Source: SourceManual}); err == nil {
t.Error("a bad stage was accepted")
}
rows, err := Scorecard(d, Filter{})
if err != nil {
t.Fatal(err)
}
if len(rows) != 2 || rows[0].Signal.ID != sig.ID || rows[1].Signal != nil {
t.Fatalf("rows: %+v", rows)
}
r := rows[0]
if r.Counts[StageContacted] != 4 || r.Counts[StageReplied] != 1 || r.Counts[StageVisited] != 30 || r.RevenueCents != 7000 {
t.Errorf("counts: %+v %d", r.Counts, r.RevenueCents)
}
for _, c := range r.Cells() {
if c.Stage == StageReplied && c.Rate != "25%" {
t.Errorf("reply rate: %+v", c)
}
if c.Stage == StageVisited && c.Rate != "" {
t.Errorf("visits aren't a share of those contacted: %+v", c)
}
}
if rows, _ := Scorecard(d, Filter{Channel: "website"}); rows[0].Counts[StageContacted] != 0 || rows[0].Counts[StageVisited] != 30 {
t.Errorf("channel filter: %+v", rows[0].Counts)
}
if rows, _ := Scorecard(d, Filter{Since: now.Add(time.Hour)}); rows[0].Total() != 0 {
t.Errorf("since filter: %+v", rows[0].Counts)
}
// Assigning a campaign moves its events and its spend.
SaveCampaign(d, Campaign{Source: SourceExplee, ID: "9", Name: "M365 movers", SpendCents: 1234, SyncedAt: now})
Record(d, Event{Stage: StageReplied, Channel: "explee", Subject: "explee:p1", Campaign: "explee:9", Source: SourceExplee, ExternalID: "r"})
if err := AssignCampaign(d, SourceExplee, "9", sig.ID); err != nil {
t.Fatal(err)
}
rows, _ = Scorecard(d, Filter{})
if rows[0].SpendCents != 1234 || rows[0].Counts[StageReplied] != 2 {
t.Errorf("after assigning: %+v spend %d", rows[0].Counts, rows[0].SpendCents)
}
if sigID, _ := CampaignSignal(d, SourceExplee, "9"); sigID != sig.ID {
t.Errorf("campaign signal: %d", sigID)
}
// Saving the campaign again keeps the assignment.
SaveCampaign(d, Campaign{Source: SourceExplee, ID: "9", Name: "renamed", SyncedAt: now})
if cs, _ := Campaigns(d); len(cs) != 1 || cs[0].SignalID != sig.ID || cs[0].Name != "renamed" {
t.Errorf("campaigns: %+v", cs)
}
// Only manual events can be deleted.
evs, _ := SubjectEvents(d, "contact:1")
if len(evs) != 4 {
t.Fatalf("subject events: %+v", evs)
}
if err := DeleteEvent(d, evs[0].ID); err != nil {
t.Error(err)
}
imported, _ := SubjectEvents(d, "explee:p1")
if err := DeleteEvent(d, imported[0].ID); err == nil {
t.Error("an imported event was deleted")
}
// Deleting a signal keeps its events, without a signal.
Delete(d, sig.ID)
if rows, _ := Scorecard(d, Filter{}); len(rows) != 1 || rows[0].Signal != nil || rows[0].Counts[StageContacted] == 0 {
t.Errorf("after delete: %+v", rows)
}
}