|
| 1 | +"""Unit tests for POST /api/generate-pdf (api/pdf.py). Closes #72.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +PDF_MAGIC = b"%PDF-" |
| 9 | + |
| 10 | + |
| 11 | +def _post_pdf( |
| 12 | + client, |
| 13 | + *, |
| 14 | + markdown: str = "", |
| 15 | + title: str = "Chat", |
| 16 | + json_data: dict[str, Any] | None = None, |
| 17 | +): |
| 18 | + if json_data is not None: |
| 19 | + return client.post( |
| 20 | + "/api/generate-pdf", |
| 21 | + json=json_data, |
| 22 | + content_type="application/json", |
| 23 | + ) |
| 24 | + return client.post( |
| 25 | + "/api/generate-pdf", |
| 26 | + json={"markdown": markdown, "title": title}, |
| 27 | + content_type="application/json", |
| 28 | + ) |
| 29 | + |
| 30 | + |
| 31 | +def _assert_pdf_response(response) -> None: |
| 32 | + assert response.status_code == 200 |
| 33 | + assert response.content_type.startswith("application/pdf") |
| 34 | + data = response.data |
| 35 | + assert len(data) > 0 |
| 36 | + assert data.startswith(PDF_MAGIC) |
| 37 | + # Trailing %%EOF is a minimal structural check (see tests/web-ui-qa-checklist.md). |
| 38 | + assert b"%%EOF" in data[-1024:] |
| 39 | + |
| 40 | + |
| 41 | +class TestGeneratePdfHappyPath: |
| 42 | + def test_normal_conversation_markdown(self, pdf_client): |
| 43 | + md = """# Chat export |
| 44 | +
|
| 45 | +## User question |
| 46 | +
|
| 47 | +Please explain **recursion** in Python. |
| 48 | +
|
| 49 | +- Base case |
| 50 | +- Recursive step |
| 51 | +
|
| 52 | +```python |
| 53 | +def fact(n): |
| 54 | + return 1 if n < 2 else n * fact(n - 1) |
| 55 | +``` |
| 56 | +
|
| 57 | +--- |
| 58 | +""" |
| 59 | + response = _post_pdf(pdf_client, markdown=md, title="Happy conversation") |
| 60 | + _assert_pdf_response(response) |
| 61 | + assert ( |
| 62 | + 'attachment; filename="Happy conversation.pdf"' |
| 63 | + in response.headers.get("Content-Disposition", "") |
| 64 | + ) |
| 65 | + |
| 66 | + def test_empty_json_body_uses_defaults(self, pdf_client): |
| 67 | + response = _post_pdf(pdf_client, json_data={}) |
| 68 | + _assert_pdf_response(response) |
| 69 | + assert ( |
| 70 | + 'attachment; filename="Chat.pdf"' |
| 71 | + in response.headers.get("Content-Disposition", "") |
| 72 | + ) |
| 73 | + |
| 74 | + def test_unsafe_title_characters_sanitized_in_filename(self, pdf_client): |
| 75 | + response = _post_pdf( |
| 76 | + pdf_client, |
| 77 | + markdown="Hello", |
| 78 | + title='bad<>:"/\\|?*name', |
| 79 | + ) |
| 80 | + _assert_pdf_response(response) |
| 81 | + assert ( |
| 82 | + 'attachment; filename="bad_________name.pdf"' |
| 83 | + in response.headers.get("Content-Disposition", "") |
| 84 | + ) |
| 85 | + |
| 86 | + |
| 87 | +class TestGeneratePdfEdgeCases: |
| 88 | + def test_empty_markdown(self, pdf_client): |
| 89 | + response = _post_pdf(pdf_client, markdown="", title="Empty chat") |
| 90 | + _assert_pdf_response(response) |
| 91 | + |
| 92 | + def test_very_long_content(self, pdf_client): |
| 93 | + line = "This is a repeated paragraph for length testing. " * 20 |
| 94 | + md = "\n".join(f"Line {i}: {line}" for i in range(500)) |
| 95 | + response = _post_pdf(pdf_client, markdown=md, title="Long chat") |
| 96 | + _assert_pdf_response(response) |
| 97 | + |
| 98 | + def test_unicode_and_emoji_content(self, pdf_client): |
| 99 | + md = ( |
| 100 | + "Smart quotes: “hello” and ’world’\n" |
| 101 | + "Emoji: 🚀🔥 should not break PDF\n" |
| 102 | + "Bullet • point\n" |
| 103 | + ) |
| 104 | + response = _post_pdf(pdf_client, markdown=md, title="Unicode chat") |
| 105 | + _assert_pdf_response(response) |
| 106 | + |
| 107 | + |
| 108 | +class TestGeneratePdfErrors: |
| 109 | + def test_pdf_engine_failure_returns_500(self, pdf_client): |
| 110 | + with patch( |
| 111 | + "fpdf.fpdf.FPDF.output", |
| 112 | + side_effect=RuntimeError("simulated failure"), |
| 113 | + ): |
| 114 | + response = _post_pdf(pdf_client, markdown="Hello", title="Fail") |
| 115 | + assert response.status_code == 500 |
| 116 | + assert response.get_json() == {"error": "Failed to generate PDF"} |
| 117 | + |
| 118 | + def test_invalid_export_payload_returns_500(self, pdf_client): |
| 119 | + # Conversation IDs are resolved client-side (tabs API) before markdown is |
| 120 | + # POSTed here. A non-string markdown field mimics a corrupted export request. |
| 121 | + response = _post_pdf( |
| 122 | + pdf_client, |
| 123 | + json_data={"markdown": ["not", "a", "string"], "title": "Bad payload"}, |
| 124 | + ) |
| 125 | + assert response.status_code == 500 |
| 126 | + assert response.get_json() == {"error": "Failed to generate PDF"} |
0 commit comments