testsAndMisc/phone_focus_mode/lib/tests/ctl_libs_harness.sh
Krzysztof kuhy Rudnicki 3ddc13f671 focus_ctl: split 1091 -> 171 + 8 siblings, one per enforcer
The largest file in phone_focus_mode, and the last one blocking deploy.sh:
splitting it grows deploy.sh's two hardcoded lists, so those lists are only
final now. All 55 functions moved verbatim.

Three while-read loops were audited before extraction. Two call pm in their
bodies and moved untouched. The third, iter_whitelist_packages, is fed by a
pipe from printf and reads nothing from stdin -- already safe, and now said
so in ctl_usage.sh's header so the distinction is not lost by a later editor
who has only read the warning in daemon_apps.sh.

136 assertions and 22 mutations, all killed. Three survived the first run
and every one was a defect in MY OWN tests, not in the subject:

  - cmd_status carries a THIRD copy of the Haversine, and I had asserted
    only that it printed "some number". A wrong Earth radius sailed through.
    Now the value is checked against a seeded fix ~4.3 km from home.
  - the two whitelist filters were each tested with input the OTHER filter
    also rejects, so either could be deleted with nothing failing. Fixed
    with a punctuation-free undotted token (only the dot check rejects it)
    and a dotted token carrying an illegal character (only the charset check
    does), making each independently observable.

That last investigation turned up dead code: the `#*)` case arm is provably
equivalent, because `set -- $line` makes a comment's first token "#", which
the dot check already rejects. Removed rather than covered by a test that
cannot fail.

Coverage is NOT 100% and the real numbers are in the evidence: 41.79% to
92.98% across the eight, up from 5-22%. Unlike the daemon split, most of
what remains is genuinely untested -- the per-enforcer status blocks that
print mount state, APK hashes and offload settings, each needing its own
device fixture. The mutations prove the assertions that exist are load-
bearing; they do not prove those branches work.

jscpd checked in a clean worktree at 1.57%: eight files each carrying a
near-identical start/stop/status trio is exactly what that gate is for, so
the tests drive the trios through tables instead of repeating them.

Two drift hazards recorded, not fixed: ctl_is_curfew_now duplicates
daemon_location.sh's is_curfew_now (pinned by an agreement test across every
boundary minute), and cmd_status's awk is the third Haversine.

Over-cap count 17 -> 16. deploy.sh is the last one here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 17:46:04 +02:00

