package web import ( "encoding/json" "net/http" "net/url" "strconv" "strings" "testing" "time" "supermarket/internal/content" "supermarket/internal/posting" "supermarket/internal/users" ) func TestAgentPauseAndLimits(t *testing.T) { s, h := newTestServer(t, nil) agent, key := newAgent(t, s, "Busy", map[string]users.Access{"content": users.Edit}) page := "/settings/agents/" + strconv.FormatInt(agent.ID, 10) // Its calls are listed on its page, errors too. mcpCall(t, h, key, "content_drafts", map[string]any{}) mcpCall(t, h, key, "content_create_draft", map[string]any{"channel": "myspace", "text": "x"}) body := do(h, http.MethodGet, page, nil, nil).Body.String() if !strings.Contains(body, "content_drafts") || !strings.Contains(body, "no channel") { t.Errorf("calls: %s", body) } // Pause: every call refused at once, with why. postForm(h, page+"/pause", url.Values{}) code, _ := mcpTools(h, key) if code != http.StatusForbidden { t.Errorf("paused: %d", code) } if body := do(h, http.MethodGet, "/settings/agents", nil, nil).Body.String(); !strings.Contains(body, "paused") { t.Error("not shown as paused") } postForm(h, page+"/resume", url.Values{}) if code, _ := mcpTools(h, key); code != http.StatusOK { t.Errorf("resumed: %d", code) } // Calls a minute. if rec := postForm(h, page+"/limits", url.Values{"per_minute": {"0"}, "out_per_day": {"1"}}); rec.Code != http.StatusUnprocessableEntity { t.Errorf("zero: %d", rec.Code) } postForm(h, page+"/limits", url.Values{"per_minute": {"3"}, "out_per_day": {"1"}}) // Two calls so far in this minute; one more is fine, the next isn't. if _, isErr := mcpCall(t, h, key, "content_drafts", map[string]any{}); isErr { t.Error("third call refused") } if text, isErr := mcpCall(t, h, key, "content_drafts", map[string]any{}); !isErr || !strings.Contains(text, "3 a minute") { t.Errorf("fourth call: %v %s", isErr, text) } calls, _ := users.RecentCalls(s.DB, agent.ID, 1) if calls[0].Outcome != users.CallRefused { t.Errorf("not recorded as refused: %+v", calls[0]) } // A minute later it may call again (the refused call doesn't count). s.DB.Exec(`UPDATE agent_calls SET at = ?`, time.Now().Add(-2*time.Minute).UTC().Format("2006-01-02T15:04:05.000Z")) // Things sent out a day: one. postForm(h, page+"/limits", url.Values{"per_minute": {"100"}, "out_per_day": {"1"}}) company := &posting.Account{Network: posting.X, Name: "@acme", External: "acme"} posting.Save(s.DB, s.Secrets, company, "t", "", time.Now()) posting.SetGrants(s.DB, map[int64][]int64{company.ID: {agent.ID}}) for i, want := range []string{"Waiting for approval", "may ask to send out 1 things a day"} { text, _ := mcpCall(t, h, key, "content_create_draft", map[string]any{"channel": "x", "text": "Post " + strconv.Itoa(i)}) var d draftView json.Unmarshal([]byte(text), &d) if text, _ := mcpCall(t, h, key, "content_publish", map[string]any{"draft_id": d.ID}); !strings.Contains(text, want) { t.Errorf("ask %d: %s", i, text) } } if body := do(h, http.MethodGet, page, nil, nil).Body.String(); !strings.Contains(body, "Waiting on") || !strings.Contains(body, "asked to send out 1 of its 1") { t.Errorf("agent page: %s", body) } } // Two agents with the same scopes each have their own server: what they // write is theirs. func TestAgentsDontShareServers(t *testing.T) { s, h := newTestServer(t, nil) scopes := map[string]users.Access{"content": users.Edit} a, ka := newAgent(t, s, "A", scopes) b, kb := newAgent(t, s, "B", scopes) for _, k := range []string{ka, kb} { mcpCall(t, h, k, "content_create_draft", map[string]any{"channel": "x", "text": "From " + k[:8]}) } list, _ := content.ListDrafts(s.DB, 10, content.StatusDraft) by := map[string]int64{} for _, d := range list { by[d.Content] = d.WrittenBy } if by["From "+ka[:8]] != a.ID || by["From "+kb[:8]] != b.ID { t.Errorf("written by %v (a %d, b %d)", by, a.ID, b.ID) } }