Files
omarchycn/test/shell.d/battery-status-test.sh
T

49 lines
1.5 KiB
Bash

#!/bin/bash
set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"
tmp_dir=$(mktemp -d)
trap 'rm -rf "$tmp_dir"' EXIT
mkdir -p "$tmp_dir/bin"
cat >"$tmp_dir/bin/upower" <<'STUB'
#!/bin/bash
if [[ $1 == "-e" ]]; then
echo "/org/freedesktop/UPower/devices/battery_BAT0"
exit 0
fi
if [[ $1 == "-i" ]]; then
cat <<'INFO'
native-path: BAT0
state: discharging
energy: 28.3 Wh
energy-full: 56.7 Wh
energy-rate: 7.3 W
time to empty: 2.5 hours
percentage: 51%
INFO
exit 0
fi
exit 1
STUB
chmod +x "$tmp_dir/bin/upower"
shell_output=$(PATH="$tmp_dir/bin:$PATH" "$ROOT/bin/omarchy-battery-status" --shell)
grep -Fx $'percentage\t51%' <<<"$shell_output" >/dev/null || fail "battery status reports percentage"
grep -Fx $'state\tdischarging' <<<"$shell_output" >/dev/null || fail "battery status reports state"
grep -Fx $'rate\t7.3W' <<<"$shell_output" >/dev/null || fail "battery status reports power rate"
grep -Fx $'size\t56Wh' <<<"$shell_output" >/dev/null || fail "battery status reports full capacity"
grep -Fx $'time\t2h 30m' <<<"$shell_output" >/dev/null || fail "battery status reports remaining time"
if matches=$(rg -n 'omarchy-battery-(capacity|remaining|remaining-time)' "$ROOT/bin" "$ROOT/test" "$ROOT/shell" "$ROOT/docs"); then
fail "battery status owns capacity and remaining calculations" "$matches"
fi
pass "battery status owns capacity and remaining calculations"