|
| 1 | +"""Tests for the pre-spawn helper in cli.py and the SSMSTUI kwarg plumbing.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +from sqlit.cli import _prewarm_process_worker |
| 8 | +from sqlit.shared.app.runtime import MockConfig, RuntimeConfig |
| 9 | + |
| 10 | + |
| 11 | +def test_prewarm_skipped_when_worker_disabled() -> None: |
| 12 | + runtime = RuntimeConfig(process_worker=False) |
| 13 | + with patch("sqlit.domains.process_worker.app.process_worker_client.ProcessWorkerClient") as klass: |
| 14 | + result = _prewarm_process_worker(runtime) |
| 15 | + assert result is None |
| 16 | + klass.assert_not_called() |
| 17 | + |
| 18 | + |
| 19 | +def test_prewarm_skipped_when_mock_enabled() -> None: |
| 20 | + runtime = RuntimeConfig(process_worker=True, mock=MockConfig(enabled=True)) |
| 21 | + with patch("sqlit.domains.process_worker.app.process_worker_client.ProcessWorkerClient") as klass: |
| 22 | + result = _prewarm_process_worker(runtime) |
| 23 | + assert result is None |
| 24 | + klass.assert_not_called() |
| 25 | + |
| 26 | + |
| 27 | +def test_prewarm_returns_client_when_enabled() -> None: |
| 28 | + runtime = RuntimeConfig(process_worker=True) |
| 29 | + sentinel = MagicMock(name="process-worker-client") |
| 30 | + with patch( |
| 31 | + "sqlit.domains.process_worker.app.process_worker_client.ProcessWorkerClient", |
| 32 | + return_value=sentinel, |
| 33 | + ): |
| 34 | + result = _prewarm_process_worker(runtime) |
| 35 | + assert result is sentinel |
| 36 | + |
| 37 | + |
| 38 | +def test_prewarm_swallows_spawn_errors() -> None: |
| 39 | + """A failing pre-spawn must not break startup; lazy path handles fallback.""" |
| 40 | + runtime = RuntimeConfig(process_worker=True) |
| 41 | + with patch( |
| 42 | + "sqlit.domains.process_worker.app.process_worker_client.ProcessWorkerClient", |
| 43 | + side_effect=ValueError("bad value(s) in fds_to_keep"), |
| 44 | + ): |
| 45 | + result = _prewarm_process_worker(runtime) |
| 46 | + assert result is None |
| 47 | + |
| 48 | + |
| 49 | +def test_ssmstui_stashes_prewarmed_worker() -> None: |
| 50 | + """A client passed via the kwarg should land on self._process_worker_client. |
| 51 | +
|
| 52 | + ProcessWorkerLifecycleMixin returns that attribute first, so this is |
| 53 | + what makes the pre-spawn actually short-circuit the lazy path. |
| 54 | + """ |
| 55 | + from sqlit.domains.shell.app.main import SSMSTUI |
| 56 | + from tests.ui.mocks import MockConnectionStore, MockSettingsStore, build_test_services, create_test_connection |
| 57 | + |
| 58 | + services = build_test_services( |
| 59 | + connection_store=MockConnectionStore([create_test_connection("test-db", "sqlite")]), |
| 60 | + settings_store=MockSettingsStore({}), |
| 61 | + ) |
| 62 | + sentinel = MagicMock(name="process-worker-client") |
| 63 | + app = SSMSTUI(services=services, process_worker_client=sentinel) |
| 64 | + assert app._process_worker_client is sentinel |
0 commit comments