@@ -127,3 +127,83 @@ class MockResponse:
127127 generate_answer (question = "hello?" , user_id = "some-user" , hf_token = None )
128128 from app .config import get_settings
129129 assert called_with_token == get_settings ().HF_TOKEN
130+
131+
132+ def test_clear_chat_history_with_shared_messages (client , auth_headers , ready_document , db_session , user ):
133+ from app .models import ChatMessage , SharedMessage
134+
135+ # Create a user ChatMessage and an assistant ChatMessage associated with ready_document
136+ user_msg = ChatMessage (
137+ user_id = user .id ,
138+ document_id = ready_document .id ,
139+ role = "user" ,
140+ content = "Hello, is anyone there?" ,
141+ )
142+ assistant_msg = ChatMessage (
143+ user_id = user .id ,
144+ document_id = ready_document .id ,
145+ role = "assistant" ,
146+ content = "Yes, I am here." ,
147+ )
148+ db_session .add_all ([user_msg , assistant_msg ])
149+ db_session .commit ()
150+ db_session .refresh (assistant_msg )
151+
152+ # Make assistant message shared by creating a SharedMessage link
153+ shared = SharedMessage (message_id = assistant_msg .id )
154+ db_session .add (shared )
155+ db_session .commit ()
156+
157+ assistant_msg_id = assistant_msg .id
158+
159+ # Expunge objects so session doesn't try to auto-refresh deleted rows
160+ db_session .expunge (user_msg )
161+ db_session .expunge (assistant_msg )
162+ db_session .expunge (shared )
163+
164+ # Call DELETE /api/v1/chat/history/{document_id}
165+ response = client .delete (
166+ f"/api/v1/chat/history/{ ready_document .id } " ,
167+ headers = auth_headers ,
168+ )
169+
170+ # Check results
171+ assert response .status_code == 200
172+ assert response .json () == {"message" : "Chat history cleared" }
173+
174+ # Check that ChatMessage records are deleted
175+ remaining_messages = db_session .query (ChatMessage ).filter (
176+ ChatMessage .document_id == ready_document .id
177+ ).all ()
178+ assert len (remaining_messages ) == 0
179+
180+ # Check that SharedMessage records are deleted
181+ remaining_shared = db_session .query (SharedMessage ).filter (
182+ SharedMessage .message_id == assistant_msg_id
183+ ).all ()
184+ assert len (remaining_shared ) == 0
185+
186+
187+ def test_clear_chat_history_repeated_or_empty (client , auth_headers , ready_document , db_session ):
188+ from app .models import ChatMessage
189+ # Check history is empty initially
190+ remaining_messages = db_session .query (ChatMessage ).filter (
191+ ChatMessage .document_id == ready_document .id
192+ ).all ()
193+ assert len (remaining_messages ) == 0
194+
195+ # First delete on empty history
196+ response = client .delete (
197+ f"/api/v1/chat/history/{ ready_document .id } " ,
198+ headers = auth_headers ,
199+ )
200+ assert response .status_code == 200
201+ assert response .json () == {"message" : "Chat history cleared" }
202+
203+ # Second delete (repeated request)
204+ response = client .delete (
205+ f"/api/v1/chat/history/{ ready_document .id } " ,
206+ headers = auth_headers ,
207+ )
208+ assert response .status_code == 200
209+ assert response .json () == {"message" : "Chat history cleared" }
0 commit comments