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"`, "us.example", "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") } }