#!/usr/bin/env bats load test_helper # Claude Code's notifications (system/claude-notify) and claude.sh's hooks, # with a fake /proc and fake desktop entries setup() { common_setup stub notify-send export CLAUDE_NOTIFY_PROC="$BATS_TEST_TMPDIR/proc" export XDG_DATA_DIRS="$BATS_TEST_TMPDIR/share" mkdir -p "$XDG_DATA_DIRS/applications" unset TMUX FLATPAK_ID XDG_DATA_HOME } # process PID PPID COMM process() { mkdir -p "$CLAUDE_NOTIFY_PROC/$1" echo "$3" > "$CLAUDE_NOTIFY_PROC/$1/comm" printf 'Name:\t%s\nPPid:\t%s\n' "$3" "$2" > "$CLAUDE_NOTIFY_PROC/$1/status" } app() { touch "$XDG_DATA_DIRS/applications/$1.desktop" } @test "a notification from Claude in Console names Console, so clicking it brings Console back" { app org.gnome.Console process 40 1 systemd process 30 40 kgx process 20 30 bash process 10 20 claude CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" 'Needs your input' <<< '{"message": "Claude needs your permission to use Bash", "cwd": "/home/me/project"}' [ "$status" -eq 0 ] grep -qF -- '--hint=string:desktop-entry:org.gnome.Console Claude Code: project Claude needs your permission to use Bash' "$STUB_LOG" } @test "without a message of its own, the notification shows the text it's given" { process 10 1 claude CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" 'Task completed' <<< '{"cwd": "/home/me/project"}' [ "$status" -eq 0 ] grep -q 'Claude Code: project Task completed$' "$STUB_LOG" } @test "in VS Codium, the notification names whichever VS Codium is installed" { app codium process 30 1 codium process 20 30 bash process 10 20 claude CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" <<< '{}' grep -qF -- '--hint=string:desktop-entry:codium ' "$STUB_LOG" } @test "in VS Codium's Flatpak, the notification names the Flatpak" { FLATPAK_ID=com.vscodium.codium run bash "$REPO/system/claude-notify" <<< '{}' grep -qF -- '--hint=string:desktop-entry:com.vscodium.codium ' "$STUB_LOG" } @test "in tmux, the app is the one tmux's client runs in" { app org.gnome.Console process 30 1 kgx process 25 30 tmux stub tmux 'echo 25' TMUX=/tmp/tmux-1000/default,1,0 run bash "$REPO/system/claude-notify" <<< '{}' grep -qF -- '--hint=string:desktop-entry:org.gnome.Console ' "$STUB_LOG" } @test "with no app it knows, the notification is still sent" { process 20 1 sshd process 10 20 claude CLAUDE_NOTIFY_PID=10 run bash "$REPO/system/claude-notify" <<< '{}' [ "$status" -eq 0 ] grep -q '^notify-send ' "$STUB_LOG" ! grep -q 'desktop-entry' "$STUB_LOG" } # --- claude.sh ----------------------------------------------------------------- claude_setup() { stub claude 'echo 2.0.0' echo 'export PATH="$HOME/.local/bin:$PATH"' > "$HOME/.bashrc" mkdir -p "$HOME/.claude" } @test "claude.sh writes hooks that run claude-notify" { claude_setup run bash "$REPO/claude.sh" [ "$status" -eq 0 ] [ "$(jq -r '.hooks.Notification[0].hooks[0].command' "$HOME/.claude/settings.json")" = \ "~/.local/share/coredesktop/system/claude-notify 'Needs your input'" ] [ "$(jq -r '.hooks.Stop[0].hooks[0].command' "$HOME/.claude/settings.json")" = \ "~/.local/share/coredesktop/system/claude-notify 'Task completed'" ] } @test "claude.sh changes the old notify-send hooks, and keeps everything else" { claude_setup cat > "$HOME/.claude/settings.json" << 'EOF' { "model": "opus", "hooks": { "Notification": [{"matcher": "", "hooks": [{"type": "command", "command": "notify-send 'Claude Code' 'Needs your input'"}]}], "Stop": [{"matcher": "", "hooks": [{"type": "command", "command": "notify-send 'Claude Code' 'Task completed'"}, {"type": "command", "command": "my-own-hook"}]}] } } EOF run bash "$REPO/claude.sh" [ "$status" -eq 0 ] [[ "$output" == *"Making Claude Code's notifications clickable"* ]] settings="$HOME/.claude/settings.json" [ "$(jq -r '.model' "$settings")" = opus ] [ "$(jq -r '.hooks.Notification[0].hooks[0].command' "$settings")" = \ "~/.local/share/coredesktop/system/claude-notify 'Needs your input'" ] [ "$(jq -r '.hooks.Stop[0].hooks[0].command' "$settings")" = \ "~/.local/share/coredesktop/system/claude-notify 'Task completed'" ] [ "$(jq -r '.hooks.Stop[0].hooks[1].command' "$settings")" = my-own-hook ] # A second run has nothing to change run bash "$REPO/claude.sh" [[ "$output" != *"Making Claude Code's notifications clickable"* ]] } @test "claude.sh leaves hooks you changed yourself, and settings without hooks" { claude_setup echo '{"hooks": {"Stop": [{"hooks": [{"type": "command", "command": "notify-send Done"}]}]}}' > "$HOME/.claude/settings.json" cp "$HOME/.claude/settings.json" "$BATS_TEST_TMPDIR/before.json" run bash "$REPO/claude.sh" [ "$status" -eq 0 ] cmp "$HOME/.claude/settings.json" "$BATS_TEST_TMPDIR/before.json" echo '{"model": "opus"}' > "$HOME/.claude/settings.json" run bash "$REPO/claude.sh" [ "$status" -eq 0 ] [ "$(cat "$HOME/.claude/settings.json")" = '{"model": "opus"}' ] }