package web import ( "bytes" "encoding/json" "net/http" "strings" "testing" "time" "supermarket/internal/audit" "supermarket/internal/content" "supermarket/internal/profile" "supermarket/internal/users" ) // mcpCall calls a tool with a key: its text, and whether it was an error. func mcpCall(t *testing.T, h http.Handler, key, tool string, args map[string]any) (string, bool) { t.Helper() raw, _ := json.Marshal(map[string]any{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": map[string]any{"name": tool, "arguments": args}}) rec := do(h, http.MethodPost, "/mcp", bytes.NewReader(raw), map[string]string{ "Content-Type": "application/json", "Accept": "application/json, text/event-stream", "Authorization": "Bearer " + key}) if rec.Code != http.StatusOK { t.Fatalf("%s: %d %s", tool, rec.Code, rec.Body.String()) } var out struct { Result struct { Content []struct{ Text string } `json:"content"` IsError bool `json:"isError"` } `json:"result"` Error *struct{ Message string } `json:"error"` } json.Unmarshal(rec.Body.Bytes(), &out) if out.Error != nil { return out.Error.Message, true } text := "" if len(out.Result.Content) > 0 { text = out.Result.Content[0].Text } return text, out.Result.IsError } // newAgent makes an agent with scopes and returns its key. func newAgent(t *testing.T, s *Server, name string, scopes map[string]users.Access) (*users.User, string) { t.Helper() u, err := users.CreateAgent(s.DB, name, "", time.Now()) if err != nil { t.Fatal(err) } if err := users.Join(s.DB, users.Team, u.ID, users.Member, scopes, time.Now()); err != nil { t.Fatal(err) } key, _, err := users.NewKey(s.DB, u.ID, time.Time{}, time.Now()) if err != nil { t.Fatal(err) } return u, key } func TestAgentModuleTools(t *testing.T) { s, h := newTestServer(t, nil) profile.Save(s.DB, &profile.Profile{Name: "Acme", Description: "Widgets.", Websites: []profile.Website{{URL: "https://acme.example"}}}) writer, key := newAgent(t, s, "Morning writer", map[string]users.Access{"content": users.Edit, "seo": users.View}) _, tools := mcpTools(h, key) got := " " + strings.Join(tools, " ") + " " for _, want := range []string{"profile_get", "content_drafts", "content_items", "content_create_draft", "content_edit_draft", "seo_sites", "seo_morning_check", "seo_next", "seo_links", "seo_changes"} { if !strings.Contains(got, " "+want+" ") { t.Errorf("missing %s: %v", want, tools) } } for _, not := range []string{"seo_edit_change", "crm_pending_actions", "market_save", "market_list", "outreach_list", "metrics_months", "sites_list"} { if strings.Contains(got, " "+not+" ") { t.Errorf("offered %s", not) } } // Reading SEO works (no Search Console yet: it says so). if text, isErr := mcpCall(t, h, key, "seo_morning_check", map[string]any{"site": 0}); !isErr || !strings.Contains(text, "Search Console") { t.Errorf("morning check: %v %s", isErr, text) } if text, isErr := mcpCall(t, h, key, "seo_next", map[string]any{}); isErr || !strings.Contains(text, `"suggestions"`) { t.Errorf("next: %v %s", isErr, text) } // A tool it isn't offered fails. if _, isErr := mcpCall(t, h, key, "seo_edit_change", map[string]any{"id": 1, "title": "x", "description": "y"}); !isErr { t.Error("called a tool it doesn't have") } // It writes a draft: it waits in Posts, by the agent. text, isErr := mcpCall(t, h, key, "content_create_draft", map[string]any{"channel": "x", "text": "Widgets, now faster.", "source_url": "https://acme.example/new"}) if isErr { t.Fatalf("create: %s", text) } var d draftView json.Unmarshal([]byte(text), &d) if d.ID == 0 || d.Status != content.StatusDraft || d.Limit != 280 { t.Errorf("draft %+v", d) } saved, _ := content.GetDraft(s.DB, d.ID) if saved.WrittenBy != writer.ID { t.Errorf("written by %d", saved.WrittenBy) } if body := do(h, http.MethodGet, "/posts", nil, nil).Body.String(); !strings.Contains(body, "by Morning writer") { t.Error("Posts doesn't say which agent wrote it") } if _, isErr := mcpCall(t, h, key, "content_create_draft", map[string]any{"channel": "reddit", "text": "x"}); !isErr { t.Error("a Reddit draft without a title") } if _, isErr := mcpCall(t, h, key, "content_create_draft", map[string]any{"channel": "myspace", "text": "x"}); !isErr { t.Error("an unknown channel") } text, _ = mcpCall(t, h, key, "content_edit_draft", map[string]any{"id": d.ID, "text": "Widgets, faster than ever."}) if !strings.Contains(text, "faster than ever") { t.Errorf("edit: %s", text) } text, _ = mcpCall(t, h, key, "content_drafts", map[string]any{}) if !strings.Contains(text, "faster than ever") { t.Errorf("list: %s", text) } // What it did is in the log, as an agent. entries, _ := audit.List(s.DB, audit.Filter{UserID: writer.ID}, 10) if len(entries) != 2 || entries[0].Kind != audit.Agent || entries[1].Action != "wrote-draft" { t.Errorf("log: %+v", entries) } } func TestAgentMarketAndProspectsTools(t *testing.T) { s, h := newTestServer(t, nil) _, key := newAgent(t, s, "Researcher", map[string]users.Access{"market": users.Edit, "prospects": users.View, "metrics": users.View}) _, tools := mcpTools(h, key) got := " " + strings.Join(tools, " ") + " " for _, want := range []string{"market_list", "market_save", "signals_list", "prospects_review", "outreach_list", "suppression_list", "metrics_months"} { if !strings.Contains(got, " "+want+" ") { t.Errorf("missing %s", want) } } for _, not := range []string{"outreach_draft", "suppress", "content_create_draft"} { if strings.Contains(got, " "+not+" ") { t.Errorf("offered %s", not) } } text, isErr := mcpCall(t, h, key, "market_save", map[string]any{"name": "Rival", "relationship": "competitor", "description": "They sell widgets too."}) if isErr || !strings.Contains(text, `"id":`) { t.Fatalf("save: %s", text) } // The same name again changes it, not a second entry. mcpCall(t, h, key, "market_save", map[string]any{"name": "rival", "relationship": "competitor", "description": "Cheaper widgets."}) text, _ = mcpCall(t, h, key, "market_list", map[string]any{}) if strings.Count(text, `"name"`) != 1 || !strings.Contains(text, "Cheaper widgets.") { t.Errorf("list: %s", text) } if _, isErr := mcpCall(t, h, key, "market_save", map[string]any{"name": "X", "relationship": "friend"}); !isErr { t.Error("bad relationship accepted") } for _, tool := range []string{"signals_list", "prospects_review", "outreach_list", "suppression_list", "metrics_months"} { if text, isErr := mcpCall(t, h, key, tool, map[string]any{}); isErr { t.Errorf("%s: %s", tool, text) } } }