@@ -221,6 +221,72 @@ async def seed():
221221 assert len (matching ) == 1
222222 assert matching [0 ]["conversation_id" ] == conv_id
223223
224+ def test_get_queries_with_user_id_filter (self , client , db_connection ):
225+ """Test that queries can be filtered by user_id."""
226+ import asyncio
227+ import json as _json
228+ import uuid
229+ from datetime import datetime , timedelta , timezone
230+
231+ now = datetime .now (timezone .utc )
232+ user_id = "test-user-hash-789"
233+
234+ # Seed records with and without user_id
235+ async def seed ():
236+ await db_connection .execute (
237+ """
238+ INSERT INTO user_interactions (id, created_at, agent_id, mcp_mode, user_id, chat_history, query, generated_answer)
239+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8),
240+ ($9, $10, $11, $12, $13, $14, $15, $16),
241+ ($17, $18, $19, $20, $21, $22, $23, $24)
242+ """ ,
243+ uuid .uuid4 (), now - timedelta (hours = 2 ), "cairo-coder" , False , user_id , _json .dumps ([]), "First msg" , "Response 1" ,
244+ uuid .uuid4 (), now - timedelta (hours = 1 ), "cairo-coder" , False , user_id , _json .dumps ([{"role" : "user" , "content" : "First msg" }]), "Second msg" , "Response 2" ,
245+ uuid .uuid4 (), now - timedelta (minutes = 30 ), "cairo-coder" , False , None , _json .dumps ([]), "Other msg" , "Other response" ,
246+ )
247+
248+ asyncio .get_event_loop ().run_until_complete (seed ())
249+
250+ # Filter by user_id
251+ resp = client .get (
252+ "/v1/insights/queries" ,
253+ params = {"user_id" : user_id , "limit" : 100 , "offset" : 0 },
254+ )
255+ assert resp .status_code == 200
256+ data = resp .json ()
257+ assert data ["total" ] == 2
258+ assert all (item ["user_id" ] == user_id for item in data ["items" ])
259+
260+ def test_get_queries_returns_user_id (self , client , db_connection ):
261+ """Test that user_id is included in the response."""
262+ import asyncio
263+ import json as _json
264+ import uuid
265+ from datetime import datetime , timezone
266+
267+ now = datetime .now (timezone .utc )
268+ user_id = "response-test-user-456"
269+
270+ async def seed ():
271+ await db_connection .execute (
272+ """
273+ INSERT INTO user_interactions (id, created_at, agent_id, mcp_mode, user_id, chat_history, query, generated_answer)
274+ VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
275+ """ ,
276+ uuid .uuid4 (), now , "cairo-coder" , False , user_id , _json .dumps ([]), "Test query" , "Test response" ,
277+ )
278+
279+ asyncio .get_event_loop ().run_until_complete (seed ())
280+
281+ resp = client .get ("/v1/insights/queries" , params = {"limit" : 100 , "offset" : 0 })
282+ assert resp .status_code == 200
283+ data = resp .json ()
284+
285+ # Find our seeded record and verify user_id is present
286+ matching = [item for item in data ["items" ] if item .get ("user_id" ) == user_id ]
287+ assert len (matching ) == 1
288+ assert matching [0 ]["user_id" ] == user_id
289+
224290
225291class TestDataIngestion :
226292 async def test_chat_completion_logs_interaction_to_db (self , client , test_db_pool ):
0 commit comments