| 1 |
"""Tests for Hook, ScriptHook, and ScriptHookLoader.""" |
| 2 |
|
| 3 |
import os |
| 4 |
import time |
| 5 |
from unittest.mock import MagicMock |
| 6 |
|
| 7 |
from s_tui.sources.hook import Hook |
| 8 |
from s_tui.sources.hook_script import ScriptHook |
| 9 |
from s_tui.sources.script_hook_loader import ScriptHookLoader |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class TestHook: |
| 17 |
def test_init_callback_stored(self): |
| 18 |
"""Hook stores the callback.""" |
| 19 |
cb = MagicMock() |
| 20 |
h = Hook(cb) |
| 21 |
assert h.callback is cb |
| 22 |
|
| 23 |
def test_init_default_timeout_zero(self): |
| 24 |
"""Default timeout is 0 (no cooldown).""" |
| 25 |
h = Hook(MagicMock()) |
| 26 |
assert h.timeout_milliseconds == 0 |
| 27 |
|
| 28 |
def test_init_custom_timeout(self): |
| 29 |
"""Custom timeout is stored.""" |
| 30 |
h = Hook(MagicMock(), timeout_milliseconds=500) |
| 31 |
assert h.timeout_milliseconds == 500 |
| 32 |
|
| 33 |
def test_is_ready_initially_true(self): |
| 34 |
"""A freshly created hook should be ready immediately.""" |
| 35 |
h = Hook(MagicMock()) |
| 36 |
assert h.is_ready() is True |
| 37 |
|
| 38 |
def test_invoke_calls_callback(self): |
| 39 |
"""invoke() should call the callback with callback_args.""" |
| 40 |
cb = MagicMock() |
| 41 |
h = Hook(cb, 0, "arg1", "arg2") |
| 42 |
h.invoke() |
| 43 |
cb.assert_called_once_with(("arg1", "arg2")) |
| 44 |
|
| 45 |
def test_invoke_no_args(self): |
| 46 |
"""invoke() passes empty tuple when no extra args given.""" |
| 47 |
cb = MagicMock() |
| 48 |
h = Hook(cb) |
| 49 |
h.invoke() |
| 50 |
cb.assert_called_once_with(()) |
| 51 |
|
| 52 |
def test_invoke_no_cooldown_always_ready(self): |
| 53 |
"""With timeout_milliseconds=0 the hook is always ready.""" |
| 54 |
cb = MagicMock() |
| 55 |
h = Hook(cb, 0) |
| 56 |
h.invoke() |
| 57 |
assert h.is_ready() is True |
| 58 |
h.invoke() |
| 59 |
assert h.is_ready() is True |
| 60 |
|
| 61 |
def test_invoke_with_cooldown_sets_not_ready(self): |
| 62 |
"""After invoke with timeout>0, the hook should not be ready.""" |
| 63 |
cb = MagicMock() |
| 64 |
h = Hook(cb, timeout_milliseconds=5000) |
| 65 |
h.invoke() |
| 66 |
assert h.is_ready() is False |
| 67 |
|
| 68 |
def test_cooldown_expires(self): |
| 69 |
"""After cooldown expires the hook is ready again.""" |
| 70 |
cb = MagicMock() |
| 71 |
h = Hook(cb, timeout_milliseconds=50) |
| 72 |
h.invoke() |
| 73 |
assert h.is_ready() is False |
| 74 |
time.sleep(0.06) |
| 75 |
assert h.is_ready() is True |
| 76 |
|
| 77 |
def test_multiple_invokes_with_cooldown(self): |
| 78 |
"""Multiple invoke() calls extend the cooldown each time.""" |
| 79 |
cb = MagicMock() |
| 80 |
h = Hook(cb, timeout_milliseconds=50) |
| 81 |
h.invoke() |
| 82 |
time.sleep(0.06) |
| 83 |
assert h.is_ready() is True |
| 84 |
h.invoke() |
| 85 |
assert h.is_ready() is False |
| 86 |
assert cb.call_count == 2 |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
class TestScriptHook: |
| 95 |
def test_init_stores_path(self, tmp_path): |
| 96 |
"""ScriptHook stores the script path.""" |
| 97 |
script = tmp_path / "test.sh" |
| 98 |
script.write_text("#!/bin/sh\nexit 0\n") |
| 99 |
sh = ScriptHook(str(script)) |
| 100 |
assert sh.path == str(script) |
| 101 |
|
| 102 |
def test_is_ready_initially(self, tmp_path): |
| 103 |
"""ScriptHook is ready immediately after creation.""" |
| 104 |
script = tmp_path / "test.sh" |
| 105 |
script.write_text("#!/bin/sh\nexit 0\n") |
| 106 |
sh = ScriptHook(str(script)) |
| 107 |
assert sh.is_ready() is True |
| 108 |
|
| 109 |
def test_is_ready_with_timeout(self, tmp_path): |
| 110 |
"""ScriptHook respects timeout from underlying Hook.""" |
| 111 |
script = tmp_path / "test.sh" |
| 112 |
script.write_text("#!/bin/sh\nexit 0\n") |
| 113 |
sh = ScriptHook(str(script), timeout_milliseconds=5000) |
| 114 |
assert sh.is_ready() is True |
| 115 |
sh.invoke() |
| 116 |
assert sh.is_ready() is False |
| 117 |
|
| 118 |
def test_invoke_runs_script(self, tmp_path, mocker): |
| 119 |
"""invoke() triggers a subprocess call.""" |
| 120 |
script = tmp_path / "test.sh" |
| 121 |
script.write_text("#!/bin/sh\nexit 0\n") |
| 122 |
script.chmod(0o755) |
| 123 |
|
| 124 |
mock_popen = mocker.patch("subprocess.Popen") |
| 125 |
sh = ScriptHook(str(script)) |
| 126 |
sh.invoke() |
| 127 |
mock_popen.assert_called_once() |
| 128 |
|
| 129 |
args, _kwargs = mock_popen.call_args |
| 130 |
assert args[0] == ["/bin/sh", str(script)] |
| 131 |
|
| 132 |
def test_internal_hook_type(self, tmp_path): |
| 133 |
"""The internal hook attribute should be a Hook instance.""" |
| 134 |
script = tmp_path / "test.sh" |
| 135 |
script.write_text("#!/bin/sh\nexit 0\n") |
| 136 |
sh = ScriptHook(str(script)) |
| 137 |
assert isinstance(sh.hook, Hook) |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
class TestScriptHookLoader: |
| 146 |
def test_init_sets_hooks_dir(self, tmp_path): |
| 147 |
"""ScriptHookLoader computes the hooks.d subdirectory path.""" |
| 148 |
loader = ScriptHookLoader(str(tmp_path)) |
| 149 |
expected = os.path.join(str(tmp_path), "hooks.d") |
| 150 |
assert loader.scripts_dir_path == expected |
| 151 |
|
| 152 |
def test_source_to_script_name(self): |
| 153 |
"""_source_to_script_name converts source name to lowercase .sh.""" |
| 154 |
loader = ScriptHookLoader("/tmp") |
| 155 |
assert loader._source_to_script_name("TempSource") == "tempsource.sh" |
| 156 |
assert loader._source_to_script_name("UtilSource") == "utilsource.sh" |
| 157 |
assert loader._source_to_script_name("CPU Freq") == "cpu freq.sh" |
| 158 |
|
| 159 |
def test_load_script_returns_none_if_missing(self, tmp_path): |
| 160 |
"""load_script() returns None when the script file doesn't exist.""" |
| 161 |
hooks_dir = tmp_path / "hooks.d" |
| 162 |
hooks_dir.mkdir() |
| 163 |
loader = ScriptHookLoader(str(tmp_path)) |
| 164 |
result = loader.load_script("NoSource") |
| 165 |
assert result is None |
| 166 |
|
| 167 |
def test_load_script_returns_script_hook(self, tmp_path): |
| 168 |
"""load_script() returns a ScriptHook when the script file exists.""" |
| 169 |
hooks_dir = tmp_path / "hooks.d" |
| 170 |
hooks_dir.mkdir() |
| 171 |
script = hooks_dir / "tempsource.sh" |
| 172 |
script.write_text("#!/bin/sh\necho 'hot'\n") |
| 173 |
script.chmod(0o755) |
| 174 |
|
| 175 |
loader = ScriptHookLoader(str(tmp_path)) |
| 176 |
result = loader.load_script("TempSource") |
| 177 |
assert isinstance(result, ScriptHook) |
| 178 |
assert result.path == str(script) |
| 179 |
|
| 180 |
def test_load_script_with_timeout(self, tmp_path): |
| 181 |
"""load_script() passes timeout to ScriptHook.""" |
| 182 |
hooks_dir = tmp_path / "hooks.d" |
| 183 |
hooks_dir.mkdir() |
| 184 |
script = hooks_dir / "fansource.sh" |
| 185 |
script.write_text("#!/bin/sh\necho 'fan'\n") |
| 186 |
script.chmod(0o755) |
| 187 |
|
| 188 |
loader = ScriptHookLoader(str(tmp_path)) |
| 189 |
result = loader.load_script("FanSource", timeoutMilliseconds=1000) |
| 190 |
assert isinstance(result, ScriptHook) |
| 191 |
assert result.hook.timeout_milliseconds == 1000 |
| 192 |
|
| 193 |
def test_load_script_no_hooks_dir(self, tmp_path): |
| 194 |
"""load_script() returns None when hooks.d directory doesn't exist.""" |
| 195 |
loader = ScriptHookLoader(str(tmp_path)) |
| 196 |
result = loader.load_script("TempSource") |
| 197 |
assert result is None |