forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmux-cli.sh
More file actions
executable file
·158 lines (149 loc) · 5.02 KB
/
Copy pathtmux-cli.sh
File metadata and controls
executable file
·158 lines (149 loc) · 5.02 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env bash
#######################################################################
# tmux-cli.sh - Unified TUI testing helper using tmux
#######################################################################
#
# DESCRIPTION:
# A unified script for testing TUI applications using tmux.
# Provides subcommands for starting, sending input, capturing output,
# and stopping test sessions. Works with any TUI app (Codebuff,
# Claude Code, Codex, custom apps, etc.).
#
# USAGE:
# ./scripts/tmux/tmux-cli.sh <command> [arguments]
#
# COMMANDS:
# start Start a new TUI test session
# send Send input to a session (uses bracketed paste)
# capture Capture output from a session
# stop Stop a session
# list List all active tmux sessions
# help Show this help message
#
# QUICK START:
# # Start a test session with a custom command
# SESSION=$(./scripts/tmux/tmux-cli.sh start --command "claude")
# echo "Started session: $SESSION"
#
# # Send a command
# ./scripts/tmux/tmux-cli.sh send "$SESSION" "/help"
#
# # Capture output with a label (auto-saves screenshot)
# ./scripts/tmux/tmux-cli.sh capture "$SESSION" --label "after-help" --wait 2
#
# # Clean up
# ./scripts/tmux/tmux-cli.sh stop "$SESSION"
#
# SESSION LOGS:
# Captures are automatically saved to debug/tmux-sessions/{session}/
# Use --label to add descriptive names to capture files.
#
# EXAMPLES:
# # Test any TUI app
# SESSION=$(./scripts/tmux/tmux-cli.sh start --command "codex chat")
# SESSION=$(./scripts/tmux/tmux-cli.sh start --command "python my_app.py")
#
# # Test Codebuff (default when no --command specified)
# SESSION=$(./scripts/tmux/tmux-cli.sh start --name my-test)
# ./scripts/tmux/tmux-cli.sh send "$SESSION" "hello world"
# ./scripts/tmux/tmux-cli.sh capture "$SESSION" --wait 3
# ./scripts/tmux/tmux-cli.sh stop "$SESSION"
#
# # Test a compiled Codebuff binary
# SESSION=$(./scripts/tmux/tmux-cli.sh start --binary)
# # Or with custom path:
# SESSION=$(./scripts/tmux/tmux-cli.sh start --binary ./path/to/codebuff)
#
# # Stop all test sessions
# ./scripts/tmux/tmux-cli.sh stop --all
#
# # Get help for a specific command
# ./scripts/tmux/tmux-cli.sh start --help
#
# INDIVIDUAL SCRIPTS:
# For more options, use the individual scripts directly:
# - scripts/tmux/tmux-start.sh
# - scripts/tmux/tmux-send.sh
# - scripts/tmux/tmux-capture.sh
# - scripts/tmux/tmux-stop.sh
#
#######################################################################
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
show_help() {
head -n 58 "$0" | tail -n +2 | sed 's/^# //' | sed 's/^#//'
}
show_short_help() {
echo "Usage: $0 <command> [arguments]"
echo ""
echo "Commands:"
echo " start Start a new TUI test session"
echo " send Send input to a session"
echo " capture Capture output from a session"
echo " stop Stop a session"
echo " list List all active tmux sessions"
echo " help Show full help message"
echo ""
echo "Run '$0 <command> --help' for command-specific help"
}
# Check for command
if [[ $# -lt 1 ]]; then
show_short_help
exit 1
fi
COMMAND="$1"
shift
case "$COMMAND" in
start)
# Run tmux-start.sh and parse its JSON output
# This gives callers a plain session name for backward compatibility
JSON_OUTPUT=$("$SCRIPT_DIR/tmux-start.sh" "$@" 2>&1) || true
# Check if output looks like JSON
if [[ "$JSON_OUTPUT" == "{"* ]]; then
# Parse JSON to extract session name or error
# Use grep/sed for portability (no jq dependency)
if echo "$JSON_OUTPUT" | grep -q '"status":"success"'; then
# Extract sessionName value
SESSION_NAME=$(echo "$JSON_OUTPUT" | sed -n 's/.*"sessionName":"\([^"]*\)".*/\1/p')
if [[ -n "$SESSION_NAME" ]]; then
echo "$SESSION_NAME"
exit 0
else
echo "Failed to extract session name from: $JSON_OUTPUT" >&2
exit 1
fi
else
# Extract error message
ERROR_MSG=$(echo "$JSON_OUTPUT" | sed -n 's/.*"error":"\([^"]*\)".*/\1/p')
echo "${ERROR_MSG:-Failed to start session}" >&2
exit 1
fi
else
# Not JSON - pass through as-is (plain mode or unexpected output)
echo "$JSON_OUTPUT"
exit 0
fi
;;
send)
exec "$SCRIPT_DIR/tmux-send.sh" "$@"
;;
capture)
exec "$SCRIPT_DIR/tmux-capture.sh" "$@"
;;
stop)
exec "$SCRIPT_DIR/tmux-stop.sh" "$@"
;;
list)
echo "Active tmux sessions:"
tmux list-sessions 2>/dev/null || echo " (none)"
;;
help|--help|-h)
show_help
;;
*)
echo "Unknown command: $COMMAND" >&2
echo ""
show_short_help
exit 1
;;
esac