-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_claudeping.sh
More file actions
149 lines (131 loc) · 5.7 KB
/
Copy pathtest_claudeping.sh
File metadata and controls
149 lines (131 loc) · 5.7 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
#!/usr/bin/env bash
# ClaudePing test suite
# Tests the claudeping.sh script behaviors
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT="$SCRIPT_DIR/claudeping.sh"
PASS=0
FAIL=0
assert_exit_0() {
local desc="$1"
local exit_code="$2"
if [[ "$exit_code" -eq 0 ]]; then
echo "PASS: $desc"
((PASS++))
else
echo "FAIL: $desc (exit code: $exit_code)"
((FAIL++))
fi
}
assert_contains() {
local desc="$1"
local haystack="$2"
local needle="$3"
if echo "$haystack" | grep -q "$needle"; then
echo "PASS: $desc"
((PASS++))
else
echo "FAIL: $desc (expected to contain: $needle)"
((FAIL++))
fi
}
assert_not_empty() {
local desc="$1"
local value="$2"
if [[ -n "$value" ]]; then
echo "PASS: $desc"
((PASS++))
else
echo "FAIL: $desc (value was empty)"
((FAIL++))
fi
}
echo "=== ClaudePing Test Suite ==="
echo ""
# Test 1: --test flag with valid .env prints success message and exits 0
# (We can't test with a real .env/Telegram, so we test structure instead)
# We test that --test with MISSING .env exits 0 silently
echo "--- Test 1: --test with missing .env exits 0 silently ---"
OUTPUT=$(bash "$SCRIPT" --test 2>&1)
EXIT_CODE=$?
assert_exit_0 "Test 1: --test with missing .env exits 0" "$EXIT_CODE"
# Test 2: --test with missing .env exits 0 silently (no crash)
echo "--- Test 2: --test with missing .env produces no error output ---"
# Already tested above; exit code is the key check
assert_exit_0 "Test 2: --test missing .env exits 0" "$EXIT_CODE"
# Test 3: empty JSON with missing .env exits 0
echo "--- Test 3: empty JSON with missing .env exits 0 ---"
OUTPUT=$(echo '{}' | bash "$SCRIPT" 2>&1)
EXIT_CODE=$?
assert_exit_0 "Test 3: echo '{}' | claudeping.sh exits 0" "$EXIT_CODE"
# Test 4: malformed input exits 0
echo "--- Test 4: malformed input exits 0 ---"
OUTPUT=$(echo 'not json' | bash "$SCRIPT" 2>&1)
EXIT_CODE=$?
assert_exit_0 "Test 4: echo 'not json' | claudeping.sh exits 0" "$EXIT_CODE"
# Test 5: Script file exists and is executable
echo "--- Test 5: Script exists and is executable ---"
if [[ -f "$SCRIPT" ]] && [[ -x "$SCRIPT" ]]; then
echo "PASS: Test 5: script exists and is executable"
((PASS++))
else
echo "FAIL: Test 5: script missing or not executable"
((FAIL++))
fi
# Test 6: Script structure - stdin read first
echo "--- Test 6: Script structure checks ---"
CONTENT=$(cat "$SCRIPT")
assert_contains "Test 6a: has shebang" "$CONTENT" "#!/usr/bin/env bash"
assert_contains "Test 6b: has trap exit 0 ERR" "$CONTENT" "trap 'exit 0' ERR"
assert_contains "Test 6c: has INPUT=\$(cat)" "$CONTENT" 'INPUT=\$(cat)'
assert_contains "Test 6d: has html_escape function" "$CONTENT" "html_escape"
assert_contains "Test 6e: has node -e" "$CONTENT" "node -e"
assert_contains "Test 6f: has BASH_SOURCE" "$CONTENT" "BASH_SOURCE"
assert_contains "Test 6g: has connect-timeout" "$CONTENT" "connect-timeout 3"
assert_contains "Test 6h: has max-time" "$CONTENT" "max-time 5"
assert_contains "Test 6i: has data-urlencode" "$CONTENT" "data-urlencode"
assert_contains "Test 6j: has --test flag" "$CONTENT" "\-\-test"
assert_contains "Test 6k: has success message" "$CONTENT" "Test notification sent successfully"
assert_contains "Test 6l: ends with exit 0" "$CONTENT" "exit 0"
# Test 7: stdin-first ordering (INPUT=$(cat) appears before .env loading)
echo "--- Test 7: stdin-first ordering ---"
INPUT_LINE=$(grep -n 'INPUT=\$(cat)' "$SCRIPT" | head -1 | cut -d: -f1)
ENV_LINE=$(grep -n 'done < "$ENV_FILE"' "$SCRIPT" | head -1 | cut -d: -f1)
if [[ -n "$INPUT_LINE" ]] && [[ -n "$ENV_LINE" ]] && [[ "$INPUT_LINE" -lt "$ENV_LINE" ]]; then
echo "PASS: Test 7: INPUT=\$(cat) (line $INPUT_LINE) before .env load (line $ENV_LINE)"
((PASS++))
else
echo "FAIL: Test 7: stdin not read before .env load (INPUT line: $INPUT_LINE, env line: $ENV_LINE)"
((FAIL++))
fi
# Test 8: Emoji HTML entities present
echo "--- Test 8: Emoji HTML entities present ---"
assert_contains "Test 8a: has checkmark entity" "$CONTENT" '✅'
assert_contains "Test 8b: has question entity" "$CONTENT" '❓'
assert_contains "Test 8c: has satellite entity" "$CONTENT" '📡'
assert_contains "Test 8d: has lock entity" "$CONTENT" '🔐'
# Test 9: Event filtering - script contains CLAUDEPING_EVENTS logic
echo "--- Test 9: Event filtering logic ---"
assert_contains "Test 9a: has CLAUDEPING_EVENTS default" "$CONTENT" 'CLAUDEPING_EVENTS:-Stop,Notification'
assert_contains "Test 9b: has comma-padded event check" "$CONTENT" ',$EVENTS,'
assert_contains "Test 9c: has exit 0 for filtered events" "$CONTENT" 'exit 0'
# Test 10: Silent notification - script contains CLAUDEPING_SILENT logic
echo "--- Test 10: Silent notification logic ---"
assert_contains "Test 10a: has CLAUDEPING_SILENT variable construction" "$CONTENT" 'CLAUDEPING_SILENT_'
assert_contains "Test 10b: has DISABLE_NOTIFICATION variable" "$CONTENT" 'DISABLE_NOTIFICATION'
assert_contains "Test 10c: has disable_notification in curl" "$CONTENT" 'disable_notification'
# Test 11: Symlink-safe resolution - script contains resolve_script_dir
echo "--- Test 11: Symlink-safe .env resolution ---"
assert_contains "Test 11a: has resolve_script_dir function" "$CONTENT" 'resolve_script_dir'
assert_contains "Test 11b: has readlink in resolution" "$CONTENT" 'readlink'
# Test 12: Event filtering behavior - SubagentStop filtered by default
echo "--- Test 12: Event filtering behavior ---"
OUTPUT=$(echo '{"hook_event_name":"SubagentStop","cwd":"/tmp"}' | CLAUDEPING_EVENTS="Stop,Notification" CLAUDEPING_BOT_TOKEN=fake CLAUDEPING_CHAT_ID=fake bash "$SCRIPT" 2>&1)
EXIT_CODE=$?
assert_exit_0 "Test 12: SubagentStop filtered out by default events exits 0" "$EXIT_CODE"
echo ""
echo "=== Results: $PASS passed, $FAIL failed ==="
if [[ "$FAIL" -gt 0 ]]; then
exit 1
else
exit 0
fi