166 lines
5.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# lib/tests/ctl_libs_harness.sh — the fake device behind the tests for the
# libraries split out of focus_ctl.sh.
#
# Sourced, not executed. The subcommands drive the phone through real binaries
# (pm, iptables, settings, setsid, ...), so the stubs go on PATH. The real
# config.sh supplies every default so this fixture cannot drift from the
# values the subcommands actually read; only the paths are redirected.
set -euo pipefail
# TESTS_DIR is declared readonly by the entry point that sources this file.
readonly PHONE_DIR="${TESTS_DIR}/../.."
PASS=0
FAIL=0
_t_pass() {
PASS=$((PASS + 1))
printf ' OK: %s\n' "$1"
}
_t_fail() {
FAIL=$((FAIL + 1))
printf ' FAIL: %s\n' "$1"
}
_t_eq() {
local want="$1" got="$2" what="$3"
if [[ "$got" == "$want" ]]; then
_t_pass "$what"
else
_t_fail "$what (want '${want}', got '${got}')"
fi
}
RUN="$(mktemp -d)"
trap 'rm -rf "${RUN}"' EXIT
mkdir -p "${RUN}/bin" "${RUN}/state"
readonly DEV="${RUN}/state"
# `date` answers from a pinned clock so the window cases are deterministic
# rather than dependent on when the suite happens to run.
cat >"${RUN}/bin/date" <<'STUB'
#!/usr/bin/env bash
if [[ -f "${DEV}/now" && "$1" == "+%H%M" ]]; then
cat "${DEV}/now"
exit 0
fi
exec /usr/bin/date "$@"
STUB
chmod +x "${RUN}/bin/date"
# --- device stubs -----------------------------------------------------------
#
# Every cmd_* subcommand drives the phone through one of these. They log each
# call so a command's effect is assertable, and each honours a fail_<name>
# flag so the "not running" / "chain missing" branches are reachable.
#
# setsid is the important one: the start commands launch a real daemon with
# it. Stubbing it keeps the suite from spawning enforcers on this machine,
# and lets a test decide whether the launch "worked" by seeding the pidfile.
_mk_stub() {
cat >"${RUN}/bin/$1" <<STUB
#!/usr/bin/env bash
printf '%s %s\n' "$1" "\$*" >>"\${DEV}/calls.log"
[[ -f "\${DEV}/fail_$1" ]] && exit 1
[[ -f "\${DEV}/out_$1" ]] && cat "\${DEV}/out_$1"
exit 0
STUB
chmod +x "${RUN}/bin/$1"
}
for _s in pm am settings dumpsys svc sqlite3 getprop content kill sleep setsid; do
_mk_stub "$_s"
done
# iptables/ip6tables also model chain existence, so the MISSING branches and
# the rule counts in the status commands are both reachable.
cat >"${RUN}/bin/iptables" <<'STUB'
#!/usr/bin/env bash
bin="$(basename "$0")"
printf '%s %s\n' "$bin" "$*" >>"${DEV}/calls.log"
case "$1" in
-L) [[ -f "${DEV}/chain_${bin}" ]] && exit 0 || exit 1 ;;
-S) [[ -f "${DEV}/chain_${bin}" ]] && cat "${DEV}/chain_${bin}" || exit 1 ;;
esac
exit 0
STUB
chmod +x "${RUN}/bin/iptables"
cp "${RUN}/bin/iptables" "${RUN}/bin/ip6tables"
export DEV
export PATH="${RUN}/bin:${PATH}"
_calls() { cat "${DEV}/calls.log" 2>/dev/null || printf ''; }
_reset_calls() { : >"${DEV}/calls.log"; }
_fail_op() { touch "${DEV}/fail_$1"; }
_clear_fail() { rm -f "${DEV}/fail_$1"; }
_seed_out() { printf '%s\n' "$2" >"${DEV}/out_$1"; }
_seed_chain() { printf '%s\n' "${2:--A rule}" >"${DEV}/chain_$1"; }
_clear_chain() { rm -f "${DEV}/chain_$1"; }
_set_now() { printf '%s\n' "$1" >"${DEV}/now"; }
# --- ambient definitions the libraries expect from focus_ctl.sh ------------
log() { :; }
# The real config.sh supplies every default, so this fixture cannot drift
# from the values the subcommands actually read. FOCUS_MODE_SCRIPT_DIR points
# its SCRIPT_DIR at the checkout; the paths are then redirected at a temp dir
# so nothing here touches /data/local/tmp.
FOCUS_MODE_SCRIPT_DIR="${PHONE_DIR}"
export FOCUS_MODE_SCRIPT_DIR
# shellcheck source=../../config.sh
. "${PHONE_DIR}/config.sh"
STATE_DIR="${RUN}/state"
LOG_FILE="${STATE_DIR}/focus.log"
MODE_FILE="${STATE_DIR}/mode"
STATUS_FILE="${STATE_DIR}/status.json"
DISABLED_APPS_FILE="${STATE_DIR}/disabled.txt"
DISABLED_COMPETITORS_FILE="${STATE_DIR}/competitors.txt"
RECHECK_TRIGGER="${STATE_DIR}/trigger_recheck"
CURFEW_OVERRIDE_FILE="${STATE_DIR}/curfew_override"
CURFEW_FORCE_FILE="${STATE_DIR}/curfew_force"
CURFEW_ENFORCER_STATE="${STATE_DIR}/curfew_applied"
CURFEW_ENFORCER_LOG="${STATE_DIR}/curfew.log"
TETHER_FORCE_FILE="${STATE_DIR}/tether_force"
TETHER_OVERRIDE_FILE="${STATE_DIR}/tether_override"
TETHER_ENFORCER_STATE="${STATE_DIR}/tether_applied"
TETHER_LOG="${STATE_DIR}/tether.log"
HOSTS_LOG="${STATE_DIR}/hosts.log"
HOSTS_CANONICAL="${STATE_DIR}/hosts.canonical"
HOSTS_SHA_FILE="${STATE_DIR}/hosts.sha256"
HOSTS_TARGET="${STATE_DIR}/hosts_target"
DNS_LOG="${STATE_DIR}/dns.log"
LAUNCHER_LOG="${STATE_DIR}/launcher.log"
LAUNCHER_ACTIVITY_FILE="${STATE_DIR}/launcher.activity"
LAUNCHER_APK="${STATE_DIR}/launcher.apk"
LAUNCHER_SHA_FILE="${STATE_DIR}/launcher.sha256"
WORKOUT_DETECTOR_LOG="${STATE_DIR}/workout.log"
WORKOUT_ACTIVE_FILE="${STATE_DIR}/workout_active"
WORKOUT_DB_PATH="${STATE_DIR}/stronglifts.db"
WORKOUT_SQLITE3_BIN="${RUN}/bin/sqlite3"
NIGHT_CURFEW_START="2300"
NIGHT_CURFEW_END="0500"
# shellcheck source=../../ctl_curfew.sh
. "${PHONE_DIR}/ctl_curfew.sh"
# shellcheck source=../../ctl_hosts.sh
. "${PHONE_DIR}/ctl_hosts.sh"
# shellcheck source=../../ctl_dns.sh
. "${PHONE_DIR}/ctl_dns.sh"
# shellcheck source=../../ctl_launcher.sh
. "${PHONE_DIR}/ctl_launcher.sh"
# shellcheck source=../../ctl_workout.sh
. "${PHONE_DIR}/ctl_workout.sh"
# shellcheck source=../../ctl_tether.sh
. "${PHONE_DIR}/ctl_tether.sh"
# shellcheck source=../../ctl_daemon.sh
. "${PHONE_DIR}/ctl_daemon.sh"
# shellcheck source=../../ctl_usage.sh
. "${PHONE_DIR}/ctl_usage.sh"