diff --git a/plugin/adapter.py b/plugin/adapter.py index a298b9b..40c415a 100644 --- a/plugin/adapter.py +++ b/plugin/adapter.py @@ -6006,6 +6006,12 @@ class RocketChatAdapter(BasePlatformAdapter): block = f"\n\n[FILE CONTENT: {fname}]\n{content[:10000]}\n[/FILE]" text += block logger.debug("[rocketchat] Inlined file %s (%d chars)", fname, len(block)) + # P2-1: Alle übrigen [FILE]-Blöcke entfernen (fehlgeschlagene Downloads) + # Non-greedy Pattern: erfasst [FILE]...Download:... bis zum nächsten \n + # re.DOTALL damit Zeilenumbrüche innerhalb des Blocks matchen + text = re.sub( + r'\[FILE\].*?Download:[^\n]+\n?', '', text, flags=re.DOTALL + ) return text async def disconnect(self) -> None: diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 7799fef..82556f2 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -337,3 +337,116 @@ class TestPresenceEngineNoneGuard: pytest.fail( f"_complete_onboarding crashed ohne _presence_engine: {e}" ) + + +class TestFileBlockStripping: + """P2-1: _download_and_inline_files muss [FILE]-Blöcke nach + fehlgeschlagenen Downloads aus dem Text entfernen.""" + + def test_file_blocks_stripped_after_failed_download(self): + """Fehlgeschlagener Download → [FILE]-Blöcke werden entfernt.""" + import asyncio + from unittest.mock import MagicMock + + from plugin.adapter import RocketChatAdapter + + adapter = RocketChatAdapter.__new__(RocketChatAdapter) + mock_rest = MagicMock() + mock_rest.download_file = MagicMock(return_value=None) # Download fails + adapter.rest = mock_rest + + text = ( + "Hier ist eine Datei:\n" + "[FILE] report.pdf (application/pdf)\n" + "Download: https://chat.example.com/file-upload/abc123/report.pdf\n" + "\n" + "Und noch eine:\n" + "[FILE] image.png (image/png)\n" + "Download: https://chat.example.com/file-upload/def456/image.png\n" + "\n" + "Ende der Nachricht." + ) + + result = asyncio.run(adapter._download_and_inline_files(text)) + + # [FILE]-Blöcke müssen entfernt sein + assert "[FILE]" not in result, ( + f"[FILE]-Marker nicht entfernt:\n{result}" + ) + assert "Download:" not in result, ( + f"Download-URL nicht entfernt:\n{result}" + ) + # Restlicher Text muss erhalten bleiben + assert "Hier ist eine Datei:" in result + assert "Ende der Nachricht." in result + + def test_file_blocks_stripped_mixed_success_failure(self): + """Ein erfolgreicher Download + ein Fehlschlag → nur Fail-Marker entfernt.""" + import asyncio + from unittest.mock import MagicMock + + from plugin.adapter import RocketChatAdapter + + adapter = RocketChatAdapter.__new__(RocketChatAdapter) + mock_rest = MagicMock() + + # Erster Download erfolgreich, zweiter fehlgeschlagen + call_count = [0] + + def download_file(fid, fname): + call_count[0] += 1 + if call_count[0] == 1: + return "Inhalt der PDF-Datei" + return None + + mock_rest.download_file = download_file + adapter.rest = mock_rest + + text = ( + "[FILE] report.pdf (application/pdf)\n" + "Download: https://chat.example.com/file-upload/abc123/report.pdf\n" + "\n" + "[FILE] missing.doc (application/msword)\n" + "Download: https://chat.example.com/file-upload/xyz789/missing.doc\n" + ) + + result = asyncio.run(adapter._download_and_inline_files(text)) + + # Erfolgreicher Download: Content muss da sein + assert "[FILE CONTENT: report.pdf]" in result + assert "Inhalt der PDF-Datei" in result + # Fehlgeschlagener Download: [FILE]-Block muss weg sein + assert "missing.doc" not in result, ( + f"Fehlgeschlagener [FILE]-Block nicht entfernt:\n{result}" + ) + assert "xyz789" not in result + + def test_file_blocks_stripped_unicode_filename(self): + """Unicode-Dateinamen in [FILE]-Blöcken werden korrekt gestrippt.""" + import asyncio + from unittest.mock import MagicMock + + from plugin.adapter import RocketChatAdapter + + adapter = RocketChatAdapter.__new__(RocketChatAdapter) + mock_rest = MagicMock() + mock_rest.download_file = MagicMock(return_value=None) + adapter.rest = mock_rest + + text = ( + "Schau dir das an:\n" + "[FILE] Überblick_2024.pdf (application/pdf)\n" + "Download: https://chat.example.com/file-upload/abc123/Überblick_2024.pdf\n" + "\n" + "Und:\n" + "[FILE] 報告書.docx (application/vnd.openxmlformats-officedocument.wordprocessingml.document)\n" + "Download: https://chat.example.com/file-upload/def456/報告書.docx\n" + ) + + result = asyncio.run(adapter._download_and_inline_files(text)) + + assert "[FILE]" not in result, ( + f"Unicode [FILE]-Marker nicht entfernt:\n{result}" + ) + assert "Download:" not in result + assert "Schau dir das an:" in result