883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
99 lines
6.8 KiB
Python
99 lines
6.8 KiB
Python
"""Offline launcher->fresh-start transition; no GUI, process, account or network."""
|
|
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import tempfile
|
|
from types import SimpleNamespace
|
|
import unittest
|
|
from unittest.mock import patch
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]/'ops'))
|
|
import client_ui
|
|
import forever_launcher_backend as backend
|
|
import start_native_client as start
|
|
|
|
|
|
class StopAfterStartup(Exception):pass
|
|
|
|
|
|
class BackgroundFreshStartTest(unittest.TestCase):
|
|
def main_window(self,pid=None):
|
|
return dict(id=10,pid=os.getpid() if pid is None else pid,classes=('wowb.exe',),width=1280,height=720,mapped=True,transient=None)
|
|
|
|
def test_local_portal_preflight_precedes_owner_launch(self):
|
|
with patch.object(sys,'argv',['start_native_client.py','--endpoint-profile','local']), \
|
|
patch.object(start,'local_listeners',return_value={3724,8090}), \
|
|
patch.object(start,'require_local_turtle_ports') as ports, \
|
|
patch('engine_login_input_binding.isolated_modern_fallback',side_effect=ValueError('nonlocal portal')) as portal, \
|
|
patch.object(start,'running_clients') as clients, \
|
|
patch.object(start.subprocess,'Popen') as spawn:
|
|
with self.assertRaisesRegex(ValueError,'nonlocal portal'):
|
|
start.main()
|
|
ports.assert_called_once_with({3724,8090});portal.assert_called_once_with()
|
|
clients.assert_not_called();spawn.assert_not_called()
|
|
|
|
def test_public_start_does_not_run_local_portal_preflight(self):
|
|
with patch.object(sys,'argv',['start_native_client.py','--endpoint-profile','octo']), \
|
|
patch.object(start,'local_listeners',return_value=set()), \
|
|
patch('engine_login_input_binding.isolated_modern_fallback') as portal, \
|
|
patch.object(start,'running_clients',side_effect=StopAfterStartup):
|
|
with self.assertRaises(StopAfterStartup):start.main()
|
|
portal.assert_not_called()
|
|
|
|
def test_launcher_fresh_command_reaches_native_metadata_without_session_focus_or_capture(self):
|
|
with tempfile.TemporaryDirectory() as tmp,contextlib.ExitStack() as stack:
|
|
root=Path(tmp);(root/'runtime').mkdir();output=io.StringIO();closed=[];child_commands=[]
|
|
stack.enter_context(patch.object(start,'ROOT',root));stack.enter_context(patch.object(backend,'ROOT',root))
|
|
stack.enter_context(patch.object(start,'owned_queue_entry_executable',return_value=True))
|
|
stack.enter_context(patch.object(start,'COPY',root/'runtime/native-client'))
|
|
stack.enter_context(patch.object(start,'local_listeners',return_value={}))
|
|
stack.enter_context(patch.object(start,'running_clients',return_value=[]))
|
|
stack.enter_context(patch.object(start,'client_debug_socket',return_value=root/'runtime/owner.sock'))
|
|
stack.enter_context(patch.object(start,'Session',side_effect=AssertionError('Background fresh path requested Session/focus')))
|
|
stack.enter_context(patch('Xlib.display.Display',return_value=SimpleNamespace(close=lambda:closed.append(True))))
|
|
stack.enter_context(patch.object(client_ui,'window_rows',return_value=[self.main_window()]))
|
|
stack.enter_context(patch.object(client_ui,'capture_text',side_effect=AssertionError('Capture forbidden')))
|
|
stack.enter_context(patch.object(client_ui,'ensure_focus',side_effect=AssertionError('Focus forbidden')))
|
|
def owner(argv,**kw):
|
|
kw['stdout'].write(b'Code diagnostic owner ready; fixture child\n');kw['stdout'].flush()
|
|
(root/'runtime/native-client.pid').write_text(str(os.getpid()))
|
|
return SimpleNamespace(pid=987654,poll=lambda:None)
|
|
stack.enter_context(patch.object(start.subprocess,'Popen',side_effect=owner))
|
|
def child_run(argv,**kw):
|
|
child_commands.append(argv)
|
|
with patch.object(sys,'argv',[argv[2],*argv[3:]]),contextlib.redirect_stdout(output):start.main()
|
|
raise StopAfterStartup
|
|
stack.enter_context(patch.object(backend.subprocess,'run',side_effect=child_run))
|
|
stack.enter_context(patch.object(backend,'emit'))
|
|
with self.assertRaises(StopAfterStartup):backend.supervise(SimpleNamespace(endpoint_profile='octo',character_guid=None,existing_client_pid=None),io.StringIO())
|
|
row=json.loads(output.getvalue());self.assertEqual(row['state'],'native-main-window-ready')
|
|
self.assertFalse(row.get('authentication_started',False));self.assertIn('--background-manual-arm',child_commands[0]);self.assertEqual(closed,[True])
|
|
|
|
def test_protected_queue_defers_until_naturally_executable(self):
|
|
with patch('Xlib.display.Display',return_value=SimpleNamespace(close=lambda:None)),patch.object(client_ui,'window_rows',return_value=[self.main_window()]),patch.object(start,'owned_queue_entry_executable',side_effect=[False,True]) as ready,patch.object(start.time,'sleep'):
|
|
self.assertEqual(start.wait_background_native_window(os.getpid()),'native-main-window-ready')
|
|
self.assertEqual(ready.call_count,2)
|
|
|
|
def test_protected_queue_timeout_preserves_client_without_install(self):
|
|
with patch('Xlib.display.Display',return_value=SimpleNamespace(close=lambda:None)),patch.object(client_ui,'window_rows',return_value=[self.main_window()]),patch.object(start,'owned_queue_entry_executable',return_value=False):
|
|
with self.assertRaisesRegex(RuntimeError,'original queue entry not executable'):
|
|
start.wait_background_native_window(os.getpid(),timeout=0)
|
|
|
|
def test_native_modal_still_blocks_background_readiness(self):
|
|
rows=[self.main_window(),dict(id=11,pid=os.getpid(),classes=('wowb.exe',),width=300,height=180,mapped=True,transient=10)]
|
|
with patch('Xlib.display.Display',return_value=SimpleNamespace(close=lambda:None)),patch.object(client_ui,'window_rows',return_value=rows):
|
|
with self.assertRaises(client_ui.ClientWindowBlocked):start.wait_background_native_window(os.getpid(),timeout=0)
|
|
|
|
def test_another_clients_surface_cannot_satisfy_owned_readiness(self):
|
|
with patch('Xlib.display.Display',return_value=SimpleNamespace(close=lambda:None)),patch.object(client_ui,'window_rows',return_value=[self.main_window(os.getpid()+1)]):
|
|
with self.assertRaisesRegex(RuntimeError,'readiness timed out'):start.wait_background_native_window(os.getpid(),timeout=0)
|
|
|
|
def test_ended_process_is_preserved_as_failure_before_window_read(self):
|
|
with patch('Xlib.display.Display',return_value=SimpleNamespace(close=lambda:None)),patch.object(client_ui,'window_rows',side_effect=AssertionError('Ended process queried')):
|
|
with self.assertRaises(ProcessLookupError):start.wait_background_native_window(2147483647,timeout=0)
|
|
|
|
|
|
if __name__=='__main__':unittest.main()
|