aboutsummaryrefslogtreecommitdiff
path: root/.ai/scripts/tests/self-inject.bats
blob: 482f61d587dcd596c25edf0400cbc1a4c4f6e41e (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
#!/usr/bin/env bats
# Tests for self-inject.sh — tmux is the external boundary, stubbed with a
# recording fake so no real server is needed.

setup() {
    SCRIPT="$BATS_TEST_DIRNAME/../self-inject.sh"
    STUB_DIR="$BATS_TEST_TMPDIR/bin"
    LOG="$BATS_TEST_TMPDIR/tmux.log"
    mkdir -p "$STUB_DIR"
}

# A tmux stub that records every invocation and answers list-panes from
# $STUB_PANES (empty by default, so pane derivation fails unless a test
# provides ancestry-matching output).
make_stub() {
    cat > "$STUB_DIR/tmux" <<'EOF'
#!/bin/sh
echo "$@" >> "$LOG"
case "$1" in
    list-panes) printf '%s\n' "$STUB_PANES" ;;
esac
EOF
    chmod +x "$STUB_DIR/tmux"
}

@test "self-inject: -t pane with no pairs echoes the pane and exits 0" {
    make_stub
    run env PATH="$STUB_DIR:$PATH" LOG="$LOG" STUB_PANES="" sh "$SCRIPT" -t %42
    [ "$status" -eq 0 ]
    [ "$output" = "%42" ]
    # Pane was supplied, nothing sent: tmux must not have been called.
    [ ! -e "$LOG" ]
}

@test "self-inject: no pane derivable and no -t exits 1 with an error" {
    make_stub
    run env PATH="$STUB_DIR:$PATH" LOG="$LOG" STUB_PANES="" sh "$SCRIPT" 0 "hello"
    [ "$status" -eq 1 ]
    case "$output" in *"no owning pane"*) : ;; *) false ;; esac
}

@test "self-inject: derives the pane from process ancestry via list-panes" {
    make_stub
    # The stub reports the bats test process itself as a pane's pane_pid;
    # the script runs as our child, so that pid is in its ancestry.
    run env PATH="$STUB_DIR:$PATH" LOG="$LOG" STUB_PANES="$$ %7" sh "$SCRIPT"
    [ "$status" -eq 0 ]
    [ "$output" = "%7" ]
}

@test "self-inject: one delay/text pair sends literal text then Enter" {
    make_stub
    run env PATH="$STUB_DIR:$PATH" LOG="$LOG" STUB_PANES="" sh "$SCRIPT" -t %3 0 "/clear"
    [ "$status" -eq 0 ]
    run cat "$LOG"
    [ "${lines[0]}" = "send-keys -t %3 -l /clear" ]
    [ "${lines[1]}" = "send-keys -t %3 Enter" ]
}

@test "self-inject: multiple pairs send in order" {
    make_stub
    run env PATH="$STUB_DIR:$PATH" LOG="$LOG" STUB_PANES="" \
        sh "$SCRIPT" -t %3 0 "/clear" 0 "go — resume"
    [ "$status" -eq 0 ]
    run cat "$LOG"
    [ "${lines[0]}" = "send-keys -t %3 -l /clear" ]
    [ "${lines[1]}" = "send-keys -t %3 Enter" ]
    [ "${lines[2]}" = "send-keys -t %3 -l go — resume" ]
    [ "${lines[3]}" = "send-keys -t %3 Enter" ]
}

@test "self-inject: dangling odd argument after pairs is ignored" {
    make_stub
    run env PATH="$STUB_DIR:$PATH" LOG="$LOG" STUB_PANES="" sh "$SCRIPT" -t %3 0 "one" 99
    [ "$status" -eq 0 ]
    run cat "$LOG"
    [ "${#lines[@]}" -eq 2 ]
}