Files
magpie/internal/tui/sessions_test.go
T
yetone 662546e78d usage: a cache hit rate reads 100% only when all of the prompt was cached (ZhangXiaof48473 on X)
Routing's request list showed "缓存 100%" for Codex requests of 77.5K
tokens that read 77,200 of them from the cache (99.6%), beside rows at
99%, and the route line said "请求缓存命中率 99%". Every hit rate was
Math.round(100 * h) + "%", so anything from 99.5% up read 100%.

One formatter now says every hit rate: hitPct(h, d) in app.js, whole
percents (d decimals) unless rounding would say 100% or 0% of a prompt
not all or none cached, then one or two decimals (99.6%, 99.95%; never
past 99.99% or under 0.01%). Its callers: Routing's request rows, their
route line and a session's Avg. cache (plugins/routing/client.js); the
tray panel's Cache hit rate and the ranking rows' hit rate (app.js); the
Usage page's tiles, agent rows, Requests table, request detail and its
KPI strip, which keeps its one decimal (plugins/usage/page.js); the
context card's Prompt cache and the session traits Cache-friendly,
Cache misses and Cache hit (context.js). The TUI's and CLI's sessions
line, which floored the integer, use usage.HitPct, the same rule in Go;
it rounds now, so the TUI test's 67% (25.2K of 37.3K, 67.6%) reads 68%.
No new strings; app.js pays its bytes with six ledger comments cut, so
the core scripts are 15 bytes smaller than at its parent, no raise. Main
is over the core budget already (1,534,476 > 1,533,802 at the parent,
#5918); this commit doesn't add to it.

Run on mote:
- new internal/gui/tests/cache-hit-pct.test.cjs (Routing rows 99.6% /
  100% / 99%, the route line, hitPct at its edges) in chromium and
  webkit, en and zh: green; with the escalation line removed by hand,
  red (99.99% where 99.6% is wanted).
- cache-hit-pct, panel-usage, context-window, i18n-keys, usage-ledger,
  tray-totals, ctx-route, ledger-cache-hit, usage-hit-rate,
  routing-request-metrics (both engines; ledger-cache-unknown and
  ledger-cache-tokens in an earlier run): 186 pass, 6 red, all in
  usage-ledger's 'newest live route needs no return-to-live button',
  red the same on origin/main 00e8242e8 (#5048, #4078).
- go test ./internal/usage ./internal/cli ./internal/tui: ok, with
  TestHitPctNeverRoundsToAllOrNone and a 99.6% case in
  TestHitRateCountsCacheWrites (red on the old floor: 99%).
- go test ./internal/gui at the first base: TestCoreScriptBudget ok; one red,
  TestGatewayModeStatsNameTheGatewaySessions, run at 00:25 local (#5690,
  the first hour of a local day).
- gofmt; go vet darwin, GOOS=linux -tags nogui, GOOS=windows: clean.
At the pushed head (rebased, comments cut): cache-hit-pct and
routing-request-metrics again, both engines, 8 of 8.
Not tried: the real app's window, or GUI files beyond those above (a
65-file run waited on mote's suite lock behind four full suites).
2026-11-10 16:28:04 +08:00

294 lines
10 KiB
Go

package tui
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
)
// sessionsHome puts the sessions package's fixtures where Claude Code and
// Codex keep their sessions, in a sandbox HOME, their models priced. Their
// days are counted in UTC, which TestMain sets for the package.
func sessionsHome(t *testing.T) {
t.Helper()
h := home(t)
t.Setenv("HERMES_HOME", filepath.Join(h, ".hermes"))
for from, env := range map[string]string{"claude": "CLAUDE_CONFIG_DIR", "codex": "CODEX_HOME"} {
dir := filepath.Join(h, "sessions", from)
if err := os.CopyFS(dir, os.DirFS(filepath.Join("..", "sessions", "testdata", from))); err != nil {
t.Fatal(err)
}
t.Setenv(env, dir)
}
oldPrice := sessions.PriceOf
sessions.PriceOf = func(_ settings.Settings, m string) (catalog.Price, bool) {
switch m {
case "claude-opus-5-5":
return catalog.Price{Input: 4, Output: 20, CacheRead: 0.2, CacheWrite: 5}, true
case "gpt-6-astra":
return catalog.Price{Input: 10, Output: 50, CacheRead: 1}, true
}
return catalog.Price{}, false
}
t.Cleanup(func() {
// the page's index is written behind it: let that write finish before
// the prices it reads go back
sessions.Reset()
sessions.PriceOf = oldPrice
})
sessions.Reset()
}
// The Sessions page shows a range's totals, a chart of its days and its
// top models and folders; a model or a folder picked narrows them.
func TestSessionsPage(t *testing.T) {
sessionsHome(t)
m := press(t, model{w: 120, h: 40, srange: 1}, "5", "A", "s")
if m.page != pageSessions || sessRanges[m.srange].days != 0 || !m.sstat {
t.Fatalf("page %d, range %d", m.page, m.srange)
}
v := m.View()
for _, want := range []string{"5 sessions", " all ", "all models", "all folders", "11.9K tokens", "cache read 25.2K (68% hit)", "active 7m on 2 days",
"█", "Sep 20", "tokens · a day", "models", "gpt-6-astra", "claude-opus-5-5", "folders", "/work/app", "/work/it's", "M model", "f folder"} {
if !strings.Contains(v, want) {
t.Errorf("missing %q in\n%s", want, v)
}
}
// pick a model: the totals are its, and active time isn't kept by model
m = press(t, m, "M")
if m.mode != modePick {
t.Fatal("no picker")
}
m = typeIn(m, "opus")
m = press(t, m, "enter")
if m.smodel != "claude-opus-5-5" || m.mode != modeList {
t.Fatalf("model %q, mode %d", m.smodel, m.mode)
}
v = m.View()
for _, want := range []string{"claude-opus-5-5 · all folders", "active — not kept by model", "folders", "/work/app"} {
if !strings.Contains(v, want) {
t.Errorf("opus: missing %q in\n%s", want, v)
}
}
if strings.Contains(v, "gpt-6-astra") {
t.Errorf("opus: Codex's model still counted\n%s", v)
}
// x clears the filters, c charts cost
m = press(t, m, "x", "c")
if m.smodel != "" || !strings.Contains(m.View(), "cost · a day") {
t.Errorf("x, c:\n%s", m.View())
}
// today has nothing in the fixtures, and no chart of one day
m = press(t, m, "t")
if v := m.View(); !strings.Contains(v, "nothing in this range") || strings.Contains(v, "a day") {
t.Errorf("today:\n%s", v)
}
}
// The page opens on the sessions themselves, the latest first, narrowed
// by the range, model and folder as the stats are; ↑↓ pick one, whose
// resume command is shown.
func TestSessionsList(t *testing.T) {
sessionsHome(t)
m := press(t, model{w: 160, h: 40, srange: 1}, "5", "A")
if m.sstat {
t.Fatal("opened on the stats")
}
v := m.View()
t.Log("\n" + v)
shown := m.sessShown()
if len(shown) == 0 {
t.Fatalf("no sessions:\n%s", v)
}
for _, want := range []string{"sessions · stats", "▸ ", "↑↓ session", "↵ resume", "s stats", "↵ runs " + shown[0].Resume} {
if !strings.Contains(v, want) {
t.Errorf("missing %q in\n%s", want, v)
}
}
m = press(t, m, "down")
if len(shown) > 1 && (m.ssel != 1 || !strings.Contains(m.View(), "↵ runs "+shown[1].Resume)) {
t.Errorf("down: sel %d\n%s", m.ssel, m.View())
}
m = press(t, m, "up", "up")
if m.ssel != 0 {
t.Errorf("up past the top: %d", m.ssel)
}
// a model picked lists only the sessions that used it
m.smodel = "gpt-6-astra"
for _, s := range m.sessShown() {
if s.Agent != "codex" {
t.Errorf("gpt-6-astra: %s %s listed", s.Agent, s.ID)
}
}
// today has nothing in the fixtures
m = press(t, m, "x", "t")
if v := m.View(); !strings.Contains(v, "nothing in this range") {
t.Errorf("today:\n%s", v)
}
// s goes to the stats and back
if m = press(t, m, "s"); !m.sstat || !strings.Contains(m.View(), "sessions · stats") {
t.Errorf("s:\n%s", m.View())
}
if m = press(t, m, "s"); m.sstat {
t.Error("s again stayed on the stats")
}
}
// Only the sessions active since the range began are listed.
func TestSessFilter(t *testing.T) {
now := time.Date(2026, 9, 28, 15, 0, 0, 0, time.Local)
all := []sessions.Session{
{ID: "a", Last: now.Add(-time.Hour), Cwd: "/x", Models: []sessions.Model{{Model: "m1"}}},
{ID: "b", Last: now.AddDate(0, 0, -1), Cwd: "/y", Models: []sessions.Model{{Model: "m2"}}},
{ID: "c", Last: now.AddDate(0, 0, -10), Cwd: "/x", Models: []sessions.Model{{Model: "m1"}, {Model: "m2"}}},
}
ids := func(ss []sessions.Session) string {
var out []string
for _, s := range ss {
out = append(out, s.ID)
}
return strings.Join(out, ",")
}
for _, c := range []struct {
days int
model, folder string
want string
}{{1, "", "", "a"}, {7, "", "", "a,b"}, {0, "", "", "a,b,c"}, {0, "m2", "", "b,c"}, {0, "", "/x", "a,c"}, {0, "m2", "/x", "c"}} {
if got := ids(sessFilter(all, c.days, c.model, c.folder, now)); got != c.want {
t.Errorf("%d %q %q: %s, want %s", c.days, c.model, c.folder, got, c.want)
}
}
}
// A list longer than the window shows the rows around the one picked,
// with how many more are above and below.
func TestSessListWindow(t *testing.T) {
now := time.Date(2026, 9, 28, 15, 0, 0, 0, time.Local)
var list []sessions.Session
for i := range 30 {
list = append(list, sessions.Session{Agent: "claude", ID: fmt.Sprint("s", i), Title: fmt.Sprint("task ", i), Last: now.Add(-time.Duration(i) * time.Hour)})
}
for _, c := range []struct {
sel int
want, gone []string
}{
{0, []string{"▸ just now", "task 0", "↓ 17 more"}, []string{"↑", "task 13\n"}},
{20, []string{"task 20", "task 21", "↑ 9 more", "↓ 8 more"}, []string{"task 8\n", "task 22"}},
{29, []string{"task 29", "↑ 17 more"}, []string{"↓"}},
} {
ls := sessListLines(list, nil, 4, "", "", c.sel, 120, 24, now)
if len(ls) > 24-3 {
t.Errorf("sel %d: %d lines don't fit 24 with the footer", c.sel, len(ls))
}
v := strings.Join(ls, "\n") + "\n"
for _, w := range c.want {
if !strings.Contains(v, w) {
t.Errorf("sel %d: missing %q in\n%s", c.sel, w, v)
}
}
for _, g := range c.gone {
if strings.Contains(v, g) {
t.Errorf("sel %d: %q in\n%s", c.sel, g, v)
}
}
}
}
// The chart is a column a day with room for gaps, a week a column when
// the days don't fit, eighths of a block for what is between.
func TestSessChart(t *testing.T) {
day := func(date string, n int) sessions.DayTotal {
d := sessions.DayTotal{Date: date}
d.Input = n
return d
}
days := []sessions.DayTotal{day("2026-09-26", 800), day("2026-09-27", 0), day("2026-09-28", 100)}
ls := sessChart(days, false, 80, 2)
if len(ls) != 3 {
t.Fatalf("%d lines: %q", len(ls), ls)
}
if ls[0] != "█ 800" || ls[1] != "█ ▂ tokens · a day" || ls[2] != "Sep 26" {
t.Errorf("%q", ls)
}
var many []sessions.DayTotal
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.Local)
for i := range 200 {
many = append(many, day(start.AddDate(0, 0, i).Format(time.DateOnly), 1000))
}
ls = sessChart(many, false, 60, 4)
if !strings.Contains(ls[len(ls)-2], "a week") || len([]rune(strings.Fields(ls[3])[0])) != 29 {
t.Errorf("weeks: %q", ls)
}
if sessChart(days[:1], false, 80, 6) != nil {
t.Error("a chart of one day")
}
}
// The page's list is the whole set, not sessions.Limit: the head says how
// many sessions there are, and the list has to be as long as that. It read
// List(sessions.Limit+1) — 201 — which kept the bug the GUI had, only past a
// different number (yetone, #1019).
func TestSessionsListIsNotCutAtTheLimit(t *testing.T) {
sessionsHome(t)
claude := os.Getenv("CLAUDE_CONFIG_DIR")
proj := filepath.Join(claude, "projects", "-work-many")
if err := os.MkdirAll(proj, 0o755); err != nil {
t.Fatal(err)
}
// past sessions.Limit and past a page of the GUI's list, as Claude Code
// keeps them: one <id>.jsonl per session in its project's folder
const n = sessions.Limit + 10
for i := range n {
id := fmt.Sprintf("%08d-1111-2222-3333-444444444444", i)
line := fmt.Sprintf(`{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":"hi %d"},"uuid":"u","timestamp":"2026-09-20T10:00:00.000Z","cwd":"/work/many","sessionId":"%s"}`+"\n"+
`{"parentUuid":"u","isSidechain":false,"message":{"model":"claude-opus-5-5","id":"msg_1","type":"message","role":"assistant","content":[{"type":"text","text":"done"}],"usage":{"input_tokens":10,"output_tokens":2}},"requestId":"req_1","type":"assistant","uuid":"a","timestamp":"2026-09-20T10:00:05.000Z","cwd":"/work/many","sessionId":"%s"}`+"\n", i, id, id)
if err := os.WriteFile(filepath.Join(proj, id+".jsonl"), []byte(line), 0o644); err != nil {
t.Fatal(err)
}
}
sessions.Reset()
m := model{w: 160, h: 40, srange: len(sessRanges) - 1} // every range
m.reloadSessions()
// the fixtures' own sessions are here too: what matters is that the ten
// past sessions.Limit are, which a list cut at the limit would drop
seen := map[string]bool{}
for _, s := range m.slist {
seen[s.ID] = true
}
for i := range n {
id := fmt.Sprintf("%08d-1111-2222-3333-444444444444", i)
if !seen[id] {
t.Fatalf("the list of %d is missing %s: it was cut at the limit", len(m.slist), id)
}
}
if got := len(m.sessShown()); got < n {
t.Fatalf("the page shows %d sessions, want at least %d", got, n)
}
}
// Not asked yet (RustCats on X), the page lists nothing and says how to
// allow an agent, where it would say none ran in the range.
func TestSessionsNotAskedSaysHow(t *testing.T) {
sessionsHome(t)
if err := settings.SetSessionReads(func(*settings.SessionReads) *settings.SessionReads {
return &settings.SessionReads{}
}); err != nil {
t.Fatal(err)
}
m := press(t, model{w: 220, h: 40, srange: 1}, "5", "A")
if n := len(m.sessShown()); n != 0 {
t.Fatalf("not asked, %d sessions read", n)
}
if v := m.View(); !strings.Contains(v, "magpie sessions read on all") {
t.Errorf("the empty page doesn't say how to allow an agent:\n%s", v)
}
}