Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 2,715 Bytes
3530326 1a19508 f1f86cc 3530326 d8c1d40 1a19508 d8c1d40 f1f86cc d8c1d40 f1f86cc 1a19508 d8c1d40 1a19508 d8c1d40 f1f86cc 1a19508 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | 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 == ""
|