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