Phase 2 part 5: find people and verify emails with Hunter.io
Review gets Find people (Domain Search by the departments, seniority and count set in Settings → Lead sources), Find their email (Email Finder for a named person at a company), and Verify (one, or every unchecked address in New). Every call is kept in enrich_runs (schema 9) with its result and credits, and shown on the card. Nothing runs on a schedule. Sources are shown by name (Hunter.io, origami.chat, explee.com). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
package enrich
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/hunter"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/prospects"
|
||||
)
|
||||
|
||||
var now = time.Date(2026, 11, 22, 10, 0, 0, 0, time.UTC)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// fake answers like Hunter.io.
|
||||
func fake(t *testing.T) *hunter.Client {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
q := r.URL.Query()
|
||||
switch r.URL.Path {
|
||||
case "/domain-search":
|
||||
w.Header().Set("X-Credits-Used", "1")
|
||||
if q.Get("domain") == "empty.example" {
|
||||
w.Write([]byte(`{"data":{"emails":[]}}`))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"data":{"emails":[
|
||||
{"value":"pat@acme.example","type":"personal","confidence":94,"first_name":"Pat","last_name":"Lee","position":"CTO","department":"it","verification":{"status":"valid"}},
|
||||
{"value":"info@acme.example","type":"generic"},
|
||||
{"value":"kim@acme.example","type":"personal","confidence":60,"first_name":"Kim","last_name":"Ng","verification":{"status":"accept_all"}},
|
||||
{"value":"no@acme.example","type":"personal","first_name":"No","last_name":"Body"}]}}`))
|
||||
case "/email-finder":
|
||||
if q.Get("last_name") == "Unknown" {
|
||||
w.Write([]byte(`{"data":{"email":null}}`))
|
||||
return
|
||||
}
|
||||
w.Header().Set("X-Credits-Used", "1")
|
||||
w.Write([]byte(`{"data":{"email":"ann.ray@acme.example","score":91,"verification":{"status":"valid"}}}`))
|
||||
case "/email-verifier":
|
||||
w.Header().Set("X-Credits-Used", "0.5")
|
||||
status := map[string]string{"pat@acme.example": "invalid", "kim@acme.example": "webmail"}[q.Get("email")]
|
||||
if q.Get("email") == "quota@acme.example" {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
if status == "" {
|
||||
status = "unknown"
|
||||
}
|
||||
w.Write([]byte(`{"data":{"status":"` + status + `"}}`))
|
||||
}
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return hunter.New("k").WithBase(srv.URL)
|
||||
}
|
||||
|
||||
func TestFindPeople(t *testing.T) {
|
||||
d := openDB(t)
|
||||
h := fake(t)
|
||||
ctx := context.Background()
|
||||
id, _, _ := prospects.AddCompany(d, prospects.Company{Name: "Acme", Website: "acme.example", Source: prospects.SourceManual, SignalID: 0}, now)
|
||||
prospects.AddContact(d, prospects.Contact{CompanyID: id, FirstName: "Kim", LastName: "Ng", Email: "kim@acme.example", Source: prospects.SourceManual}, now)
|
||||
prospects.Suppress(d, "email", "no@acme.example", "", now)
|
||||
|
||||
run, err := FindPeople(ctx, d, h, id, DefaultSettings, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if run.Found != 3 || run.Added != 1 || run.Credits != 1 ||
|
||||
run.Result != "3 people found, 1 new one, 1 already a prospect, 1 on the suppression list." {
|
||||
t.Fatalf("run: %+v", run)
|
||||
}
|
||||
c, _ := prospects.GetCompany(d, id)
|
||||
if len(c.Contacts) != 2 {
|
||||
t.Fatalf("contacts: %+v", c.Contacts)
|
||||
}
|
||||
pat, kim := c.Contacts[1], c.Contacts[0]
|
||||
if pat.Email != "pat@acme.example" || pat.EmailStatus != prospects.EmailValid || pat.Title != "CTO" ||
|
||||
pat.Source != prospects.SourceHunter || pat.SourceDetail != "Hunter.io, 94% confidence, it" || pat.Status != prospects.StatusNew {
|
||||
t.Errorf("pat: %+v", pat)
|
||||
}
|
||||
if kim.EmailStatus != prospects.EmailRisky || kim.Source != prospects.SourceManual {
|
||||
t.Errorf("kim (existing) should keep her source and gain a status: %+v", kim)
|
||||
}
|
||||
latest, _ := Latest(d, "company:1", "company:9")
|
||||
if latest["company:1"].Result != run.Result || len(latest) != 1 {
|
||||
t.Errorf("latest: %+v", latest)
|
||||
}
|
||||
|
||||
other, _, _ := prospects.AddCompany(d, prospects.Company{Name: "Empty", Website: "empty.example", Source: prospects.SourceManual}, now)
|
||||
if run, err := FindPeople(ctx, d, h, other, DefaultSettings, now); err != nil || run.Result != "Nobody in those roles found at empty.example." {
|
||||
t.Errorf("empty: %+v %v", run, err)
|
||||
}
|
||||
nodomain, _, _ := prospects.AddCompany(d, prospects.Company{Name: "No Site", Source: prospects.SourceManual}, now)
|
||||
if _, err := FindPeople(ctx, d, h, nodomain, DefaultSettings, now); err == nil || !strings.Contains(err.Error(), "no website") {
|
||||
t.Errorf("no domain: %v", err)
|
||||
}
|
||||
if used, _ := CreditsSince(d, now.Add(-time.Hour)); used != 2 {
|
||||
t.Errorf("credits used: %v", used)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindEmailAndVerify(t *testing.T) {
|
||||
d := openDB(t)
|
||||
h := fake(t)
|
||||
ctx := context.Background()
|
||||
id, _, _ := prospects.AddCompany(d, prospects.Company{Name: "Acme", Website: "acme.example", Source: prospects.SourceManual}, now)
|
||||
ann, _, _ := prospects.AddContact(d, prospects.Contact{CompanyID: id, FirstName: "Ann", LastName: "Ray", Source: "origami"}, now)
|
||||
unk, _, _ := prospects.AddContact(d, prospects.Contact{CompanyID: id, FirstName: "Who", LastName: "Unknown", Source: "origami"}, now)
|
||||
loose, _, _ := prospects.AddContact(d, prospects.Contact{FirstName: "Lone", LastName: "Wolf", Source: "origami"}, now)
|
||||
|
||||
run, err := FindEmail(ctx, d, h, ann, now)
|
||||
if err != nil || run.Result != "Found ann.ray@acme.example (91% confidence)." || run.Credits != 1 {
|
||||
t.Fatalf("find: %+v %v", run, err)
|
||||
}
|
||||
if p, _ := prospects.GetContact(d, ann); p.Email != "ann.ray@acme.example" || p.EmailStatus != prospects.EmailValid {
|
||||
t.Errorf("ann: %+v", p)
|
||||
}
|
||||
if run, err := FindEmail(ctx, d, h, unk, now); err != nil || run.Result != "No address found at acme.example." || run.Credits != 0 {
|
||||
t.Errorf("not found: %+v %v", run, err)
|
||||
}
|
||||
if _, err := FindEmail(ctx, d, h, loose, now); err == nil || !strings.Contains(err.Error(), "no company") {
|
||||
t.Errorf("loose: %v", err)
|
||||
}
|
||||
if _, err := FindEmail(ctx, d, h, ann, now); err == nil {
|
||||
t.Error("found an address for someone who has one")
|
||||
}
|
||||
|
||||
pat, _, _ := prospects.AddContact(d, prospects.Contact{CompanyID: id, FirstName: "Pat", Email: "pat@acme.example", Source: prospects.SourceManual}, now)
|
||||
kim, _, _ := prospects.AddContact(d, prospects.Contact{CompanyID: id, FirstName: "Kim", Email: "kim@acme.example", Source: prospects.SourceManual}, now)
|
||||
quota, _, _ := prospects.AddContact(d, prospects.Contact{CompanyID: id, FirstName: "Q", Email: "quota@acme.example", Source: prospects.SourceManual}, now)
|
||||
n, err := VerifyMany(ctx, d, h, []int64{pat, kim, quota, ann}, now)
|
||||
if n != 2 || !errors.Is(err, hunter.ErrNoCredits) {
|
||||
t.Fatalf("verify many: %d %v", n, err)
|
||||
}
|
||||
for id, want := range map[int64]string{pat: prospects.EmailInvalid, kim: prospects.EmailRisky, quota: "", ann: prospects.EmailValid} {
|
||||
if p, _ := prospects.GetContact(d, id); p.EmailStatus != want {
|
||||
t.Errorf("%s: status %q, want %q", p.Email, p.EmailStatus, want)
|
||||
}
|
||||
}
|
||||
latest, _ := Latest(d, prospects.Subject("contact", quota), prospects.Subject("contact", pat))
|
||||
if !strings.Contains(latest[prospects.Subject("contact", quota)].Error, "credits are used up") ||
|
||||
latest[prospects.Subject("contact", pat)].Result != "pat@acme.example bounces." {
|
||||
t.Errorf("latest: %+v", latest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSettings(t *testing.T) {
|
||||
d := openDB(t)
|
||||
if s := LoadSettings(d); s.PerCompany != 5 || len(s.Departments) != 4 {
|
||||
t.Errorf("defaults: %+v", s)
|
||||
}
|
||||
SaveSettings(d, Settings{Departments: []string{"sales", "bogus", "it"}, Seniority: nil, PerCompany: 0})
|
||||
if s := LoadSettings(d); strings.Join(s.Departments, ",") != "it,sales" || len(s.Seniority) != 0 || s.PerCompany != 5 {
|
||||
t.Errorf("saved: %+v", s)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user