Skip to content
Closed
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
52 changes: 42 additions & 10 deletions bin/omarchy-bluetooth-device
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,60 @@ power_on() {
omarchy-bluetooth-power on || true
}

trust_device() {
bluetoothctl trust "$address" >/dev/null 2>&1 || true
device_error() {
local message="$1"
echo "$message" >&2
omarchy-notification-send --app-name Bluetooth -u normal "Bluetooth $action failed" "$message" || true
exit 1
}

run_device_command() {
local seconds="$1" command="$2" output
if output=$(timeout "$seconds" bluetoothctl "$command" "$address" 2>&1); then
# bluetoothctl can print a failure while returning success.
if [[ $output == *"Failed to "* || $output == *"not available"* ]]; then
device_error "$output"
fi
else
device_error "${output:-Bluetooth $command timed out or failed for $address}"
fi
}

device_has_state() {
local info
info=$(timeout 3s bluetoothctl info "$address" 2>/dev/null) || return 1
[[ $info == *"$1: yes"* ]]
}

connect_device() {
if ! device_has_state Connected; then
run_device_command 20s connect
device_has_state Connected || device_error "Connection did not complete for $address"
fi
}

case "$action" in
pair)
power_on
timeout 20s bluetoothctl pair "$address" >/dev/null 2>&1 || true
trust_device
timeout 20s bluetoothctl connect "$address" >/dev/null 2>&1 || true
run_device_command 20s pair
device_has_state Paired || device_error "Pairing did not complete for $address"
run_device_command 10s trust
connect_device
;;
connect)
power_on
trust_device
timeout 20s bluetoothctl connect "$address" >/dev/null 2>&1 || true
run_device_command 10s trust
connect_device
;;
disconnect)
timeout 10s bluetoothctl disconnect "$address" >/dev/null 2>&1 || true
run_device_command 10s disconnect
;;
forget)
power_on
timeout 10s bluetoothctl disconnect "$address" >/dev/null 2>&1 || true
timeout 10s bluetoothctl remove "$address" >/dev/null 2>&1 || true
# An already disconnected device can still be forgotten.
if device_has_state Connected; then
run_device_command 10s disconnect

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Disconnect failure blocks forgetting If disconnecting a connected device fails or times out, this call exits the script before it attempts bluetoothctl remove. The device remains paired even though the user asked to forget it. Report the disconnect failure, but still attempt removal.

fi
run_device_command 10s remove
;;
esac
63 changes: 63 additions & 0 deletions test/shell.d/bluetooth-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ cat >"$mock_bin/bluetoothctl" <<'SH'
#!/bin/bash

printf '%s\n' "$*" >>"$BLUETOOTHCTL_LOG"
if [[ $1 == "${MOCK_DEVICE_FAIL:-}" ]]; then
echo "Failed to $1: org.bluez.Error.AuthenticationFailed"
exit "${MOCK_DEVICE_EXIT:-0}"
fi
if [[ $1 == "info" ]]; then
echo "Paired: ${MOCK_PAIRED:-yes}"
echo "Connected: ${MOCK_CONNECTED:-no}"
fi
if [[ $1 == "connect" ]]; then
touch "$POWERED_FILE.connected"
fi
if [[ $1 == "info" && -f "$POWERED_FILE.connected" ]]; then
echo "Connected: yes"
fi
[[ $1 == "power" && $2 == "on" ]] && echo yes >"$POWERED_FILE"
[[ $1 == "list" ]] &&
for c in ${MOCK_CONTROLLERS:-AA:BB:CC:DD:EE:FF}; do printf 'Controller %s mock\n' "$c"; done
Expand All @@ -180,6 +194,12 @@ printf 'rfkill %s\n' "$*" >>"$BLUETOOTHCTL_LOG"
exit 0
SH

cat >"$mock_bin/omarchy-notification-send" <<'SH'
#!/bin/bash
printf 'notification %s\n' "$*" >>"$BLUETOOTHCTL_LOG"
SH
chmod +x "$mock_bin/omarchy-notification-send"

chmod +x "$mock_bin/bluetoothctl" "$mock_bin/rfkill"

# $ROOT/bin so omarchy-bluetooth-device resolves the real omarchy-bluetooth-power.
Expand All @@ -189,6 +209,7 @@ bluetooth_run() {

echo "$powered" >"$POWERED_FILE"
: >"$device_tmp/log"
rm -f "$POWERED_FILE.connected"
PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" \
OMARCHY_BLUETOOTH_POWER_WAIT_SECONDS=0 "$@" ||
fail "$* exits cleanly with Powered: $powered"
Expand Down Expand Up @@ -280,3 +301,45 @@ pass "bluetooth counts a secondary controller as on"
grep -q 'AutoEnable=false' "$ROOT/install/hardware/bluetooth.sh" &&
fail "bluetooth install leaves AutoEnable at its default"
pass "bluetooth install leaves AutoEnable at its default"

# Detached panel commands must expose errors even when bluetoothctl exits zero.
for status in 0 1 124; do
echo yes >"$POWERED_FILE"
: >"$device_tmp/log"
if PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" MOCK_DEVICE_FAIL=pair MOCK_DEVICE_EXIT="$status" \
"$ROOT/bin/omarchy-bluetooth-device" pair AA:BB:CC:DD:EE:FF >"$device_tmp/error" 2>&1; then
fail "pairing failure exits nonzero (bluetoothctl status $status)"
fi
grep -q 'notification Bluetooth pairing\|notification .*Bluetooth pair failed' "$device_tmp/log" || fail "pair failure notifies"
if grep -Eq '^(trust|connect) ' "$device_tmp/log"; then
fail "pair failure must not trust or connect"
fi
pass "pair failure reports and stops (bluetoothctl status $status)"
done

paired_log=$(MOCK_CONNECTED=yes bluetooth_run yes "$ROOT/bin/omarchy-bluetooth-device" pair AA:BB:CC:DD:EE:FF)
grep -qx 'trust AA:BB:CC:DD:EE:FF' "$paired_log" || fail "successful pair saves trust"
if grep -q '^connect ' "$paired_log"; then
fail "already connected pair skips redundant connect"
fi
pass "already connected pair saves trust without reconnecting"

echo yes >"$POWERED_FILE"
: >"$device_tmp/log"
if PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" MOCK_PAIRED=no \
"$ROOT/bin/omarchy-bluetooth-device" pair AA:BB:CC:DD:EE:FF >"$device_tmp/error" 2>&1; then
fail "pairing must verify Paired state"
fi
if grep -Eq '^(trust|connect) ' "$device_tmp/log"; then
fail "unconfirmed pairing must not trust or connect"
fi
pass "pairing verifies state before trusting"

: >"$device_tmp/log"
rm -f "$POWERED_FILE.connected"
if PATH="$mock_bin:$ROOT/bin:$PATH" BLUETOOTHCTL_LOG="$device_tmp/log" MOCK_DEVICE_FAIL=connect \
"$ROOT/bin/omarchy-bluetooth-device" connect AA:BB:CC:DD:EE:FF >"$device_tmp/error" 2>&1; then
fail "connection failure exits nonzero even when bluetoothctl exits zero"
fi
grep -q 'Bluetooth connect failed' "$device_tmp/log" || fail "connection failure notifies"
pass "connection failure is reported"
Comment on lines +338 to +345

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Forget flow lacks tests The new action tests cover pair and connect, but never exercise the changed disconnect or forget paths. Add cases for a disconnect failure during forget and for forgetting an already-disconnected device, so regressions in the new removal flow do not go unnoticed.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!