Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
| from __future__ import annotations | |
| from types import SimpleNamespace | |
| from app import ( | |
| _request_key, | |
| _view_values, | |
| begin_run, | |
| begin_upload, | |
| change_zoom, | |
| clear_run_progress, | |
| ) | |
| from ui_helpers import EMPTY_TEXT | |
| def request(*, headers=None, host="fallback") -> SimpleNamespace: | |
| return SimpleNamespace( | |
| headers=headers or {}, | |
| client=SimpleNamespace(host=host) if host else None, | |
| ) | |
| def test_request_key_prefers_forwarded_client_address() -> None: | |
| assert ( | |
| _request_key( | |
| request( | |
| headers={ | |
| "x-forwarded-for": "203.0.113.5, 10.0.0.1", | |
| "x-real-ip": "198.51.100.1", | |
| } | |
| ) | |
| ) | |
| == "203.0.113.5" | |
| ) | |
| def test_request_key_falls_back_to_real_ip_then_client() -> None: | |
| assert ( | |
| _request_key(request(headers={"x-real-ip": "198.51.100.1"})) | |
| == "198.51.100.1" | |
| ) | |
| assert _request_key(request(host="127.0.0.1")) == "127.0.0.1" | |
| assert _request_key(None) == "unknown" | |
| def test_begin_upload_shows_persistent_preparation_status() -> None: | |
| update = begin_upload("/tmp/document.pdf") | |
| assert update.visible is True | |
| assert "Preparing document" in update.value | |
| def test_begin_run_disables_button_and_shows_first_unparsed_page() -> None: | |
| state = { | |
| "pages": [{}, {}, {}], | |
| "results": [{"text": "done"}, None, None], | |
| "current_page": 0, | |
| } | |
| status, button, text_output, tabs = begin_run(state) | |
| assert status.visible is False | |
| assert button.interactive is False | |
| assert "Parsing document" in text_output | |
| assert "Processing page 2 / 3" in text_output | |
| assert tabs.visible is True | |
| def test_clear_run_progress_restores_controls() -> None: | |
| status, button, text_output, tabs = clear_run_progress() | |
| assert status.visible is False | |
| assert button.interactive is True | |
| assert text_output == EMPTY_TEXT | |
| assert tabs.visible is False | |
| def test_change_zoom_bounds_scale_without_a_document() -> None: | |
| scale, viewer, label = change_zoom(None, 95, 10, False) | |
| assert scale == 100 | |
| assert "100%" in label | |
| scale, viewer, label = change_zoom(None, 10, -10, False) | |
| assert scale == 10 | |
| assert "10%" in label | |
| def test_view_values_hide_an_empty_page_warning() -> None: | |
| state = { | |
| "name": "sample.pdf", | |
| "size": 100, | |
| "kind": "pdf", | |
| "total_pages": 1, | |
| "current_page": 0, | |
| "pages": [{"number": 1, "png": b"image", "width": 100, "height": 200}], | |
| "results": [None], | |
| } | |
| warning = _view_values(state, False, True, 50)[2] | |
| assert warning.visible is False | |
| assert warning.value == "" | |