| from pathlib import Path | |
| import pytest | |
| from backend.input_files import resolve_uploaded_name, resolve_uploaded_path | |
| def test_resolve_uploaded_path_accepts_string_path(tmp_path: Path) -> None: | |
| path = tmp_path / "book.epub" | |
| path.write_text("x", encoding="utf-8") | |
| resolved = resolve_uploaded_path(str(path)) | |
| assert resolved == path | |
| def test_resolve_uploaded_path_accepts_gradio_dict_payload(tmp_path: Path) -> None: | |
| path = tmp_path / "book.epub" | |
| path.write_text("x", encoding="utf-8") | |
| resolved = resolve_uploaded_path({"path": str(path), "meta": {"_type": "gradio.FileData"}}) | |
| assert resolved == path | |
| def test_resolve_uploaded_path_rejects_missing_path_key() -> None: | |
| with pytest.raises(TypeError, match="Unsupported uploaded file payload"): | |
| resolve_uploaded_path({"url": "/tmp/book.epub"}) | |
| def test_resolve_uploaded_name_prefers_original_filename() -> None: | |
| name = resolve_uploaded_name( | |
| {"path": "/tmp/gradio/abcd", "orig_name": "real-book.epub", "meta": {"_type": "gradio.FileData"}} | |
| ) | |
| assert name == "real-book.epub" | |
| def test_resolve_uploaded_name_falls_back_to_path_name(tmp_path: Path) -> None: | |
| path = tmp_path / "fallback.epub" | |
| path.write_text("x", encoding="utf-8") | |
| name = resolve_uploaded_name(str(path)) | |
| assert name == "fallback.epub" | |