-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.sh
More file actions
317 lines (263 loc) · 10.4 KB
/
test.sh
File metadata and controls
317 lines (263 loc) · 10.4 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
#!/usr/bin/env bash
# test.sh — Claude Code Notify 测试脚本 (macOS/Linux)
# 覆盖: JSON解析、项目名检测、终端检测、标签构建、通知构建、图标路径
# Usage: bash test.sh
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PASS=0
FAIL=0
# ============================================================
# Helpers
# ============================================================
assert_eq() {
local expected="$1" actual="$2" name="$3"
if [ "$expected" = "$actual" ]; then
PASS=$((PASS + 1))
printf " \033[32mPASS\033[0m: %s\n" "$name"
else
FAIL=$((FAIL + 1))
printf " \033[31mFAIL\033[0m: %s\n" "$name"
printf " Expected: '%s'\n" "$expected" >&2
printf " Actual: '%s'\n" "$actual" >&2
fi
}
assert_match() {
local pattern="$1" actual="$2" name="$3"
if echo "$actual" | grep -qE "$pattern"; then
PASS=$((PASS + 1))
printf " \033[32mPASS\033[0m: %s\n" "$name"
else
FAIL=$((FAIL + 1))
printf " \033[31mFAIL\033[0m: %s\n" "$name"
printf " Pattern: '%s'\n" "$pattern" >&2
printf " Actual: '%s'\n" "$actual" >&2
fi
}
assert_true() {
local cond="$1" name="$2"
if eval "$cond"; then
PASS=$((PASS + 1))
printf " \033[32mPASS\033[0m: %s\n" "$name"
else
FAIL=$((FAIL + 1))
printf " \033[31mFAIL\033[0m: %s\n" "$name"
fi
}
# ============================================================
# Source functions from notify.sh (extract reusable logic)
# ============================================================
ICON_DIR="$HOME/.claude"
ICON_PATH="$ICON_DIR/notify-icon.png"
APP_NAME="Claude Code"
json_get() {
local json="$1" key="$2"
if command -v jq &>/dev/null; then
echo "$json" | jq -r ".$key // empty" 2>/dev/null || echo ""
return
fi
local py
py=$(command -v python3 || command -v python || true)
if [ -n "$py" ]; then
echo "$json" | "$py" -c "import sys,json; d=json.load(sys.stdin); print(d.get(sys.argv[1],''))" "$key" 2>/dev/null || echo ""
return
fi
echo "$json" | sed -n "s/.*\"$key\"[[:space:]]*:[[:space:]]*\"\\([^\"]*\\)\".*/\\1/p" | head -1 || echo ""
}
get_project_name() {
local cwd="$1"
[ -z "$cwd" ] && return
if command -v git &>/dev/null; then
local git_root
git_root=$(git -C "$cwd" rev-parse --show-toplevel 2>/dev/null || true)
if [ -n "$git_root" ]; then
local name branch
name=$(basename "$git_root")
branch=$(git -C "$cwd" branch --show-current 2>/dev/null || true)
if [ -n "$branch" ]; then
echo "$name [$branch]"
return
fi
echo "$name"
return
fi
fi
echo "$cwd" | awk -F/ '{if(NF>=2) print $(NF-1)"/"$NF; else print $NF}'
}
get_terminal_identity() {
if [ -n "${WT_SESSION:-}" ]; then echo "Windows Terminal"; return; fi
case "${TERM_PROGRAM:-}" in
vscode) echo "VS Code"; return ;;
WezTerm) echo "WezTerm"; return ;;
alacritty) echo "Alacritty"; return ;;
iTerm.app) echo "iTerm2"; return ;;
ghostty) echo "Ghostty"; return ;;
esac
case "${TERM:-}" in
xterm-kitty) echo "Kitty"; return ;;
esac
echo "Terminal"
}
build_label() {
local project="$1" terminal="$2"
local result=""
[ -n "$project" ] && result="$project"
[ -n "$terminal" ] && result="${result:+$result · }$terminal"
echo "$result"
}
get_type_config() {
case "$1" in
success) echo "Done|✓|Default|Task completed" ;;
warning) echo "Attention|⚠|IM|Claude needs your approval" ;;
error) echo "Error|✗|Reminder|An error occurred" ;;
*) echo "Idle|●|Default|Claude is waiting for your input" ;;
esac
}
# ============================================================
# 测试: json_get
# ============================================================
printf "\n=== json_get ===\n"
JSON='{"cwd":"/home/user/myapp","message":"Hello world"}'
assert_eq "/home/user/myapp" "$(json_get "$JSON" "cwd")" "extract cwd"
assert_eq "Hello world" "$(json_get "$JSON" "message")" "extract message"
JSON_EMPTY='{}'
assert_eq "" "$(json_get "$JSON_EMPTY" "cwd")" "missing key returns empty"
JSON_SPACE='{"cwd":"/path/with spaces/app"}'
assert_eq "/path/with spaces/app" "$(json_get "$JSON_SPACE" "cwd")" "value with spaces"
JSON_SPECIAL='{"message":"He said \"hello\" and bye"}'
result=$(json_get "$JSON_SPECIAL" "message")
assert_match "hello" "$result" "value with escaped quotes"
# Invalid JSON should not crash (returns empty)
result=$(json_get "not json at all" "cwd" 2>/dev/null || true)
assert_eq "" "$result" "invalid JSON returns empty without crash"
# ============================================================
# 测试: get_project_name
# ============================================================
printf "\n=== get_project_name ===\n"
# Git repo
name=$(get_project_name "$SCRIPT_DIR")
assert_match "claude-code-notify" "$name" "git repo returns project name"
# Empty
result=$(get_project_name "" 2>/dev/null || true)
assert_eq "" "$result" "empty cwd returns empty"
# Non-git path
result=$(get_project_name "/some/random/path")
assert_match "random/path" "$result" "non-git path returns last two segments"
# Single segment
result=$(get_project_name "myapp")
assert_eq "myapp" "$result" "single segment returns itself"
# ============================================================
# 测试: get_terminal_identity
# ============================================================
printf "\n=== get_terminal_identity ===\n"
# Should return something (not crash)
result=$(get_terminal_identity)
assert_true "[ -n \"$result\" ]" "returns non-empty: '$result'"
# Simulate VS Code
result=$(TERM_PROGRAM=vscode bash -c 'case "${TERM_PROGRAM:-}" in vscode) echo "VS Code";; esac')
assert_eq "VS Code" "$result" "TERM_PROGRAM=vscode logic works"
# Simulate iTerm2
result=$(TERM_PROGRAM=iTerm.app bash -c 'case "${TERM_PROGRAM:-}" in iTerm.app) echo "iTerm2";; esac')
assert_eq "iTerm2" "$result" "TERM_PROGRAM=iTerm.app logic works"
# Simulate Kitty
result=$(TERM_PROGRAM= TERM=xterm-kitty bash -c 'case "${TERM:-}" in xterm-kitty) echo "Kitty";; esac')
assert_eq "Kitty" "$result" "TERM=xterm-kitty logic works"
# ============================================================
# 测试: build_label (BUG 2 regression test)
# ============================================================
printf "\n=== build_label ===\n"
result=$(build_label "myapp [main]" "Windows Terminal")
assert_eq "myapp [main] · Windows Terminal" "$result" "project + terminal joined with ·"
result=$(build_label "myapp" "VS Code")
assert_eq "myapp · VS Code" "$result" "project + terminal (simple)"
result=$(build_label "" "Terminal")
assert_eq "Terminal" "$result" "terminal only"
result=$(build_label "myapp" "")
assert_eq "myapp" "$result" "project only"
result=$(build_label "" "")
assert_eq "" "$result" "both empty"
# ============================================================
# 测试: get_type_config
# ============================================================
printf "\n=== get_type_config ===\n"
# info
IFS='|' read -r prefix emoji sound fallback <<< "$(get_type_config info)"
assert_eq "Idle" "$prefix" "info prefix"
assert_eq "Default" "$sound" "info sound"
assert_eq "Claude is waiting for your input" "$fallback" "info fallback"
# warning
IFS='|' read -r prefix emoji sound fallback <<< "$(get_type_config warning)"
assert_eq "Attention" "$prefix" "warning prefix"
assert_eq "IM" "$sound" "warning sound"
# error
IFS='|' read -r prefix emoji sound fallback <<< "$(get_type_config error)"
assert_eq "Error" "$prefix" "error prefix"
assert_eq "Reminder" "$sound" "error sound"
# success
IFS='|' read -r prefix emoji sound fallback <<< "$(get_type_config success)"
assert_eq "Done" "$prefix" "success prefix"
assert_eq "Default" "$sound" "success sound"
# unknown defaults to info
IFS='|' read -r prefix _ _ fallback <<< "$(get_type_config unknown)"
assert_eq "Idle" "$prefix" "unknown type defaults to info"
# ============================================================
# 测试: osascript 转义 (BUG 1 regression test)
# ============================================================
printf "\n=== osascript escaping ===\n"
# Simulate the escaping logic
test_escape() {
local s="$1"
s="${s//\\/\\\\}"; s="${s//\"/\\\"}"
echo "$s"
}
assert_eq 'hello' "$(test_escape 'hello')" "no special chars"
assert_eq 'he said \"hello\"' "$(test_escape 'he said "hello"')" "quotes escaped"
assert_eq 'path\\\\file' "$(test_escape 'path\\file')" "backslashes escaped"
assert_eq 'a\\\"b' "$(test_escape 'a\"b')" "mixed backslash+quote"
# ============================================================
# 测试: 图标文件
# ============================================================
printf "\n=== Icon Files ===\n"
if [ -f "$SCRIPT_DIR/notify-icon.png" ]; then
PASS=$((PASS + 1))
printf " \033[32mPASS\033[0m: bundled icon exists in repo\n"
else
FAIL=$((FAIL + 1))
printf " \033[31mFAIL\033[0m: bundled icon exists in repo\n"
fi
printf " INFO: Icon path = %s\n" "$ICON_PATH"
# ============================================================
# 测试: 完整通知流程 (模拟)
# ============================================================
printf "\n=== Full Notification Flow (simulated) ===\n"
JSON_INPUT='{"cwd":"'"$SCRIPT_DIR"'","message":"Test notification"}'
stdin_data="$JSON_INPUT"
cwd=$(json_get "$stdin_data" "cwd")
message=$(json_get "$stdin_data" "message")
[ -z "$cwd" ] && cwd="$PWD"
project=$(get_project_name "$cwd")
terminal=$(get_terminal_identity)
label=$(build_label "$project" "$terminal")
type_config=$(get_type_config "info")
IFS='|' read -r prefix emoji sound fallback <<< "$type_config"
if [ -n "$label" ]; then
title="$emoji $prefix · $label"
else
title="$emoji $prefix"
fi
[ -z "$message" ] && message="$fallback"
assert_match "Idle" "$title" "simulated title has Idle prefix"
assert_match "claude-code-notify" "$title" "simulated title has project name"
assert_eq "Test notification" "$message" "simulated message from JSON"
# ============================================================
# Summary
# ============================================================
printf "\n========================================\n"
total=$((PASS + FAIL))
if [ "$FAIL" -gt 0 ]; then
printf " Total: %d | \033[32mPASS: %d\033[0m | \033[31mFAIL: %d\033[0m\n" "$total" "$PASS" "$FAIL"
else
printf " Total: %d | \033[32mPASS: %d\033[0m | \033[32mFAIL: %d\033[0m\n" "$total" "$PASS" "$FAIL"
fi
printf "========================================\n\n"
[ "$FAIL" -gt 0 ] && exit 1
exit 0