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:
@@ -0,0 +1,157 @@
|
||||
package leadsource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/explee"
|
||||
"supermarket/internal/api/origami"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/prospects"
|
||||
"supermarket/internal/signals"
|
||||
)
|
||||
|
||||
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 serve(t *testing.T, key string, routes map[string]any) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("X-API-Key") != key && r.Header.Get("Authorization") != "Bearer "+key {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
body, ok := routes[r.URL.Path]
|
||||
if !ok {
|
||||
t.Errorf("unexpected %s", r.URL)
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
json.NewEncoder(w).Encode(body)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
var now = time.Date(2026, 11, 21, 18, 0, 0, 0, time.UTC)
|
||||
|
||||
func TestImportExplee(t *testing.T) {
|
||||
d := openDB(t)
|
||||
sig := &signals.Signal{Name: "Price rise", Status: signals.StatusTesting}
|
||||
signals.Save(d, sig)
|
||||
srv := serve(t, "k", map[string]any{
|
||||
"/autogtm/campaigns": map[string]any{"campaigns": []any{
|
||||
map[string]any{"id": 7, "name": "M365 movers", "status": "outreach", "target_url": "https://cal.example/x"},
|
||||
}},
|
||||
"/autogtm/campaigns/7/analytics": map[string]any{"status": "sending", "emails_sent": 40, "total_replies": 3,
|
||||
"hot_leads": 1, "spend_usd": 12.5},
|
||||
"/autogtm/campaigns/7/leads": map[string]any{"has_more": false, "leads": []any{
|
||||
map[string]any{"person_id": "p1", "email": "pat@acme.example", "first_sent_at": "2026-11-01T10:00:00Z",
|
||||
"replied": true, "last_reply_at": "2026-11-02T09:00:00Z", "status": "Hot"},
|
||||
map[string]any{"person_id": "p2", "email": "sam@other.example", "first_sent_at": "2026-11-01T10:00:00Z", "unsubscribed": true},
|
||||
map[string]any{"person_id": "p3", "erased": true, "first_sent_at": "2026-11-01T10:00:00Z"},
|
||||
}},
|
||||
"/autogtm/hot-leads": map[string]any{"has_more": false, "leads": []any{
|
||||
map[string]any{"person_id": "p1", "campaign_id": 7, "name": "Pat Lee", "email": "pat@acme.example",
|
||||
"job_title": "CTO", "company_name": "Acme", "company_domain": "acme.example",
|
||||
"why_hot": "Yes, let's talk next week.", "became_hot_at": "2026-11-02T09:05:00Z"},
|
||||
}},
|
||||
})
|
||||
c := explee.New("k").WithBase(srv.URL)
|
||||
|
||||
r, err := ImportExplee(context.Background(), d, c, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 3 contacted + 1 replied + 1 interested (the hot lead repeats it).
|
||||
if r.Campaigns != 1 || r.Events != 5 || r.Prospects != 1 || r.Suppressed != 1 || len(r.Warnings) != 0 {
|
||||
t.Fatalf("result: %+v", r)
|
||||
}
|
||||
if sup, _ := prospects.IsSuppressed(d, "sam@other.example", ""); !sup {
|
||||
t.Error("the unsubscribe wasn't suppressed")
|
||||
}
|
||||
cs, _, _ := prospects.Review(d, prospects.StatusNew, 0, 10)
|
||||
if len(cs) != 1 || cs[0].Name != "Acme" || len(cs[0].Contacts) != 1 || cs[0].Contacts[0].Title != "CTO" ||
|
||||
cs[0].Source != signals.SourceExplee {
|
||||
t.Fatalf("prospects: %+v", cs)
|
||||
}
|
||||
|
||||
// Assigning the campaign tags its events; the next import tags the
|
||||
// prospect too, and adds nothing twice.
|
||||
if err := signals.AssignCampaign(d, signals.SourceExplee, "7", sig.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r, err = ImportExplee(context.Background(), d, c, now)
|
||||
if err != nil || r.Events != 0 || r.Prospects != 0 || r.Suppressed != 0 {
|
||||
t.Fatalf("second import: %+v %v", r, err)
|
||||
}
|
||||
if c, _ := prospects.GetCompany(d, cs[0].ID); c.SignalID != sig.ID || c.Contacts[0].SignalID != sig.ID {
|
||||
t.Errorf("prospect not tagged: %+v", c)
|
||||
}
|
||||
rows, _ := signals.Scorecard(d, signals.Filter{})
|
||||
if rows[0].Signal.ID != sig.ID || rows[0].Counts[signals.StageContacted] != 3 || rows[0].Counts[signals.StageInterested] != 1 ||
|
||||
rows[0].SpendCents != 1250 {
|
||||
t.Errorf("scorecard: %+v spend %d", rows[0].Counts, rows[0].SpendCents)
|
||||
}
|
||||
if last, _ := Last(d, signals.SourceExplee); last == nil || last.Campaigns != 1 {
|
||||
t.Errorf("last: %+v", last)
|
||||
}
|
||||
|
||||
if _, err := ImportExplee(context.Background(), d, explee.New("wrong").WithBase(srv.URL), now); err != explee.ErrUnauthorized {
|
||||
t.Errorf("bad key: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImportOrigami(t *testing.T) {
|
||||
d := openDB(t)
|
||||
srv := serve(t, "og", map[string]any{
|
||||
"/send/campaigns": map[string]any{"object": "list", "next_cursor": nil, "items": []any{
|
||||
map[string]any{"object": "campaign", "id": "c1", "name": "Self-hosters", "status": "active"},
|
||||
}},
|
||||
"/send/campaigns/c1/stats": map[string]any{"object": "campaign_stats", "sent": 20, "replies": 2,
|
||||
"rates": map[string]any{"reply": 0.1}},
|
||||
"/send/campaigns/c1/people": map[string]any{"object": "list", "next_cursor": nil, "items": []any{
|
||||
map[string]any{"sequence_id": "s1", "status": "replied", "first_name": "Ana", "last_name": "Ruiz",
|
||||
"email": "ana@coop.example", "company": "Co-op", "company_domain": "coop.example", "first_sent_at": "2026-11-03T08:00:00Z"},
|
||||
map[string]any{"sequence_id": "s2", "status": "unsubscribed", "email": "no@thanks.example"},
|
||||
map[string]any{"sequence_id": "s3", "status": "scheduled"},
|
||||
}},
|
||||
"/send/replies": map[string]any{"object": "list", "next_cursor": nil, "items": []any{
|
||||
map[string]any{"sequence_id": "s1", "campaign_id": "c1", "received_at": "2026-11-04T08:00:00Z", "body": "Sounds interesting"},
|
||||
map[string]any{"sequence_id": "s1", "campaign_id": "c1", "received_at": "2026-11-03T12:00:00Z", "body": "Tell me more"},
|
||||
}},
|
||||
})
|
||||
c := origami.New("og", "").WithBase(srv.URL)
|
||||
r, err := ImportOrigami(context.Background(), d, c, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// s1 and s2 contacted (s3 is only scheduled), s1 replied once.
|
||||
if r.Campaigns != 1 || r.Events != 3 || r.Prospects != 1 || r.Suppressed != 1 {
|
||||
t.Fatalf("result: %+v", r)
|
||||
}
|
||||
evs, _ := signals.SubjectEvents(d, "origami:s1")
|
||||
if len(evs) != 2 || evs[1].Stage != signals.StageReplied || !evs[1].At.Equal(time.Date(2026, 11, 3, 12, 0, 0, 0, time.UTC)) {
|
||||
t.Errorf("s1 events: %+v", evs)
|
||||
}
|
||||
cs, _ := signals.Campaigns(d)
|
||||
if len(cs) != 1 || cs[0].Stats["sent"] != float64(20) || cs[0].Stats["rates reply"] != 0.1 {
|
||||
t.Errorf("campaign: %+v", cs)
|
||||
}
|
||||
sample, err := c.Sample(context.Background())
|
||||
if err != nil || sample["campaigns"] != 1 || sample["stats"] == nil {
|
||||
t.Errorf("sample: %+v %v", sample, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user