@@ -155,6 +155,72 @@ def test_get_queries_without_dates_with_filters(self, client, populated_db_conne
155155 assert data ["total" ] >= 1
156156 assert all (item ["agent_id" ] == "cairo-coder" for item in data ["items" ])
157157
158+ def test_get_queries_with_conversation_id_filter (self , client , db_connection ):
159+ """Test that queries can be filtered by conversation_id."""
160+ import asyncio
161+ import json as _json
162+ import uuid
163+ from datetime import datetime , timedelta , timezone
164+
165+ now = datetime .now (timezone .utc )
166+ conv_id = "test-conversation-123"
167+
168+ # Seed records with and without conversation_id
169+ async def seed ():
170+ await db_connection .execute (
171+ """
172+ INSERT INTO user_interactions (id, created_at, agent_id, mcp_mode, conversation_id, chat_history, query, generated_answer)
173+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8),
174+ ($9, $10, $11, $12, $13, $14, $15, $16),
175+ ($17, $18, $19, $20, $21, $22, $23, $24)
176+ """ ,
177+ uuid .uuid4 (), now - timedelta (hours = 2 ), "cairo-coder" , False , conv_id , _json .dumps ([]), "First msg" , "Response 1" ,
178+ uuid .uuid4 (), now - timedelta (hours = 1 ), "cairo-coder" , False , conv_id , _json .dumps ([{"role" : "user" , "content" : "First msg" }]), "Second msg" , "Response 2" ,
179+ uuid .uuid4 (), now - timedelta (minutes = 30 ), "cairo-coder" , False , None , _json .dumps ([]), "Other msg" , "Other response" ,
180+ )
181+
182+ asyncio .get_event_loop ().run_until_complete (seed ())
183+
184+ # Filter by conversation_id
185+ resp = client .get (
186+ "/v1/insights/queries" ,
187+ params = {"conversation_id" : conv_id , "limit" : 100 , "offset" : 0 },
188+ )
189+ assert resp .status_code == 200
190+ data = resp .json ()
191+ assert data ["total" ] == 2
192+ assert all (item ["conversation_id" ] == conv_id for item in data ["items" ])
193+
194+ def test_get_queries_returns_conversation_id (self , client , db_connection ):
195+ """Test that conversation_id is included in the response."""
196+ import asyncio
197+ import json as _json
198+ import uuid
199+ from datetime import datetime , timezone
200+
201+ now = datetime .now (timezone .utc )
202+ conv_id = "response-test-conv-456"
203+
204+ async def seed ():
205+ await db_connection .execute (
206+ """
207+ INSERT INTO user_interactions (id, created_at, agent_id, mcp_mode, conversation_id, chat_history, query, generated_answer)
208+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
209+ """ ,
210+ uuid .uuid4 (), now , "cairo-coder" , False , conv_id , _json .dumps ([]), "Test query" , "Test response" ,
211+ )
212+
213+ asyncio .get_event_loop ().run_until_complete (seed ())
214+
215+ resp = client .get ("/v1/insights/queries" , params = {"limit" : 100 , "offset" : 0 })
216+ assert resp .status_code == 200
217+ data = resp .json ()
218+
219+ # Find our seeded record and verify conversation_id is present
220+ matching = [item for item in data ["items" ] if item .get ("conversation_id" ) == conv_id ]
221+ assert len (matching ) == 1
222+ assert matching [0 ]["conversation_id" ] == conv_id
223+
158224
159225class TestDataIngestion :
160226 async def test_chat_completion_logs_interaction_to_db (self , client , test_db_pool ):
0 commit comments