Files
magpie/internal/gui/usage_arrange_test.go
T
yetone e21ea07e47 gui: a client in the Usage page's Agents list can be hidden from it, and shown again from the line under it (ikunbots on X)
ikunbots asked to hide curl, Go-http-client, python-httpx and other clients
from the Agents list beside the agents they actually use.

What changed:
- Each Agents row has a "…" button that shows on hover (always on touch) and
  opens the app's row menu (openRowMenu) with "Hide from this list".
  Right-clicking the row opens the same menu. No native select.
- The hidden ids are saved by client id in settings.UsageAgentsHidden,
  trimmed and deduped. They go through POST /api/usage/arrange
  (agentsHidden), which changes only that field, and the Settings save
  carries the field over.
- Hiding is display only. The ledger keeps the calls, and the totals, the
  chart and every share still count them.
- When anything is hidden, a quiet "3 hidden" / "已隐藏 3 个" line sits under
  the list. It opens the hidden rows, dimmed, each with "Unhide" / "取消隐藏".
  No click scrolls the page: the scroll guard holds the clicked line in place.
- Where a pointer hovers, the "…" takes no room. Its margins give back its
  width and gap, so a row at rest is laid out as on main (mobile-web's
  desktop comparison is the same). It shows over the end of the share bar.
  On phones (body.web) it has its own grid column.
- Translated into zh, zh-TW, ja and de. docs/subsystems/gui-shell.md
  describes it.

What was run, in a worktree off origin/main:
- gofmt -l: clean.
- go build ./... and go vet ./...: pass.
- GOOS=linux go vet -tags nogui ./... and GOOS=windows go vet ./...: pass.
- HOME=<scratch> go test -tags nogui ./...: 49 packages ok. Includes the new
  TestUsageArrangeAgentsHidden, which covers saving, surviving a settings
  save, narrowing and emptying.
- New usage-agents-hide.test.cjs: 32/32 in Chromium and WebKit. It covers
  en, zh, zh-TW, ja and de at 1200, 560 and 390 (web). It hides by "…" and
  by right-click, checks the POST body, unchanged totals and an unmoved
  page, then unhides, reloads and checks the hidden rows persist.
- All 48 usage, panel-arrange and click-scroll GUI files: 503/503.
  old-webkit and mobile-web: pass.
- Full GUI suite (6489 tests, concurrency 5): the reds outside this change
  fail the same when run alone on origin/main 3deb85685, in WebKit only.
  They are filed in #1672 and #1609, and in #1745 (new: group-member-info,
  searcher-pick).
- Broken by hand:
  - Without the hidden filter in renderUsage, the new GUI test fails 16/16
    in Chromium (the shown rows still list curl).
  - Without the arrange handler, TestUsageArrangeAgentsHidden fails at
    line 82.
  - Without the Settings-save carry-over, it fails at line 84.
2026-10-12 14:56:28 +08:00

90 lines
3.4 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/settings"
)
// The tray panel's Allowances tab leaves out the subscriptions set hidden
// there (H20 on Discord), kept beside the Usage page's order: each is saved
// on its own, the other left as it was, and both outlast a save of the
// Settings page.
func TestUsageArrangePanelHidden(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
post := func(path, body string) {
t.Helper()
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
if rec.Code != http.StatusOK {
t.Fatalf("%s: %d %s", path, rec.Code, rec.Body)
}
}
want := func(order, hidden []string) {
t.Helper()
s := settings.Load()
if !slices.Equal(s.UsageOrder, order) || !slices.Equal(s.PanelUsageHidden, hidden) {
t.Fatalf("order %v hidden %v, want %v %v", s.UsageOrder, s.PanelUsageHidden, order, hidden)
}
}
post("/api/usage/arrange", `{"order":["kimi","codex"]}`)
want([]string{"kimi", "codex"}, nil)
post("/api/usage/arrange", `{"panelHidden":["deepseek"," deepseek","claude"]}`)
want([]string{"kimi", "codex"}, []string{"deepseek", "claude"})
post("/api/usage/arrange", `{"order":["codex","kimi"]}`)
want([]string{"codex", "kimi"}, []string{"deepseek", "claude"})
post("/api/settings", `{"theme":"dark"}`)
want([]string{"codex", "kimi"}, []string{"deepseek", "claude"})
post("/api/usage/arrange", `{"panelHidden":[]}`)
want([]string{"codex", "kimi"}, nil)
}
// The Usage page's Agents list leaves out the clients hidden there
// (ikunbots on X: curl, Go-http-client, python-httpx beside the agents in
// use): saved on its own, beside the panel's, and kept through a save of
// the Settings page.
func TestUsageArrangeAgentsHidden(t *testing.T) {
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("APPDATA", filepath.Join(h, "AppData", "Roaming"))
t.Setenv("LOCALAPPDATA", filepath.Join(h, "AppData", "Local"))
post := func(path, body string) {
t.Helper()
rec := httptest.NewRecorder()
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
if rec.Code != http.StatusOK {
t.Fatalf("%s: %d %s", path, rec.Code, rec.Body)
}
}
want := func(agents, panel []string) {
t.Helper()
s := settings.Load()
if !slices.Equal(s.UsageAgentsHidden, agents) || !slices.Equal(s.PanelUsageHidden, panel) {
t.Fatalf("agents hidden %v panel hidden %v, want %v %v", s.UsageAgentsHidden, s.PanelUsageHidden, agents, panel)
}
}
post("/api/usage/arrange", `{"panelHidden":["deepseek"]}`)
post("/api/usage/arrange", `{"agentsHidden":["curl","Go-http-client"," curl"]}`)
want([]string{"curl", "Go-http-client"}, []string{"deepseek"})
post("/api/settings", `{"theme":"dark"}`)
want([]string{"curl", "Go-http-client"}, []string{"deepseek"})
post("/api/usage/arrange", `{"agentsHidden":["Go-http-client"]}`)
want([]string{"Go-http-client"}, []string{"deepseek"})
post("/api/usage/arrange", `{"agentsHidden":[]}`)
want(nil, []string{"deepseek"})
}