package profile import ( "context" "encoding/json" "net/http" "net/http/httptest" "strings" "testing" "supermarket/internal/ai" ) type fakeAI struct { prompt string reply any } func (f *fakeAI) Generate(_ context.Context, req ai.Request) (string, error) { f.prompt = req.Prompt raw, _ := json.Marshal(f.reply) return string(raw), nil } func TestDraftReadsUsefulPages(t *testing.T) { mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.NotFound(w, r) return } w.Header().Set("Content-Type", "text/html") w.Write([]byte(`
Fast widgets.
About us Pricing Blog Partner`)) }) mux.HandleFunc("/about", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Write([]byte(`Founded in 2020.
`)) }) mux.HandleFunc("/pricing", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") w.Write([]byte(`$10 a month.
`)) }) srv := httptest.NewServer(mux) defer srv.Close() fake := &fakeAI{reply: map[string]any{ "name": "Acme", "description": "Widgets for teams.", "positioning": "", "voice": "plain", "keywords": []string{"widgets", " "}, "products": []any{map[string]any{"name": "Widget", "description": "", "url": "", "pricing": "$10", "benefits": []string{}}}, "targets": map[string]any{"industries": []string{}, "company_sizes": []string{}, "roles": []string{"CTO"}, "regions": []string{}, "problems": []string{}, "buying_signals": []string{}, "technologies": []string{}}, "accounts": map[string]any{"x": "@acme", "linkedin": ""}, }} p, err := Draft(context.Background(), fake, srv.URL) if err != nil { t.Fatal(err) } if p.Name != "Acme" || p.Accounts.X != "acme" || len(p.Keywords) != 1 || p.Targets.Roles[0] != "CTO" { t.Errorf("draft: %+v", p) } if len(p.Websites) != 1 || !strings.HasPrefix(p.Websites[0].URL, srv.URL) { t.Errorf("website: %+v", p.Websites) } for _, want := range []string{"Title: Acme & Co", "Description: Widgets for teams", "Fast widgets.", "Founded in 2020.", "$10 a month."} { if !strings.Contains(fake.prompt, want) { t.Errorf("prompt is missing %q", want) } } for _, unwanted := range []string{"ignore me", "elsewhere"} { if strings.Contains(fake.prompt, unwanted) { t.Errorf("prompt contains %q", unwanted) } } } func TestDraftRejectsBadURL(t *testing.T) { if _, err := Draft(context.Background(), &fakeAI{}, "ftp://example.com"); err == nil { t.Error("expected an error") } }