Phase 2 part 2: prospects, the review screen and the suppression list
Schema 6 adds prospect_companies, prospect_contacts (merged by domain and email, each with its source, detail and date) and suppressions. Prospects -> Review lists New, Accepted and Set aside: accepting a company queues an EspoCRM Account, a person a Lead at their company, with provenance and why they fit in the description; the page warns about records already in the CRM as of the last sync, and shows each accepted prospect's CRM progress. Prospects can be added by hand. The suppression list (emails and domains) is checked on add and again before queueing, and suppressing sets matching new prospects aside. Search finds prospects too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
package prospects
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/crm"
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var now = time.Date(2026, 11, 21, 15, 0, 0, 0, time.UTC)
|
||||
|
||||
func TestAddMergesByDomainAndEmail(t *testing.T) {
|
||||
d := openDB(t)
|
||||
id, added, err := AddCompany(d, Company{Name: "Acme", Website: "https://www.Acme.com/about", Source: SourceManual}, now)
|
||||
if err != nil || !added {
|
||||
t.Fatalf("add: %v %v", added, err)
|
||||
}
|
||||
id2, added, err := AddCompany(d, Company{Name: "Acme Inc", Domain: "acme.com", Industry: "Widgets", Source: "discovery"}, now)
|
||||
if err != nil || added || id2 != id {
|
||||
t.Fatalf("same domain: %d %v %v", id2, added, err)
|
||||
}
|
||||
c, err := GetCompany(d, id)
|
||||
if err != nil || c.Name != "Acme" || c.Domain != "acme.com" || c.Industry != "Widgets" || c.Source != SourceManual || c.Status != StatusNew {
|
||||
t.Fatalf("merged company: %+v %v", c, err)
|
||||
}
|
||||
|
||||
pid, _, err := AddContact(d, Contact{CompanyID: id, FirstName: "Pat", Email: "Pat@Acme.com", Source: SourceManual}, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pid2, added, err := AddContact(d, Contact{LastName: "Lee", Title: "CTO", Email: "pat@acme.com", EmailStatus: "valid", Source: "hunter"}, now)
|
||||
if err != nil || added || pid2 != pid {
|
||||
t.Fatalf("same email: %d %v %v", pid2, added, err)
|
||||
}
|
||||
p, _ := GetContact(d, pid)
|
||||
if p.Name() != "Pat Lee" || p.Title != "CTO" || p.EmailStatus != "valid" || p.CompanyID != id {
|
||||
t.Fatalf("merged contact: %+v", p)
|
||||
}
|
||||
if _, _, err := AddContact(d, Contact{Email: "not-an-email", Source: SourceManual}, now); err == nil {
|
||||
t.Error("bad email accepted")
|
||||
}
|
||||
if _, _, err := AddCompany(d, Company{Name: "No source"}, now); err == nil {
|
||||
t.Error("a company without a source was accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptQueuesCRMActions(t *testing.T) {
|
||||
d := openDB(t)
|
||||
cid, _, _ := AddCompany(d, Company{Name: "Acme", Website: "acme.com", Why: "Runs 40 servers", Source: "discovery",
|
||||
SourceDetail: "https://news.example/acme"}, now)
|
||||
pid, _, _ := AddContact(d, Contact{CompanyID: cid, FirstName: "Pat", LastName: "Lee", Title: "CTO", Email: "pat@acme.com",
|
||||
LinkedIn: "https://linkedin.com/in/patlee", Source: "hunter"}, now)
|
||||
noName, _, _ := AddContact(d, Contact{CompanyID: cid, Email: "info@acme.com", Source: "hunter"}, now)
|
||||
|
||||
if _, err := AcceptContact(d, noName, now); err == nil || !strings.Contains(err.Error(), "last name") {
|
||||
t.Errorf("a contact without a name: %v", err)
|
||||
}
|
||||
a, err := AcceptContact(d, pid, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if a.Entity != "Lead" || a.Fields["accountName"] != "Acme" || a.Fields["emailAddress"] != "pat@acme.com" ||
|
||||
a.Match["emailAddress"] != "pat@acme.com" || a.Origin != "prospect-contact:"+itoa(pid) {
|
||||
t.Errorf("lead action: %+v", a)
|
||||
}
|
||||
desc, _ := a.Fields["description"].(string)
|
||||
for _, want := range []string{"Source: hunter", "found 21 Nov 2026", "Why it fits: Runs 40 servers", "LinkedIn: https://linkedin.com/in/patlee"} {
|
||||
if !strings.Contains(desc, want) {
|
||||
t.Errorf("description lacks %q: %s", want, desc)
|
||||
}
|
||||
}
|
||||
// Accepting twice doesn't queue twice.
|
||||
if b, _ := AcceptContact(d, pid, now); b.ID != a.ID {
|
||||
t.Error("accepting twice queued another action")
|
||||
}
|
||||
|
||||
acc, err := AcceptCompany(d, cid, now)
|
||||
if err != nil || acc.Entity != "Account" || acc.Fields["website"] != "https://acme.com" || acc.Match["website"] != "https://acme.com" {
|
||||
t.Fatalf("account action: %+v %v", acc, err)
|
||||
}
|
||||
p, _ := GetContact(d, pid)
|
||||
if p.Status != StatusAccepted || p.ActionID != a.ID || p.CRM.Status != crm.StatusPending {
|
||||
t.Errorf("accepted contact: %+v", p)
|
||||
}
|
||||
|
||||
// The CRM's result shows on the prospect.
|
||||
crm.Claim(d, now, 10)
|
||||
crm.Report(d, a.ID, crm.Result{Outcome: "created", EspoID: "abc"}, now)
|
||||
p, _ = GetContact(d, pid)
|
||||
if p.CRM.Status != crm.StatusDone || p.CRM.EspoID != "abc" {
|
||||
t.Errorf("crm state: %+v", p.CRM)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviewAndReject(t *testing.T) {
|
||||
d := openDB(t)
|
||||
cid, _, _ := AddCompany(d, Company{Name: "Acme", Domain: "acme.com", Source: SourceManual}, now)
|
||||
p1, _, _ := AddContact(d, Contact{CompanyID: cid, LastName: "One", Source: SourceManual}, now)
|
||||
p2, _, _ := AddContact(d, Contact{CompanyID: cid, LastName: "Two", Source: SourceManual}, now)
|
||||
loose, _, _ := AddContact(d, Contact{LastName: "Loose", Email: "l@else.org", Source: SourceManual}, now)
|
||||
|
||||
cs, ps, err := Review(d, StatusNew, 50)
|
||||
if err != nil || len(cs) != 1 || len(cs[0].Contacts) != 2 || len(ps) != 1 || ps[0].ID != loose {
|
||||
t.Fatalf("new: %+v %+v %v", cs, ps, err)
|
||||
}
|
||||
AcceptContact(d, p1, now)
|
||||
cs, _, _ = Review(d, StatusAccepted, 50)
|
||||
if len(cs) != 1 || len(cs[0].Contacts) != 1 || cs[0].Contacts[0].ID != p1 {
|
||||
t.Errorf("accepted tab: %+v", cs)
|
||||
}
|
||||
if err := RejectCompany(d, cid, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, _ := GetContact(d, p2); p.Status != StatusRejected {
|
||||
t.Error("rejecting the company should reject its new contacts")
|
||||
}
|
||||
if p, _ := GetContact(d, p1); p.Status != StatusAccepted {
|
||||
t.Error("rejecting the company shouldn't touch accepted contacts")
|
||||
}
|
||||
if err := Reconsider(d, "contact", p2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, _ := GetContact(d, p2); p.Status != StatusNew {
|
||||
t.Error("reconsider")
|
||||
}
|
||||
counts, _ := Counts(d)
|
||||
if counts[StatusNew] != 2 || counts[StatusAccepted] != 1 || counts[StatusRejected] != 1 {
|
||||
t.Errorf("counts: %v", counts)
|
||||
}
|
||||
if err := AcceptAll(d, cid, now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, _ := GetContact(d, p2); p.Status != StatusAccepted {
|
||||
t.Error("accept all")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuppression(t *testing.T) {
|
||||
d := openDB(t)
|
||||
cid, _, _ := AddCompany(d, Company{Name: "Acme", Domain: "acme.com", Source: SourceManual}, now)
|
||||
pid, _, _ := AddContact(d, Contact{CompanyID: cid, LastName: "Lee", Email: "pat@acme.com", Source: SourceManual}, now)
|
||||
other, _, _ := AddContact(d, Contact{LastName: "Ray", Email: "ray@gmail.com", Source: SourceManual}, now)
|
||||
|
||||
if err := Suppress(d, "", "nope", "", now); err == nil {
|
||||
t.Error("a bad value was accepted")
|
||||
}
|
||||
if err := Suppress(d, "", "Ray@Gmail.com", "asked not to be contacted", now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if p, _ := GetContact(d, other); p.Status != StatusRejected {
|
||||
t.Error("a suppressed email should be rejected")
|
||||
}
|
||||
if _, err := AcceptContact(d, other, now); !errors.Is(err, ErrSuppressed) {
|
||||
t.Errorf("accepting a suppressed contact: %v", err)
|
||||
}
|
||||
if _, _, err := AddContact(d, Contact{LastName: "Ray", Email: "ray@gmail.com", Source: "hunter"}, now); !errors.Is(err, ErrSuppressed) {
|
||||
t.Errorf("re-adding a suppressed contact: %v", err)
|
||||
}
|
||||
// Free mail: suppressing one address doesn't suppress gmail.com.
|
||||
if sup, _ := IsSuppressed(d, "someone@gmail.com", ""); sup {
|
||||
t.Error("one gmail address suppressed all of gmail")
|
||||
}
|
||||
|
||||
if err := Suppress(d, "domain", "https://www.acme.com", "existing customer", now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c, _ := GetCompany(d, cid); c.Status != StatusRejected || c.Contacts[0].Status != StatusRejected {
|
||||
t.Errorf("suppressed domain: %+v", c)
|
||||
}
|
||||
if _, _, err := AddContact(d, Contact{LastName: "New", Email: "new@acme.com", Source: "hunter"}, now); !errors.Is(err, ErrSuppressed) {
|
||||
t.Errorf("a contact at a suppressed domain: %v", err)
|
||||
}
|
||||
if _, err := AcceptContact(d, pid, now); !errors.Is(err, ErrSuppressed) {
|
||||
t.Errorf("accepting at a suppressed domain: %v", err)
|
||||
}
|
||||
list, _ := Suppressions(d)
|
||||
if len(list) != 2 {
|
||||
t.Fatalf("list: %+v", list)
|
||||
}
|
||||
if err := Unsuppress(d, "domain", "acme.com"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if sup, _ := IsSuppressed(d, "pat@acme.com", ""); sup {
|
||||
t.Error("unsuppress")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCRMMatches(t *testing.T) {
|
||||
d := openDB(t)
|
||||
rec, _ := crm.RecordFrom("Account", map[string]any{"id": "a1", "name": "Acme", "website": "acme.com"}, now)
|
||||
lead, _ := crm.RecordFrom("Lead", map[string]any{"id": "l1", "firstName": "Pat", "lastName": "Lee", "emailAddress": "pat@acme.com"}, now)
|
||||
crm.Ingest(d, []crm.Record{rec, lead})
|
||||
cid, _, _ := AddCompany(d, Company{Name: "Acme Corp", Website: "https://acme.com", Source: SourceManual}, now)
|
||||
c, _ := GetCompany(d, cid)
|
||||
if m, _ := c.CRMMatches(d); len(m) != 2 {
|
||||
t.Errorf("company matches: %+v", m)
|
||||
}
|
||||
pid, _, _ := AddContact(d, Contact{LastName: "Lee", Email: "PAT@acme.com", Source: SourceManual}, now)
|
||||
p, _ := GetContact(d, pid)
|
||||
if m, _ := p.CRMMatches(d); len(m) != 1 || m[0].EspoID != "l1" {
|
||||
t.Errorf("contact matches: %+v", m)
|
||||
}
|
||||
}
|
||||
|
||||
func itoa(n int64) string { return strconv.FormatInt(n, 10) }
|
||||
Reference in New Issue
Block a user