883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
97 lines
3.7 KiB
Python
97 lines
3.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Read-only startup focus pause without X, clients or credentials."""
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
from types import SimpleNamespace
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'ops'))
|
|
from owned_startup_focus import octo_focus_race, wait_for_owned_startup_focus
|
|
|
|
|
|
class FakeUI:
|
|
def __init__(self, error=None):
|
|
self.error = error
|
|
self.octo = True
|
|
self.ours = False
|
|
self.target_id = 42
|
|
self.calls = []
|
|
|
|
def ensure_focus(self, *_):
|
|
self.calls.append('ensure')
|
|
if self.error:
|
|
raise RuntimeError(self.error)
|
|
|
|
def owns_input(self, *_):
|
|
self.calls.append('read-owned')
|
|
return self.ours
|
|
|
|
def official_octo_focused(self, *_):
|
|
self.calls.append('read-octo')
|
|
return self.octo
|
|
|
|
def target(self, *_):
|
|
self.calls.append('read-target')
|
|
return SimpleNamespace(id=self.target_id)
|
|
|
|
|
|
class StartupFocusTest(unittest.TestCase):
|
|
guard = 'Official Octo is focused; pause automation until a fresh ten-second notice and explicit focus transfer'
|
|
|
|
def invoke(self, ui, *, alive=lambda: True, transfer=True, target=42, timeout=3):
|
|
now = [0.0]
|
|
events = []
|
|
def sleep(seconds):
|
|
now[0] += seconds
|
|
if transfer:
|
|
ui.octo = False
|
|
ui.ours = True
|
|
ui.target_id = target
|
|
duration = wait_for_owned_startup_focus(
|
|
ui=ui, display=object(), window=SimpleNamespace(id=42),
|
|
owned_alive=alive, stage=lambda event, **payload: events.append((event, payload)),
|
|
context=lambda: {'credential_submissions': 0, 'expected_source_guid': 4509},
|
|
timeout=timeout, clock=lambda: now[0], sleep=sleep)
|
|
return duration, events
|
|
|
|
def test_normal_focus_does_not_pause_or_emit_input(self):
|
|
ui = FakeUI()
|
|
duration, events = self.invoke(ui)
|
|
self.assertEqual(duration, 0)
|
|
self.assertEqual(events, [])
|
|
self.assertEqual(ui.calls, ['ensure'])
|
|
|
|
def test_octo_focus_pauses_until_external_owned_transfer(self):
|
|
ui = FakeUI(self.guard)
|
|
duration, events = self.invoke(ui)
|
|
self.assertGreater(duration, 0)
|
|
self.assertEqual([x[0] for x in events], ['startup_focus_required', 'startup_focus_resumed'])
|
|
self.assertEqual(ui.calls.count('ensure'), 1)
|
|
self.assertTrue(set(ui.calls) <= {'ensure', 'read-owned', 'read-octo', 'read-target'})
|
|
self.assertEqual(events[-1][1]['credential_submissions'], 0)
|
|
|
|
def test_unrelated_errors_and_changed_window_fail_closed(self):
|
|
with self.assertRaisesRegex(RuntimeError, 'different failure'):
|
|
self.invoke(FakeUI('different failure'))
|
|
with self.assertRaisesRegex(RuntimeError, 'main window changed'):
|
|
self.invoke(FakeUI(self.guard), target=43)
|
|
|
|
def test_client_exit_and_timeout_never_activate_or_retry(self):
|
|
with self.assertRaisesRegex(RuntimeError, 'client exited'):
|
|
self.invoke(FakeUI(self.guard), alive=lambda: False)
|
|
ui = FakeUI(self.guard)
|
|
with self.assertRaisesRegex(RuntimeError, 'timed out'):
|
|
self.invoke(ui, transfer=False, timeout=1)
|
|
self.assertEqual(ui.calls.count('ensure'), 1)
|
|
|
|
def test_only_known_focus_races_are_recoverable_while_octo_focused(self):
|
|
ui = FakeUI()
|
|
self.assertTrue(octo_focus_race(RuntimeError('Focus changed during capture; image discarded'), ui=ui, display=None))
|
|
self.assertFalse(octo_focus_race(RuntimeError('different failure'), ui=ui, display=None))
|
|
ui.octo = False
|
|
self.assertFalse(octo_focus_race(RuntimeError('WowB lost focus; input stopped'), ui=ui, display=None))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|