NorethSea's bridge (hermes-codex-thread-bridge) starts Codex's
app-server under a User-Agent of its own, so its calls were a row
apart from Codex on Usage, and its threads weren't Codex's in Sessions.
Rather than learn one bridge's name, the user says which agent a client
counts as.
- settings: clientAgents, client name (lowercased) -> agent id.
ClientAgentsNow reads just that field through filememo, so a pass over
the ledger costs a stat.
- usage: ReportedAs(ua) is AgentOf, then the user's mapping. It is used
for what is reported only: the summary, the agents list and filters,
request rows and pages, priced blocks, direct, Vias and gateway
sessions. The gateway's routing, keys and caps still go by AgentOf,
the client's own name. The mapping is read once per ledger pass
(readLogSnapshot) and is part of the keys of the summary price cache,
the vias cache and the request page cache, so a change redraws the
history already counted. A request row keeps the client's own name in
a new "client" field, shown as "Client" in its details.
- gui: POST /api/usage/arrange takes {count:{client,as}}. It refuses a
client magpie already knows as an agent, "other" or magpie's own, and
an "as" that isn't a known agent or is magpie. as "" counts it on its
own again. /api/settings keeps the mapping it doesn't send. On the
Usage page an unknown client's "…" offers "Count as an agent…" (a
picker of the known agents), the agent's row says "with <client>",
and the agent's "…" offers "Count <client> on its own". Strings are
in all 5 languages.
Run:
- go test -count=1 ./internal/usage ./internal/settings ./internal/gui: ok.
- TestClientCountedAsAgent goes red with reportedAs broken, and with the
mapping left out of the vias key or the request page key. With the
summary key alone left out it stays green, because the settings stat
already moves it.
- TestUsageArrangeCountClient and TestSettingsSaveKeepsWhatItDoesNotSend
(ClientAgents added) pass.
- Playwright usage-count-client.test.cjs (new) and usage-agents-hide
(its menu now has two items for an unknown client) pass in Chromium
and WebKit, in all 5 languages, at 1100 and 420: 54/54. With
countable() forced false, the new test goes red.
- gofmt -l is clean. go vet is clean for darwin, linux -tags nogui and
windows.
Not tried: a real hermes bridge (the records' UA is from the issue), and
the Client line in a request's details has no GUI test.
138 lines
5.4 KiB
Go
138 lines
5.4 KiB
Go
package gui
|
|
|
|
import (
|
|
"maps"
|
|
"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"})
|
|
}
|
|
|
|
// A client magpie doesn't know is counted as one of its agents from the
|
|
// Usage page's Agents list (#1958, NorethSea), saved on its own and kept
|
|
// through a save of the Settings page; As "" counts it on its own again.
|
|
// An agent magpie knows isn't counted as another, nor anything as
|
|
// magpie, nor as a client that isn't an agent.
|
|
func TestUsageArrangeCountClient(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) int {
|
|
t.Helper()
|
|
rec := httptest.NewRecorder()
|
|
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
|
return rec.Code
|
|
}
|
|
want := func(m map[string]string) {
|
|
t.Helper()
|
|
if got := settings.Load().ClientAgents; !maps.Equal(got, m) {
|
|
t.Fatalf("clientAgents %v, want %v", got, m)
|
|
}
|
|
}
|
|
if c := post("/api/usage/arrange", `{"count":{"client":"hermes-codex-thread-bridge","as":"codex"}}`); c != http.StatusOK {
|
|
t.Fatalf("count: %d", c)
|
|
}
|
|
want(map[string]string{"hermes-codex-thread-bridge": "codex"})
|
|
post("/api/settings", `{"theme":"dark"}`)
|
|
want(map[string]string{"hermes-codex-thread-bridge": "codex"})
|
|
for _, bad := range []string{
|
|
`{"count":{"client":"claude-code","as":"codex"}}`,
|
|
`{"count":{"client":"magpie-router","as":"codex"}}`,
|
|
`{"count":{"client":"my-bridge","as":"magpie"}}`,
|
|
`{"count":{"client":"my-bridge","as":"Python-urllib"}}`,
|
|
`{"count":{"client":"","as":"codex"}}`,
|
|
} {
|
|
if c := post("/api/usage/arrange", bad); c == http.StatusOK {
|
|
t.Fatalf("%s was taken", bad)
|
|
}
|
|
}
|
|
want(map[string]string{"hermes-codex-thread-bridge": "codex"})
|
|
post("/api/usage/arrange", `{"count":{"client":"Hermes-Codex-Thread-Bridge","as":""}}`)
|
|
want(nil)
|
|
}
|