#!/usr/bin/env python3 """Offline regression tests for sxmo_timer.sh, dmenu timer integration, and hooks.""" import os import pty import select import shutil import stat import subprocess import tempfile import time import unittest BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TIMER_SH = os.path.join(BASE_DIR, "overlay/rootfs/usr/bin/sxmo_timer.sh") def read_text(rel_path): with open(os.path.join(BASE_DIR, rel_path), "r", encoding="utf-8") as handle: return handle.read() class TestTimer(unittest.TestCase): def setUp(self): self.tmp = tempfile.mkdtemp(prefix="bananarchy-timer-test-") self.bin_dir = os.path.join(self.tmp, "bin") self.rt_dir = os.path.join(self.tmp, "runtime") self.home_dir = os.path.join(self.tmp, "home") os.makedirs(self.bin_dir, exist_ok=True) os.makedirs(self.rt_dir, exist_ok=True) os.makedirs(os.path.join(self.home_dir, ".config", "sxmo"), exist_ok=True) self.ring_flag = os.path.join(self.tmp, "sxmo_timer_ringing") self.notify_log = os.path.join(self.tmp, "notify.log") self.vibrate_log = os.path.join(self.tmp, "vibrate.log") self.wake_log = os.path.join(self.tmp, "wake.log") # Minimal sxmo_common.sh stub and mock helpers so BusyBox/POSIX ash behaves # identically offline without X11 or hardware access. self._write_exec( "sxmo_common.sh", "#!/bin/sh\n" "export XDG_RUNTIME_DIR=\"${XDG_RUNTIME_DIR:-/tmp}\"\n" "export XDG_CONFIG_HOME=\"${XDG_CONFIG_HOME:-$HOME/.config}\"\n", ) self._write_exec( "sxmo_aligned_sleep", "#!/bin/sh\nsleep \"${1:-1}\"\n", ) self._write_exec( "dunstify", f"#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{self.notify_log}'\n", ) self._write_exec( "notify-send", f"#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{self.notify_log}'\n", ) self._write_exec( "sxmo_vibrate", f"#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{self.vibrate_log}'\nexit 0\n", ) self._write_exec( "dunstctl", "#!/bin/sh\nexit 0\n", ) self._write_exec( "killall", "#!/bin/sh\nexit 0\n", ) self.env = os.environ.copy() self.env.update({ "PATH": f"{self.bin_dir}:{self.env.get('PATH', '')}", "HOME": self.home_dir, "XDG_CONFIG_HOME": os.path.join(self.home_dir, ".config"), "XDG_RUNTIME_DIR": self.rt_dir, "SXMO_TIMER_DIR": os.path.join(self.rt_dir, "sxmo_timers"), "SXMO_TIMER_RING_FLAG": self.ring_flag, "SXMO_TIMER_RING_SECS": "5", }) def tearDown(self): try: subprocess.run( [TIMER_SH, "stop"], env=self.env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=5, check=False, ) finally: shutil.rmtree(self.tmp, ignore_errors=True) def _write_exec(self, name, content): path = os.path.join(self.bin_dir, name) with open(path, "w", encoding="utf-8") as handle: handle.write(content) os.chmod(path, 0o755) def test_script_and_hooks_invariants(self): st_mode = os.stat(TIMER_SH).st_mode self.assertTrue(bool(st_mode & stat.S_IXUSR), "sxmo_timer.sh must be executable") subprocess.check_call(["sh", "-n", TIMER_SH]) text = read_text("overlay/rootfs/usr/bin/sxmo_timer.sh") self.assertNotIn("boottime_alarm", text) self.assertNotIn("pkill", text) self.assertIn("HUP INT TERM EXIT", text) self.assertIn("stop_ringing()", text) for rel in ( "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_mute_ring.sh", "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_discard.sh", ): hook_txt = read_text(rel) self.assertIn("/usr/bin/sxmo_timer.sh stop_ringing", hook_txt) dmenu_c = read_text("src/sxmo-dmenu/dmenu.c") for expected in ("timer_menu_state(", "Stop Timer", "Cancel Timer", "/usr/bin/sxmo_timer.sh stop"): self.assertIn(expected, dmenu_c) def test_countdown_no_permission_error_and_stop(self): proc = subprocess.Popen( [TIMER_SH, "timerrun", "10m"], env=self.env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) try: timer_dir = self.env["SXMO_TIMER_DIR"] deadline = time.monotonic() + 3.0 timer_files = [] while time.monotonic() < deadline: if os.path.isdir(timer_dir): timer_files = [f for f in os.listdir(timer_dir) if f.endswith(".timer")] if timer_files: break time.sleep(0.05) self.assertTrue(timer_files, "expected active .timer state file during countdown") st = subprocess.run( [TIMER_SH, "status"], env=self.env, capture_output=True, text=True, check=True, ) self.assertIn("running", st.stdout) self.assertIn("10m", st.stdout) subprocess.run([TIMER_SH, "stop"], env=self.env, check=True) out, err = proc.communicate(timeout=4) self.assertEqual(err.strip(), "", f"unexpected stderr: {err!r}") self.assertIn("Timer: 10m", out) self.assertEqual([f for f in os.listdir(timer_dir) if f.endswith(".timer")], []) finally: if proc.poll() is None: proc.kill() proc.wait(timeout=3) for pipe in (proc.stdout, proc.stderr): if pipe is not None: pipe.close() def test_expiry_ringing_and_stop_ringing(self): proc = subprocess.Popen( [TIMER_SH, "timerrun", "1s"], env=self.env, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, ) try: deadline = time.monotonic() + 4.0 while time.monotonic() < deadline and not os.path.exists(self.ring_flag): time.sleep(0.05) self.assertTrue(os.path.exists(self.ring_flag), "ring flag must appear on expiry") time.sleep(0.3) subprocess.run([TIMER_SH, "stop_ringing"], env=self.env, check=True) out, err = proc.communicate(timeout=4) self.assertEqual(err.strip(), "") self.assertIn("Done with 1s", out) self.assertFalse(os.path.exists(self.ring_flag), "ring flag must be cleaned on stop_ringing") self.assertTrue(os.path.exists(self.notify_log), "expected expiry notification") self.assertTrue(os.path.exists(self.vibrate_log), "expected expiry vibrate") finally: if proc.poll() is None: proc.kill() proc.wait(timeout=3) for pipe in (proc.stdout, proc.stderr): if pipe is not None: pipe.close() def test_pty_keypress_cancels_countdown(self): master_fd, slave_fd = pty.openpty() proc = subprocess.Popen( [TIMER_SH, "timerrun", "10m"], env=self.env, stdin=slave_fd, stdout=slave_fd, stderr=subprocess.PIPE, close_fds=True, ) os.close(slave_fd) output = b"" try: deadline = time.monotonic() + 3.0 while time.monotonic() < deadline: rlist, _, _ = select.select([master_fd], [], [], 0.2) if rlist: output += os.read(master_fd, 4096) if b"Timer: 10m" in output: break self.assertIn(b"Timer: 10m", output) os.write(master_fd, b"q") rc = proc.wait(timeout=4) self.assertEqual(rc, 0) err = proc.stderr.read().decode("utf-8", errors="replace") self.assertEqual(err.strip(), "") finally: os.close(master_fd) if proc.stderr is not None: proc.stderr.close() if proc.poll() is None: proc.kill() proc.wait(timeout=3) def test_pty_expiry_any_key_and_sighup_clean(self): master_fd, slave_fd = pty.openpty() proc = subprocess.Popen( [TIMER_SH, "timerrun", "1s"], env=self.env, stdin=slave_fd, stdout=slave_fd, stderr=subprocess.PIPE, close_fds=True, ) os.close(slave_fd) try: deadline = time.monotonic() + 4.0 while time.monotonic() < deadline and not os.path.exists(self.ring_flag): rlist, _, _ = select.select([master_fd], [], [], 0.1) if rlist: os.read(master_fd, 4096) self.assertTrue(os.path.exists(self.ring_flag), "ring flag must appear on PTY expiry") os.write(master_fd, b"\r") rc = proc.wait(timeout=4) self.assertEqual(rc, 0) err = proc.stderr.read().decode("utf-8", errors="replace") self.assertEqual(err.strip(), "") self.assertFalse(os.path.exists(self.ring_flag), "keypress must clear ring flag") finally: os.close(master_fd) if proc.stderr is not None: proc.stderr.close() if proc.poll() is None: proc.kill() proc.wait(timeout=3) proc_hup = subprocess.Popen( [TIMER_SH, "timerrun", "10m"], env=self.env, stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True, ) try: timer_dir = self.env["SXMO_TIMER_DIR"] deadline = time.monotonic() + 3.0 while time.monotonic() < deadline: if os.path.isdir(timer_dir) and os.listdir(timer_dir): break time.sleep(0.05) self.assertTrue(os.listdir(timer_dir), "expected timer state file before SIGHUP") proc_hup.send_signal(1) _, err_hup = proc_hup.communicate(timeout=4) self.assertEqual(err_hup.strip(), "") self.assertEqual([f for f in os.listdir(timer_dir) if f.endswith(".timer")], []) finally: if proc_hup.poll() is None: proc_hup.kill() proc_hup.wait(timeout=3) if proc_hup.stderr is not None: proc_hup.stderr.close() if __name__ == "__main__": unittest.main()