|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from banks import Prompt |
| 7 | +from banks.filters.document import _get_document_format_from_url, _is_url, document |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def tiny_pdf(): |
| 12 | + here = Path(__file__).parent |
| 13 | + return here / "data" / "1x1.pdf" |
| 14 | + |
| 15 | + |
| 16 | +def test_document_with_file_path(tiny_pdf): |
| 17 | + """Test document filter with a file path input""" |
| 18 | + result = document(str(tiny_pdf)) |
| 19 | + |
| 20 | + # Verify the content block wrapper |
| 21 | + assert result.startswith("<content_block>") |
| 22 | + assert result.endswith("</content_block>") |
| 23 | + |
| 24 | + # Parse the JSON content |
| 25 | + json_content = result[15:-16] # Remove wrapper tags |
| 26 | + content_block = json.loads(json_content) |
| 27 | + |
| 28 | + assert content_block["type"] == "document" |
| 29 | + assert content_block["input_document"]["format"].startswith("pdf") |
| 30 | + |
| 31 | + |
| 32 | +def test_document_with_nonexistent_file(): |
| 33 | + """Test document filter with a nonexistent file path""" |
| 34 | + with pytest.raises(FileNotFoundError): |
| 35 | + document("nonexistent/document.pdf") |
| 36 | + |
| 37 | + |
| 38 | +def test_document_with_url(): |
| 39 | + """Test document filter with a URL input (no filesystem access).""" |
| 40 | + url = "https://example.com/document.css" |
| 41 | + result = document(url) |
| 42 | + |
| 43 | + assert result.startswith("<content_block>") |
| 44 | + assert result.endswith("</content_block>") |
| 45 | + |
| 46 | + json_content = result[15:-16] # Remove wrapper tags |
| 47 | + content_block = json.loads(json_content) |
| 48 | + |
| 49 | + assert content_block["type"] == "document" |
| 50 | + assert content_block["input_document"]["data"] == url |
| 51 | + assert content_block["input_document"]["format"] == "css" |
| 52 | + |
| 53 | + |
| 54 | +def test_is_url_variants(): |
| 55 | + assert _is_url("relative/path.pdf") is False |
| 56 | + assert _is_url("https://example.com/document.pdf") is True |
| 57 | + assert _is_url("data:application/pdf;base64,AAAA") is True |
| 58 | + assert _is_url("data:text/plain;base64,AAAA") is True |
| 59 | + assert _is_url("data:audio/mp3;base64,AAAA") is False |
| 60 | + |
| 61 | + |
| 62 | +def test_get_document_format_from_url(): |
| 63 | + assert _get_document_format_from_url("https://example.com/document.WAV") == "pdf" |
| 64 | + assert _get_document_format_from_url("https://example.com/document") == "pdf" |
| 65 | + |
| 66 | + |
| 67 | +def test_document_no_chat_block(tiny_pdf): |
| 68 | + prompt = Prompt("{{ test }} and {{ another | document }}") |
| 69 | + messages = prompt.chat_messages({"test": "hello world", "another": str(tiny_pdf)}) |
| 70 | + assert len(messages) == 1 |
| 71 | + message = messages[0] |
| 72 | + assert len(message.content) == 2 |
| 73 | + assert message.content[0].text == "hello world and" # type: ignore |
| 74 | + assert message.content[1].type == "document" # type:ignore |
0 commit comments