Test application startup and shutdown sequence.
()
| 134 | |
| 135 | @pytest.mark.asyncio |
| 136 | async def test_lifespan_startup_shutdown(): |
| 137 | """Test application startup and shutdown sequence.""" |
| 138 | app_mock = MagicMock() |
| 139 | |
| 140 | # Set state.is_page_ready before the test |
| 141 | state.is_page_ready = True |
| 142 | mock_logger = MagicMock() |
| 143 | state.logger = mock_logger |
| 144 | |
| 145 | # Mock all the dependencies |
| 146 | with ( |
| 147 | patch("api_utils.app._setup_logging") as mock_setup_logging, |
| 148 | patch("api_utils.app._initialize_globals") as mock_init_globals, |
| 149 | patch("api_utils.app._initialize_proxy_settings") as mock_init_proxy, |
| 150 | patch("api_utils.app.load_excluded_models") as mock_load_models, |
| 151 | patch( |
| 152 | "api_utils.app._start_stream_proxy", new_callable=AsyncMock |
| 153 | ) as mock_start_proxy, |
| 154 | patch( |
| 155 | "api_utils.app._initialize_browser_and_page", new_callable=AsyncMock |
| 156 | ) as mock_init_browser, |
| 157 | patch( |
| 158 | "api_utils.app._shutdown_resources", new_callable=AsyncMock |
| 159 | ) as mock_shutdown, |
| 160 | patch("api_utils.queue_worker", new_callable=AsyncMock), |
| 161 | patch("api_utils.app.restore_original_streams") as mock_restore_streams, |
| 162 | ): |
| 163 | mock_setup_logging.return_value = (MagicMock(), MagicMock()) |
| 164 | |
| 165 | # Get the lifespan context manager |
| 166 | from api_utils.app import lifespan |
| 167 | |
| 168 | async with lifespan(app_mock): |
| 169 | # Verify startup actions |
| 170 | mock_init_globals.assert_called_once() |
| 171 | mock_init_proxy.assert_called_once() |
| 172 | mock_load_models.assert_called_once() |
| 173 | mock_start_proxy.assert_called_once() |
| 174 | mock_init_browser.assert_called_once() |
| 175 | # Check actual log messages from the implementation |
| 176 | mock_logger.info.assert_any_call("Starting AI Studio Proxy Server...") |
| 177 | |
| 178 | # Verify shutdown actions |
| 179 | mock_shutdown.assert_called_once() |
| 180 | mock_restore_streams.assert_called() |
| 181 | mock_logger.info.assert_any_call("Shutting down server...") |
| 182 | |
| 183 | |
| 184 | @pytest.mark.asyncio |
nothing calls this directly
no test coverage detected