Files
supermarket/internal/web/outreach_test.go
T
davidandClaude Opus 5.5 c2772a7927 Phase 2 part 6: outreach by email and LinkedIn
Drafts are written by the AI from the person's buying signal (hypothesis
and angle), their company and their history, then reviewed and edited.
Queued emails go out through the Core's mail server one at a time, within
the sending hours, the daily limit and a gap, never to a suppressed or
bouncing address or someone emailed in the last 14 days; with a signature,
an opt-out line, UTM-tagged links and a copy in Sent. The inbox is read
read-only every 10 minutes: replies (by Message-ID or sender) count as
Replied, bounces mark the address, and "stop" replies are suppressed.
LinkedIn messages are copied, sent by hand and marked sent. New pages:
Prospects → Outreach and Settings → Email (with an SMTP, IMAP, SPF, DKIM
and DMARC check). Schema 10; emersion/go-imap for IMAP.

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

110 lines
5.0 KiB
Go

package web
import (
"context"
"net/http"
"net/url"
"strings"
"testing"
"supermarket/internal/ai"
"supermarket/internal/core"
"supermarket/internal/outreach"
"supermarket/internal/secrets"
)
type cannedAI struct{ out string }
func (c cannedAI) Generate(context.Context, ai.Request) (string, error) { return c.out, nil }
func TestOutreach(t *testing.T) {
newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) {
return cannedAI{`{"subject":"per-seat pricing","body":"Hi Pat, a short note."}`}, nil
}
defer func() { newAIClient = ai.New }()
s, h := newTestServer(t, &core.Core{Domain: "example.com", Email: "me@example.com", Name: "Me"})
postForm(h, "/prospects/new", url.Values{"company_name": {"Acme"}, "website": {"acme.example"}, "first_name": {"Pat"},
"last_name": {"Lee"}, "email": {"pat@acme.example"}, "linkedin": {"https://linkedin.com/in/patlee"}})
// Review offers both channels; drafting lands in Outreach.
body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String()
if !strings.Contains(body, "Write an email") || !strings.Contains(body, "Write a LinkedIn message") {
t.Fatal("no outreach buttons on Review")
}
rec := postForm(h, "/outreach/draft", url.Values{"contact": {"1"}, "channel": {"email"}})
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/outreach?msg=drafted-outreach#m-1" {
t.Fatalf("draft: %d %s %s", rec.Code, rec.Header().Get("Location"), rec.Body)
}
body = do(h, http.MethodGet, "/outreach", nil, nil).Body.String()
for _, want := range []string{"per-seat pricing", "Hi Pat, a short note.", "Set up the mailbox", "Queue to send", `title="1 drafts to review"`} {
if !strings.Contains(body, want) {
t.Errorf("outreach lacks %q", want)
}
}
if body := do(h, http.MethodGet, "/prospects", nil, nil).Body.String(); !strings.Contains(body, "Email drafted") {
t.Error("Review doesn't show the draft")
}
// Edit and queue; back to drafts; queue again.
if rec := postForm(h, "/outreach/1/queue", url.Values{"subject": {"pricing"}, "body": {"Hi Pat, edited."}}); rec.Code != http.StatusSeeOther {
t.Fatalf("queue: %d %s", rec.Code, rec.Body)
}
if m, _ := outreach.Get(s.DB, 1); m.Status != outreach.StatusQueued || m.Body != "Hi Pat, edited." || m.Subject != "pricing" {
t.Errorf("queued: %+v", m)
}
if body := do(h, http.MethodGet, "/outreach?tab=queued", nil, nil).Body.String(); !strings.Contains(body, "Send now") {
t.Error("the queue lacks Send now")
}
if rec := postForm(h, "/outreach/1/send-now", url.Values{"tab": {"queued"}}); !strings.Contains(rec.Body.String(), "set up email first") {
t.Errorf("send without a mailbox: %s", rec.Body)
}
postForm(h, "/outreach/1/unqueue", nil)
if m, _ := outreach.Get(s.DB, 1); m.Status != outreach.StatusDraft {
t.Errorf("unqueued: %+v", m)
}
// LinkedIn: sent and answered by hand.
postForm(h, "/outreach/draft", url.Values{"contact": {"1"}, "channel": {"linkedin"}})
if rec := postForm(h, "/outreach/2/mark-sent", url.Values{"body": {"Hi Pat"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("mark sent: %d %s", rec.Code, rec.Body)
}
if rec := postForm(h, "/outreach/2/replied", url.Values{"reply": {"Tell me more"}, "tab": {"sent"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("replied: %d %s", rec.Code, rec.Body)
}
if body := do(h, http.MethodGet, "/outreach?tab=replied", nil, nil).Body.String(); !strings.Contains(body, "Tell me more") {
t.Error("the reply isn't shown")
}
if body := do(h, http.MethodGet, "/signals", nil, nil).Body.String(); !strings.Contains(body, "LinkedIn") {
t.Error("the scorecard doesn't show the LinkedIn steps")
}
// Settings: the defaults come from the Core; bad hours are refused.
if body := do(h, http.MethodGet, "/settings/email", nil, nil).Body.String(); !strings.Contains(body, `value="mail.example.com"`) {
t.Error("the mail server isn't suggested from the Core")
}
form := url.Values{"smtp_host": {"mail.example.com"}, "smtp_port": {"587"}, "imap_host": {"mail.example.com"}, "imap_port": {"993"},
"username": {"me@example.com"}, "from_name": {"Me"}, "from_address": {"me@example.com"}, "daily_limit": {"15"},
"gap_minutes": {"4"}, "start_hour": {"17"}, "end_hour": {"9"}, "footer": {"Me"}, "read_replies": {"on"}}
if rec := postForm(h, "/settings/email", form); !strings.Contains(rec.Body.String(), "start before they end") {
t.Errorf("bad hours: %d", rec.Code)
}
form.Set("start_hour", "9")
form.Set("end_hour", "17")
form.Set(secrets.MailPassword, "pw")
if rec := postForm(h, "/settings/email", form); rec.Code != http.StatusSeeOther {
t.Fatalf("save: %d %s", rec.Code, rec.Body)
}
if set, mb, err := s.mailbox(); err != nil || set.DailyLimit != 15 || mb.Password != "pw" || set.SaveToSent {
t.Errorf("mailbox: %+v %v", set, err)
}
if body := do(h, http.MethodGet, "/outreach", nil, nil).Body.String(); !strings.Contains(body, "of 15 emails sent today") {
t.Error("the status line doesn't show the limit")
}
// Discard.
postForm(h, "/outreach/1/discard", nil)
if m, _ := outreach.Get(s.DB, 1); m.Status != outreach.StatusDiscarded {
t.Errorf("discarded: %+v", m)
}
}