From 9939a00d17d031d6da78170f4fb83726eb4e1ade Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 29 Jul 2026 10:41:22 -0700 Subject: [PATCH] Guard the Bluetooth power check and cover it Every other bluetoothctl call in the script is silenced and time-boxed; the new power check was neither, so a wedged D-Bus could hang pair before it reached its own timeout and spill dbus assertions onto stderr. Cover both paths with a stubbed bluetoothctl: the power-on sequence is skipped when the adapter reports Powered: yes, and still runs when it does not. Co-Authored-By: Claude Opus 5 (1M context) --- bin/omarchy-bluetooth-device | 2 +- test/shell.d/bluetooth-test.sh | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/bin/omarchy-bluetooth-device b/bin/omarchy-bluetooth-device index e2da3223..664e136d 100755 --- a/bin/omarchy-bluetooth-device +++ b/bin/omarchy-bluetooth-device @@ -19,7 +19,7 @@ address=${2:-} [[ $address =~ ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$ ]] || usage power_on() { - bluetoothctl show | grep -Fq "Powered: yes" && return + [[ $(timeout 2s bluetoothctl show 2>/dev/null) == *"Powered: yes"* ]] && return bluetoothctl power on >/dev/null 2>&1 || true sleep 0.5 } diff --git a/test/shell.d/bluetooth-test.sh b/test/shell.d/bluetooth-test.sh index 6c52038b..8e71f24b 100644 --- a/test/shell.d/bluetooth-test.sh +++ b/test/shell.d/bluetooth-test.sh @@ -91,3 +91,45 @@ assert( 'bluetooth ignores non-sink nodes when matching audio outputs' ) JS + +# The power-on shortcut is the whole point of skipping the stabilization sleep: +# pair/connect from the panel run against an adapter that is already powered. +device_tmp=$(mktemp -d) +trap 'rm -rf "$device_tmp"' EXIT + +mock_bin="$device_tmp/bin" +mkdir -p "$mock_bin" + +cat >"$mock_bin/bluetoothctl" <<'SH' +#!/bin/bash + +printf '%s\n' "$*" >>"$BLUETOOTHCTL_LOG" +[[ $1 == "show" ]] && printf '\tPowered: %s\n' "$BLUETOOTHCTL_POWERED" +exit 0 +SH +chmod +x "$mock_bin/bluetoothctl" + +bluetooth_device_log() { + local powered="$1" + local log="$device_tmp/$powered.log" + + : >"$log" + PATH="$mock_bin:$PATH" BLUETOOTHCTL_LOG="$log" BLUETOOTHCTL_POWERED="$powered" \ + "$ROOT/bin/omarchy-bluetooth-device" connect AA:BB:CC:DD:EE:FF || + fail "omarchy-bluetooth-device exits cleanly with Powered: $powered" + printf '%s' "$log" +} + +powered_log=$(bluetooth_device_log yes) +grep -qx "power on" "$powered_log" && + fail "bluetooth skips the power-on delay when the adapter is already powered" +pass "bluetooth skips the power-on delay when the adapter is already powered" + +grep -qx "connect AA:BB:CC:DD:EE:FF" "$powered_log" || + fail "bluetooth still connects when the adapter is already powered" +pass "bluetooth still connects when the adapter is already powered" + +unpowered_log=$(bluetooth_device_log no) +grep -qx "power on" "$unpowered_log" || + fail "bluetooth powers the adapter on when it is off" +pass "bluetooth powers the adapter on when it is off"