test-mail.sh 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. #!/bin/bash
  2. # test-mail.sh - Test harness for mail-ops
  3. # Outputs: number of passing test cases
  4. # Each test prints PASS/FAIL and we count PASSes at the end
  5. set -uo pipefail
  6. MAIL_DB="$HOME/.claude/mail.db"
  7. MAIL_SCRIPT="$(dirname "$0")/mail-db.sh"
  8. HOOK_SCRIPT="$(dirname "$0")/../../hooks/check-mail.sh"
  9. # Resolve relative to repo root if needed
  10. if [ ! -f "$HOOK_SCRIPT" ]; then
  11. HOOK_SCRIPT="$(cd "$(dirname "$0")/../../.." && pwd)/hooks/check-mail.sh"
  12. fi
  13. PASS=0
  14. FAIL=0
  15. TOTAL=0
  16. assert() {
  17. local name="$1"
  18. local expected="$2"
  19. local actual="$3"
  20. TOTAL=$((TOTAL + 1))
  21. if [ "$expected" = "$actual" ]; then
  22. echo "PASS: $name"
  23. PASS=$((PASS + 1))
  24. else
  25. echo "FAIL: $name (expected='$expected', actual='$actual')"
  26. FAIL=$((FAIL + 1))
  27. fi
  28. }
  29. assert_contains() {
  30. local name="$1"
  31. local needle="$2"
  32. local haystack="$3"
  33. TOTAL=$((TOTAL + 1))
  34. if echo "$haystack" | grep -qF "$needle"; then
  35. echo "PASS: $name"
  36. PASS=$((PASS + 1))
  37. else
  38. echo "FAIL: $name (expected to contain '$needle')"
  39. FAIL=$((FAIL + 1))
  40. fi
  41. }
  42. assert_not_empty() {
  43. local name="$1"
  44. local value="$2"
  45. TOTAL=$((TOTAL + 1))
  46. if [ -n "$value" ]; then
  47. echo "PASS: $name"
  48. PASS=$((PASS + 1))
  49. else
  50. echo "FAIL: $name (was empty)"
  51. FAIL=$((FAIL + 1))
  52. fi
  53. }
  54. assert_empty() {
  55. local name="$1"
  56. local value="$2"
  57. TOTAL=$((TOTAL + 1))
  58. if [ -z "$value" ]; then
  59. echo "PASS: $name"
  60. PASS=$((PASS + 1))
  61. else
  62. echo "FAIL: $name (expected empty, got '$value')"
  63. FAIL=$((FAIL + 1))
  64. fi
  65. }
  66. assert_exit_code() {
  67. local name="$1"
  68. local expected="$2"
  69. local actual="$3"
  70. TOTAL=$((TOTAL + 1))
  71. if [ "$expected" = "$actual" ]; then
  72. echo "PASS: $name"
  73. PASS=$((PASS + 1))
  74. else
  75. echo "FAIL: $name (exit code expected=$expected, actual=$actual)"
  76. FAIL=$((FAIL + 1))
  77. fi
  78. }
  79. # No-op: cooldown was removed, but tests still call this
  80. clear_cooldown() { :; }
  81. # --- Setup: clean slate ---
  82. rm -f "$MAIL_DB"
  83. echo "=== Basic Operations ==="
  84. # T1: Init creates database
  85. bash "$MAIL_SCRIPT" init >/dev/null 2>&1
  86. assert "init creates database" "true" "$([ -f "$MAIL_DB" ] && echo true || echo false)"
  87. # T2: Count on empty inbox
  88. result=$(bash "$MAIL_SCRIPT" count)
  89. assert "empty inbox count is 0" "0" "$result"
  90. # T3: Send a message
  91. result=$(bash "$MAIL_SCRIPT" send "test-project" "Hello" "World" 2>&1)
  92. assert_contains "send succeeds" "Sent to test-project" "$result"
  93. # T4: Count after send (we're in claude-mods, sent to test-project)
  94. result=$(bash "$MAIL_SCRIPT" count)
  95. assert "count still 0 for sender project" "0" "$result"
  96. # T5: Send to self
  97. result=$(bash "$MAIL_SCRIPT" send "claude-mods" "Self mail" "Testing self-send" 2>&1)
  98. assert_contains "self-send succeeds" "Sent to claude-mods" "$result"
  99. # T6: Count after self-send
  100. result=$(bash "$MAIL_SCRIPT" count)
  101. assert "count is 1 after self-send" "1" "$result"
  102. # T7: Unread shows message
  103. result=$(bash "$MAIL_SCRIPT" unread)
  104. assert_contains "unread shows subject" "Self mail" "$result"
  105. # T8: Read marks as read
  106. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  107. result=$(bash "$MAIL_SCRIPT" count)
  108. assert "count is 0 after read" "0" "$result"
  109. # T9: List shows read messages
  110. result=$(bash "$MAIL_SCRIPT" list)
  111. assert_contains "list shows read status" "read" "$result"
  112. # T10: Projects lists known projects
  113. result=$(bash "$MAIL_SCRIPT" projects)
  114. assert_contains "projects lists claude-mods" "claude-mods" "$result"
  115. assert_contains "projects lists test-project" "test-project" "$result"
  116. echo ""
  117. echo "=== Edge Cases ==="
  118. # T11: Empty body - should fail gracefully
  119. result=$(bash "$MAIL_SCRIPT" send "target" "subject" "" 2>&1)
  120. exit_code=$?
  121. # Empty body should either fail or send empty - document the behavior
  122. TOTAL=$((TOTAL + 1))
  123. if [ $exit_code -ne 0 ] || echo "$result" | grep -qiE "error|required|empty"; then
  124. echo "PASS: empty body rejected or warned"
  125. PASS=$((PASS + 1))
  126. else
  127. echo "FAIL: empty body accepted silently"
  128. FAIL=$((FAIL + 1))
  129. fi
  130. # T12: Missing arguments to send
  131. result=$(bash "$MAIL_SCRIPT" send 2>&1)
  132. exit_code=$?
  133. assert_exit_code "send with no args fails" "1" "$exit_code"
  134. # T13: SQL injection in subject
  135. bash "$MAIL_SCRIPT" send "claude-mods" "'; DROP TABLE messages; --" "injection test" >/dev/null 2>&1
  136. result=$(bash "$MAIL_SCRIPT" count)
  137. # If table still exists and count works, injection failed (good)
  138. TOTAL=$((TOTAL + 1))
  139. if [ -n "$result" ] && [ "$result" -ge 0 ] 2>/dev/null; then
  140. echo "PASS: SQL injection in subject blocked"
  141. PASS=$((PASS + 1))
  142. else
  143. echo "FAIL: SQL injection may have succeeded"
  144. FAIL=$((FAIL + 1))
  145. fi
  146. # T14: SQL injection in body
  147. bash "$MAIL_SCRIPT" send "claude-mods" "test" "'); DELETE FROM messages; --" >/dev/null 2>&1
  148. result=$(bash "$MAIL_SCRIPT" count)
  149. TOTAL=$((TOTAL + 1))
  150. if [ -n "$result" ] && [ "$result" -ge 0 ] 2>/dev/null; then
  151. echo "PASS: SQL injection in body blocked"
  152. PASS=$((PASS + 1))
  153. else
  154. echo "FAIL: SQL injection in body may have succeeded"
  155. FAIL=$((FAIL + 1))
  156. fi
  157. # T15: SQL injection in project name
  158. bash "$MAIL_SCRIPT" send "'; DROP TABLE messages; --" "test" "injection via project" >/dev/null 2>&1
  159. result=$(bash "$MAIL_SCRIPT" count)
  160. TOTAL=$((TOTAL + 1))
  161. if [ -n "$result" ] && [ "$result" -ge 0 ] 2>/dev/null; then
  162. echo "PASS: SQL injection in project name blocked"
  163. PASS=$((PASS + 1))
  164. else
  165. echo "FAIL: SQL injection in project name may have succeeded"
  166. FAIL=$((FAIL + 1))
  167. fi
  168. # T16: Special characters in body (newlines, quotes, backslashes)
  169. bash "$MAIL_SCRIPT" send "claude-mods" "special chars" 'Line1\nLine2 "quoted" and back\\slash' >/dev/null 2>&1
  170. result=$(bash "$MAIL_SCRIPT" read 2>&1)
  171. assert_contains "special chars preserved" "special chars" "$result"
  172. # T17: Very long message body (1000+ chars)
  173. long_body=$(python3 -c "print('x' * 2000)" 2>/dev/null || printf '%0.s.' $(seq 1 2000))
  174. bash "$MAIL_SCRIPT" send "claude-mods" "long msg" "$long_body" >/dev/null 2>&1
  175. result=$(bash "$MAIL_SCRIPT" count)
  176. assert "long message accepted" "1" "$result"
  177. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  178. # T18: Unicode in subject and body
  179. bash "$MAIL_SCRIPT" send "claude-mods" "Unicode test" "Hello from Tokyo" >/dev/null 2>&1
  180. result=$(bash "$MAIL_SCRIPT" read 2>&1)
  181. assert_contains "unicode in body" "Tokyo" "$result"
  182. # T19: Read by specific ID
  183. bash "$MAIL_SCRIPT" send "claude-mods" "ID test" "Read me by ID" >/dev/null 2>&1
  184. msg_id=$(sqlite3 "$MAIL_DB" "SELECT id FROM messages WHERE subject='ID test' AND read=0 LIMIT 1;")
  185. result=$(bash "$MAIL_SCRIPT" read "$msg_id" 2>&1)
  186. assert_contains "read by ID works" "Read me by ID" "$result"
  187. # T20: Read by invalid ID
  188. result=$(bash "$MAIL_SCRIPT" read 99999 2>&1)
  189. assert_empty "read invalid ID returns nothing" "$result"
  190. echo ""
  191. echo "=== Hook Tests ==="
  192. # T21: Hook silent on empty inbox
  193. bash "$MAIL_SCRIPT" read >/dev/null 2>&1 # clear any unread
  194. clear_cooldown
  195. result=$(bash "$HOOK_SCRIPT" 2>&1)
  196. assert_empty "hook silent when no mail" "$result"
  197. # T22: Hook delivers message inline (does NOT auto-read)
  198. bash "$MAIL_SCRIPT" send "claude-mods" "Hook test" "Should trigger hook" >/dev/null 2>&1
  199. clear_cooldown
  200. result=$(bash "$HOOK_SCRIPT" 2>&1)
  201. assert_contains "hook shows INCOMING MAIL" "INCOMING MAIL" "$result"
  202. assert_contains "hook shows subject" "Hook test" "$result"
  203. assert_contains "hook shows body" "Should trigger hook" "$result"
  204. # Signal cleared after first delivery, so second call is silent
  205. result2=$(bash "$HOOK_SCRIPT" 2>&1)
  206. assert_empty "hook silent after signal cleared" "$result2"
  207. # But messages are still unread (hook does NOT auto-read)
  208. unread_count=$(bash "$MAIL_SCRIPT" count 2>&1)
  209. assert_contains "messages persist unread after hook" "1" "$unread_count"
  210. # Manually mark read for cleanup
  211. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  212. # T23: Hook with missing database
  213. clear_cooldown
  214. backup_db="${MAIL_DB}.testbak"
  215. mv "$MAIL_DB" "$backup_db"
  216. result=$(bash "$HOOK_SCRIPT" 2>&1)
  217. exit_code=$?
  218. assert_exit_code "hook exits 0 with missing db" "0" "$exit_code"
  219. assert_empty "hook silent with missing db" "$result"
  220. mv "$backup_db" "$MAIL_DB"
  221. echo ""
  222. echo "=== Cleanup ==="
  223. # T24: Clear old messages
  224. bash "$MAIL_SCRIPT" read >/dev/null 2>&1 # mark all as read
  225. result=$(bash "$MAIL_SCRIPT" clear 0 2>&1)
  226. assert_contains "clear reports deleted count" "Cleared" "$result"
  227. # T25: Count after clear
  228. result=$(bash "$MAIL_SCRIPT" count)
  229. assert "count 0 after clear" "0" "$result"
  230. # T26: Help command
  231. result=$(bash "$MAIL_SCRIPT" help 2>&1)
  232. assert_contains "help shows usage" "Usage" "$result"
  233. # T27: Unknown command
  234. result=$(bash "$MAIL_SCRIPT" nonexistent 2>&1)
  235. exit_code=$?
  236. assert_exit_code "unknown command fails" "1" "$exit_code"
  237. echo ""
  238. echo "=== Input Validation ==="
  239. # T28: Non-numeric message ID rejected
  240. result=$(bash "$MAIL_SCRIPT" read "abc" 2>&1)
  241. exit_code=$?
  242. assert_exit_code "non-numeric ID rejected" "1" "$exit_code"
  243. # T29: SQL injection via message ID
  244. bash "$MAIL_SCRIPT" send "claude-mods" "id-inject-test" "before injection" >/dev/null 2>&1
  245. result=$(bash "$MAIL_SCRIPT" read "1 OR 1=1" 2>&1)
  246. exit_code=$?
  247. assert_exit_code "SQL injection via ID rejected" "1" "$exit_code"
  248. # T30: Non-numeric limit in list
  249. result=$(bash "$MAIL_SCRIPT" list "abc" 2>&1)
  250. exit_code=$?
  251. assert_exit_code "non-numeric limit handled" "0" "$exit_code"
  252. # T31: Non-numeric days in clear
  253. result=$(bash "$MAIL_SCRIPT" clear "abc" 2>&1)
  254. assert_contains "non-numeric days handled" "Cleared" "$result"
  255. # T32: Single quotes in subject preserved
  256. bash "$MAIL_SCRIPT" read >/dev/null 2>&1 # clear unread
  257. bash "$MAIL_SCRIPT" send "claude-mods" "it's working" "body with 'quotes'" >/dev/null 2>&1
  258. result=$(bash "$MAIL_SCRIPT" read 2>&1)
  259. assert_contains "single quotes in subject" "it's working" "$result"
  260. # T33: Double quotes in body preserved
  261. bash "$MAIL_SCRIPT" send "claude-mods" "quotes" 'She said "hello"' >/dev/null 2>&1
  262. result=$(bash "$MAIL_SCRIPT" read 2>&1)
  263. assert_contains "double quotes in body" "hello" "$result"
  264. # T34: Project name with spaces (edge case)
  265. bash "$MAIL_SCRIPT" send "my project" "spaces" "project name has spaces" >/dev/null 2>&1
  266. result=$(bash "$MAIL_SCRIPT" projects)
  267. assert_contains "project with spaces stored" "my project" "$result"
  268. # T35: Multiple rapid sends
  269. for i in 1 2 3 4 5; do
  270. bash "$MAIL_SCRIPT" send "claude-mods" "rapid-$i" "rapid fire test $i" >/dev/null 2>&1
  271. done
  272. result=$(bash "$MAIL_SCRIPT" count)
  273. assert "5 rapid sends all counted" "5" "$result"
  274. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  275. # T36: Init is idempotent
  276. bash "$MAIL_SCRIPT" init >/dev/null 2>&1
  277. bash "$MAIL_SCRIPT" init >/dev/null 2>&1
  278. result=$(bash "$MAIL_SCRIPT" count)
  279. assert "init idempotent" "0" "$result"
  280. # T37: Empty subject defaults
  281. result=$(bash "$MAIL_SCRIPT" send "claude-mods" "" "empty subject body" 2>&1)
  282. assert_contains "empty subject accepted" "Sent to claude-mods" "$result"
  283. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  284. echo ""
  285. echo "=== Reply ==="
  286. # T38: Reply to a message
  287. bash "$MAIL_SCRIPT" send "claude-mods" "Original msg" "Please reply" >/dev/null 2>&1
  288. msg_id=$(sqlite3 "$MAIL_DB" "SELECT id FROM messages WHERE subject='Original msg' AND read=0 LIMIT 1;")
  289. bash "$MAIL_SCRIPT" read "$msg_id" >/dev/null 2>&1
  290. result=$(bash "$MAIL_SCRIPT" reply "$msg_id" "Here is my reply" 2>&1)
  291. assert_contains "reply succeeds" "Replied to claude-mods" "$result"
  292. assert_contains "reply has Re: prefix" "Re: Original msg" "$result"
  293. # T39: Reply to nonexistent message
  294. result=$(bash "$MAIL_SCRIPT" reply 99999 "reply to nothing" 2>&1)
  295. exit_code=$?
  296. assert_exit_code "reply to nonexistent fails" "1" "$exit_code"
  297. # T40: Reply with empty body
  298. result=$(bash "$MAIL_SCRIPT" reply "$msg_id" "" 2>&1)
  299. exit_code=$?
  300. assert_exit_code "reply with empty body fails" "1" "$exit_code"
  301. # T41: Reply with non-numeric ID
  302. result=$(bash "$MAIL_SCRIPT" reply "abc" "body" 2>&1)
  303. exit_code=$?
  304. assert_exit_code "reply with non-numeric ID fails" "1" "$exit_code"
  305. # Clean up
  306. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  307. echo ""
  308. echo "=== Priority & Search ==="
  309. # T38: Send urgent message
  310. result=$(bash "$MAIL_SCRIPT" send --urgent "claude-mods" "Server down" "Production is on fire" 2>&1)
  311. assert_contains "urgent send succeeds" "URGENT" "$result"
  312. # T39: Hook delivers urgent message with marker
  313. clear_cooldown
  314. result=$(bash "$HOOK_SCRIPT" 2>&1)
  315. assert_contains "hook shows URGENT" "URGENT" "$result"
  316. assert_contains "hook shows urgent body" "Production is on fire" "$result"
  317. # T40: Normal send still works after priority feature
  318. result=$(bash "$MAIL_SCRIPT" send "claude-mods" "Normal msg" "not urgent" 2>&1)
  319. TOTAL=$((TOTAL + 1))
  320. if echo "$result" | grep -qvF "URGENT"; then
  321. echo "PASS: normal send has no URGENT tag"
  322. PASS=$((PASS + 1))
  323. else
  324. echo "FAIL: normal send incorrectly tagged URGENT"
  325. FAIL=$((FAIL + 1))
  326. fi
  327. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  328. # T41: Search by keyword in subject
  329. bash "$MAIL_SCRIPT" send "claude-mods" "API endpoint changed" "details here" >/dev/null 2>&1
  330. bash "$MAIL_SCRIPT" send "claude-mods" "unrelated" "nothing relevant" >/dev/null 2>&1
  331. result=$(bash "$MAIL_SCRIPT" search "API" 2>&1)
  332. assert_contains "search finds by subject" "API endpoint" "$result"
  333. # T42: Search by keyword in body
  334. result=$(bash "$MAIL_SCRIPT" search "relevant" 2>&1)
  335. assert_contains "search finds by body" "unrelated" "$result"
  336. # T43: Search with no results
  337. result=$(bash "$MAIL_SCRIPT" search "xyznonexistent" 2>&1)
  338. assert_empty "search no results is empty" "$result"
  339. # T44: Search with no keyword fails
  340. result=$(bash "$MAIL_SCRIPT" search 2>&1)
  341. exit_code=$?
  342. assert_exit_code "search no keyword fails" "1" "$exit_code"
  343. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  344. echo ""
  345. echo "=== Broadcast & Status ==="
  346. # Setup: ensure multiple projects exist
  347. bash "$MAIL_SCRIPT" send "project-a" "setup" "creating project-a" >/dev/null 2>&1
  348. bash "$MAIL_SCRIPT" send "project-b" "setup" "creating project-b" >/dev/null 2>&1
  349. # T42: Broadcast sends to all known projects except self
  350. result=$(bash "$MAIL_SCRIPT" broadcast "Announcement" "Main is frozen" 2>&1)
  351. assert_contains "broadcast reports count" "Broadcast to" "$result"
  352. # T43: Broadcast doesn't send to self
  353. self_count=$(sqlite3 "$MAIL_DB" "SELECT COUNT(*) FROM messages WHERE to_project='claude-mods' AND subject='Announcement';")
  354. assert "broadcast skips self" "0" "$self_count"
  355. # T44: Broadcast with empty body fails
  356. result=$(bash "$MAIL_SCRIPT" broadcast "test" "" 2>&1)
  357. exit_code=$?
  358. assert_exit_code "broadcast empty body fails" "1" "$exit_code"
  359. # T45: Status shows inbox summary
  360. bash "$MAIL_SCRIPT" send "claude-mods" "Status test 1" "msg1" >/dev/null 2>&1
  361. bash "$MAIL_SCRIPT" send "claude-mods" "Status test 2" "msg2" >/dev/null 2>&1
  362. result=$(bash "$MAIL_SCRIPT" status 2>&1)
  363. assert_contains "status shows unread count" "unread" "$result"
  364. assert_contains "status shows Inbox" "Inbox" "$result"
  365. # T46: Status on empty inbox
  366. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  367. result=$(bash "$MAIL_SCRIPT" status 2>&1)
  368. assert_contains "status shows 0 unread" "0 unread" "$result"
  369. echo ""
  370. echo "=== Alias (Rename) ==="
  371. # Setup: send messages with old project name
  372. bash "$MAIL_SCRIPT" send "old-project" "before rename" "testing alias" >/dev/null 2>&1
  373. bash "$MAIL_SCRIPT" send "claude-mods" "from old" "message from old name" >/dev/null 2>&1
  374. # T47: Alias renames in all messages
  375. result=$(bash "$MAIL_SCRIPT" alias "old-project" "new-project" 2>&1)
  376. assert_contains "alias reports rename" "Renamed" "$result"
  377. assert_contains "alias shows old name" "old-project" "$result"
  378. assert_contains "alias shows new name" "new-project" "$result"
  379. # T48: Old project name no longer appears
  380. result=$(bash "$MAIL_SCRIPT" projects)
  381. TOTAL=$((TOTAL + 1))
  382. if echo "$result" | grep -qF "old-project"; then
  383. echo "FAIL: old project name still present after alias"
  384. FAIL=$((FAIL + 1))
  385. else
  386. echo "PASS: old project name removed after alias"
  387. PASS=$((PASS + 1))
  388. fi
  389. # T49: New project name appears
  390. assert_contains "new project name present" "new-project" "$result"
  391. # T50: Alias with missing args fails
  392. result=$(bash "$MAIL_SCRIPT" alias "only-one" 2>&1)
  393. exit_code=$?
  394. assert_exit_code "alias with missing arg fails" "1" "$exit_code"
  395. # Clean up
  396. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  397. echo ""
  398. echo "=== Hook ==="
  399. # T52: Hook delivers without auto-read
  400. bash "$MAIL_SCRIPT" send "claude-mods" "hook test" "testing hook" >/dev/null 2>&1
  401. result1=$(bash "$HOOK_SCRIPT" 2>&1)
  402. assert_contains "hook delivers message" "INCOMING MAIL" "$result1"
  403. # T53: Signal cleared after delivery, second call silent
  404. result2=$(bash "$HOOK_SCRIPT" 2>&1)
  405. assert_empty "hook silent after signal cleared (2)" "$result2"
  406. # Messages still unread - verify then clean up
  407. bash "$MAIL_SCRIPT" read >/dev/null 2>&1
  408. echo ""
  409. echo "=== Purge ==="
  410. # T54: Purge removes messages for current project
  411. bash "$MAIL_SCRIPT" send "claude-mods" "purge test 1" "msg1" >/dev/null 2>&1
  412. bash "$MAIL_SCRIPT" send "claude-mods" "purge test 2" "msg2" >/dev/null 2>&1
  413. # Insert a message not involving claude-mods at all
  414. sqlite3 "$MAIL_DB" "INSERT INTO messages (from_project, to_project, subject, body) VALUES ('alpha', 'beta', 'unrelated', 'should survive');"
  415. result=$(bash "$MAIL_SCRIPT" purge 2>&1)
  416. assert_contains "purge reports count" "Purged" "$result"
  417. # T55: Unrelated project messages survive purge
  418. other_count=$(sqlite3 "$MAIL_DB" "SELECT COUNT(*) FROM messages WHERE from_project='alpha';")
  419. assert "unrelated messages survive purge" "1" "$other_count"
  420. # T56: Purge --all removes everything
  421. bash "$MAIL_SCRIPT" send "claude-mods" "test" "body" >/dev/null 2>&1
  422. result=$(bash "$MAIL_SCRIPT" purge --all 2>&1)
  423. assert_contains "purge --all reports count" "Purged all" "$result"
  424. total=$(sqlite3 "$MAIL_DB" "SELECT COUNT(*) FROM messages;")
  425. assert "purge --all empties db" "0" "$total"
  426. echo ""
  427. echo "=== Per-Project Disable ==="
  428. # T52: Hook respects .claude/agentmail.disable
  429. bash "$MAIL_SCRIPT" send "claude-mods" "disable test" "should not appear" >/dev/null 2>&1
  430. clear_cooldown
  431. mkdir -p .claude
  432. touch .claude/agentmail.disable
  433. result=$(bash "$HOOK_SCRIPT" 2>&1)
  434. assert_empty "hook silent when disabled" "$result"
  435. # T53: Hook delivers after re-enable
  436. rm -f .claude/agentmail.disable
  437. clear_cooldown
  438. result=$(bash "$HOOK_SCRIPT" 2>&1)
  439. assert_contains "hook works after re-enable" "INCOMING MAIL" "$result"
  440. echo ""
  441. echo "=== Results ==="
  442. echo "Passed: $PASS / $TOTAL"
  443. echo "Failed: $FAIL / $TOTAL"
  444. echo ""
  445. echo "$PASS"