"""Integration: the bot's /ai_query endpoint enforces the shared key, validates the payload, and queues accepted prompts onto AI_QUERY_Q for the AI worker. """ import communication_subroutine as cs def _client(key="test-secret"): cs.API_KEY = key return cs.app.test_client() def _drain(): while not cs.AI_QUERY_Q.empty(): cs.AI_QUERY_Q.get() def test_ai_query_rejected_without_key(): _drain() client = _client() resp = client.post("/ai_query", json={"prompt": "x", "channel_id": 1}) assert resp.status_code == 401 assert cs.AI_QUERY_Q.empty() # nothing queued on a rejected call def test_ai_query_accepted_with_key_and_queued(): _drain() client = _client() resp = client.post( "/ai_query", json={"prompt": "posortuj DOI", "channel_id": 42, "username": "siara"}, headers={"X-Conjurer-Api-Key": "test-secret"}, ) assert resp.status_code == 200 item = cs.AI_QUERY_Q.get() assert item["prompt"] == "posortuj DOI" assert item["channel_id"] == 42 assert item["username"] == "siara" def test_ai_query_missing_prompt_is_rejected(): _drain() client = _client() resp = client.post( "/ai_query", json={"channel_id": 1}, headers={"X-Conjurer-Api-Key": "test-secret"}, ) assert resp.status_code == 400 assert cs.AI_QUERY_Q.empty() def test_ai_query_open_when_key_unset(): _drain() client = _client(key=None) resp = client.post("/ai_query", json={"prompt": "y", "channel_id": 1}) assert resp.status_code == 200 def test_submit_ai_query_enqueues_expected_shape(): _drain() cs.submit_ai_query( prompt="P", channel_id=7, request_type="NONE", username="u", query_uuid="uid-1" ) assert cs.AI_QUERY_Q.get() == { "uuid": "uid-1", "prompt": "P", "channel_id": 7, "request_type": "NONE", "username": "u", }