Files
davidandClaude Opus 5.5 d23d5e2a87 Phase 2 part 7: the end of the funnel
Posts can be written for a buying signal: the prompt gets its hypothesis
and angle, and links carry its campaign tag. Plausible (Stats API v2) is
read once a day for visitors on tagged links, recorded per signal as
Visited. Stripe (restricted key) is read every hour: $0 checkouts are
Signed up, paid invoices and one-time payments are Paid with their value,
tied to a signal by the checkout's utm_campaign metadata or the payer's
email. The Core's Cal.com has no API, so booking emails from Cal.com in
the outreach mailbox count as Booked. New Settings → Website & sales;
the signal page lists its posts and tagged links. Schema 11.

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

81 lines
3.3 KiB
Go

package web
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"supermarket/internal/core"
"supermarket/internal/profile"
"supermarket/internal/secrets"
"supermarket/internal/signals"
)
func TestWebsiteAndSales(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/v2/query":
json.NewEncoder(w).Encode(map[string]any{"results": []any{
map[string]any{"dimensions": []string{"2026-11-20", "leaving-m365", "linkedin", "social", "(none)"}, "metrics": []float64{3}},
}})
case "/v1/checkout/sessions":
json.NewEncoder(w).Encode(map[string]any{"data": []any{
map[string]any{"id": "cs_1", "created": 1, "status": "complete", "mode": "subscription", "amount_total": 0,
"customer": "cus_1", "customer_details": map[string]any{"email": "a@b.example", "name": "Bea"},
"metadata": map[string]any{"utm_campaign": "leaving-m365"}},
}})
case "/v1/invoices":
json.NewEncoder(w).Encode(map[string]any{"data": []any{}})
}
}))
defer srv.Close()
stripeBase = srv.URL
defer func() { stripeBase = "" }()
s, h := newTestServer(t, &core.Core{Domain: "example.com", Email: "me@example.com"})
if err := profile.Save(s.DB, &profile.Profile{Name: "Us", Websites: []profile.Website{{URL: "https://us.example"}}}); err != nil {
t.Fatal(err)
}
sig := &signals.Signal{Name: "Leaving M365", Slug: "leaving-m365"}
signals.Save(s.DB, sig)
body := do(h, http.MethodGet, "/settings/tracking", nil, nil).Body.String()
for _, want := range []string{`value="https://plausible.example.com"`, "<code>us.example</code>", "metadata[utm_campaign]", "Give Cal.com"} {
if !strings.Contains(body, want) {
t.Errorf("settings lack %q", want)
}
}
if rec := postForm(h, "/settings/tracking", url.Values{"plausible_url": {srv.URL}, secrets.PlausibleAPIKey: {"k"}, secrets.StripeAPIKey: {"rk_test_x"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("save: %d %s", rec.Code, rec.Body)
}
if body := postForm(h, "/settings/tracking/test/plausible", nil).Body.String(); !strings.Contains(body, "us.example: 3 visitors") {
t.Errorf("plausible test: %s", body)
}
if body := postForm(h, "/settings/tracking/test/stripe", nil).Body.String(); !strings.Contains(body, "test-mode key") {
t.Errorf("stripe test: %s", body)
}
for _, src := range []string{"plausible", "stripe"} {
if rec := postForm(h, "/settings/tracking/run/"+src, nil); rec.Code != http.StatusSeeOther {
t.Errorf("run %s: %d %s", src, rec.Code, rec.Body)
}
}
body = do(h, http.MethodGet, "/settings/tracking", nil, nil).Body.String()
for _, want := range []string{"3 visitors on links tagged with a signal", "1 sign-up and 0 payments", "Bea"} {
if !strings.Contains(body, want) {
t.Errorf("after the imports, settings lack %q", want)
}
}
body = do(h, http.MethodGet, "/signals/1", nil, nil).Body.String()
for _, want := range []string{"https://us.example?utm_campaign=leaving-m365", "Content for it", "Signed up", "Visited"} {
if !strings.Contains(body, want) {
t.Errorf("signal page lacks %q", want)
}
}
if body := do(h, http.MethodGet, "/content/own", nil, nil).Body.String(); !strings.Contains(body, "For a buying signal") && strings.Contains(body, "Write drafts") {
t.Error("the draft form doesn't offer the signals")
}
}