#!/bin/bash set -euo pipefail source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" test_tmp=$(mktemp -d) trap 'rm -rf "$test_tmp"' EXIT root="$test_tmp/root" sandbox="$test_tmp/omarchy" calls="$test_tmp/calls" mkdir -p "$root/etc/pacman.d" "$sandbox/bin" "$sandbox/install/config" "$sandbox/default/region" "$test_tmp/bin" "$test_tmp/home" cp -r "$ROOT/default/region/china" "$sandbox/default/region/" rewrite() { sed -e "s|/etc/|$root/etc/|g" -e "s| /\\.cargo/| $root/.cargo/|g" "$1" >"$2"; chmod +x "$2"; } rewrite "$ROOT/install/config/region.sh" "$sandbox/install/config/region.sh" rewrite "$ROOT/bin/omarchy-region" "$sandbox/bin/omarchy-region" for command in omarchy-update-pacman omarchy-pkg-add locale-gen rime-ice-installer omarchy-setup-claude-api systemctl omarchy-system-logout; do printf '#!/bin/bash\necho "%s $*" >>"%s"\n' "$command" "$calls" >"$test_tmp/bin/$command" done printf '#!/bin/bash\necho "omarchy-pkg-add $*" >>"%s"\n[[ -z ${STUB_PKG_FAIL:-} ]]\n' "$calls" >"$test_tmp/bin/omarchy-pkg-add" printf '#!/bin/bash\n[[ $1 == env ]] && shift\nexec env "$@"\n' >"$test_tmp/bin/sudo" printf '#!/bin/bash\necho "gum $*" >>"%s"\nexit "${GUM_ANSWER:-1}"\n' "$calls" >"$test_tmp/bin/gum" chmod +x "$test_tmp/bin"/* export PATH="$test_tmp/bin:$sandbox/bin:$PATH" OMARCHY_PATH="$sandbox" HOME="$test_tmp/home" cp "$ROOT/default/pacman/pacman-stable.conf" "$root/etc/pacman.conf" cp "$ROOT/default/pacman/mirrorlist-stable" "$root/etc/pacman.d/mirrorlist" printf '#zh_CN.UTF-8 UTF-8\nen_US.UTF-8 UTF-8\n' >"$root/etc/locale.gen" printf 'LANG=en_US.UTF-8\n' >"$root/etc/locale.conf" china_conf=$(sed -f "$ROOT/default/region/china/pacman.sed" "$ROOT/default/pacman/pacman-stable.conf") china_mirrors=$(sed -f "$ROOT/default/region/china/pacman.sed" "$ROOT/default/pacman/mirrorlist-stable") omarchy-region china >/dev/null [[ $(<"$root/etc/pacman.conf") == "$china_conf" && $(<"$root/etc/pacman.d/mirrorlist") == "$china_mirrors" ]] || fail "switching to China lists the .cn mirrors first" [[ $(omarchy-region) == "China" && $(<"$root/etc/locale.conf") == "LANG=zh_CN.UTF-8" ]] || fail "switching to China records it and sets the language" [[ $(<"$calls") == $'omarchy-update-pacman -Syu --noconfirm\nomarchy-pkg-add fcitx5-configtool fcitx5-rime librime opencc rime-ice-installer\nlocale-gen \nsystemctl --user try-restart omarchy-fcitx5.service\nrime-ice-installer --yes' ]] || fail "China upgrades in full before installing Rime, then sets Rime Ice up" "$(<"$calls")" pass "switching to China sets mirrors, language and Rime Ice" : >"$calls" mkdir -p "$HOME/.local/share/fcitx5/rime" && touch "$HOME/.local/share/fcitx5/rime/rime_ice.schema.yaml" omarchy-region china >/dev/null [[ $(<"$root/etc/pacman.conf") == "$china_conf" && $(<"$root/etc/pacman.d/mirrorlist") == "$china_mirrors" ]] || fail "switching to China twice adds no second .cn line" grep -q '^rime-ice-installer\|^systemctl' "$calls" && fail "an existing Rime Ice setup is not redone" pass "switching to China again changes nothing it already did" printf 'Server = https://stable-mirror.omarchy.cn/$repo/os/$arch\nServer = https://stable-mirror.omarchy.org/$repo/os/$arch\nServer = https://mirror.omarchy.org/$repo/os/$arch\n' >"$root/etc/pacman.d/mirrorlist" omarchy-region china >/dev/null [[ $(<"$root/etc/pacman.d/mirrorlist") == $'Server = https://stable-mirror.omarchy.cn/$repo/os/$arch\nServer = https://stable-mirror.omarchy.org/$repo/os/$arch\nServer = https://mirror.omarchy.cn/$repo/os/$arch\nServer = https://mirror.omarchy.org/$repo/os/$arch' ]] || fail "a partly switched mirrorlist gets every missing .cn twin" "$(<"$root/etc/pacman.d/mirrorlist")" cp "$ROOT/default/pacman/mirrorlist-stable" "$root/etc/pacman.d/mirrorlist" sed -i -f "$ROOT/default/region/china/pacman.sed" "$root/etc/pacman.d/mirrorlist" pass "every .org server ends up with exactly one .cn twin in front" : >"$calls" omarchy-region global >/dev/null [[ $(<"$root/etc/pacman.conf") == "$(<"$ROOT/default/pacman/pacman-stable.conf")" && $(<"$root/etc/pacman.d/mirrorlist") == "$(<"$ROOT/default/pacman/mirrorlist-stable")" ]] || fail "switching to Global drops exactly the .cn mirrors" [[ $(omarchy-region) == "Global" && $(<"$root/etc/locale.conf") == "LANG=en_US.UTF-8" ]] || fail "switching to Global records it and restores English" [[ $(<"$calls") == "omarchy-setup-claude-api --remove" ]] || fail "Global removes the Claude Code third-party API and keeps Rime" "$(<"$calls")" [[ -f $HOME/.local/share/fcitx5/rime/rime_ice.schema.yaml ]] || fail "Global keeps the input method" pass "switching to Global restores mirrors and language, keeps Rime, removes the Claude API setup" if output=$(omarchy-region mars 2>&1); then fail "an unknown region is refused" "$output" fi pass "an unknown region is refused" if output=$(STUB_PKG_FAIL=1 omarchy-region china 2>&1); then fail "switching to China fails when its packages do not install" "$output" fi [[ $(omarchy-region) == "Global" && $(<"$root/etc/pacman.conf") == "$(<"$ROOT/default/pacman/pacman-stable.conf")" && $(<"$root/etc/pacman.d/mirrorlist") == "$(<"$ROOT/default/pacman/mirrorlist-stable")" ]] || fail "a failed switch to China leaves the region and the mirrors Global" pass "a failed switch to China leaves the machine Global" grep -q '^gum' "$calls" && fail "a switch without a terminal asks nothing" "$(<"$calls")" : >"$calls" output=$(GUM_ANSWER=1 script -qec "omarchy-region china" /dev/null) grep -qxF 'gum confirm --affirmative 立即注销 --negative 稍后 立即注销,切换到中文界面和国内镜像?' "$calls" || fail "switching to China offers to log out, in Chinese" "$(<"$calls")" [[ $output == *"区域已设为中国。重新登录后切换到中文界面和国内镜像。"* ]] && ! grep -q '^omarchy-system-logout' "$calls" || fail "declining the logout leaves a reminder and the session" "$output" : >"$calls" GUM_ANSWER=0 script -qec "omarchy-region global" /dev/null >/dev/null grep -qxF 'gum confirm --affirmative Log out now --negative Later Log out now to switch to English and the global mirrors?' "$calls" && grep -qx 'omarchy-system-logout ' "$calls" || fail "switching to Global offers to log out in English and logs out on yes" "$(<"$calls")" pass "a switch in a terminal offers to log out in the new region's language" rc_migration="$test_tmp/1788112314.sh" rewrite "$ROOT/migrations/1788112314.sh" "$rc_migration" cp "$ROOT/default/pacman/mirrorlist-rc" "$root/etc/pacman.d/mirrorlist" sed -e 's|pkgs.omarchy.org/rc/|pkgs.omarchy.org/edge/|' "$ROOT/default/pacman/pacman-rc.conf" | sed -f "$ROOT/default/region/china/pacman.sed" >"$root/etc/pacman.conf" bash -e "$rc_migration" >/dev/null [[ $(grep '^Server' "$root/etc/pacman.conf") == $'Server = https://pkgs.omarchy.cn/rc/$arch\nServer = https://pkgs.omarchy.org/rc/$arch' ]] || fail "the rc repository migration moves the .cn twin with its .org server" "$(grep '^Server' "$root/etc/pacman.conf")" pass "the rc repository migration moves the .cn twin with its .org server"