-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks_test.go
More file actions
237 lines (226 loc) · 5.25 KB
/
hooks_test.go
File metadata and controls
237 lines (226 loc) · 5.25 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
package main
import (
"encoding/json"
"strings"
"testing"
"github.com/ProgenyAlpha/periscope/internal/forecast"
"github.com/ProgenyAlpha/periscope/internal/store"
)
func TestCleanFirstPrompt(t *testing.T) {
tests := []struct {
name string
in string
want string
}{
{
name: "slash command stripped",
in: "/plan foo bar",
want: "Foo bar",
},
{
name: "agent mention stripped",
in: "@researcher do thing",
want: "Do thing",
},
{
name: "HTML tags stripped",
in: "<b>hello</b> world",
want: "Hello world",
},
{
name: "whitespace collapsed",
in: " lots of spaces ",
want: "Lots of spaces",
},
{
name: "long string truncated at word boundary",
in: "This is a really long string that definitely exceeds the fifty character limit we set",
want: func() string {
s := "This is a really long string that definitely exceeds the fifty character limit we set"
// Should truncate around 50 chars on word boundary
if len(s) <= 50 {
return s
}
cut := 50
for cut > 30 && s[cut] != ' ' {
cut--
}
if s[cut] == ' ' {
return s[:cut] + "..."
}
return s[:50] + "..."
}(),
},
{
name: "empty after strip returns original",
in: "/command",
want: "/command",
},
{
name: "unicode uppercase",
in: "\u00fcber cool",
want: "\u00dcber cool",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := store.CleanFirstPrompt(tt.in)
if got != tt.want {
t.Errorf("CleanFirstPrompt(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}
func TestGetTurnInfo(t *testing.T) {
tests := []struct {
name string
content json.RawMessage
wantType string
wantTool []string
wantAgent []string
}{
{
name: "nil content",
content: nil,
wantType: "chat",
},
{
name: "text only",
content: json.RawMessage(`[{"type":"text","text":"hello"}]`),
wantType: "chat",
},
{
name: "tool use",
content: json.RawMessage(`[{"type":"tool_use","name":"Read"}]`),
wantType: "tool",
wantTool: []string{"Read"},
},
{
name: "agent task with subagent",
content: json.RawMessage(`[{"type":"tool_use","name":"Task","input":{"subagent_type":"explore"}}]`),
wantType: "agent",
wantTool: []string{"Task"},
wantAgent: []string{"explore"},
},
{
name: "mixed tool and agent, agent wins",
content: json.RawMessage(`[{"type":"tool_use","name":"Read"},{"type":"tool_use","name":"Task","input":{"subagent_type":"Bash"}}]`),
wantType: "agent",
wantTool: []string{"Read", "Task"},
wantAgent: []string{"Bash"},
},
{
name: "invalid json",
content: json.RawMessage(`invalid json`),
wantType: "chat",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
info := getTurnInfo(tt.content)
if info.turnType != tt.wantType {
t.Errorf("turnType = %q, want %q", info.turnType, tt.wantType)
}
if !sliceEqual(info.tools, tt.wantTool) {
t.Errorf("tools = %v, want %v", info.tools, tt.wantTool)
}
if !sliceEqual(info.agents, tt.wantAgent) {
t.Errorf("agents = %v, want %v", info.agents, tt.wantAgent)
}
})
}
}
func sliceEqual(a, b []string) bool {
if len(a) == 0 && len(b) == 0 {
return true
}
if len(a) != len(b) {
return false
}
return strings.Join(a, ",") == strings.Join(b, ",")
}
func TestFmtTokens(t *testing.T) {
tests := []struct {
name string
in float64
want string
}{
{"zero", 0, "0"},
{"hundreds", 500, "500"},
{"thousands", 1500, "2K"},
{"millions", 1_500_000, "1.5M"},
{"billions", 1_500_000_000, "1.5B"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := fmtTokens(tt.in)
if got != tt.want {
t.Errorf("fmtTokens(%.0f) = %q, want %q", tt.in, got, tt.want)
}
})
}
}
func TestProgressBar(t *testing.T) {
tests := []struct {
name string
pct int
w int
want string
}{
{"zero", 0, 20, "--------------------"},
{"half", 50, 20, "##########----------"},
{"full", 100, 20, "####################"},
{"over", 150, 20, "####################"},
{"negative", -5, 20, "--------------------"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := progressBar(tt.pct, tt.w)
if got != tt.want {
t.Errorf("progressBar(%d, %d) = %q, want %q", tt.pct, tt.w, got, tt.want)
}
})
}
}
func TestIntOrDefault(t *testing.T) {
tests := []struct {
name string
in any
def int
want int
}{
{"float64", float64(42), -1, 42},
{"int", int(7), -1, 7},
{"string", "hello", -1, -1},
{"nil", nil, -1, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := forecast.IntOrDefault(tt.in, tt.def)
if got != tt.want {
t.Errorf("IntOrDefault(%v, %d) = %d, want %d", tt.in, tt.def, got, tt.want)
}
})
}
}
func TestFloatOrDefault(t *testing.T) {
tests := []struct {
name string
in any
def float64
want float64
}{
{"float64", float64(3.14), -1, 3.14},
{"int", int(7), -1, 7.0},
{"string", "hello", -1, -1},
{"nil", nil, -1, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := forecast.FloatOrDefault(tt.in, tt.def)
if got != tt.want {
t.Errorf("forecast.FloatOrDefault(%v, %.2f) = %.2f, want %.2f", tt.in, tt.def, got, tt.want)
}
})
}
}