"""W1: exercise the shared implementations, mocking only external boundaries.""" from contextlib import ExitStack from datetime import date from pathlib import Path from tempfile import TemporaryDirectory from types import SimpleNamespace import unittest from unittest.mock import Mock, patch from plugins import ita_plugin as ita from test.ita.ita_test_fakes import FakeChromiumOptions, FakeKeyboard, FakeMouse, FakePage from utils import plugin_utils as shared from vs_types import VSAccount, VSPlgConfig class SharedInitializationTests(unittest.TestCase): def test_real_shared_options_cover_direct_and_authenticated_proxy(self): for proxy in (None, SimpleNamespace(ip="proxy.test", port=8080, proto="http", username="", password=""), SimpleNamespace(ip="proxy.test", port=8080, proto="http", username="fixture", password="synthetic")): with self.subTest(authenticated=bool(proxy and proxy.username)), ExitStack() as stack: options, tunnel = Mock(), Mock() tunnel.start.return_value = "http://127.0.0.1:9901" stack.enter_context(patch.object(shared, "ChromiumOptions", return_value=options)) stack.enter_context(patch.object(shared, "get_free_port", return_value=9900)) factory = stack.enter_context(patch.object(shared, "MihomoTunnel", return_value=tunnel)) fp = stack.enter_context(patch.object(shared, "FingerprintGenerator")) fp.return_value.generate.return_value = {"seed": 9, "platform": "macos", "brand": "Chrome"} stack.enter_context(patch.object(shared.configure, "CHROME_PATH", "/fixture/chrome")) stack.enter_context(patch.object(shared.configure, "MIHOMO_BIN_PATH", "/fixture/mihomo")) stack.enter_context(patch.object(shared.configure, "MIHOMO_RELAY_NODES", None)) stack.enter_context(patch.object(shared.os.path, "exists", return_value=True)) actual_options, actual_tunnel = shared.init_chromium_options( "/fixture/profile", username_seed="fixture@example.test", proxy=proxy) self.assertIs(actual_options, options) options.set_local_port.assert_called_once_with(9900) options.set_user_data_path.assert_called_once_with("/fixture/profile") options.set_paths.assert_called_once_with(browser_path="/fixture/chrome") options.headless.assert_called_once_with(False) options.set_argument.assert_any_call("--window-size=1920,1080") fp.return_value.generate.assert_called_once_with("fixture@example.test") if proxy and proxy.username: self.assertIs(actual_tunnel, tunnel) factory.assert_called_once() options.set_argument.assert_any_call("--proxy-server=http://127.0.0.1:9901") else: self.assertIsNone(actual_tunnel) factory.assert_not_called() if proxy: options.set_argument.assert_any_call("--proxy-server=http://proxy.test:8080") def test_italy_calls_real_shared_initializer_and_creates_only_one_page(self): page = FakePage() with TemporaryDirectory() as workspace, ExitStack() as stack: stack.enter_context(patch.object(ita.os.path, "abspath", return_value=workspace)) plugin = ita.ItaPlugin("offline-shared-init") plugin.set_log(Mock()) plugin.set_config(VSPlgConfig(account=VSAccount(username="fixture@example.test", password="synthetic"))) stack.enter_context(patch.object(shared, "ChromiumOptions", FakeChromiumOptions)) stack.enter_context(patch.object(shared, "get_free_port", return_value=9900)) initialize = stack.enter_context(patch.object(ita, "init_chromium_options", wraps=shared.init_chromium_options)) create = stack.enter_context(patch.object(ita, "ChromiumPage", return_value=page)) stack.enter_context(patch.object(ita, "HumanMouse", FakeMouse)) stack.enter_context(patch.object(ita, "HumanKeyboard", FakeKeyboard)) stack.enter_context(patch.object(ita.time, "sleep")) try: plugin.create_session() self.assertTrue(plugin.is_healthy) initialize.assert_called_once_with( user_data_path=plugin.user_data_path, username_seed=plugin.config.account.username, proxy=plugin.config.proxy, headless=False, window_size="1920,1080", logger=plugin._log) create.assert_called_once() self.assertEqual(page.clicked.count("submit"), 1) finally: plugin.cleanup() class SharedCleanupTests(unittest.TestCase): def test_legacy_positional_call_still_swallows_failures_and_cleans_other_resources(self): # Exact positional signature used by VFS/TLS; no opt-in behavior change. with TemporaryDirectory() as parent: root = Path(parent) / "workspace" root.mkdir() page, tunnel = Mock(), Mock() page.quit.side_effect = RuntimeError("synthetic") tunnel.stop.side_effect = RuntimeError("synthetic") with patch.object(shared.time, "sleep"): self.assertIsNone(shared.cleanup_browser_workspace(page, str(root), tunnel, Mock())) page.quit.assert_called_once_with(force=True) tunnel.stop.assert_called_once() self.assertFalse(root.exists()) def test_italy_cleanup_uses_the_unchanged_shared_contract(self): with TemporaryDirectory() as parent, ExitStack() as stack: root = Path(parent) / "workspace" root.mkdir() plugin = ita.ItaPlugin.__new__(ita.ItaPlugin) plugin.page, plugin.tunnel = Mock(), Mock() page, tunnel = plugin.page, plugin.tunnel plugin.root_workspace = str(root) plugin.is_healthy = True plugin._prepared_calendar = object() plugin._log = Mock() cleanup = stack.enter_context(patch.object( ita, "cleanup_browser_workspace", wraps=shared.cleanup_browser_workspace, )) stack.enter_context(patch.object(shared.time, "sleep")) plugin.cleanup() cleanup.assert_called_once_with(page, str(root), tunnel, plugin._log) self.assertFalse(root.exists()) self.assertFalse(plugin.is_healthy) class SharedDateTests(unittest.TestCase): def test_positive_days_use_shared_filter_without_shuffle_or_missing_bound(self): days = tuple(ita.CalendarDay(date(2026, 10, n), 1, 0) for n in (20, 13, 9)) for bounds, expected in (({}, (9, 13, 20)), ({"start_day": date(2026, 10, 13)}, (13, 20)), ({"end_day": date(2026, 10, 13)}, (9, 13))): with self.subTest(bounds=bounds), patch.object(ita, "filter_dates_in_range", wraps=shared.filter_dates_in_range) as select: actual = ita.available_calendar_days(days, **bounds) self.assertEqual(tuple(d.day.day for d in actual), expected) select.assert_called_once() self.assertIs(select.call_args.kwargs["shuffle"], False) self.assertTrue(select.call_args.args[1]) self.assertTrue(select.call_args.args[2]) if __name__ == "__main__": unittest.main()