-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathextractors_test.go
More file actions
283 lines (252 loc) · 8.04 KB
/
Copy pathextractors_test.go
File metadata and controls
283 lines (252 loc) · 8.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package main
import (
"encoding/json"
"path/filepath"
"testing"
)
func TestExtractOutputTokensSnapshot(t *testing.T) {
// One LLM call with thinking + text + builtin tool + MCP tool + Skill.
// All blocks share the same message.id so DeduplicateUsage can attribute.
const msgID = "msg_abc123"
entries := []TranscriptEntry{
{
Type: "assistant",
UUID: "u1",
Message: &Message{
ID: msgID,
Model: "claude-opus-4-8",
Usage: &Usage{OutputTokens: 1000},
Content: ContentSlice{
{Type: "thinking", Thinking: "..."},
{Type: "text", Text: "hello world"},
{Type: "tool_use", ID: "t1", Name: "Bash", Input: map[string]interface{}{"command": "ls"}},
{Type: "tool_use", ID: "t2", Name: "mcp__slack__send", Input: map[string]interface{}{}},
{Type: "tool_use", ID: "t3", Name: "Skill", Input: map[string]interface{}{}},
},
},
},
}
parsed := ParseAssistantMessages(entries)
DeduplicateUsage(parsed)
snap := extractOutputTokensSnapshot(entries, parsed)
if snap == nil {
t.Fatal("expected non-nil snapshot")
}
summary, _ := snap["summary"].(map[string]interface{})
if summary == nil {
t.Fatal("missing summary")
}
total, _ := summary["total_tokens"].(int)
if total != 1000 {
t.Errorf("total_tokens = %d, want 1000", total)
}
cat, _ := snap["by_category"].(map[string]interface{})
if cat == nil {
t.Fatal("missing by_category")
}
// Sum of all categories must equal total.
catSum := 0
for _, key := range []string{"thinking", "assistant_text", "builtin_tool_use", "mcp_tool_use", "skill_invocations"} {
v, _ := cat[key].(int)
catSum += v
}
if catSum != total {
t.Errorf("sum(by_category) = %d, want %d (total_tokens)", catSum, total)
}
// thinking must be > 0 (leftover after non-thinking blocks).
if thinking, _ := cat["thinking"].(int); thinking == 0 {
t.Error("thinking should be > 0")
}
// Each non-thinking category must have been assigned something.
for _, key := range []string{"assistant_text", "builtin_tool_use", "mcp_tool_use", "skill_invocations"} {
if v, _ := cat[key].(int); v == 0 {
t.Errorf("by_category[%s] = 0, expected > 0", key)
}
}
}
func TestExtractOutputTokensSnapshotNilOnEmpty(t *testing.T) {
snap := extractOutputTokensSnapshot(nil, nil)
if snap != nil {
t.Errorf("expected nil on empty entries, got %v", snap)
}
}
func TestExtractThinkingSnapshotByLevel(t *testing.T) {
// Three LLM calls with different thinking budgets:
// msg1 → 200 tokens (minimal)
// msg2 → 2000 tokens (light)
// msg3 → 15000 tokens (heavy)
makeEntry := func(msgID string, thinkingTokens int) TranscriptEntry {
return TranscriptEntry{
Type: "assistant",
Message: &Message{
ID: msgID,
Model: "claude-sonnet-4-6",
Usage: &Usage{OutputTokens: thinkingTokens},
Content: ContentSlice{
{Type: "thinking", Thinking: "..."},
},
},
}
}
entries := []TranscriptEntry{
makeEntry("msg1", 200),
makeEntry("msg2", 2000),
makeEntry("msg3", 15000),
}
parsed := ParseAssistantMessages(entries)
DeduplicateUsage(parsed)
snap := extractThinkingSnapshot(entries, parsed)
if snap == nil {
t.Fatal("expected non-nil snapshot")
}
summary := snap["summary"].(map[string]interface{})
if total := summary["total_tokens"].(int); total != 17200 {
t.Errorf("total_tokens = %d, want 17200", total)
}
if calls := summary["call_count"].(int); calls != 3 {
t.Errorf("call_count = %d, want 3", calls)
}
byLevel := snap["by_level"].([]map[string]interface{})
levels := map[string]map[string]interface{}{}
for _, l := range byLevel {
levels[l["level"].(string)] = l
}
if _, ok := levels["minimal"]; !ok {
t.Error("expected minimal level")
}
if _, ok := levels["light"]; !ok {
t.Error("expected light level")
}
if _, ok := levels["heavy"]; !ok {
t.Error("expected heavy level")
}
if _, ok := levels["medium"]; ok {
t.Error("unexpected medium level")
}
if levels["minimal"]["call_count"].(int) != 1 {
t.Errorf("minimal call_count = %d, want 1", levels["minimal"]["call_count"])
}
if levels["heavy"]["tokens"].(int) != 15000 {
t.Errorf("heavy tokens = %d, want 15000", levels["heavy"]["tokens"])
}
}
func TestExtractFileAttachmentsSnapshotByType(t *testing.T) {
makeAttachment := func(path, content string) TranscriptEntry {
raw, _ := json.Marshal(map[string]interface{}{
"file": map[string]string{"path": path, "content": content},
})
return TranscriptEntry{
Type: "attachment",
Attachment: &Attachment{
Type: "file",
Content: raw,
},
}
}
entries := []TranscriptEntry{
makeAttachment("/repo/main.go", "package main\nfunc main() {}"),
makeAttachment("/repo/util.go", "package main"),
makeAttachment("/repo/README.md", "# Hello"),
makeAttachment("/repo/Makefile", "build:"), // no extension → "other"
}
snap := extractFileAttachmentsSnapshot(entries)
if snap == nil {
t.Fatal("expected non-nil snapshot")
}
summary := snap["summary"].(map[string]interface{})
if fc := summary["file_count"].(int); fc != 4 {
t.Errorf("file_count = %d, want 4", fc)
}
byType := snap["by_type"].([]map[string]interface{})
exts := map[string]map[string]interface{}{}
for _, row := range byType {
exts[row["ext"].(string)] = row
}
if _, ok := exts[".go"]; !ok {
t.Error("expected .go entry")
}
if _, ok := exts[".md"]; !ok {
t.Error("expected .md entry")
}
if _, ok := exts["other"]; !ok {
t.Error("expected other entry for Makefile")
}
if exts[".go"]["file_count"].(int) != 2 {
t.Errorf(".go file_count = %d, want 2", exts[".go"]["file_count"])
}
}
func TestExtractAgentsSnapshotPrefersFrontmatterName(t *testing.T) {
home := t.TempDir()
cwd := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("CLAUDE_PROJECT_DIR", cwd)
// File on disk is meta-auditor.md, but its frontmatter announces the
// agent as `config-auditor`. /context uses the frontmatter name, so we
// must too — otherwise dashboards drift from what users see.
writeFile(t, filepath.Join(cwd, ".claude", "agents", "meta-auditor.md"), `---
name: config-auditor
description: |
Audits Claude config files for duplication.
---
# Body — not counted toward the always-on cost
`)
snap := extractAgentsSnapshot()
if snap == nil {
t.Fatal("expected agents snapshot")
}
agents, _ := snap["agents"].([]map[string]interface{})
if len(agents) != 1 {
t.Fatalf("want 1 agent, got %d", len(agents))
}
if name, _ := agents[0]["name"].(string); name != "config-auditor" {
t.Errorf("name = %q, want config-auditor", name)
}
}
func TestExtractAgentsSnapshotFiltersDisabledPlugins(t *testing.T) {
home := t.TempDir()
cwd := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("CLAUDE_PROJECT_DIR", cwd)
// Two plugins on disk via installed_plugins.json — only one enabled
// via the settings layer. Disabled plugin's agent must NOT appear.
enabledPlugin := filepath.Join(home, "plugins", "good")
disabledPlugin := filepath.Join(home, "plugins", "bad")
writeFile(t, filepath.Join(enabledPlugin, "agents", "real-reviewer.md"), `---
name: real-reviewer
description: Active reviewer.
---
`)
writeFile(t, filepath.Join(disabledPlugin, "agents", "ghost.md"), `---
name: ghost
description: Should not show up.
---
`)
manifest := map[string]interface{}{
"plugins": map[string]interface{}{
"good@mp": []interface{}{map[string]interface{}{"installPath": enabledPlugin}},
"bad@mp": []interface{}{map[string]interface{}{"installPath": disabledPlugin}},
},
}
mb, _ := json.Marshal(manifest)
writeFile(t, filepath.Join(home, ".claude", "plugins", "installed_plugins.json"), string(mb))
writeFile(t, filepath.Join(home, ".claude", "settings.json"), `{
"enabledPlugins": {"good@mp": true, "bad@mp": false}
}`)
snap := extractAgentsSnapshot()
if snap == nil {
t.Fatal("expected agents snapshot")
}
agents, _ := snap["agents"].([]map[string]interface{})
names := map[string]bool{}
for _, a := range agents {
if n, ok := a["name"].(string); ok {
names[n] = true
}
}
if !names["good:real-reviewer"] {
t.Errorf("good:real-reviewer should be present, got %v", names)
}
if names["bad:ghost"] {
t.Errorf("bad:ghost must be filtered out (disabled plugin), got %v", names)
}
}