Phase 2 part 4: discovery per signal (Claude web search, explee.com)
Each signal's page can search for organizations that show it, by hand or every 7, 14 or 30 days. Claude with the web_search_20260209 server tool (pause_turn resumed) works from the signal's "How to spot it" lines and the profile's targets; a company is kept only if its evidence URL was among the pages searched. explee.com's company search runs the signal's description in the target sizes, scored against the hypothesis, keeping strong matches. Our sites, the inventory, existing prospects, CRM Accounts and suppressed domains are left out. New companies go to Review tagged with the signal. Schema 8 adds discovery_runs (counts, searches, why companies were left out) and the signal's schedule and explee.com description. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/ai"
|
||||
"supermarket/internal/api/explee"
|
||||
"supermarket/internal/db"
|
||||
"supermarket/internal/inventory"
|
||||
"supermarket/internal/profile"
|
||||
"supermarket/internal/prospects"
|
||||
"supermarket/internal/signals"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
type fakeResearcher struct {
|
||||
got ai.ResearchRequest
|
||||
res *ai.ResearchResult
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeResearcher) Research(_ context.Context, req ai.ResearchRequest) (*ai.ResearchResult, error) {
|
||||
f.got = req
|
||||
return f.res, f.err
|
||||
}
|
||||
|
||||
var now = time.Date(2026, 11, 21, 18, 0, 0, 0, time.UTC)
|
||||
|
||||
var prof = &profile.Profile{
|
||||
Name: "Federated Computer", Description: "Open-source business apps, single tenant.",
|
||||
Websites: []profile.Website{{URL: "https://federated.computer"}},
|
||||
Targets: profile.Targets{CompanySizes: []string{"2-50", "50-300", "300-2000"}, Regions: []string{"Europe"}},
|
||||
}
|
||||
|
||||
func TestFindWithClaude(t *testing.T) {
|
||||
d := openDB(t)
|
||||
sig := &signals.Signal{Name: "Leaving Microsoft 365", Hypothesis: "Price rise", Spot: "microsoft 365 alternative"}
|
||||
signals.Save(d, sig)
|
||||
inventory.Save(d, &inventory.Entry{Name: "Rival", Relationship: "competitor", Websites: []string{"https://rival.example"}})
|
||||
prospects.Suppress(d, "domain", "nope.example", "", now)
|
||||
prospects.AddCompany(d, prospects.Company{Name: "Known", Website: "known.example", Source: prospects.SourceManual}, now)
|
||||
|
||||
answer := map[string]any{"companies": []any{
|
||||
map[string]any{"name": "Acme", "website": "https://www.acme.example", "why": "Complained about the price rise",
|
||||
"evidence_url": "https://news.example/acme-m365/", "evidence_quote": "We're done with per-seat pricing", "size": "80"},
|
||||
map[string]any{"name": "Made Up", "website": "https://madeup.example", "why": "x", "evidence_url": "https://invented.example/page"},
|
||||
map[string]any{"name": "Rival", "website": "https://rival.example", "evidence_url": "https://news.example/rival"},
|
||||
map[string]any{"name": "Nope", "website": "https://nope.example", "evidence_url": "https://news.example/nope"},
|
||||
map[string]any{"name": "Us", "website": "https://federated.computer", "evidence_url": "https://news.example/us"},
|
||||
map[string]any{"name": "Known again", "website": "https://known.example", "evidence_url": "https://news.example/known"},
|
||||
}}
|
||||
raw, _ := json.Marshal(answer)
|
||||
r := &fakeResearcher{res: &ai.ResearchResult{
|
||||
Text: "Here is what I found:\n" + string(raw),
|
||||
Searches: 4,
|
||||
Sources: []ai.Source{{URL: "https://news.example/acme-m365"}, {URL: "https://news.example/rival"},
|
||||
{URL: "https://news.example/nope"}, {URL: "https://news.example/us"}, {URL: "http://news.example/known?ref=x"}},
|
||||
}}
|
||||
run, err := FindWithClaude(context.Background(), d, r, sig, prof, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if run.Found != 6 || run.Added != 1 || run.Existing != 1 || run.Skipped != 4 || run.Searches != 4 || run.Status != "done" {
|
||||
t.Fatalf("run: %+v", run)
|
||||
}
|
||||
for _, want := range []string{"Made Up: its source wasn't among the pages searched", "Rival: in your inventory", "Nope: on the suppression list", "Us: that's you"} {
|
||||
if !strings.Contains(strings.Join(run.Notes, "\n"), want) {
|
||||
t.Errorf("notes lack %q: %q", want, run.Notes)
|
||||
}
|
||||
}
|
||||
for _, want := range []string{"microsoft 365 alternative", "known.example", "rival.example", "2-50; 50-300; 300-2000"} {
|
||||
if !strings.Contains(r.got.Prompt, want) {
|
||||
t.Errorf("prompt lacks %q", want)
|
||||
}
|
||||
}
|
||||
cs, _, _ := prospects.Review(d, prospects.StatusNew, sig.ID, 10)
|
||||
if len(cs) != 2 {
|
||||
t.Fatalf("tagged prospects: %+v", cs)
|
||||
}
|
||||
acme := cs[0]
|
||||
if acme.Name != "Acme" || acme.Source != ProspectSourceClaude || acme.SourceDetail != "https://news.example/acme-m365/" ||
|
||||
!strings.Contains(acme.Why, "per-seat pricing") {
|
||||
t.Errorf("acme: %+v", acme)
|
||||
}
|
||||
runs, _ := Runs(d, sig.ID, 5)
|
||||
if len(runs) != 1 || runs[0].Added != 1 || len(runs[0].Notes) != 4 {
|
||||
t.Errorf("runs: %+v", runs)
|
||||
}
|
||||
if last, _ := LastStarted(d, sig.ID, SourceClaude); !last.Equal(now) {
|
||||
t.Errorf("last started: %v", last)
|
||||
}
|
||||
|
||||
// A failed search is recorded as failed.
|
||||
r.err, r.res = context.DeadlineExceeded, &ai.ResearchResult{Searches: 2}
|
||||
run, err = FindWithClaude(context.Background(), d, r, sig, prof, now)
|
||||
if err == nil || run.Status != "failed" || run.Searches != 2 {
|
||||
t.Errorf("failed run: %+v %v", run, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindWithExplee(t *testing.T) {
|
||||
d := openDB(t)
|
||||
sig := &signals.Signal{Name: "Self-hosting Nextcloud", Hypothesis: "Runs Nextcloud itself", ExpleeDefinition: "managed IT services"}
|
||||
signals.Save(d, sig)
|
||||
var got map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/search/companies" || r.Method != http.MethodPost {
|
||||
t.Errorf("unexpected %s %s", r.Method, r.URL)
|
||||
}
|
||||
json.NewDecoder(r.Body).Decode(&got)
|
||||
json.NewEncoder(w).Encode(map[string]any{"success": true, "companies": []any{
|
||||
map[string]any{"name": "Strong IT", "domain": "strong.example", "size": 40, "geo": "DE", "match": 3,
|
||||
"criteria": map[string]any{"Runs Nextcloud itself": map[string]any{"score": 5, "reasoning": "Hosts its own Nextcloud"}}},
|
||||
map[string]any{"name": "Weak IT", "domain": "weak.example", "match": 1,
|
||||
"criteria": map[string]any{"Runs Nextcloud itself": map[string]any{"score": 1, "reasoning": "No sign"}}},
|
||||
}})
|
||||
}))
|
||||
defer srv.Close()
|
||||
run, err := FindWithExplee(context.Background(), d, explee.New("k").WithBase(srv.URL), sig, prof, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if run.Found != 2 || run.Added != 1 || run.Skipped != 1 {
|
||||
t.Fatalf("run: %+v", run)
|
||||
}
|
||||
filters, _ := got["filters"].(map[string]any)
|
||||
size, _ := filters["size"].(map[string]any)
|
||||
if filters["definition"] != "managed IT services" || size["min"] != float64(2) || size["max"] != float64(2000) || got["page_size"] != float64(10) {
|
||||
t.Errorf("request: %+v", got)
|
||||
}
|
||||
cs, _, _ := prospects.Review(d, prospects.StatusNew, sig.ID, 10)
|
||||
if len(cs) != 1 || cs[0].Why != "Hosts its own Nextcloud" || cs[0].Location != "DE" || cs[0].Size != "40 people" {
|
||||
t.Errorf("prospect: %+v", cs)
|
||||
}
|
||||
sig.ExpleeDefinition = ""
|
||||
if _, err := FindWithExplee(context.Background(), d, explee.New("k").WithBase(srv.URL), sig, prof, now); err == nil {
|
||||
t.Error("a signal without a definition was searched")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user