diff --git a/test/shell.d/agent-account-exec-test.sh b/test/shell.d/agent-account-exec-test.sh index 2cbe92c56b17..e03970fd38a9 100755 --- a/test/shell.d/agent-account-exec-test.sh +++ b/test/shell.d/agent-account-exec-test.sh @@ -48,10 +48,17 @@ SH chmod +x "$real_bin/$provider" done +# Only Omarchy's wrapper declarations: mise installs a declared lazy tool +# before it runs that tool's wrapper, so with omarchy-tools.toml in place it +# would try to fetch the real CLI instead of reaching the fake one. +mise_system_config="$test_tmp/etc/mise" +mkdir -p "$mise_system_config/conf.d" +cp "$ROOT/etc/mise/conf.d/omarchy-agent-accounts.toml" "$mise_system_config/conf.d/" + run_isolated() { env -i HOME="$account_test_home" \ XDG_CONFIG_HOME="$test_tmp/config" XDG_DATA_HOME="$test_tmp/data" XDG_CACHE_HOME="$test_tmp/cache" \ - MISE_DATA_DIR="$mise_data" MISE_SYSTEM_CONFIG_DIR="$ROOT/etc/mise" MISE_OFFLINE=1 \ + MISE_DATA_DIR="$mise_data" MISE_SYSTEM_CONFIG_DIR="$mise_system_config" MISE_OFFLINE=1 \ OMARCHY_PATH="$ROOT" PATH="$mise_data/command-wrappers/bin:$ROOT/bin:$real_bin:/usr/bin" \ "$@" } @@ -165,6 +172,8 @@ run_isolated env OMARCHY_TEST_AGENT_EXIT=42 claude >/dev/null || status=$? pass "account dispatch preserves the agent exit status" # A dispatcher alone must not prevent the CLI from being installed on first use. +# mise itself refuses a wrapper whose tool has no binary yet, so call account +# dispatch the way mise hands off to it: with its wrapper directory off PATH. # Omarchy declares the agent, so installing it must leave the user's config # alone; without that declaration, the user's config selects it. mv "$real_bin/grok" "$test_tmp/grok-fixture" @@ -186,18 +195,18 @@ SH chmod +x "$real_bin/mise" select_account grok side declarations="$ROOT/etc/mise/conf.d/omarchy-tools.toml" -output=$(run_isolated env OMARCHY_MISE_CONFIG_PATH="$declarations" OMARCHY_TEST_EXPECTED_INSTALL="install --quiet grok" grok) +output=$(run_isolated env OMARCHY_MISE_CONFIG_PATH="$declarations" OMARCHY_TEST_EXPECTED_INSTALL="install --quiet grok" PATH="$ROOT/bin:$real_bin:/usr/bin" omarchy-agent-account-exec grok) [[ $output == "$test_tmp/accounts/grok side" ]] || fail "a dispatcher without a CLI installs Omarchy's declared agent and uses the selected account" "$output" pass "a dispatcher without a CLI installs Omarchy's declared agent and uses the selected account" rm "$real_bin/grok" -output=$(run_isolated env OMARCHY_MISE_CONFIG_PATH="$test_tmp/no-declarations.toml" OMARCHY_TEST_EXPECTED_INSTALL="use -g --quiet grok" grok) +output=$(run_isolated env OMARCHY_MISE_CONFIG_PATH="$test_tmp/no-declarations.toml" OMARCHY_TEST_EXPECTED_INSTALL="use -g --quiet grok" PATH="$ROOT/bin:$real_bin:/usr/bin" omarchy-agent-account-exec grok) [[ $output == "$test_tmp/accounts/grok side" ]] || fail "a dispatcher without Omarchy's declaration selects the agent in the user config" "$output" pass "a dispatcher without Omarchy's declaration selects the agent in the user config" rm "$real_bin/grok" status=0 -run_isolated env OMARCHY_MISE_CONFIG_PATH="$declarations" OMARCHY_TEST_EXPECTED_INSTALL="install --quiet grok" OMARCHY_TEST_INSTALL_FAIL=yes grok >/dev/null || status=$? +run_isolated env OMARCHY_MISE_CONFIG_PATH="$declarations" OMARCHY_TEST_EXPECTED_INSTALL="install --quiet grok" OMARCHY_TEST_INSTALL_FAIL=yes PATH="$ROOT/bin:$real_bin:/usr/bin" omarchy-agent-account-exec grok >/dev/null || status=$? (( status == 42 )) || fail "a failed first-run install stops dispatch" "$status" pass "a failed first-run install stops dispatch" rm "$real_bin/mise" diff --git a/test/shell.d/agent-usage-kimchi-test.sh b/test/shell.d/agent-usage-kimchi-test.sh index 47300cbd18d7..d81ae16f9973 100755 --- a/test/shell.d/agent-usage-kimchi-test.sh +++ b/test/shell.d/agent-usage-kimchi-test.sh @@ -297,11 +297,14 @@ expect("a budget cached under its old name is read, elapsed at zero and an unclo sandbox() sessions = harness() / "sessions" / "--home-me--" now = iso_in(0) +# A repeated entry is the same id at the same time. Two calls could stamp it a +# millisecond apart, which would make it two replies. +repeated = reply("a-1", "minimax-m3", {"input": 100, "output": 20, "cacheRead": 800, "cacheWrite": 5, "totalTokens": 925}) write_pi_session(sessions / "today.jsonl", [ {"type": "session", "id": "s-1"}, {"type": "message", "id": "u-1", "timestamp": now, "message": {"role": "user", "content": "hi", "usage": {"input": 999}}}, - reply("a-1", "minimax-m3", {"input": 100, "output": 20, "cacheRead": 800, "cacheWrite": 5, "totalTokens": 925}), - reply("a-1", "minimax-m3", {"input": 100, "output": 20, "cacheRead": 800, "cacheWrite": 5, "totalTokens": 925}), + repeated, + repeated, reply("a-2", "glm-5.3", {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0, "totalTokens": 75}), {"type": "custom", "id": "c-1", "timestamp": now, "data": {"usage": {"input": 999}}}, {"type": "message", "id": "a-3", "message": {"role": "assistant", "model": "glm-5.3", "timestamp": ms_in(0), "usage": {"input": 10, "output": 5}}}, diff --git a/test/shell.d/base-test.sh b/test/shell.d/base-test.sh index 047abac2e46f..30f9d13074f1 100644 --- a/test/shell.d/base-test.sh +++ b/test/shell.d/base-test.sh @@ -130,13 +130,35 @@ QML_FIXTURE_TMP="" QML_FIXTURE_PID="" QML_FIXTURE_LOG="" +# Each fixture's Quickshell leads a process group of its own (setsid), so +# everything it started can be stopped through the group, even once Quickshell +# itself has crashed and left its children to init. +qml_fixture_stop() { + local group=$QML_FIXTURE_PID attempt + [[ -n $group ]] || return 0 + QML_FIXTURE_PID="" + kill -TERM -- "-$group" 2>/dev/null || true + wait "$group" 2>/dev/null || true + for attempt in {1..20}; do + pgrep -g "$group" >/dev/null 2>&1 || return 0 + sleep 0.1 + done + kill -KILL -- "-$group" 2>/dev/null || true +} + qml_fixture_cleanup() { - if [[ -n $QML_FIXTURE_PID ]] && kill -0 "$QML_FIXTURE_PID" 2>/dev/null; then - kill "$QML_FIXTURE_PID" 2>/dev/null || true - wait "$QML_FIXTURE_PID" 2>/dev/null || true + local attempt + # Processes a fixture started can still be writing into its HOME while that + # is removed, so stop them first. + qml_fixture_stop + if [[ -n $QML_FIXTURE_TMP && -d $QML_FIXTURE_TMP ]]; then + # A child that was mid-write when it got the signal can still add a file. + for attempt in {1..20}; do + rm -rf "$QML_FIXTURE_TMP" 2>/dev/null && break + sleep 0.1 + done + [[ ! -e $QML_FIXTURE_TMP ]] || echo "Could not remove $QML_FIXTURE_TMP" >&2 fi - QML_FIXTURE_PID="" - [[ -n $QML_FIXTURE_TMP && -d $QML_FIXTURE_TMP ]] && rm -rf "$QML_FIXTURE_TMP" return 0 } @@ -164,10 +186,7 @@ run_qml_fixture() { qml_fixture_scratch # A second fixture in the same file must not read the first one's result. - if [[ -n $QML_FIXTURE_PID ]] && kill -0 "$QML_FIXTURE_PID" 2>/dev/null; then - kill "$QML_FIXTURE_PID" 2>/dev/null || true - wait "$QML_FIXTURE_PID" 2>/dev/null || true - fi + qml_fixture_stop config_dir="$QML_FIXTURE_TMP/$fixture" result="$QML_FIXTURE_TMP/result.json" QML_FIXTURE_LOG="$QML_FIXTURE_TMP/quickshell.log" @@ -187,7 +206,7 @@ run_qml_fixture() { QML2_IMPORT_PATH="$ROOT/shell${QML2_IMPORT_PATH:+:$QML2_IMPORT_PATH}" \ QML_IMPORT_PATH="$ROOT/shell${QML_IMPORT_PATH:+:$QML_IMPORT_PATH}" \ PATH="$QML_FIXTURE_TMP/bin:$ROOT/bin:$PATH" \ - quickshell -p "$config_dir" --no-color >"$QML_FIXTURE_LOG" 2>&1 & + setsid quickshell -p "$config_dir" --no-color >"$QML_FIXTURE_LOG" 2>&1 & QML_FIXTURE_PID=$! for _ in {1..150}; do diff --git a/test/shell.d/i18n-startup-test.sh b/test/shell.d/i18n-startup-test.sh index 0447a9b2e95c..522d597d2445 100644 --- a/test/shell.d/i18n-startup-test.sh +++ b/test/shell.d/i18n-startup-test.sh @@ -52,9 +52,17 @@ persian = dict(english, language='fa', locale='fa-IR', number='۱۳٫۵', rtl=Tr french_fallback = dict(english, language='fr', locale='fr-FR', number='13,5') -def write_json(path, data): +# The shell watches these files and reloads them as they change, so replace +# each one whole: a file written in place can be read while still empty. +def write_text(path, text): path.parent.mkdir(parents=True, exist_ok=True) - path.write_text(json.dumps(data, ensure_ascii=False), encoding='utf-8') + staged = path.with_name(path.name + '.tmp') + staged.write_text(text, encoding='utf-8') + os.replace(staged, path) + + +def write_json(path, data): + write_text(path, json.dumps(data, ensure_ascii=False)) class Probe: @@ -159,7 +167,7 @@ try: try: probe.wait(english) for language, expected in [('da', danish), ('pt-BR', portuguese), ('fr', french_fallback), ('en', english)]: - probe.language.write_text(language) + write_text(probe.language, language) probe.wait(expected) print('ok - live language changes update shell, plugin, and locale bindings', flush=True) @@ -175,21 +183,21 @@ try: other = probe.path / 'other-plugin' write_json(other / 'en.json', dict(en_catalog, **{'test.hello': 'Other %1', 'test.baseOnly': 'English base'})) - probe.directory.write_text(str(other)) + write_text(probe.directory, str(other)) changed = dict(changed, pluginText='Other Ada') probe.wait(changed) - probe.directory.write_text(str(probe.plugin)) + write_text(probe.directory, str(probe.plugin)) changed = dict(changed, pluginText='Plugin Ada') probe.wait(changed) print('ok - changing the plugin catalog directory refreshes existing bindings', flush=True) - probe.language.write_text('da') + write_text(probe.language, 'da') probe.wait(danish) probe.language.unlink() probe.wait(changed) print('ok - deleting the language selection restores English', flush=True) - probe.language.write_text('da') + write_text(probe.language, 'da') probe.wait(danish) (probe.catalogs / 'da/shell.json').unlink() (probe.plugin / 'da.json').unlink() diff --git a/test/shell.d/input-indicator-test.sh b/test/shell.d/input-indicator-test.sh index 1229fa9d38c8..2f4b46fbcf03 100755 --- a/test/shell.d/input-indicator-test.sh +++ b/test/shell.d/input-indicator-test.sh @@ -348,6 +348,19 @@ print("ok - the watched profile follows Fcitx's FCITX_CONFIG_HOME override") os.makedirs(scratch.name + "/daemon/fcitx") daemon = subprocess.Popen(["sleep", "30"], cwd=scratch.name, env={"HOME": scratch.name, "FCITX_CONFIG_HOME": "daemon/fcitx"}) +# Popen returns while the exec is still under way, and until it finishes the +# child's /proc environ reads empty, is refused, or is the parent's. Fcitx owns +# its bus name only well after its exec, so wait for the environment here too. +def daemon_environment(): + try: + with open(f"/proc/{daemon.pid}/environ", "rb") as file: + return file.read() + except PermissionError: + return b"" +for _ in range(500): + if b"FCITX_CONFIG_HOME=" in daemon_environment(): + break + time.sleep(0.01) class OwnerBus: def call_sync(self, name, path, interface, method, args, *rest): assert method == "GetConnectionUnixProcessID" and args.unpack() == (indicator.BUS,) diff --git a/test/shell.d/runtime-smoke-test.sh b/test/shell.d/runtime-smoke-test.sh index 8f644ba2614f..eb8648ef4664 100755 --- a/test/shell.d/runtime-smoke-test.sh +++ b/test/shell.d/runtime-smoke-test.sh @@ -349,6 +349,23 @@ bar_background() { HOME="$SCRATCH_HOME" OMARCHY_PATH="$SCRATCH_ROOT" PATH="$ROOT/bin:$PATH" "$ROOT/bin/omarchy-bar" background "$@" } +# Elsewhen saves its first-run cities once its probe returns, and that save +# writes the shell's whole config back to shell.json. Landing after an edit +# from outside the shell, it puts the old config back, so wait for it before +# editing shell.json. A machine slower than a desktop returns the probe late. +wait_for_elsewhen_cities() { + local saved='any(.bar.layout[][]; .id? == "omarchy.elsewhen" and .cities? != null)' + local config="$SCRATCH_HOME/.config/omarchy/shell.json" + + for _ in {1..300}; do + jq -e "$saved" "$config" >/dev/null 2>&1 && return 0 + kill -0 "$SCRATCH_PID" 2>/dev/null || fail_with_log "test shell exited before Elsewhen saved its cities" + sleep 0.1 + done + fail_with_log "Elsewhen saves its first-run cities before shell.json is edited" +} + +wait_for_elsewhen_cities bar_background none >/dev/null for _ in {1..50}; do (( $(text_color_count) > 0 )) && break @@ -632,40 +649,32 @@ pass "bar put leaves a widget already on the bar alone" # A hand edit that drops the closing brace puts the shell on the defaults, # which have no omarchy.microphone. A bar change then applies for the session # only, leaving the file as the user left it until they fix it. -config="$test_home/.config/omarchy/shell.json" -# Elsewhen saves its first-run cities once its probe returns. Landing between -# the edit and the shell's reload, that save would write valid JSON back. -for _ in {1..80}; do - jq -e 'any(.bar.layout[][]; .id? == "omarchy.elsewhen" and .cities? != null)' "$config" >/dev/null 2>&1 && break - kill -0 "$QS_PID" 2>/dev/null || fail_with_log "test shell exited before Elsewhen saved its cities" - sleep 0.1 -done -jq -e 'any(.bar.layout[][]; .id? == "omarchy.elsewhen" and .cities? != null)' "$config" >/dev/null || - fail_with_log "Elsewhen saves its first-run cities before shell.json is edited" +config="$SCRATCH_HOME/.config/omarchy/shell.json" +wait_for_elsewhen_cities sed -i '$ d' "$config" -cp "$config" "$TMPDIR/broken.json" +cp "$config" "$SCRATCH_DIR/broken.json" for _ in {1..80}; do [[ $(center_ids) != *omarchy.microphone* ]] && break - kill -0 "$QS_PID" 2>/dev/null || fail_with_log "test shell exited while reading an unreadable shell.json" + kill -0 "$SCRATCH_PID" 2>/dev/null || fail_with_log "test shell exited while reading an unreadable shell.json" sleep 0.1 done [[ $(center_ids) != *omarchy.microphone* ]] || fail_with_log "shell falls back to defaults on an unreadable shell.json" -[[ $(shell_ipc shell setBarWidget omarchy.clock format '"HH:mm"' '{}') == "ok" ]] || +[[ $(scratch_ipc shell setBarWidget omarchy.clock format '"HH:mm"' '{}') == "ok" ]] || fail_with_log "bar widget setting is applied while shell.json is unreadable" jq -e 'any(.bar.layout[][]; .id? == "omarchy.clock" and .format? == "HH:mm")' \ - <<<"$(shell_ipc shell listShellConfig)" >/dev/null || + <<<"$(scratch_ipc shell listShellConfig)" >/dev/null || fail_with_log "a bar change still applies in memory while shell.json is unreadable" sleep 0.5 -cmp -s "$config" "$TMPDIR/broken.json" || fail "a bar change leaves an unreadable shell.json as the user left it" "$(cat "$config")" +cmp -s "$config" "$SCRATCH_DIR/broken.json" || fail "a bar change leaves an unreadable shell.json as the user left it" "$(cat "$config")" pass "a bar change leaves an unreadable shell.json as the user left it" printf '}\n' >>"$config" for _ in {1..80}; do [[ $(center_ids) == *omarchy.microphone* ]] && break - kill -0 "$QS_PID" 2>/dev/null || fail_with_log "test shell exited while reading a fixed shell.json" + kill -0 "$SCRATCH_PID" 2>/dev/null || fail_with_log "test shell exited while reading a fixed shell.json" sleep 0.1 done -[[ $(shell_ipc shell setBarWidget omarchy.clock format '"H:mm"' '{}') == "ok" ]] || +[[ $(scratch_ipc shell setBarWidget omarchy.clock format '"H:mm"' '{}') == "ok" ]] || fail_with_log "bar widget setting is applied once shell.json is fixed" saved='any(.bar.layout[][]; (.id? // .) == "omarchy.microphone") and any(.bar.layout[][]; .id? == "omarchy.clock" and .format? == "H:mm")' for _ in {1..40}; do