import importlib.util,json,os,tempfile,unittest from pathlib import Path from unittest.mock import patch spec=importlib.util.spec_from_file_location('gamemode',Path(__file__).resolve().parents[1]/'quickshell/helpers/gamemode.py') gm=importlib.util.module_from_spec(spec);spec.loader.exec_module(gm) class GameModeTests(unittest.TestCase): def setUp(self): # HyprGlass is not loaded here, and the user has shadows off. self.state={'animations:enabled':1,'decoration:blur:enabled':1,'decoration:shadow:enabled':0} def env(self,temp,sig='fixture'): return patch.dict(os.environ,{'XDG_STATE_HOME':temp,'HYPRLAND_INSTANCE_SIGNATURE':sig}) def fakes(self,profile='balanced'): self.profile=profile def setp(name):self.profile=name return patch.multiple(gm,get_many=lambda keys:{k:self.state[k] for k in keys if k in self.state},put_many=lambda values:self.state.update(values),get_profile=lambda:self.profile,set_profile=setp) def test_on_off_restores_exact_values(self): with tempfile.TemporaryDirectory() as temp,self.env(temp): with self.fakes(): original=dict(self.state) self.assertTrue(gm.dispatch({'action':'on'})['active']) self.assertEqual(set(self.state.values()),{0}) self.assertEqual(self.profile,'performance') self.assertNotIn('plugin:hyprglass:enabled',self.state) self.assertTrue(gm.dispatch({'action':'status'})['active']) self.assertFalse(gm.dispatch({'action':'off'})['active']) self.assertEqual(self.state,original) self.assertEqual(self.profile,'balanced') self.assertFalse(gm.dispatch({'action':'status'})['active']) def test_on_twice_keeps_first_journal(self): with tempfile.TemporaryDirectory() as temp,self.env(temp): with self.fakes(): gm.dispatch({'action':'on'});gm.dispatch({'action':'on'}) gm.dispatch({'action':'off'}) self.assertEqual(self.state['animations:enabled'],1) def test_crash_recovery_and_other_sessions(self): with tempfile.TemporaryDirectory() as temp: with self.fakes(): with self.env(temp,'old'):gm.dispatch({'action':'on'}) # A new Hyprland session never applies the old journal. with self.env(temp,'new'): self.assertFalse(gm.dispatch({'action':'status'})['active']) gm.dispatch({'action':'off'}) self.assertEqual(self.state['animations:enabled'],0) # The same session (shell restarted) restores it. with self.env(temp,'old'): gm.dispatch({'action':'off'}) self.assertEqual(self.state['animations:enabled'],1) def test_options_and_no_profiles_daemon(self): with tempfile.TemporaryDirectory() as temp,self.env(temp): with self.fakes(profile=None): gm.dispatch({'action':'on','effects':False,'performance':True}) self.assertEqual(self.state['animations:enabled'],1) self.assertIsNone(self.profile) gm.dispatch({'action':'off'}) with self.fakes(): gm.dispatch({'action':'on','effects':True,'performance':False}) self.assertEqual(self.profile,'balanced') gm.dispatch({'action':'off'}) self.assertEqual(self.state['animations:enabled'],1) def test_parses_missing_options(self): out='{"option": "animations:enabled", "int": 1, "set": true }\n\n\nno such option\n\n{"option": "decoration:blur:enabled", "int": 0, "set": true }\n' with patch.object(gm,'run',lambda args:out): self.assertEqual(gm.get_many(['animations:enabled','plugin:hyprglass:enabled','decoration:blur:enabled']),{'animations:enabled':1,'decoration:blur:enabled':0}) def test_rejected_keyword_raises(self): with patch.object(gm,'run',lambda args:'ok\nconfig option does not exist.'): with self.assertRaises(ValueError):gm.put_many({'x':0,'y':0}) def test_unknown_action(self): with tempfile.TemporaryDirectory() as temp,self.env(temp): with self.assertRaises(ValueError):gm.dispatch({'action':'nope'}) if __name__=='__main__':unittest.main()