|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +repo_root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P) |
| 6 | +gwai_cmux_script="${GWAI_CMUX_SCRIPT:-$repo_root/home/dot_local/bin/executable_gwai-cmux}" |
| 7 | +test_dir=$(mktemp -d "${TMPDIR:-/tmp}/gwai-cmux-worktree-capture-test.XXXXXX") |
| 8 | +test_dir=$(cd "$test_dir" && pwd -P) |
| 9 | +background_pid="" |
| 10 | + |
| 11 | +cleanup() { |
| 12 | + if [[ -n "$background_pid" ]] && kill -0 "$background_pid" 2>/dev/null; then |
| 13 | + kill "$background_pid" |
| 14 | + wait "$background_pid" 2>/dev/null || true |
| 15 | + fi |
| 16 | + rm -rf "$test_dir" |
| 17 | +} |
| 18 | +trap cleanup EXIT |
| 19 | + |
| 20 | +fail() { |
| 21 | + printf 'FAIL: %s\n' "$*" >&2 |
| 22 | + exit 1 |
| 23 | +} |
| 24 | + |
| 25 | +bin_dir="$test_dir/bin" |
| 26 | +base_dir="$test_dir/base" |
| 27 | +worktree_dir="$test_dir/worktree" |
| 28 | +run_output="$test_dir/run-output" |
| 29 | +cmux_log="$test_dir/cmux.log" |
| 30 | +zsh_log="$test_dir/zsh.log" |
| 31 | +background_pid_file="$test_dir/background.pid" |
| 32 | +mkdir -p "$bin_dir" "$base_dir" |
| 33 | +git init -q "$base_dir" |
| 34 | +git -C "$base_dir" config user.name 'gwai-cmux test' |
| 35 | +git -C "$base_dir" config user.email 'gwai-cmux@example.invalid' |
| 36 | +printf 'base\n' >"$base_dir/base.txt" |
| 37 | +git -C "$base_dir" add base.txt |
| 38 | +git -C "$base_dir" commit -qm 'test: add base file' |
| 39 | + |
| 40 | +cat >"$bin_dir/cmux" <<'MOCK' |
| 41 | +#!/usr/bin/env bash |
| 42 | +set -euo pipefail |
| 43 | +
|
| 44 | +printf '%s\n' "$*" >>"$TEST_CMUX_LOG" |
| 45 | +MOCK |
| 46 | + |
| 47 | +cat >"$bin_dir/gw-create-worktree" <<'MOCK' |
| 48 | +#!/usr/bin/env bash |
| 49 | +set -euo pipefail |
| 50 | +
|
| 51 | +sleep 5 & |
| 52 | +printf '%s\n' "$!" >"$TEST_BACKGROUND_PID_FILE" |
| 53 | +printf 'helper progress (stdout)\n' |
| 54 | +printf 'helper progress (stderr)\n' >&2 |
| 55 | +git -C "$TEST_BASE_DIR" worktree add -q -b "$1" "$TEST_WORKTREE_DIR" |
| 56 | +printf 'WORKTREE_PATH=%s\n' "$TEST_WORKTREE_DIR" |
| 57 | +MOCK |
| 58 | + |
| 59 | +cat >"$bin_dir/zsh" <<'MOCK' |
| 60 | +#!/usr/bin/env bash |
| 61 | +set -euo pipefail |
| 62 | +
|
| 63 | +printf '%s\n' "$*" >"$TEST_ZSH_LOG" |
| 64 | +MOCK |
| 65 | + |
| 66 | +chmod +x "$bin_dir/cmux" "$bin_dir/gw-create-worktree" "$bin_dir/zsh" |
| 67 | + |
| 68 | +started_at=$(date +%s) |
| 69 | +set +e |
| 70 | +PATH="$bin_dir:/usr/bin:/bin" \ |
| 71 | + CMUX_BIN="$bin_dir/cmux" \ |
| 72 | + CMUX_WORKSPACE_ID='' \ |
| 73 | + TEST_BACKGROUND_PID_FILE="$background_pid_file" \ |
| 74 | + TEST_BASE_DIR="$base_dir" \ |
| 75 | + TEST_CMUX_LOG="$cmux_log" \ |
| 76 | + TEST_WORKTREE_DIR="$worktree_dir" \ |
| 77 | + TEST_ZSH_LOG="$zsh_log" \ |
| 78 | + bash "$gwai_cmux_script" \ |
| 79 | + --inside-tab \ |
| 80 | + --provider codex \ |
| 81 | + --base-dir "$base_dir" \ |
| 82 | + --branch-name fix-worktree-capture \ |
| 83 | + 'capture race regression' >"$run_output" 2>&1 |
| 84 | +run_status=$? |
| 85 | +set -e |
| 86 | +elapsed_seconds=$(($(date +%s) - started_at)) |
| 87 | + |
| 88 | +((run_status == 0)) || { |
| 89 | + printf '%s\n' "$(<"$run_output")" >&2 |
| 90 | + fail "gwai-cmux exited with status $run_status" |
| 91 | +} |
| 92 | +((elapsed_seconds < 4)) || |
| 93 | + fail "gwai-cmux waited for the helper's background child (${elapsed_seconds}s)" |
| 94 | + |
| 95 | +[[ -s "$background_pid_file" ]] || fail "helper did not start its background child" |
| 96 | +background_pid=$(<"$background_pid_file") |
| 97 | +kill -0 "$background_pid" 2>/dev/null || |
| 98 | + fail "helper background child was not still running" |
| 99 | + |
| 100 | +grep -Fqx -- 'rename-workspace --title fix-worktree-capture' "$cmux_log" || |
| 101 | + fail "cmux workspace rename was not requested" |
| 102 | +grep -Fq -- "cd $worktree_dir && cdx capture\\ race\\ regression" "$zsh_log" || |
| 103 | + fail "start_agent did not receive the expected worktree path" |
| 104 | +grep -Fq -- 'helper progress (stdout)' "$run_output" || |
| 105 | + fail "helper stdout was not visible" |
| 106 | +grep -Fq -- 'helper progress (stderr)' "$run_output" || |
| 107 | + fail "helper stderr was not visible" |
| 108 | + |
| 109 | +printf 'test-gwai-cmux-worktree-capture: OK\n' |
0 commit comments