"""Tests for the M9/D12 game-launch watcher (RunningAppID parse + transitions).""" import tempfile import unittest from pathlib import Path from unittest import mock from rigdoctor.core import watcher _REGISTRY = """"Registry" { \t"HKCU" \t{ \t\t"Software" \t\t{ \t\t\t"Valve" \t\t\t{ \t\t\t\t"Steam" \t\t\t\t{ \t\t\t\t\t"RunningAppID"\t\t"%s" \t\t\t\t} \t\t\t} \t\t} \t} } """ class TransitionTests(unittest.TestCase): def test_transitions(self): self.assertEqual(watcher.transition(0, 570), "start") self.assertEqual(watcher.transition(570, 0), "stop") self.assertIsNone(watcher.transition(570, 570)) self.assertIsNone(watcher.transition(0, 0)) class FindKeyTests(unittest.TestCase): def test_case_insensitive_nested(self): data = {"Registry": {"HKCU": {"steam": {"runningappid": "42"}}}} self.assertEqual(watcher._find_key(data, "RunningAppID"), "42") def test_missing(self): self.assertIsNone(watcher._find_key({"a": {"b": "c"}}, "RunningAppID")) class RunningAppIdTests(unittest.TestCase): def _with_registry(self, content): d = tempfile.mkdtemp() path = Path(d) / "registry.vdf" path.write_text(content) return path def test_reads_running_appid(self): path = self._with_registry(_REGISTRY % "570") with mock.patch.object(watcher, "_registry_path", return_value=path): self.assertEqual(watcher.running_appid(), 570) def test_zero_when_idle(self): path = self._with_registry(_REGISTRY % "0") with mock.patch.object(watcher, "_registry_path", return_value=path): self.assertEqual(watcher.running_appid(), 0) def test_zero_when_no_registry(self): with mock.patch.object(watcher, "_registry_path", return_value=None): self.assertEqual(watcher.running_appid(), 0) if __name__ == "__main__": unittest.main()