aboutsummaryrefslogtreecommitdiff
path: root/scripts/tests/ai-wrap-teardown-hook.bats
blob: 05c49f110aea505ae75d8cda3d9b0bc49fa3d538 (plain)
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
#!/usr/bin/env bats
# hooks/ai-wrap-teardown.sh — Stop hook that tears down the ai-term session
# (or powers off) after a wrap-up, gated on a sentinel wrap-it-up drops. On a
# normal stop (no sentinel) it is a silent no-op. The emacsclient call is
# stubbed here so the test records the elisp form without a live daemon.

setup() {
    REPO_ROOT="$(cd "$(dirname "$BATS_TEST_FILENAME")/../.." && pwd)"
    SCRIPT="$REPO_ROOT/hooks/ai-wrap-teardown.sh"
    TMPDIR_T="$(mktemp -d)"
    PROJ="proj-$$-$BATS_TEST_NUMBER"          # unique so /tmp sentinels don't collide
    CWD="$TMPDIR_T/$PROJ"
    mkdir -p "$CWD"
    TEARDOWN_SENTINEL="/tmp/ai-wrap-teardown-${PROJ}"
    SHUTDOWN_SENTINEL="/tmp/ai-wrap-shutdown-${PROJ}"

    # Stub emacsclient on PATH: record the elisp form it was called with.
    BIN="$TMPDIR_T/bin"
    mkdir -p "$BIN"
    EC_LOG="$TMPDIR_T/emacsclient.log"
    cat >"$BIN/emacsclient" <<EOF
#!/usr/bin/env bash
# args: -e <form>
shift   # drop -e
printf '%s\n' "\$1" >> "$EC_LOG"
EOF
    chmod +x "$BIN/emacsclient"
}

teardown() {
    rm -rf "$TMPDIR_T"
    rm -f "$TEARDOWN_SENTINEL" "$SHUTDOWN_SENTINEL"
}

run_hook() {
    # invoke with the stubbed emacsclient on PATH, feeding Stop-hook JSON
    printf '{"cwd":"%s","hook_event_name":"Stop"}' "$CWD" \
        | PATH="$BIN:$PATH" bash "$SCRIPT"
}

@test "no sentinel: silent no-op, emacsclient never called" {
    run run_hook
    [ "$status" -eq 0 ]
    [ -z "$output" ]
    [ ! -f "$EC_LOG" ]
}

@test "teardown sentinel: calls cj/ai-term-quit with the project basename" {
    : > "$TEARDOWN_SENTINEL"
    run run_hook
    [ "$status" -eq 0 ]
    grep -q "cj/ai-term-quit \"$PROJ\"" "$EC_LOG"
}

@test "teardown sentinel is removed after firing" {
    : > "$TEARDOWN_SENTINEL"
    run run_hook
    [ "$status" -eq 0 ]
    [ ! -f "$TEARDOWN_SENTINEL" ]
}

@test "shutdown sentinel: calls cj/ai-term-shutdown-countdown" {
    : > "$SHUTDOWN_SENTINEL"
    run run_hook
    [ "$status" -eq 0 ]
    grep -q "cj/ai-term-shutdown-countdown" "$EC_LOG"
}

@test "shutdown supersedes teardown when both sentinels exist" {
    : > "$TEARDOWN_SENTINEL"
    : > "$SHUTDOWN_SENTINEL"
    run run_hook
    [ "$status" -eq 0 ]
    grep -q "cj/ai-term-shutdown-countdown" "$EC_LOG"
    ! grep -q "cj/ai-term-quit" "$EC_LOG"
    [ ! -f "$TEARDOWN_SENTINEL" ]
    [ ! -f "$SHUTDOWN_SENTINEL" ]
}

@test "emacsclient absent: clears the sentinel and exits 0 (graceful)" {
    : > "$TEARDOWN_SENTINEL"
    status=0
    output="$(printf '{"cwd":"%s","hook_event_name":"Stop"}' "$CWD" \
        | PATH="/usr/bin:/bin" bash "$SCRIPT")" || status=$?
    [ "$status" -eq 0 ]
    [ ! -f "$TEARDOWN_SENTINEL" ]
}

@test "falls back to PWD basename when cwd is absent from JSON" {
    # No cwd key: hook uses $PWD. Run from CWD so basename resolves to PROJ.
    : > "$TEARDOWN_SENTINEL"
    run env "PATH=$BIN:$PATH" bash -c "cd '$CWD' && printf '{}' | bash '$SCRIPT'"
    [ "$status" -eq 0 ]
    grep -q "cj/ai-term-quit \"$PROJ\"" "$EC_LOG"
}

@test "emits no stderr noise on a normal stop" {
    err="$(printf '{"cwd":"%s","hook_event_name":"Stop"}' "$CWD" \
        | PATH="$BIN:$PATH" bash "$SCRIPT" 2>&1 >/dev/null)"
    [ -z "$err" ]
}