#!/usr/bin/env python3 """System adapter boundaries. All commands and power capabilities are mocked.""" import importlib.util,json,subprocess,sys,tempfile,unittest from pathlib import Path from unittest.mock import patch ROOT=Path(__file__).resolve().parents[1] spec=importlib.util.spec_from_file_location('system_provider',ROOT/'quickshell/helpers/system.py') system=importlib.util.module_from_spec(spec);spec.loader.exec_module(system) OPTIONS=json.loads((ROOT/'quickshell/config/defaults.json').read_text())['system'] class Providers(unittest.TestCase): def test_power_requires_confirmation_before_any_command(self): with patch.object(system,'run') as run,patch.object(system,'power',return_value={'reboot':True,'shutdown':True,'logout':True}) as capability,patch.object(system,'snapshot',return_value={}): for value in ['reboot','shutdown','logout']: with self.assertRaisesRegex(ValueError,'confirmation'):system.action({'options':OPTIONS,'action':'power','value':value}) run.assert_not_called();capability.assert_not_called() system.action({'options':OPTIONS,'action':'power','value':'reboot','confirmed':True}) self.assertEqual(run.call_args.args[0][-3:],['Reboot','b','true']) with self.assertRaises(ValueError):system.action({'options':OPTIONS,'action':'power','value':'arbitrary','confirmed':True}) def test_validation_and_argv(self): for value in [-1,101,float('nan'),True,'40']: with self.assertRaises(ValueError):system.bounded(value,1,100) self.assertEqual(system.command(['tool','$(echo never); quoted','{value}','{enabled}'],value=40,enabled=False),['tool','$(echo never); quoted','40','false']) with self.assertRaises(ValueError):system.command('sh -c command') with patch.object(system,'run',return_value='{"value":75,"available":"false"}'): with self.assertRaises(ValueError):system.brightness({'statusCommand':['provider']}) with patch.object(system,'run',return_value='{"enabled":"false"}'): with self.assertRaises(ValueError):system.night({'statusCommand':['provider']}) def test_custom_providers_readback(self): opts=json.loads(json.dumps(OPTIONS));opts['brightness'].update(statusCommand=['status'],setCommand=['setter','--level','{value}']) with patch.object(system,'run',return_value='{"value":73,"available":true}') as run,patch.object(system,'snapshot',return_value={'brightness':{'available':True,'value':73}}): result=system.action({'options':opts,'action':'brightness','value':73}) run.assert_called_once_with(['setter','--level','73']) self.assertEqual(result['state']['brightness']['value'],73) def test_missing_providers_do_not_crash_snapshot(self): with patch.object(system,'run',side_effect=FileNotFoundError('missing tool')),patch.object(system,'backlights',return_value=[]),patch.object(system.shutil,'which',return_value=None): state=system.snapshot(OPTIONS) self.assertFalse(state['brightness']['available']);self.assertFalse(state['nightLight']['available']);self.assertFalse(state['profiles']['available']);self.assertFalse(state['dnd']['available']);self.assertFalse(state['power']['shutdown']) def test_unavailable_action_rejected(self): with patch.object(system,'power',return_value={'suspend':False}),patch.object(system,'run') as run: with self.assertRaises(ValueError):system.action({'options':OPTIONS,'action':'power','value':'suspend'}) run.assert_not_called() def test_dnd_never_activates_notification_daemon(self): with patch.object(system,'run',side_effect=['{"type":"u","data":[1]}','{"type":"b","data":[true]}']) as run: state=system.notifications() self.assertTrue(state['enabled']);self.assertEqual(state['provider'],'swaync') args=run.call_args.args[0] self.assertIn('--auto-start=no',args);self.assertEqual(args[-1],'GetDnd') with patch.object(system,'notifications',return_value={'available':True,'provider':'swaync'}),patch.object(system,'snapshot',return_value={}),patch.object(system,'run') as run: system.action({'options':OPTIONS,'action':'dnd','value':False}) self.assertIn('--auto-start=no',run.call_args.args[0]);self.assertEqual(run.call_args.args[0][-3:],['SetDnd','b','false']) def test_shell_owned_notifications_keep_dnd_in_the_shell(self): with patch.object(system,'run',return_value='{"type":"u","data":[%d]}'%system.os.getppid()) as run: self.assertEqual(system.notifications(),{'available':True,'provider':'shell'}) self.assertEqual(run.call_count,1) with patch.object(system,'notifications',return_value={'available':True,'provider':'shell'}),patch.object(system,'run') as run: with self.assertRaises(ValueError):system.action({'options':OPTIONS,'action':'dnd','value':True}) run.assert_not_called() def test_missing_notification_server_is_reported(self): with patch.object(system,'run',side_effect=ValueError('NameHasNoOwner')): self.assertFalse(system.notifications()['available']) def test_cli_errors_are_json(self): p=subprocess.run([sys.executable,str(ROOT/'quickshell/helpers/system.py'),'{"op":"unknown"}'],capture_output=True,text=True) self.assertFalse(json.loads(p.stdout)['ok']);self.assertEqual(p.returncode,1);self.assertEqual(p.stderr,'') if __name__=='__main__':unittest.main()