#!/bin/bash set -euo pipefail source "$(dirname "$0")/base-test.sh" test_tmp=$(mktemp -d) trap 'rm -rf "$test_tmp"' EXIT stub_bin="$test_tmp/bin" test_home="$test_tmp/home" runtime_dir="$test_tmp/runtime" mkdir -p "$stub_bin" "$test_home" "$runtime_dir" run_with_lock_env() { HOME="$test_home" \ XDG_RUNTIME_DIR="$runtime_dir" \ XDG_STATE_HOME="$test_tmp/state" \ PATH="$stub_bin:$ROOT/bin:$PATH" \ "$@" } write_stub() { local name="$1" local body="$2" cat >"$stub_bin/$name" <"$TEST_MARKER"; sleep 2; exit 0' OMARCHY_UPDATE_LOGGED=1 TEST_MARKER="$update_snapshot_marker" run_with_lock_env "$ROOT/bin/omarchy-update" -y >"$test_tmp/update-first.out" 2>&1 & update_pid=$! for _ in {1..50}; do [[ -f $update_snapshot_marker ]] && break sleep 0.05 done [[ -f $update_snapshot_marker ]] || fail "first omarchy-update reached snapshot under lock" set +e OMARCHY_UPDATE_LOGGED=1 TEST_MARKER="$test_tmp/update-second-snapshot-started" run_with_lock_env "$ROOT/bin/omarchy-update" -y >"$test_tmp/update-second.out" 2>&1 update_second_status=$? set -e wait "$update_pid" [[ $update_second_status -ne 0 ]] || fail "second omarchy-update exits non-zero while update lock is held" grep -q "already running" "$test_tmp/update-second.out" || fail "second omarchy-update reports held update lock" [[ ! -f $test_tmp/update-second-snapshot-started ]] || fail "second omarchy-update did not snapshot while lock was held" pass "omarchy-update prevents overlapping top-level updates" # omarchy-update-perform is now only a compatibility wrapper around # omarchy-update -y, but it should still respect the same update lock. perform_marker="$test_tmp/perform-started" write_stub omarchy-update-keyring 'echo started >"$TEST_MARKER"; sleep 2; exit 0' TEST_MARKER="$perform_marker" run_with_lock_env "$ROOT/bin/omarchy-update-perform" >"$test_tmp/perform-first.out" 2>&1 & perform_pid=$! for _ in {1..50}; do [[ -f $perform_marker ]] && break sleep 0.05 done [[ -f $perform_marker ]] || fail "first omarchy-update-perform delegated to update under lock" set +e TEST_MARKER="$test_tmp/perform-second-started" run_with_lock_env "$ROOT/bin/omarchy-update-perform" >"$test_tmp/perform-second.out" 2>&1 perform_second_status=$? set -e wait "$perform_pid" [[ $perform_second_status -ne 0 ]] || fail "second omarchy-update-perform exits non-zero while update lock is held" grep -q "already running" "$test_tmp/perform-second.out" || fail "second omarchy-update-perform reports held update lock" [[ ! -f $test_tmp/perform-second-started ]] || fail "second omarchy-update-perform did not snapshot while lock was held" pass "omarchy-update-perform compatibility wrapper respects update lock" # Update-owned Stay Awake state must be cleared before the restart helper can # reboot the machine, rather than relying on an EXIT trap during shutdown. write_stub omarchy-snapshot 'exit 0' write_stub omarchy-update-keyring 'exit 0' write_stub omarchy-toggle-idle ' state_file="$HOME/.local/state/omarchy/indicators/stay-awake" case "$1" in stay-awake) mkdir -p "$(dirname "$state_file")" touch "$state_file" ;; allow-idle) rm -f "$state_file" ;; esac' write_stub omarchy-update-restart ' state_file="$HOME/.local/state/omarchy/indicators/stay-awake" if [[ ${EXPECT_STAY_AWAKE:-0} == "1" ]]; then [[ -f $state_file ]] else [[ ! -f $state_file ]] fi' rm -f "$test_home/.local/state/omarchy/indicators/stay-awake" OMARCHY_UPDATE_LOGGED=1 run_with_lock_env "$ROOT/bin/omarchy-update" -y [[ ! -f $test_home/.local/state/omarchy/indicators/stay-awake ]] || fail "update clears its Stay Awake state before restart handling" mkdir -p "$test_home/.local/state/omarchy/indicators" touch "$test_home/.local/state/omarchy/indicators/stay-awake" OMARCHY_UPDATE_LOGGED=1 EXPECT_STAY_AWAKE=1 run_with_lock_env "$ROOT/bin/omarchy-update" -y [[ -f $test_home/.local/state/omarchy/indicators/stay-awake ]] || fail "update preserves pre-existing Stay Awake state" pass "omarchy-update restores only its own Stay Awake state before restart handling"