2026-05-10 02:58:11 +02:00
#!/bin/bash
# Regression tests for the music parallelism daemon's polling cadence.
set -euo pipefail
SCRIPT_DIR = $( cd -- " $( dirname -- " ${ BASH_SOURCE [0] } " ) " && pwd )
REPO_DIR = $( cd -- " $SCRIPT_DIR /.. " && pwd )
2026-05-16 15:46:02 +02:00
TARGET_SCRIPT = " $REPO_DIR /scripts/digital_wellbeing/music_parallelism.sh "
2026-05-10 02:58:11 +02:00
fail( ) {
printf 'FAIL: %s\n' " $1 " >& 2
exit 1
}
assert_equals( ) {
local expected = " $1 "
local actual = " $2 "
local context = " $3 "
if [ [ " $expected " != " $actual " ] ] ; then
fail " $context (expected: ' $expected ', actual: ' $actual ') "
fi
}
TMP_DIR = $( mktemp -d)
cleanup( ) {
rm -rf " $TMP_DIR "
}
trap cleanup EXIT
WORKTREE = " $TMP_DIR /worktree "
BIN_DIR = " $TMP_DIR /bin "
2026-05-16 15:46:02 +02:00
mkdir -p " $WORKTREE /scripts/digital_wellbeing " " $WORKTREE /scripts/lib " " $BIN_DIR "
cp " $TARGET_SCRIPT " " $WORKTREE /scripts/digital_wellbeing/music_parallelism.sh "
2026-05-10 02:58:11 +02:00
cat >" $WORKTREE /scripts/lib/common.sh " <<'EOF'
#!/bin/bash
FOCUS_APPS_WINDOWS = ( "Mock Focus App" )
FOCUS_APPS_PROCESSES = ( "mock-focus-proc" )
is_focus_app_running( ) {
if [ [ ${ MOCK_FOCUS_ACTIVE :- 0 } -eq 1 ] ] ; then
printf '%s\n' "Mock Focus App"
return 0
fi
return 1
}
get_timestamp( ) {
printf '%s\n' " ${ MOCK_TIMESTAMP :- 1000 } "
}
log_message( ) {
:
}
EOF
chmod +x " $WORKTREE /scripts/lib/common.sh "
2026-05-14 19:55:42 +02:00
create_fake_proc_process( ) {
local proc_root = " $1 "
local pid = " $2 "
local name = " $3 "
mkdir -p " $proc_root / $pid "
printf '%s\n' " $name " >" $proc_root / $pid /comm "
}
2026-05-10 02:58:11 +02:00
run_case( ) {
2026-05-14 19:55:42 +02:00
local expected_wait = " $1 "
2026-05-10 02:58:11 +02:00
local focus_active = " $2 "
2026-05-14 19:55:42 +02:00
local music_proc_name = " ${ 3 :- } "
local mode = " ${ 4 :- instant } "
local wait_log = " $TMP_DIR /wait.log "
local proc_root = " $TMP_DIR /proc "
: >" $wait_log "
rm -rf " $proc_root "
mkdir -p " $proc_root "
if [ [ -n $music_proc_name ] ] ; then
create_fake_proc_process " $proc_root " 4242 " $music_proc_name "
fi
2026-05-10 02:58:11 +02:00
PATH = " $BIN_DIR : $PATH " \
2026-05-14 19:55:42 +02:00
MUSIC_PARALLELISM_TEST_WAIT_LOG = " $wait_log " \
MUSIC_PARALLELISM_TEST_EXIT_AFTER_WAIT = 1 \
XDOTOOL_LOG = " ${ XDOTOOL_LOG :- } " \
PROC_ROOT = " $proc_root " \
2026-05-10 02:58:11 +02:00
MOCK_FOCUS_ACTIVE = " $focus_active " \
2026-05-16 15:46:02 +02:00
bash " $WORKTREE /scripts/digital_wellbeing/music_parallelism.sh " " $mode " \
2026-05-10 02:58:11 +02:00
>/dev/null 2>& 1 || true
2026-05-14 19:55:42 +02:00
assert_equals " $expected_wait " " $( <" $wait_log " ) " "music_parallelism.sh should pick the expected wait interval"
2026-05-10 02:58:11 +02:00
}
printf 'Checking stable-focus backoff uses the slower interval...\n'
2026-05-14 19:55:42 +02:00
run_case 15 1
2026-05-10 02:58:11 +02:00
printf 'Checking conflict handling uses the faster retry interval...\n'
2026-05-14 19:55:42 +02:00
run_case 5 1 spotify
2026-05-10 02:58:11 +02:00
printf 'Checking idle mode uses the idle interval...\n'
2026-05-14 19:55:42 +02:00
run_case 30 0
printf 'Checking conflict path avoids duplicate xdotool searches...\n'
xdotool_log = " $TMP_DIR /xdotool.log "
: >" $xdotool_log "
XDOTOOL_LOG = " $xdotool_log "
cat >" $BIN_DIR /xdotool " <<'EOF'
#!/bin/bash
printf '%s\n' " $1 " >> " ${ XDOTOOL_LOG : ? } "
if [ [ ${ 1 :- } = = search ] ] ; then
exit 1
fi
if [ [ ${ 1 :- } = = windowclose ] ] ; then
exit 0
fi
exit 0
EOF
chmod +x " $BIN_DIR /xdotool "
run_case 5 1 spotify
search_calls = $( grep -c '^search$' " $xdotool_log " 2>/dev/null || true )
assert_equals '1' " $search_calls " 'music_parallelism.sh should avoid duplicate xdotool search calls when process-only music is detected'
printf 'Checking monitor loop also avoids duplicate xdotool searches...\n'
: >" $xdotool_log "
run_case 15 1 spotify monitor
monitor_search_calls = $( grep -c '^search$' " $xdotool_log " 2>/dev/null || true )
assert_equals '1' " $monitor_search_calls " 'music_parallelism.sh monitor loop should avoid duplicate xdotool search calls when process-only music is detected'
2026-05-10 02:58:11 +02:00
printf 'music_parallelism.sh regression checks passed.\n'