package web import ( "context" "fmt" "net/http" "net/http/httptest" "strings" "testing" "supermarket/internal/ai" "supermarket/internal/profile" "supermarket/internal/secrets" "supermarket/internal/seo" ) type cannedResearcher struct{ cannedAI } func (cannedResearcher) Research(context.Context, ai.ResearchRequest) (*ai.ResearchResult, error) { return &ai.ResearchResult{Text: `{"results":[{"url":"https://forum.example/1","title":"A thread","kind":"forum","note":"old"}]}`, Sources: []ai.Source{{URL: "https://forum.example/1"}}, Searches: 1}, nil } func TestKeywordsPage(t *testing.T) { newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) { return cannedResearcher{}, nil } defer func() { newAIClient = ai.New }() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, ``) })) defer srv.Close() s, h := newTestServer(t, nil) profile.Save(s.DB, &profile.Profile{Name: "Us", Keywords: []string{"private cloud pricing"}, Websites: []profile.Website{{URL: srv.URL}}}) if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "No candidates yet") { t.Fatal("no empty state") } if rec := postForm(h, "/seo/keywords?site=0", nil); rec.Code != http.StatusSeeOther { t.Fatalf("find: %d %s", rec.Code, rec.Body) } body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String() for _, want := range []string{"private cloud pricing", "buyer", "From profile keyword", "Read the live top 3"} { if !strings.Contains(body, want) { t.Errorf("page lacks %q", want) } } ks, _ := seo.Keywords(s.DB, srv.URL, seo.KeywordNew) id := fmt.Sprint(ks[0].ID) if rec := postForm(h, "/seo/keywords/"+id+"/top", nil); rec.Code != http.StatusSeeOther { t.Fatalf("top: %d %s", rec.Code, rec.Body) } if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "A thread") || !strings.Contains(body, "winnable for a small site") { t.Error("the top results aren't shown") } postForm(h, "/seo/keywords/"+id+"/pick", nil) if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "

Picked

") { t.Error("the pick isn't shown") } postForm(h, "/seo/keywords/"+id+"/dismiss", nil) if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "Dismissed (1)") { t.Error("dismissed isn't shown") } }