Files
magpie/internal/gui/context_scripts_test.go
yetone 6d154ef1e1 context: scripts (curl, Bun, python-httpx…) are one Scripts card, not an agent each
A request whose User-Agent names only an HTTP library or an SDK (curl,
Bun, node-fetch, undici, python-httpx, requests, aiohttp, Go-http-client,
okhttp, the OpenAI/Anthropic SDKs, …) or nothing at all got a Context
scorecard of its own, as if it were an agent (theforeveriris on X: "Bun",
"curl", "python-httpx" cards). usage.IsLibrary names those clients;
contextOfRecs folds them and "other" into one "Scripts" card at read
time, so older history reads the same way, and the card's meta line and
tooltip list the libraries by count. The cached day records are not
changed. No setting: it works as is. The Usage page's Agents list still
shows each library by name, with its Hide and Count-as-agent controls.
i18n for zh, zh-TW, ja, de. Existing context.js comments were condensed
so TestCoreScriptBudget holds without a raise (core scripts -28 bytes).

Run, at this head (rebased on 40af66027):
- gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows
  (mote) for usage and gui; earlier the whole tree on all three, exit 0.
- mote: go test -count=1 -tags nogui -timeout 45m ./internal/usage
  ./internal/gui: ok, ok.
- Windows box: context-scripts + i18n-keys 26/26 (chromium, webkit,
  5 languages, 360 and 1100px). Before the rebase, every reachable file
  (context-*, compact-one-value, group-context, routing-context-fold,
  routing-ctx-fold, routing-ctx-still, i18n-keys, gui-de/ja/zh-tw,
  header-relang): 333/349; the 16 reds are routing-ctx-still, red the
  same on origin/main 40af66027 on that box, listed in #4124.
Broken by hand: IsLibrary false and no fold -> TestIsLibrary and
TestContextScriptsAreOneCard red (cards python-httpx, claude, Bun, ...);
the JS label/clients removed -> context-scripts.test.cjs 0/22.
Not tried: a real app run with real curl/Bun traffic; Linux WebKitGTK;
GUI files on mote (its browsers were busy with another suite).
2026-10-30 16:05:58 +08:00

82 lines
2.3 KiB
Go

package gui
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/gateway"
)
// A request whose User-Agent names only an HTTP library, a runtime or an
// SDK is no agent (theforeveriris on X: Bun, curl and python-httpx each
// had an agent's scorecard). The Context tab puts them on one card,
// "scripts", which names them, the most used first; an agent keeps its
// own. Read from the history as it was kept before, so days already on
// disk read the same way, and the days kept for the next refresh keep each
// route's own agent.
func TestContextScriptsAreOneCard(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
mux := keptMux(t)
ctxKept.Lock()
ctxKept.days, ctxKept.key = map[string]ctxDay{}, ""
ctxKept.Unlock()
now := time.Now()
var b bytes.Buffer
id := int64(0)
add := func(agent, session string, n int) {
for range n {
id++
r := ctxRoute(id, agent, session, 3000, int(id)*100, 200000, 0)
r.Time = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Add(time.Duration(id) * time.Second)
j, _ := json.Marshal(r)
b.Write(append(j, '\n'))
}
}
add("python-httpx", "p1", 4)
add("Bun", "b1", 2)
add("curl", "", 1)
add("other", "", 1)
add("OpenAI", "o1", 1)
add("claude", "c1", 3)
dir := gateway.HistoryDir()
os.MkdirAll(dir, 0o700)
os.WriteFile(filepath.Join(dir, now.Format("2006-01-02")+".jsonl"), b.Bytes(), 0o600)
for round := range 2 {
var out ctxJSON
getJSON(t, mux, "/api/context?days=1", &out)
var ids []string
for _, a := range out.Agents {
ids = append(ids, a.Agent)
}
if !slices.Equal(ids, []string{"scripts", "claude"}) {
t.Fatalf("round %d: cards %v", round, ids)
}
s := out.Agents[0]
if s.Requests != 9 || !slices.Equal(s.Clients, []string{"python-httpx", "Bun", "OpenAI", "curl"}) {
t.Fatalf("round %d: scripts' card %+v", round, s)
}
for _, x := range out.Sessions {
if x.Agent != "scripts" && x.Agent != "claude" {
t.Fatalf("round %d: a session under %q", round, x.Agent)
}
}
}
ctxKept.Lock()
defer ctxKept.Unlock()
for _, d := range ctxKept.days {
for _, r := range d.recs {
if r.Agent == "scripts" {
t.Fatalf("the kept day's route %d lost its own agent", r.ID)
}
}
}
}