| 1 | import json |
| 2 | import os |
| 3 | import unittest |
| 4 | from unittest import mock |
| 5 | |
| 6 | from lib import xai_x |
| 7 | from lib.xai_x import parse_x_response |
| 8 | |
| 9 | |
| 10 | def _wrap_items(items): |
| 11 | """Wrap items list in the xAI response envelope.""" |
| 12 | payload = json.dumps({"items": items}) |
| 13 | return { |
| 14 | "output": [ |
| 15 | { |
| 16 | "type": "message", |
| 17 | "content": [{"type": "output_text", "text": payload}], |
| 18 | } |
| 19 | ] |
| 20 | } |
| 21 | |
| 22 | |
| 23 | class TestXaiXEngagementZero(unittest.TestCase): |
| 24 | def test_zero_likes_preserved(self): |
| 25 | raw_items = [ |
| 26 | { |
| 27 | "text": "test", |
| 28 | "url": "https://x.com/u/status/1", |
| 29 | "engagement": {"likes": 0, "reposts": 5}, |
| 30 | "relevance": 0.8, |
| 31 | } |
| 32 | ] |
| 33 | items = parse_x_response(_wrap_items(raw_items)) |
| 34 | self.assertEqual(0, items[0]["engagement"]["likes"]) |
| 35 | self.assertEqual(5, items[0]["engagement"]["reposts"]) |
| 36 | |
| 37 | def test_none_engagement_when_missing(self): |
| 38 | raw_items = [ |
| 39 | { |
| 40 | "text": "test", |
| 41 | "url": "https://x.com/u/status/1", |
| 42 | "engagement": {}, |
| 43 | "relevance": 0.8, |
| 44 | } |
| 45 | ] |
| 46 | items = parse_x_response(_wrap_items(raw_items)) |
| 47 | self.assertIsNone(items[0]["engagement"]["likes"]) |
| 48 | |
| 49 | def test_nonzero_engagement(self): |
| 50 | raw_items = [ |
| 51 | { |
| 52 | "text": "test", |
| 53 | "url": "https://x.com/u/status/1", |
| 54 | "engagement": {"likes": 42, "reposts": 3, "replies": 1, "quotes": 0}, |
| 55 | "relevance": 0.9, |
| 56 | } |
| 57 | ] |
| 58 | items = parse_x_response(_wrap_items(raw_items)) |
| 59 | eng = items[0]["engagement"] |
| 60 | self.assertEqual(42, eng["likes"]) |
| 61 | self.assertEqual(3, eng["reposts"]) |
| 62 | self.assertEqual(1, eng["replies"]) |
| 63 | self.assertEqual(0, eng["quotes"]) |
| 64 | |
| 65 | |
| 66 | class TestXaiXBaseUrlOverride(unittest.TestCase): |
| 67 | """XAI_BASE_URL redirects the x_search request like the reasoning client.""" |
| 68 | |
| 69 | def _posted_url(self, env): |
| 70 | with mock.patch.dict(os.environ, env, clear=True), mock.patch( |
| 71 | "lib.xai_x.http.post", return_value={"output": []} |
| 72 | ) as post: |
| 73 | xai_x.search_x("xai-test", "grok-test", "topic", "2026-05-01", "2026-06-01") |
| 74 | return post.call_args.args[0] |
| 75 | |
| 76 | def test_default_endpoint_without_override(self): |
| 77 | self.assertEqual(xai_x.XAI_RESPONSES_URL, self._posted_url({})) |
| 78 | |
| 79 | def test_api_root_override_is_honored(self): |
| 80 | self.assertEqual( |
| 81 | "https://gateway.test/v1/responses", |
| 82 | self._posted_url({"XAI_BASE_URL": "https://gateway.test/v1"}), |
| 83 | ) |
| 84 | |
| 85 | def test_override_preserves_request_and_deadline(self): |
| 86 | endpoint = "https://gateway.test/v1/responses" |
| 87 | cancel = object() |
| 88 | with mock.patch.dict(os.environ, {"XAI_BASE_URL": endpoint}), mock.patch( |
| 89 | "lib.xai_x.http.post", return_value={"output": []} |
| 90 | ) as post: |
| 91 | xai_x.search_x( |
| 92 | "xai-test", "grok-test", "routing topic", "2026-05-01", "2026-06-01", |
| 93 | depth="quick", deadline_monotonic=1234.5, cancel=cancel, |
| 94 | ) |
| 95 | |
| 96 | post.assert_called_once() |
| 97 | url, payload = post.call_args.args |
| 98 | self.assertEqual(endpoint, url) |
| 99 | self.assertEqual("grok-test", payload["model"]) |
| 100 | self.assertIn("routing topic", payload["input"][0]["content"]) |
| 101 | self.assertEqual( |
| 102 | [{"type": "x_search", "from_date": "2026-05-01", "to_date": "2026-06-01"}], |
| 103 | payload["tools"], |
| 104 | ) |
| 105 | self.assertEqual("Bearer xai-test", post.call_args.kwargs["headers"]["Authorization"]) |
| 106 | self.assertEqual(90, post.call_args.kwargs["timeout"]) |
| 107 | self.assertEqual(1, post.call_args.kwargs["retries"]) |
| 108 | self.assertEqual(1234.5, post.call_args.kwargs["deadline_monotonic"]) |
| 109 | self.assertIs(cancel, post.call_args.kwargs["cancel"]) |
| 110 | |
| 111 | |
| 112 | if __name__ == "__main__": |
| 113 | unittest.main() |
| 114 |