origami.chat people have no email, so each import added them again. Prospects now match on email, then LinkedIn profile, then name at the same company; companies without a website match by name, and get the domain when one turns up. explee.com's hot-leads list allows at most 200 per page (500 was refused, so no hot leads became prospects). origami.chat people's company website is read from identity.website_url. explee.com's key test no longer says "the key works" twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
251 lines
10 KiB
Go
251 lines
10 KiB
Go
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, 0, 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, 0, 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) }
|
|
|
|
// People from tools without emails (origami.chat) must not be added twice.
|
|
func TestAddWithoutEmails(t *testing.T) {
|
|
d := openDB(t)
|
|
c1, _, _ := AddCompany(d, Company{Name: "DATAGROUP", Source: "origami"}, now)
|
|
c2, added, _ := AddCompany(d, Company{Name: "Datagroup", Website: "https://www.datagroup.example/", Source: "origami"}, now)
|
|
if added || c2 != c1 {
|
|
t.Fatalf("same name, now with a website: %d %d %v", c1, c2, added)
|
|
}
|
|
if c, _ := GetCompany(d, c1); c.Domain != "datagroup.example" {
|
|
t.Errorf("the domain wasn't filled in: %+v", c)
|
|
}
|
|
p1, _, _ := AddContact(d, Contact{CompanyID: c1, FirstName: "Helge", LastName: "Viehof", LinkedIn: "https://www.linkedin.com/in/helge-v/", Source: "origami"}, now)
|
|
if p2, added, _ := AddContact(d, Contact{FirstName: "Helge", LastName: "Viehof", LinkedIn: "http://linkedin.com/in/helge-v", Source: "origami"}, now); added || p2 != p1 {
|
|
t.Errorf("same LinkedIn: %d %d %v", p1, p2, added)
|
|
}
|
|
if p3, added, _ := AddContact(d, Contact{CompanyID: c1, FirstName: "helge", LastName: "viehof", Source: "origami"}, now); added || p3 != p1 {
|
|
t.Errorf("same name at the same company: %d %d %v", p1, p3, added)
|
|
}
|
|
// A later email is filled in; a namesake elsewhere is someone else.
|
|
if p4, added, _ := AddContact(d, Contact{FirstName: "Helge", LastName: "Viehof", Email: "helge@datagroup.example", LinkedIn: "linkedin.com/in/helge-v", Source: "hunter"}, now); added || p4 != p1 {
|
|
t.Errorf("email added later: %d %d %v", p1, p4, added)
|
|
}
|
|
if p, _ := GetContact(d, p1); p.Email != "helge@datagroup.example" {
|
|
t.Errorf("email not filled in: %+v", p)
|
|
}
|
|
if _, added, _ := AddContact(d, Contact{FirstName: "Helge", LastName: "Viehof", Source: "origami"}, now); !added {
|
|
t.Error("a namesake without a company was merged")
|
|
}
|
|
}
|