|
| 1 | +#!/bin/bash |
| 2 | +# Test suite for devcube-session.sh — the attach-or-create zellij launcher that |
| 3 | +# the `devc` wrapper runs inside the container. |
| 4 | +# |
| 5 | +# The script execs the real `zellij` binary, so to drive its branches in |
| 6 | +# isolation we point ZELLIJ_BIN at a stub. The stub: |
| 7 | +# |
| 8 | +# * answers `list-sessions` from the FAKE_SESSIONS env var (one session name |
| 9 | +# per line; empty -> behaves like zellij with no sessions: error to stderr, |
| 10 | +# non-zero exit), and |
| 11 | +# * records every other invocation (attach / delete-session / --session ...) |
| 12 | +# to FAKE_LOG, one argv per line, so a test can assert exactly what the |
| 13 | +# script asked zellij to do. |
| 14 | +# |
| 15 | +# This lets us cover the three states that matter: a saved/attachable session |
| 16 | +# (Save), a deleted/absent session (Discard -> create fresh), and a wedged dead |
| 17 | +# remnant left by an unsaved exit (clear it, then create fresh). |
| 18 | + |
| 19 | +set -euo pipefail |
| 20 | + |
| 21 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 22 | +SCRIPT="$SCRIPT_DIR/../../dot_files/devcube/devcube-session.sh" |
| 23 | + |
| 24 | +RED='\033[0;31m' |
| 25 | +GREEN='\033[0;32m' |
| 26 | +NC='\033[0m' |
| 27 | + |
| 28 | +test_count=0 |
| 29 | +pass_count=0 |
| 30 | + |
| 31 | +WORK="$(mktemp -d)" |
| 32 | +trap 'rm -rf "$WORK"' EXIT |
| 33 | + |
| 34 | +# A stub `zellij` on ZELLIJ_BIN. `list-sessions` is sourced from FAKE_SESSIONS; |
| 35 | +# the "already exists, but is dead" wedge is modelled by FAKE_DEAD: while it's |
| 36 | +# set, a create (`--session`) fails like real zellij until delete-session clears |
| 37 | +# it (the stub removes the FAKE_DEAD marker file when asked to delete). |
| 38 | +STUB="$WORK/zellij" |
| 39 | +cat >"$STUB" <<'STUB_EOF' |
| 40 | +#!/bin/bash |
| 41 | +log() { printf '%s\n' "$*" >>"$FAKE_LOG"; } |
| 42 | +
|
| 43 | +case "$1" in |
| 44 | +list-sessions) |
| 45 | + if [ -z "${FAKE_SESSIONS:-}" ]; then |
| 46 | + echo "No active zellij sessions found." >&2 |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + # Mimic `--no-formatting`: "<name> [Created ...]", name in the first field. |
| 50 | + while IFS= read -r s; do |
| 51 | + [ -n "$s" ] && printf '%s [Created 1s ago]\n' "$s" |
| 52 | + done <<<"$FAKE_SESSIONS" |
| 53 | + ;; |
| 54 | +delete-session) |
| 55 | + log "$*" |
| 56 | + # Clearing the wedge: drop the dead-remnant marker if present. |
| 57 | + [ -n "${FAKE_DEAD:-}" ] && rm -f "$FAKE_DEAD" |
| 58 | + ;; |
| 59 | +attach) |
| 60 | + log "$*" |
| 61 | + ;; |
| 62 | +--session) |
| 63 | + log "$*" |
| 64 | + # A live dead remnant makes `zellij --session` refuse to start. |
| 65 | + if [ -n "${FAKE_DEAD:-}" ] && [ -e "$FAKE_DEAD" ]; then |
| 66 | + echo "Session with name $2 already exists, but is dead." >&2 |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | + ;; |
| 70 | +*) |
| 71 | + log "$*" |
| 72 | + ;; |
| 73 | +esac |
| 74 | +STUB_EOF |
| 75 | +chmod +x "$STUB" |
| 76 | + |
| 77 | +# Run devcube-session.sh with the stub. Args: session [layout]. |
| 78 | +# Env in: FAKE_SESSIONS (newline list), FAKE_DEAD (marker file path or empty). |
| 79 | +# Sets globals: RUN_RC, RUN_LOG (path to the recorded argv log). |
| 80 | +run_script() { |
| 81 | + FAKE_LOG="$WORK/log.$test_count" |
| 82 | + : >"$FAKE_LOG" |
| 83 | + set +e |
| 84 | + ZELLIJ_BIN="$STUB" FAKE_LOG="$FAKE_LOG" \ |
| 85 | + FAKE_SESSIONS="${FAKE_SESSIONS:-}" FAKE_DEAD="${FAKE_DEAD:-}" \ |
| 86 | + bash "$SCRIPT" "$@" >/dev/null 2>&1 |
| 87 | + RUN_RC=$? |
| 88 | + set -e |
| 89 | + RUN_LOG="$FAKE_LOG" |
| 90 | +} |
| 91 | + |
| 92 | +# Assert the recorded log matches the expected argv lines exactly. |
| 93 | +check() { |
| 94 | + local test_name=$1 |
| 95 | + local expected=$2 |
| 96 | + test_count=$((test_count + 1)) |
| 97 | + local actual |
| 98 | + actual="$(cat "$RUN_LOG")" |
| 99 | + if [[ "$RUN_RC" -eq 0 && "$actual" == "$expected" ]]; then |
| 100 | + printf "Test $test_count: $test_name\n ${GREEN}✓ PASS${NC}\n\n" |
| 101 | + pass_count=$((pass_count + 1)) |
| 102 | + else |
| 103 | + printf "Test $test_count: $test_name\n" |
| 104 | + printf " ${RED}✗ FAIL${NC} (rc=$RUN_RC)\n" |
| 105 | + printf " expected: %q\n" "$expected" |
| 106 | + printf " actual: %q\n\n" "$actual" |
| 107 | + fi |
| 108 | +} |
| 109 | + |
| 110 | +echo "Running tests for devcube-session.sh" |
| 111 | +echo "==========================================" |
| 112 | +echo "" |
| 113 | + |
| 114 | +# 1. Saved session present -> resurrect/attach it, never create. (Save path.) |
| 115 | +FAKE_SESSIONS="devcube-proj-123" FAKE_DEAD="" run_script "devcube-proj-123" "workmux" |
| 116 | +check "attaches an existing (saved/resurrectable) session" \ |
| 117 | + "attach devcube-proj-123" |
| 118 | + |
| 119 | +# 2. No session at all (e.g. Discard deleted it) with a layout -> create fresh |
| 120 | +# with the layout. A stray delete-session first is fine (clears nothing). |
| 121 | +FAKE_SESSIONS="" FAKE_DEAD="" run_script "devcube-proj-123" "workmux" |
| 122 | +check "creates fresh with layout when no session exists" \ |
| 123 | + "$(printf 'delete-session devcube-proj-123 --force\n--session devcube-proj-123 --new-session-with-layout workmux')" |
| 124 | + |
| 125 | +# 3. No session and no layout -> create a fresh plain named session. |
| 126 | +FAKE_SESSIONS="" FAKE_DEAD="" run_script "devcube-proj-123" |
| 127 | +check "creates fresh plain session when no session and no layout" \ |
| 128 | + "$(printf 'delete-session devcube-proj-123 --force\n--session devcube-proj-123')" |
| 129 | + |
| 130 | +# 4. Unsaved exit left a wedged dead remnant: list-sessions doesn't surface it, |
| 131 | +# and a naive create would fail with "already exists, but is dead". The script |
| 132 | +# must clear it first, then create fresh and succeed. (The regression fix.) |
| 133 | +DEAD_MARK="$WORK/dead.marker" |
| 134 | +: >"$DEAD_MARK" |
| 135 | +FAKE_SESSIONS="" FAKE_DEAD="$DEAD_MARK" run_script "devcube-proj-123" "workmux" |
| 136 | +check "clears a dead remnant then creates fresh (unsaved-exit recovery)" \ |
| 137 | + "$(printf 'delete-session devcube-proj-123 --force\n--session devcube-proj-123 --new-session-with-layout workmux')" |
| 138 | + |
| 139 | +# 5. A different session is running, but not ours -> we don't attach to it; we |
| 140 | +# create our own. Guards against a loose substring match in list-sessions. |
| 141 | +FAKE_SESSIONS="$(printf 'some-other-session\ndevcube-proj-999')" FAKE_DEAD="" \ |
| 142 | + run_script "devcube-proj-123" "workmux" |
| 143 | +check "ignores unrelated sessions and creates our own" \ |
| 144 | + "$(printf 'delete-session devcube-proj-123 --force\n--session devcube-proj-123 --new-session-with-layout workmux')" |
| 145 | + |
| 146 | +# Summary |
| 147 | +echo "==========================================" |
| 148 | +echo "Test Results: $pass_count/$test_count passed" |
| 149 | +echo "==========================================" |
| 150 | + |
| 151 | +if [[ $pass_count -eq $test_count ]]; then |
| 152 | + printf "${GREEN}All tests passed!${NC}\n" |
| 153 | + exit 0 |
| 154 | +else |
| 155 | + printf "${RED}Some tests failed${NC}\n" |
| 156 | + exit 1 |
| 157 | +fi |
0 commit comments