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>
This commit is contained in:
2026-11-21 18:06:58 -07:00
co-authored by Claude Opus 5.5
parent ee579ec9f4
commit b025a97e4a
27 changed files with 3031 additions and 53 deletions
+89 -1
View File
@@ -27,6 +27,7 @@ import (
"supermarket/internal/profile"
"supermarket/internal/prospects"
"supermarket/internal/secrets"
"supermarket/internal/signals"
)
const testToken = "0123456789abcdef0123456789abcdef"
@@ -99,7 +100,8 @@ func TestEveryPageRenders(t *testing.T) {
"/settings/ai", "/settings/accounts", "/settings/core", "/static/app.css", "/favicon.ico", "/static/favicon.png", "/static/logo.png", "/static/apple-touch-icon.png", "/search", "/crm", "/search?q=rival", "/search?q=nothing+here", "/static/htmx.min.js", "/healthz",
"/content", "/content/1", "/content/1?dismissed=1", "/posts", "/posts?msg=drafted",
"/content/scheduled", "/content/published",
"/prospects", "/prospects?status=accepted", "/prospects?status=rejected", "/prospects/new", "/prospects/suppressed"} {
"/prospects", "/prospects?status=accepted", "/prospects?status=rejected", "/prospects/new", "/prospects/suppressed",
"/signals", "/signals?period=30d&channel=email", "/signals/new", "/settings/sources"} {
rec := do(h, http.MethodGet, path, nil, nil)
if rec.Code != http.StatusOK {
t.Errorf("core=%v GET %s: %d %s", c != nil, path, rec.Code, rec.Body.String())
@@ -599,3 +601,89 @@ func TestProspectsReview(t *testing.T) {
t.Errorf("missing prospect: %d", rec.Code)
}
}
// Signals: define one, tag a prospect, record outcomes, and see them on
// the scorecard.
func TestSignalsFunnel(t *testing.T) {
s, h := newTestServer(t, nil)
rec := postForm(h, "/signals/new", url.Values{"name": {"Leaving Microsoft 365"}, "status": {"testing"},
"hypothesis": {"Price rise makes them switch"}, "spot": {"m365 alternative"}})
if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/signals/1") {
t.Fatalf("create: %d %s", rec.Code, rec.Body.String())
}
if rec := postForm(h, "/signals/new", url.Values{"name": {""}}); !strings.Contains(rec.Body.String(), "needs a name") {
t.Error("a nameless signal was accepted")
}
postForm(h, "/prospects/new", url.Values{"company_name": {"Acme"}, "website": {"acme.example"}, "first_name": {"Pat"},
"last_name": {"Lee"}, "email": {"pat@acme.example"}, "signal": {"1"}})
body := do(h, http.MethodGet, "/prospects?signal=1", nil, nil).Body.String()
if !strings.Contains(body, "Pat Lee") || !strings.Contains(body, `<option value="1" selected>Leaving Microsoft 365</option>`) {
t.Error("the prospect isn't shown with its signal")
}
if body := do(h, http.MethodGet, "/prospects?signal=99", nil, nil).Body.String(); strings.Contains(body, "Pat Lee") {
t.Error("the signal filter doesn't filter")
}
for _, f := range []url.Values{
{"stage": {"contacted"}, "channel": {"email"}, "date": {"2026-11-20"}},
{"stage": {"replied"}, "channel": {"email"}, "note": {"Interested, call next week"}},
{"stage": {"paid"}, "channel": {"email"}, "amount": {"$1,070.50"}},
} {
f.Set("tab", "new")
f.Set("filter", "1")
if rec := postForm(h, "/prospects/contact/1/outcome", f); rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/prospects?status=new&signal=1&msg=outcome" {
t.Fatalf("outcome: %d %s %s", rec.Code, rec.Header().Get("Location"), rec.Body.String())
}
}
if rec := postForm(h, "/prospects/contact/1/outcome", url.Values{"stage": {"paid"}, "channel": {"email"}, "amount": {"lots"}}); !strings.Contains(rec.Body.String(), "an amount") {
t.Errorf("a bad amount was accepted: %d", rec.Code)
}
body = do(h, http.MethodGet, "/signals", nil, nil).Body.String()
for _, want := range []string{"Leaving Microsoft 365", "$1,070.50", `<span class="rate">100%</span>`} {
if !strings.Contains(body, want) {
t.Errorf("scorecard lacks %q", want)
}
}
body = do(h, http.MethodGet, "/signals/1", nil, nil).Body.String()
if !strings.Contains(body, "Interested, call next week") || !strings.Contains(body, "Its prospects (1 new)") {
t.Error("the signal's page")
}
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); !strings.Contains(body, "Interested, call next week") {
t.Error("the prospect's steps aren't shown")
}
// Moving the company to no signal moves its person and their steps.
postForm(h, "/prospects/company/1/signal", url.Values{"signal": {"0"}})
if rows, _ := signals.Scorecard(s.DB, signals.Filter{}); rows[0].Total() != 0 || rows[1].Signal != nil || rows[1].Counts["paid"] != 1 {
t.Errorf("after untagging: %+v", rows)
}
// An outside campaign, assigned to the signal.
signals.SaveCampaign(s.DB, signals.Campaign{Source: "explee", ID: "7", Name: "M365 movers", SpendCents: 990, SyncedAt: time.Now()})
if body := do(h, http.MethodGet, "/signals", nil, nil).Body.String(); !strings.Contains(body, "M365 movers") {
t.Error("campaign not listed")
}
postForm(h, "/signals/campaigns", url.Values{"source": {"explee"}, "id": {"7"}, "signal": {"1"}})
if cs, _ := signals.Campaigns(s.DB); cs[0].SignalID != 1 {
t.Errorf("assign: %+v", cs)
}
if rec := postForm(h, "/lead-sources/explee/import", nil); !strings.Contains(rec.Body.String(), "API key first") {
t.Errorf("import without a key: %s", rec.Body.String())
}
if body := do(h, http.MethodGet, "/search?q=microsoft", nil, nil).Body.String(); !strings.Contains(body, "/signals/1") {
t.Error("search doesn't find the signal")
}
// Lead source keys.
postForm(h, "/settings/sources", url.Values{secrets.ExpleeAPIKey: {"ex-key"}, "origami_project": {"proj"}})
if s.Secrets.Get(secrets.ExpleeAPIKey) != "ex-key" || s.origamiProject() != "proj" {
t.Error("lead source settings not saved")
}
if rec := postForm(h, "/settings/sources/test/origami", nil); !strings.Contains(rec.Body.String(), "Save the key first") {
t.Errorf("test without a key: %s", rec.Body.String())
}
postForm(h, "/signals/1/delete", nil)
if list, _ := signals.List(s.DB); len(list) != 0 {
t.Error("delete")
}
}