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) } }