-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·399 lines (321 loc) · 16.6 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·399 lines (321 loc) · 16.6 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env bash
# Test suite for timer with SQLite database
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TIMER="$SCRIPT_DIR/timer"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
TESTS_RUN=0
TESTS_PASSED=0
pass() {
TESTS_PASSED=$((TESTS_PASSED + 1))
echo -e "${GREEN}PASS${NC}: $1"
}
fail() {
echo -e "${RED}FAIL${NC}: $1"
echo " Expected: $2"
echo " Got: $3"
}
# Setup: create temp directory with mock SQLite database
setup() {
TEST_DIR=$(mktemp -d)
TEST_CWD="$TEST_DIR/project"
mkdir -p "$TEST_CWD"
# Create mock data directory structure
DATA_DIR="$TEST_DIR/data"
mkdir -p "$DATA_DIR"
DB_PATH="$DATA_DIR/opencode.db"
# Create SQLite database with opencode schema
sqlite3 "$DB_PATH" <<'SQL'
CREATE TABLE project (
id TEXT PRIMARY KEY,
worktree TEXT NOT NULL,
vcs TEXT,
name TEXT,
time_created INTEGER NOT NULL,
time_updated INTEGER NOT NULL,
sandboxes TEXT NOT NULL DEFAULT '[]'
);
CREATE TABLE session (
id TEXT PRIMARY KEY,
project_id TEXT NOT NULL REFERENCES project(id) ON DELETE CASCADE,
parent_id TEXT,
slug TEXT NOT NULL,
directory TEXT NOT NULL,
title TEXT NOT NULL,
version TEXT NOT NULL,
time_created INTEGER NOT NULL,
time_updated INTEGER NOT NULL
);
CREATE TABLE message (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL REFERENCES session(id) ON DELETE CASCADE,
time_created INTEGER NOT NULL,
time_updated INTEGER NOT NULL,
data TEXT NOT NULL
);
CREATE TABLE part (
id TEXT PRIMARY KEY,
message_id TEXT NOT NULL REFERENCES message(id) ON DELETE CASCADE,
session_id TEXT NOT NULL,
time_created INTEGER NOT NULL,
time_updated INTEGER NOT NULL,
data TEXT NOT NULL
);
SQL
export TEST_DIR TEST_CWD DATA_DIR DB_PATH
}
# Teardown: remove temp directory
teardown() {
rm -rf "$TEST_DIR"
}
# Insert test data for a basic session
insert_basic_session() {
local session_title="${1:-Test Session}"
local now=1700000000000
local created=$((now - 60000))
local assistant_created=$((created + 1000))
local assistant_completed=$((created + 11000))
sqlite3 "$DB_PATH" "INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('proj_001', '$TEST_CWD', $created, $now, '[]');"
sqlite3 "$DB_PATH" "INSERT INTO session VALUES ('session_001', 'proj_001', NULL, 'test-slug', '$TEST_CWD', '$session_title', '1.2.0', $created, $now);"
# User message with modern schema (no summary.title, has agent field)
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_001', 'session_001', $created, $now, '{\"role\":\"user\",\"time\":{\"created\":$created},\"summary\":{\"diffs\":[]},\"agent\":\"build\"}');"
# Assistant message with tokens and timing (10 seconds thinking time)
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_002', 'session_001', $assistant_created, $now, '{\"role\":\"assistant\",\"time\":{\"created\":$assistant_created,\"completed\":$assistant_completed},\"tokens\":{\"input\":1000,\"output\":500,\"reasoning\":100,\"cache\":{\"read\":800,\"write\":200}},\"parentID\":\"msg_001\"}');"
}
# Insert session with question tool (to test wait time subtraction)
insert_session_with_question() {
local now=1700000000000
local created=$((now - 120000))
local assistant_created=$((created + 1000))
local assistant_completed=$((created + 31000)) # 30s total
local question_start=$((created + 5000))
local question_end=$((created + 15000)) # 10s wait time
sqlite3 "$DB_PATH" "INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('proj_002', '$TEST_CWD', $created, $now, '[]');"
sqlite3 "$DB_PATH" "INSERT INTO session VALUES ('session_002', 'proj_002', NULL, 'question-slug', '$TEST_CWD', 'Session with Question', '1.2.0', $created, $now);"
# User message (modern schema)
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_003', 'session_002', $created, $now, '{\"role\":\"user\",\"time\":{\"created\":$created},\"agent\":\"build\"}');"
# Assistant message: 30s total, but 10s was waiting for question answer
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_004', 'session_002', $assistant_created, $now, '{\"role\":\"assistant\",\"time\":{\"created\":$assistant_created,\"completed\":$assistant_completed},\"tokens\":{\"input\":2000,\"output\":1000,\"reasoning\":0,\"cache\":{\"read\":0,\"write\":0}},\"parentID\":\"msg_003\"}');"
# Question tool part: 10s wait time
sqlite3 "$DB_PATH" "INSERT INTO part VALUES ('part_001', 'msg_004', 'session_002', $question_start, $now, '{\"type\":\"tool\",\"tool\":\"question\",\"state\":{\"status\":\"completed\",\"time\":{\"start\":$question_start,\"end\":$question_end}}}');"
}
# Test: No sessions found
test_no_sessions() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
# Override HOME to use test data dir, run from test directory
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" 2>&1) || true
if [[ "$output" == *"No opencode session found"* ]]; then
pass "no_sessions: shows error message"
else
fail "no_sessions" "Error message about no sessions" "$output"
fi
teardown
}
# Test: Basic session output format
test_basic_session() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
insert_basic_session "My Test Session"
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" 2>&1)
# Should contain time format HH:MM:SS
if [[ "$output" =~ [0-9]{2}:[0-9]{2}:[0-9]{2} ]]; then
pass "basic_session: time format correct"
else
fail "basic_session: time format" "HH:MM:SS format" "$output"
fi
TESTS_RUN=$((TESTS_RUN + 1))
# Should contain prompt count > 0 (not "0 prompts")
if [[ "$output" =~ [1-9][0-9]*\ prompt ]]; then
pass "basic_session: prompt count > 0"
else
fail "basic_session: prompt count > 0" "N prompts where N > 0" "$output"
fi
TESTS_RUN=$((TESTS_RUN + 1))
# Should contain session title
if [[ "$output" == *"My Test Session"* ]]; then
pass "basic_session: title present"
else
fail "basic_session: title" "My Test Session" "$output"
fi
TESTS_RUN=$((TESTS_RUN + 1))
# Should contain output and speed stats
if [[ "$output" =~ output:.*tok/s ]]; then
pass "basic_session: token stats present"
else
fail "basic_session: token stats" "output: X (Y tok/s)" "$output"
fi
teardown
}
# Test: Question wait time is subtracted
test_question_wait_time() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
insert_session_with_question
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" 2>&1)
# Total time was 30s, question wait was 10s, so thinking time should be 20s
# Output should show 00:00:20 (or close to it given test timing)
if [[ "$output" =~ 00:00:[12][0-9] ]]; then
pass "question_wait_time: time correctly excludes question wait"
else
fail "question_wait_time" "~00:00:20 (30s - 10s question wait)" "$output"
fi
teardown
}
# Test: Verbose mode
test_verbose_mode() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
insert_basic_session "Verbose Test"
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" -v 2>&1)
# Verbose mode should show OUTPUT and CONTEXT sections
if [[ "$output" == *"OUTPUT"* && "$output" == *"CONTEXT"* ]]; then
pass "verbose_mode: shows detailed sections"
else
fail "verbose_mode" "OUTPUT and CONTEXT sections" "$output"
fi
TESTS_RUN=$((TESTS_RUN + 1))
# Should show cache percentage
if [[ "$output" == *"cached:"* && "$output" == *"%"* ]]; then
pass "verbose_mode: shows cache percentage"
else
fail "verbose_mode: cache percentage" "cached: X%" "$output"
fi
teardown
}
# Test: Database not found
test_no_database() {
TESTS_RUN=$((TESTS_RUN + 1))
# Use a non-existent path
output=$(HOME="/nonexistent" XDG_DATA_HOME="/nonexistent" "$TIMER" 2>&1) || true
if [[ "$output" == *"Database not found"* ]] || [[ "$output" == *"No opencode session"* ]]; then
pass "no_database: handles missing database"
else
fail "no_database" "Error about missing database" "$output"
fi
}
# Insert session with tool permission wait (bash/read/etc)
insert_session_with_tool_permission() {
local now=1700000000000
local created=$((now - 120000))
local assistant_created=$((created + 1000))
local assistant_completed=$((created + 36000)) # 35s total message duration
local tool_start=$((created + 2000))
local tool_end=$((created + 32000)) # 30s tool wait (permission prompt)
sqlite3 "$DB_PATH" "INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('proj_003', '$TEST_CWD', $created, $now, '[]');"
sqlite3 "$DB_PATH" "INSERT INTO session VALUES ('session_003', 'proj_003', NULL, 'tool-slug', '$TEST_CWD', 'Session with Tool Permission', '1.2.0', $created, $now);"
# User message (modern schema)
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_005', 'session_003', $created, $now, '{\"role\":\"user\",\"time\":{\"created\":$created},\"agent\":\"build\"}');"
# Assistant message: 35s total, but 30s was waiting for bash permission
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_006', 'session_003', $assistant_created, $now, '{\"role\":\"assistant\",\"time\":{\"created\":$assistant_created,\"completed\":$assistant_completed},\"tokens\":{\"input\":2000,\"output\":1000,\"reasoning\":0,\"cache\":{\"read\":0,\"write\":0}},\"parentID\":\"msg_005\"}');"
# Bash tool part: 30s wait time (user permission prompt)
sqlite3 "$DB_PATH" "INSERT INTO part VALUES ('part_002', 'msg_006', 'session_003', $tool_start, $now, '{\"type\":\"tool\",\"tool\":\"bash\",\"state\":{\"status\":\"completed\",\"time\":{\"start\":$tool_start,\"end\":$tool_end}}}');"
}
# Insert session with parallel tool calls requiring permission
insert_session_with_parallel_tools() {
local now=1700000000000
local created=$((now - 120000))
local assistant_created=$((created + 1000))
local assistant_completed=$((created + 36000)) # 35s total
# All tools start at nearly the same time (parallel), all blocked on permission
local tool1_start=$((created + 2000))
local tool1_end=$((created + 32000)) # 30s
local tool2_start=$((created + 2100))
local tool2_end=$((created + 32050)) # 30s (overlapping)
local tool3_start=$((created + 2200))
local tool3_end=$((created + 32100)) # 30s (overlapping)
sqlite3 "$DB_PATH" "INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('proj_004', '$TEST_CWD', $created, $now, '[]');"
sqlite3 "$DB_PATH" "INSERT INTO session VALUES ('session_004', 'proj_004', NULL, 'parallel-slug', '$TEST_CWD', 'Session with Parallel Tools', '1.2.0', $created, $now);"
# User message (modern schema)
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_007', 'session_004', $created, $now, '{\"role\":\"user\",\"time\":{\"created\":$created},\"agent\":\"build\"}');"
# Assistant message: 35s total, but ~30s was parallel tool permission wait
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_008', 'session_004', $assistant_created, $now, '{\"role\":\"assistant\",\"time\":{\"created\":$assistant_created,\"completed\":$assistant_completed},\"tokens\":{\"input\":2000,\"output\":1000,\"reasoning\":0,\"cache\":{\"read\":0,\"write\":0}},\"parentID\":\"msg_007\"}');"
# Three parallel tool calls - should be merged, not summed
sqlite3 "$DB_PATH" "INSERT INTO part VALUES ('part_003', 'msg_008', 'session_004', $tool1_start, $now, '{\"type\":\"tool\",\"tool\":\"bash\",\"state\":{\"status\":\"completed\",\"time\":{\"start\":$tool1_start,\"end\":$tool1_end}}}');"
sqlite3 "$DB_PATH" "INSERT INTO part VALUES ('part_004', 'msg_008', 'session_004', $tool2_start, $now, '{\"type\":\"tool\",\"tool\":\"glob\",\"state\":{\"status\":\"completed\",\"time\":{\"start\":$tool2_start,\"end\":$tool2_end}}}');"
sqlite3 "$DB_PATH" "INSERT INTO part VALUES ('part_005', 'msg_008', 'session_004', $tool3_start, $now, '{\"type\":\"tool\",\"tool\":\"grep\",\"state\":{\"status\":\"completed\",\"time\":{\"start\":$tool3_start,\"end\":$tool3_end}}}');"
}
# Test: Tool permission wait time is subtracted (single tool)
test_tool_permission_wait_time() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
insert_session_with_tool_permission
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" 2>&1)
# Total time was 35s, tool wait was 30s, so thinking time should be ~5s
# Output should show 00:00:05 (or close to it)
if [[ "$output" =~ 00:00:0[4-6] ]]; then
pass "tool_permission_wait: time correctly excludes tool wait"
else
fail "tool_permission_wait" "~00:00:05 (35s - 30s tool wait)" "$output"
fi
teardown
}
# Test: Parallel tool permission waits are merged (not summed)
test_parallel_tool_permission_wait() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
insert_session_with_parallel_tools
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" 2>&1)
# Total time was 35s, parallel tools spanned ~30s (merged), so thinking time ~5s
# If tools were summed instead of merged, we'd get 35s - 90s = negative (clamped to 0)
# Output should show ~00:00:05, NOT 00:00:00
if [[ "$output" =~ 00:00:0[4-6] ]]; then
pass "parallel_tool_permission: time correctly merges overlapping tool waits"
else
fail "parallel_tool_permission" "~00:00:05 (merged parallel waits)" "$output"
fi
teardown
}
# Test: Only shows most recent session when multiple exist
test_single_session_output() {
TESTS_RUN=$((TESTS_RUN + 1))
setup
# Insert two sessions - older and newer
local now=1700000000000
local old_created=$((now - 120000))
local new_created=$((now - 60000))
sqlite3 "$DB_PATH" "INSERT INTO project (id, worktree, time_created, time_updated, sandboxes) VALUES ('proj_001', '$TEST_CWD', $old_created, $now, '[]');"
# Old session
sqlite3 "$DB_PATH" "INSERT INTO session VALUES ('session_old', 'proj_001', NULL, 'old', '$TEST_CWD', 'Old Session', '1.2.0', $old_created, $old_created);"
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_old_1', 'session_old', $old_created, $old_created, '{\"role\":\"user\",\"time\":{\"created\":$old_created},\"agent\":\"build\"}');"
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_old_2', 'session_old', $((old_created+1000)), $old_created, '{\"role\":\"assistant\",\"time\":{\"created\":$((old_created+1000)),\"completed\":$((old_created+5000))},\"tokens\":{\"input\":100,\"output\":50,\"reasoning\":0,\"cache\":{\"read\":0,\"write\":0}},\"parentID\":\"msg_old_1\"}');"
# New session (more recent)
sqlite3 "$DB_PATH" "INSERT INTO session VALUES ('session_new', 'proj_001', NULL, 'new', '$TEST_CWD', 'New Session', '1.2.0', $new_created, $now);"
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_new_1', 'session_new', $new_created, $now, '{\"role\":\"user\",\"time\":{\"created\":$new_created},\"agent\":\"build\"}');"
sqlite3 "$DB_PATH" "INSERT INTO message VALUES ('msg_new_2', 'session_new', $((new_created+1000)), $now, '{\"role\":\"assistant\",\"time\":{\"created\":$((new_created+1000)),\"completed\":$((new_created+10000))},\"tokens\":{\"input\":200,\"output\":100,\"reasoning\":0,\"cache\":{\"read\":0,\"write\":0}},\"parentID\":\"msg_new_1\"}');"
output=$(cd "$TEST_CWD" && HOME="$TEST_DIR" XDG_DATA_HOME="$DATA_DIR" "$TIMER" 2>&1)
# Should show only "New Session", not "Old Session"
if [[ "$output" == *"New Session"* && "$output" != *"Old Session"* ]]; then
pass "single_session: shows only most recent session"
else
fail "single_session" "Only 'New Session' shown" "$output"
fi
teardown
}
# Run all tests
main() {
echo "Running timer tests..."
echo ""
test_no_database
test_no_sessions
test_basic_session
test_question_wait_time
test_tool_permission_wait_time
test_parallel_tool_permission_wait
test_verbose_mode
test_single_session_output
echo ""
echo "================================"
echo "Tests: $TESTS_RUN, Passed: $TESTS_PASSED, Failed: $((TESTS_RUN - TESTS_PASSED))"
if [[ $TESTS_PASSED -eq $TESTS_RUN ]]; then
echo -e "${GREEN}All tests passed!${NC}"
exit 0
else
echo -e "${RED}Some tests failed${NC}"
exit 1
fi
}
main "$@"