package web import ( "context" "fmt" "net/http" "net/http/httptest" "net/url" "strings" "testing" "supermarket/internal/ai" "supermarket/internal/profile" "supermarket/internal/secrets" "supermarket/internal/seo" ) type briefWriter struct{} func (briefWriter) Generate(context.Context, ai.Request) (string, error) { return "## Prices\n\nWe tested it with 12 users. Plans are flat.", nil } func (briefWriter) Research(context.Context, ai.ResearchRequest) (*ai.ResearchResult, error) { return &ai.ResearchResult{Searches: 3, Text: `{"finding":"Nobody shows prices.","top":[{"url":"https://a.example","title":"A","promises":"a list","lacks":"prices","read":true}], "plan":{"title":"Flat-price workspace","description":"Plans.","h1":"Workspace","angle":"Prices","sections":["Prices","Moving"], "questions":["How much?"],"links":[],"ask":["Have you moved a team?"],"needs":[]}}`}, nil } func TestBriefPages(t *testing.T) { newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) { return briefWriter{}, 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", Websites: []profile.Website{{URL: srv.URL}}}) if body := do(h, http.MethodGet, "/seo/briefs", nil, nil).Body.String(); !strings.Contains(body, "Write a brief") { t.Fatal("no form") } rec := postForm(h, "/seo/briefs?site=0", url.Values{"keyword": {"workspace alternative"}}) if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), "/seo/briefs/1") { t.Fatalf("new: %d %s %s", rec.Code, rec.Header().Get("Location"), rec.Body) } body := do(h, http.MethodGet, "/seo/briefs/1", nil, nil).Body.String() for _, want := range []string{"Nobody shows prices.", `value="Flat-price workspace"`, "Have you moved a team?", "/workspace-alternative/", "Write the page"} { if !strings.Contains(body, want) { t.Errorf("brief lacks %q", want) } } form := url.Values{"title": {"Flat-price workspace"}, "description": {"Plans."}, "h1": {"Workspace"}, "angle": {"Prices"}, "sections": {"Prices\nMoving\nFAQ"}, "questions": {"How much?"}, "answer": {"Yes, 12 users."}, "path": {"/workspace/"}} if rec := postForm(h, "/seo/briefs/1/write", form); rec.Code != http.StatusSeeOther { t.Fatalf("write: %d %s", rec.Code, rec.Body) } b, _ := seo.GetBrief(s.DB, 1) if len(b.Plan.Sections) != 3 || b.Path != "/workspace/" || b.Answers[0] != "Yes, 12 users." || b.Status != seo.BriefPage { t.Errorf("saved: %+v", b) } body = do(h, http.MethodGet, "/seo/briefs/1", nil, nil).Body.String() if !strings.Contains(body, "We tested it with 12 users.") || !strings.Contains(body, `name="claim" value="We tested it with 12 users."`) { t.Error("the draft or its claim isn't shown") } if rec := postForm(h, "/seo/briefs/1/pr", url.Values{}); !strings.Contains(rec.Body.String(), "Save a Gitea token") { t.Errorf("pr without Gitea: %d", rec.Code) } s.Secrets.Set(map[string]string{secrets.GiteaToken: "tok"}) s.DB.PutJSON(giteaURLKey, "https://gitea.example.com") if rec := postForm(h, "/seo/briefs/1/pr", url.Values{"draft": {b.Draft}}); !strings.Contains(rec.Body.String(), "Tick each sentence") { t.Errorf("pr with an unconfirmed claim: %d", rec.Code) } postForm(h, "/seo/briefs/1/drop", nil) if body := do(h, http.MethodGet, "/seo/briefs", nil, nil).Body.String(); strings.Contains(body, "workspace alternative") { t.Error("a dropped brief is listed") } }