Files
magpie/internal/sessions/stats_tools_test.go
T
yetone 317bc70875 sessions: one index run a page shows once, a spinner, a session-shape chart that reads, and every session counted in it (Image #9–#13: 排版不对…土土的; 这个原点的动画可以换成别的吗; 这个图我着实没看懂; 出现之后又消失又出现…跑完之后突然又出现; 为什么只有5个会话)
- The index hero flickered and ran again: the sessions list, the stats and a
  range change each started their own refresh of just the files they wanted,
  and the page showed a fallback hero after 700 ms whether or not anything was
  being read. List and stats now refresh every changed file in one run, so the
  read after it has nothing to do and a wider range finds the files read; the
  hero shows only while the server says it is indexing, and once it is done it
  stays at 100% ("Indexed · putting the page together…") and never starts over.
- The hero: a dot field in the accent alone, a big percentage, a hairline bar;
  a thin spinning ring in place of the pulsing dot, still when done or for
  reduced motion.
- Session shape: bars stand on a baseline with no grey column behind them,
  an empty bin is "0" over a stub, the busiest bin is in full accent (only
  when one is busiest), "Autonomy" is "Tool calls" (工具调用), and a line says
  what each axis is (横轴:… · 柱高:会话数).
- Only 5 sessions in the shape: parses kept by an early cacheVersion 4 had no
  prompts or replies, and were never read again, so the chart skipped them.
  cacheVersion 5 reads them again (on real data 385 of 462 sessions over 30
  days, rather than 5). Sessions of agents that tell no messages (pi, grok…)
  now count by their minutes at work.

Tested: TestShapesMinutesWithoutMessages; sessions-overview.test.cjs in Chromium
and WebKit, en and zh (the bars, zero stub, peak, both footers, the tool-calls
label); go test -tags nogui ./..., builds for darwin, linux, windows. Two GUI
files failing in the parallel run (click-scroll WebKit, login-import chromium
zh) pass alone.
2026-09-30 01:15:58 +08:00

130 lines
6.7 KiB
Go

package sessions
import (
"os"
"path/filepath"
"testing"
)
// The messages each way, the tools called and the skills called up are
// counted from Claude Code's and Codex's files, read fast and read whole
// alike, and the overview sums them up: the shape of the sessions, the
// tools by category and week, and the skills.
func TestStatsToolsAndSkills(t *testing.T) {
inZone(t, 0)
claude, codex := setup(t)
id := "44444444-2222-3333-4444-555555555555"
where := `,"cwd":"/work/tools","sessionId":"` + id + `"}` + "\n"
user := func(at, content string) string {
return `{"parentUuid":null,"isSidechain":false,"type":"user","message":{"role":"user","content":` + content + `},"timestamp":"` + at + `"` + where
}
reply := func(msg, at, content string) string {
return `{"parentUuid":null,"isSidechain":false,"message":{"model":"claude-opus-5-5","id":"` + msg + `","type":"message","role":"assistant","content":` + content +
`,"usage":{"input_tokens":10,"output_tokens":5}},"type":"assistant","timestamp":"` + at + `"` + where
}
lines := user("2026-09-27T10:00:00.000Z", `"fix the build"`) +
reply("m1", "2026-09-27T10:00:05.000Z", `[{"type":"text","text":"on it"}]`) +
// the same message's next blocks: two tool calls, one reply
reply("m1", "2026-09-27T10:00:06.000Z", `[{"type":"tool_use","id":"toolu_1","name":"Bash","input":{"command":"go build"}}]`) +
reply("m1", "2026-09-27T10:00:07.000Z", `[{"type":"tool_use","id":"toolu_2","name":"Skill","input":{"skill":"artifact-design"},"caller":{"type":"direct"}}]`) +
user("2026-09-27T10:00:08.000Z", `[{"tool_use_id":"toolu_1","type":"tool_result","content":"ok"}]`) +
// laid out otherwise: read whole
`{"type":"assistant","message":{"role":"assistant","id":"m2","model":"claude-opus-5-5","content":[{"type":"tool_use","name":"mcp__slack__send","id":"toolu_3","input":{}},{"type":"tool_use","name":"Edit","id":"toolu_4","input":{}}],"usage":{"input_tokens":1,"output_tokens":1}},"timestamp":"2026-09-27T10:00:09.000Z"` + where +
user("2026-09-27T10:01:00.000Z", `[{"type":"text","text":"<command-name>/clear</command-name>"}]`) +
user("2026-09-27T10:02:00.000Z", `"[Request interrupted by user]"`)
path := filepath.Join(claude, "projects", "-work-tools", id+".jsonl")
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(lines), 0o644)
// a subagent's tool calls count, its messages not
sub := filepath.Join(claude, "projects", "-work-tools", id, "subagents", "agent-a1.jsonl")
os.MkdirAll(filepath.Dir(sub), 0o755)
os.WriteFile(sub, []byte(user("2026-09-27T10:00:10.000Z", `"look around"`)+
reply("s1", "2026-09-27T10:00:11.000Z", `[{"type":"tool_use","id":"toolu_5","name":"Grep","input":{}}]`)), 0o644)
cx := filepath.Join(codex, "sessions", "2026", "09", "27", "rollout-2026-09-27T10-00-00-55555555-2222-3333-4444-555555555555.jsonl")
os.MkdirAll(filepath.Dir(cx), 0o755)
os.WriteFile(cx, []byte(
`{"timestamp":"2026-09-27T10:00:00.000Z","type":"session_meta","payload":{"id":"55555555-2222-3333-4444-555555555555","cwd":"/work/cx"}}`+"\n"+
`{"timestamp":"2026-09-27T10:00:01.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"<environment_context>x</environment_context>"}]}}`+"\n"+
`{"timestamp":"2026-09-27T10:00:02.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"add a test"}]}}`+"\n"+
`{"timestamp":"2026-09-27T10:00:03.000Z","type":"response_item","payload":{"type":"function_call","id":"a","name":"exec_command","arguments":"{}","call_id":"c1"}}`+"\n"+
`{"timestamp":"2026-09-27T10:00:04.000Z","type":"response_item","payload":{"type":"custom_tool_call","status":"completed","call_id":"c2","name":"apply_patch","input":"x"}}`+"\n"+
`{"timestamp":"2026-09-27T10:00:05.000Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"done"}]}}`+"\n"+
`{"timestamp":"2026-09-27T10:00:06.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"thanks"}]}}`+"\n"), 0o644)
s := statsAt(0, statsNow)
var cc, cxs Summary
for _, x := range s.Sessions {
switch x.Cwd {
case "/work/tools":
cc = x
case "/work/cx":
cxs = x
}
}
// the prompt and /clear typed; m1 and m2 back; five calls
if cc.Prompts != 2 || cc.Replies != 2 || cc.ToolCalls != 5 {
t.Fatalf("claude: %d prompts, %d replies, %d calls (%v)", cc.Prompts, cc.Replies, cc.ToolCalls, cc.tools)
}
if cc.tools["Bash"] != 1 || cc.tools["Grep"] != 1 || cc.tools["mcp__slack__send"] != 1 || cc.skills["artifact-design"] != 1 {
t.Fatalf("claude tools %v skills %v", cc.tools, cc.skills)
}
if cxs.Prompts != 2 || cxs.Replies != 1 || cxs.ToolCalls != 2 || cxs.tools["exec_command"] != 1 || cxs.tools["apply_patch"] != 1 {
t.Fatalf("codex: %+v %v", cxs, cxs.tools)
}
o := s.Overview("", "", "/work/tools", 10)
if o.Tools.Calls != 5 || o.Tools.Sessions != 1 || len(o.Tools.Top) != 5 {
t.Fatalf("tools %+v", o.Tools)
}
cats := map[string]int{}
for _, c := range o.Tools.Categories {
cats[c.Name] = c.Calls
}
if cats["Bash"] != 1 || cats["Edit"] != 1 || cats["Grep"] != 1 || cats["Tool"] != 1 || cats["Other"] != 1 {
t.Fatalf("categories %v", cats)
}
if len(o.Tools.Weeks) != 1 || o.Tools.Weeks[0].Start != "2026-09-21" || o.Tools.Weeks[0].Calls["Bash"] != 1 {
t.Fatalf("weeks %+v", o.Tools.Weeks)
}
if o.Skills.Calls != 1 || o.Skills.Count != 1 || o.Skills.Top[0].Name != "artifact-design" || o.Skills.Top[0].Last != "2026-09-27" ||
o.Skills.Top[0].Agents["claude"] != 1 || o.Skills.Top[0].Projects[0].Name != "/work/tools" {
t.Fatalf("skills %+v", o.Skills)
}
// 4 messages, 2 minutes' work, 5 calls in 2 prompts
if sh := o.Shape["messages"]; sh.Total != 1 || sh.Counts[0] != 1 {
t.Fatalf("shape by messages %+v", sh)
}
if sh := o.Shape["autonomy"]; sh.Counts[1] != 1 {
t.Fatalf("shape by autonomy %+v", sh)
}
day := -1
for i := range o.Days {
if o.Messages[i] > 0 {
day = i
}
}
if day < 0 || o.Messages[day] != 4 || o.Output[day] != 5+1+5 { // the subagent's reply too
t.Fatalf("messages %v output %v", o.Messages, o.Output)
}
}
// TestShapesMinutesWithoutMessages: a session whose agent tells no messages
// (pi, grok…) still counts by its minutes at work, and only there.
func TestShapesMinutesWithoutMessages(t *testing.T) {
sh := shapes([]Summary{
{Agent: "claude", Prompts: 2, Replies: 3, ToolCalls: 4, Active: 600},
{Agent: "pi", Active: 1200},
{Agent: "grok"},
})
if m := sh["minutes"]; m.Total != 2 || m.Counts[1] != 1 || m.Counts[2] != 1 {
t.Fatalf("minutes %+v", m)
}
if m := sh["messages"]; m.Total != 1 || m.Counts[0] != 1 {
t.Fatalf("messages %+v", m)
}
if a := sh["autonomy"]; a.Total != 1 || a.Counts[1] != 1 {
t.Fatalf("autonomy %+v", a)
}
}