diff --git a/backend/app/api/report.py b/backend/app/api/report.py index a5c3148f..deba8420 100644 --- a/backend/app/api/report.py +++ b/backend/app/api/report.py @@ -5,7 +5,6 @@ Provides simulation report generation, retrieval, and chat endpoints import io import os -import tempfile import traceback import threading import markdown as md_lib @@ -480,12 +479,12 @@ def download_report(report_id: str): as_attachment=True, download_name=f"{report_id}.md" ) - with tempfile.NamedTemporaryFile(mode='w', suffix='.md', - delete=False, encoding='utf-8') as f: - f.write(markdown_content) - temp_path = f.name - return send_file(temp_path, as_attachment=True, - download_name=f"{report_id}.md") + return send_file( + io.BytesIO(markdown_content.encode('utf-8')), + mimetype='text/markdown; charset=utf-8', + as_attachment=True, + download_name=f"{report_id}.md" + ) # fmt == 'pdf' pdf_bytes = _generate_pdf_bytes(markdown_content) diff --git a/backend/tests/test_report_download.py b/backend/tests/test_report_download.py index 953df54e..814fee82 100644 --- a/backend/tests/test_report_download.py +++ b/backend/tests/test_report_download.py @@ -80,13 +80,7 @@ class TestDownloadPDF: assert resp.status_code == 404 - def test_download_pdf_invalid_format(self, client, tmp_path): + def test_download_pdf_invalid_format(self, client): """Returns 400 for unknown format parameter.""" - mock_report = _make_mock_report() - md_path = _make_md_file(tmp_path, mock_report.report_id, mock_report.markdown_content) - - with patch('app.api.report.ReportManager.get_report', return_value=mock_report), \ - patch('app.api.report.ReportManager._get_report_markdown_path', return_value=md_path): - resp = client.get(f'/api/report/{mock_report.report_id}/download?format=docx') - + resp = client.get('/api/report/any_id/download?format=docx') assert resp.status_code == 400