Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/droid-control-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ env:
CARGO_TERM_COLOR: always

jobs:
tctl-macos:
name: tctl close with stock macOS tools
runs-on: macos-latest
steps:
- uses: actions/checkout@v4
- run: python3 plugins/droid-control/tests/test_tctl.py -v TctlCloseTest

showcase:
name: Showcase composition, render helper, real renders
runs-on: ubuntu-latest
Expand Down
37 changes: 23 additions & 14 deletions plugins/droid-control/bin/tctl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ require_cmd() {
command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}

now_ms() {
if [[ "$OSTYPE" == darwin* ]]; then
# macOS date does not support %N. Use the system Perl for millisecond precision.
/usr/bin/perl -MTime::HiRes=time -e 'printf "%.0f\n", int(time() * 1000)'
else
date +%s%3N
fi
}

runtime_dir_value() {
if [[ -n "${XDG_RUNTIME_DIR:-}" ]]; then
printf '%s\n' "$XDG_RUNTIME_DIR"
Expand All @@ -74,9 +83,9 @@ wait_for_wayland_socket() {
local pid="$1"
local socket_path="$2"
local timeout_ms="$3"
local deadline=$(( $(date +%s%3N) + timeout_ms ))
local deadline=$(( $(now_ms) + timeout_ms ))

while (( $(date +%s%3N) <= deadline )); do
while (( $(now_ms) <= deadline )); do
[[ -S "$socket_path" ]] && return 0
kill -0 "$pid" >/dev/null 2>&1 || return 1
sleep 0.05
Expand Down Expand Up @@ -124,9 +133,9 @@ terminate_true_input_stack() {
kill -TERM "$pid" >/dev/null 2>&1 || true
done

local deadline=$(( $(date +%s%3N) + grace_ms ))
local deadline=$(( $(now_ms) + grace_ms ))
local alive=1
while (( $(date +%s%3N) <= deadline )); do
while (( $(now_ms) <= deadline )); do
alive=0
kill -0 -- "-$root" >/dev/null 2>&1 && alive=1
if (( ! alive )); then
Expand Down Expand Up @@ -155,8 +164,8 @@ terminate_recorder_pid() {
local grace_ms="${2:-3000}"
[[ -n "$pid" ]] || return 0
kill -INT "$pid" >/dev/null 2>&1 || true
local deadline=$(( $(date +%s%3N) + grace_ms ))
while kill -0 "$pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do
local deadline=$(( $(now_ms) + grace_ms ))
while kill -0 "$pid" >/dev/null 2>&1 && (( $(now_ms) <= deadline )); do
sleep 0.05
done
if kill -0 "$pid" >/dev/null 2>&1; then
Expand Down Expand Up @@ -538,8 +547,8 @@ cleanup_tuistory_asciinema() {
if [[ -n "$pid" ]] && kill -0 "$pid" >/dev/null 2>&1; then
if pid_is_expected_asciinema_recording "$pid" "$record_path"; then
kill -TERM "$pid" >/dev/null 2>&1 || true
local deadline=$(( $(date +%s%3N) + 2000 ))
while kill -0 "$pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do
local deadline=$(( $(now_ms) + 2000 ))
while kill -0 "$pid" >/dev/null 2>&1 && (( $(now_ms) <= deadline )); do
sleep 0.05
done
if kill -0 "$pid" >/dev/null 2>&1; then
Expand All @@ -562,8 +571,8 @@ cleanup_tuistory_asciinema() {
[[ "$orphan_args" == *"$record_path"* ]] || continue

kill -TERM "$orphan_pid" >/dev/null 2>&1 || true
local deadline=$(( $(date +%s%3N) + 2000 ))
while kill -0 "$orphan_pid" >/dev/null 2>&1 && (( $(date +%s%3N) <= deadline )); do
local deadline=$(( $(now_ms) + 2000 ))
while kill -0 "$orphan_pid" >/dev/null 2>&1 && (( $(now_ms) <= deadline )); do
sleep 0.05
done
if kill -0 "$orphan_pid" >/dev/null 2>&1; then
Expand Down Expand Up @@ -708,8 +717,8 @@ wait_for_pattern_in_log() {
local log_file="$1"
local pattern="$2"
local timeout_ms="$3"
local deadline=$(( $(date +%s%3N) + timeout_ms ))
while (( $(date +%s%3N) <= deadline )); do
local deadline=$(( $(now_ms) + timeout_ms ))
while (( $(now_ms) <= deadline )); do
local snapshot
snapshot="$(strip_log "$log_file")"
if SNAPSHOT_TEXT="$snapshot" python3 - "$pattern" <<'PY'
Expand Down Expand Up @@ -741,10 +750,10 @@ PY
wait_for_idle_in_log() {
local log_file="$1"
local timeout_ms="$2"
local deadline=$(( $(date +%s%3N) + timeout_ms ))
local deadline=$(( $(now_ms) + timeout_ms ))
local stable_for=0
local last_size=-1
while (( $(date +%s%3N) <= deadline )); do
while (( $(now_ms) <= deadline )); do
local size=0
[[ -f "$log_file" ]] && size=$(stat -c %s "$log_file")
if [[ "$size" == "$last_size" ]]; then
Expand Down
58 changes: 56 additions & 2 deletions plugins/droid-control/tests/test_tctl.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
"""tctl's native input boundary.
"""tctl's native input and session cleanup boundaries.

The default suite runs the real tctl against a recording wtype executable.
The default suite uses lightweight stand-ins for wtype, tuistory and asciinema.
TCTL_NATIVE_E2E=1 also checks incoming bytes in real headless Ghostty, without a
CLI, login fixture, clipboard, or extra script(1) wrapper. TCTL_SH selects an
alternate tctl for negative controls.
"""

import json
import os
import select
import shlex
import shutil
import signal
import subprocess
import sys
import tempfile
Expand Down Expand Up @@ -52,6 +54,58 @@
"""


class TctlCloseTest(unittest.TestCase):
def assert_recording_close(self, term_handler, expected_signal):
with tempfile.TemporaryDirectory(prefix="tctl-close-test-") as tmp:
root = Path(tmp)
session = f"close-test-{uuid.uuid4().hex}"
session_dir = Path("/tmp/tctl-sessions") / session
session_dir.mkdir(parents=True)
self.addCleanup(shutil.rmtree, session_dir, ignore_errors=True)
tuistory = root / "tuistory"
tuistory.write_text(f'#!/bin/sh\n[ "$*" = "-s {session} close" ]\n')
tuistory.chmod(0o755)
# No GNU coreutils or newer Bash ahead of the stock macOS tools.
env = {"PATH": f"{root}:/usr/bin:/bin"}
cast = root / "recording.cast"
recording = b'{"version": 2, "width": 80, "height": 24}\n'
cast.write_bytes(recording)
(session_dir / "meta").write_text(
f"BACKEND=tuistory\nRECORD_PATH={shlex.quote(str(cast))}\n"
)
probe = root / "asciinema"
probe.write_text(
"import signal\n"
f"signal.signal(signal.SIGTERM, signal.{term_handler})\n"
"print('ready', flush=True)\n"
"signal.pause()\n"
)
with subprocess.Popen(
[sys.executable, str(probe), "rec", str(cast)], stdout=subprocess.PIPE,
) as recorder:
try:
self.assertTrue(select.select([recorder.stdout], [], [], 5)[0])
self.assertEqual(recorder.stdout.readline(), b"ready\n")
(session_dir / "asciinema.pid").write_text(f"{recorder.pid}\n")
result = subprocess.run(
["/bin/bash", str(TCTL), "-s", session, "close"],
env=env, capture_output=True, text=True, timeout=10,
)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertEqual(recorder.wait(timeout=5), -expected_signal)
self.assertFalse(session_dir.exists())
self.assertEqual(cast.read_bytes(), recording)
finally:
if recorder.poll() is None:
recorder.kill()

def test_close_cleans_up_active_recorder_and_keeps_cast(self):
self.assert_recording_close("SIG_DFL", signal.SIGTERM)

def test_close_escalates_for_recorder_that_ignores_term(self):
self.assert_recording_close("SIG_IGN", signal.SIGKILL)


class TctlInputTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory(prefix="tctl-input-test-")
Expand Down