44from unittest .mock import Mock , patch
55
66import pytest
7+ from telegram import Document , User
8+ from telegram .ext import CallbackContext
9+ from workflows_acp .llm_wrapper import LLMWrapper
10+ from workflows_acp .llms .openai_llm import OpenAILLM
11+ from workflows_acp .tools .agentfs import load_all_files
12+ from workflows_acp .workflow import AgentWorkflow
13+
714from lobsterx .constants import (
815 DATA_DIR ,
916 DEFAULT_TO_AVOID ,
2330 handle_prompt ,
2431 start ,
2532)
26- from telegram import Document , User
27- from telegram .ext import CallbackContext
28- from workflows_acp .llm_wrapper import LLMWrapper
29- from workflows_acp .llms .openai_llm import OpenAILLM
30- from workflows_acp .tools .agentfs import load_all_files
31- from workflows_acp .workflow import AgentWorkflow
3233
3334from .conftest import (
3435 AgentWorkflowMock ,
@@ -79,7 +80,10 @@ async def test_handle_documents_success(
7980 to_avoid_files = DEFAULT_TO_AVOID_FILES ,
8081 )
8182 document = cast (
82- Document , TelegramDocumentMock (file_name = "hello.pdf" , file_id = "123" )
83+ Document ,
84+ TelegramDocumentMock (
85+ file_name = "hello.pdf" , file_id = "123" , mime_type = "application/pdf"
86+ ),
8387 )
8488 callback_context = cast (CallbackContext , TelegramCallBackContextMock ())
8589 result = await handle_documents (document , callback_context )
@@ -90,6 +94,93 @@ async def test_handle_documents_success(
9094 )
9195
9296
97+ @pytest .mark .asyncio
98+ async def test_handle_documents_no_name_mimetype (
99+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
100+ ) -> None :
101+ monkeypatch .chdir (tmp_path )
102+ (tmp_path / "test.txt" ).write_text ("Hello world" )
103+ await load_all_files (
104+ to_avoid_dirs = DEFAULT_TO_AVOID ,
105+ to_avoid_files = DEFAULT_TO_AVOID_FILES ,
106+ )
107+ document = cast (
108+ Document ,
109+ TelegramDocumentMock (
110+ file_name = None ,
111+ file_id = "123" ,
112+ mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
113+ ),
114+ )
115+ callback_context = cast (CallbackContext , TelegramCallBackContextMock ())
116+ result = await handle_documents (document , callback_context )
117+ assert (
118+ result .startswith ("Your file has been successfully downloaded at: " )
119+ and result .endswith (
120+ ". Use this path to reference the file in your follow-up requests to the agent"
121+ )
122+ and ".docx" in result
123+ )
124+
125+
126+ @pytest .mark .asyncio
127+ async def test_handle_documents_no_name_no_mimetype (
128+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
129+ ) -> None :
130+ monkeypatch .chdir (tmp_path )
131+ (tmp_path / "test.txt" ).write_text ("Hello world" )
132+ await load_all_files (
133+ to_avoid_dirs = DEFAULT_TO_AVOID ,
134+ to_avoid_files = DEFAULT_TO_AVOID_FILES ,
135+ )
136+ document = cast (
137+ Document ,
138+ TelegramDocumentMock (
139+ file_name = None ,
140+ file_id = "123" ,
141+ mime_type = None ,
142+ ),
143+ )
144+ callback_context = cast (CallbackContext , TelegramCallBackContextMock ())
145+ result = await handle_documents (document , callback_context )
146+ assert (
147+ result .startswith ("Your file has been successfully downloaded at: " )
148+ and result .endswith (
149+ ". Use this path to reference the file in your follow-up requests to the agent"
150+ )
151+ and ".pdf" in result
152+ )
153+
154+
155+ @pytest .mark .asyncio
156+ async def test_handle_documents_no_extension (
157+ tmp_path : Path , monkeypatch : pytest .MonkeyPatch
158+ ) -> None :
159+ monkeypatch .chdir (tmp_path )
160+ (tmp_path / "test.txt" ).write_text ("Hello world" )
161+ await load_all_files (
162+ to_avoid_dirs = DEFAULT_TO_AVOID ,
163+ to_avoid_files = DEFAULT_TO_AVOID_FILES ,
164+ )
165+ document = cast (
166+ Document ,
167+ TelegramDocumentMock (
168+ file_name = "file" ,
169+ file_id = "123" ,
170+ mime_type = "text/plain" ,
171+ ),
172+ )
173+ callback_context = cast (CallbackContext , TelegramCallBackContextMock ())
174+ result = await handle_documents (document , callback_context )
175+ assert (
176+ result .startswith ("Your file has been successfully downloaded at: " )
177+ and result .endswith (
178+ ". Use this path to reference the file in your follow-up requests to the agent"
179+ )
180+ and "file.txt" in result
181+ )
182+
183+
93184@pytest .mark .asyncio
94185async def test_handle_documents_fail (
95186 tmp_path : Path , monkeypatch : pytest .MonkeyPatch
@@ -101,7 +192,10 @@ async def test_handle_documents_fail(
101192 to_avoid_files = DEFAULT_TO_AVOID_FILES ,
102193 )
103194 document = cast (
104- Document , TelegramDocumentMock (file_name = "hello.pdf" , file_id = "123" )
195+ Document ,
196+ TelegramDocumentMock (
197+ file_name = "hello.pdf" , file_id = "123" , mime_type = "application/pdf"
198+ ),
105199 )
106200 callback_context = cast (
107201 CallbackContext , TelegramCallBackContextMock (should_fail = True )
0 commit comments