|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import unittest |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from auralogger.client.client_log import ( |
| 7 | + AuraClient, |
| 8 | + ClientLogInputs, |
| 9 | + auralog, |
| 10 | + client_log, |
| 11 | + close_client_log_socket, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class _FakeSocket: |
| 16 | + def __init__(self) -> None: |
| 17 | + self.sent: list[str] = [] |
| 18 | + self.closed = False |
| 19 | + |
| 20 | + def send(self, body: str) -> None: |
| 21 | + self.sent.append(body) |
| 22 | + |
| 23 | + def close(self) -> None: |
| 24 | + self.closed = True |
| 25 | + |
| 26 | + |
| 27 | +class AuraClientSdkTests(unittest.TestCase): |
| 28 | + def test_configure_rejects_empty_token(self) -> None: |
| 29 | + with self.assertRaises(ValueError): |
| 30 | + AuraClient.configure(" ") |
| 31 | + |
| 32 | + def test_sync_from_secret_validates_required_fields(self) -> None: |
| 33 | + with patch( |
| 34 | + "auralogger.client.client_log.fetch_proj_auth_payload", |
| 35 | + return_value={"project_id": "p1", "session": ""}, |
| 36 | + ): |
| 37 | + with self.assertRaises(ValueError): |
| 38 | + AuraClient.sync_from_secret("cipher-token") |
| 39 | + |
| 40 | + def test_log_delegates_to_client_log_function(self) -> None: |
| 41 | + with patch("auralogger.client.client_log.client_log") as mocked: |
| 42 | + AuraClient.log("info", "hello", "tests/client", {"k": 1}) |
| 43 | + mocked.assert_called_once_with("info", "hello", "tests/client", {"k": 1}) |
| 44 | + |
| 45 | + def test_auralog_uses_typed_inputs(self) -> None: |
| 46 | + with patch("auralogger.client.client_log.AuraClient.log") as mocked: |
| 47 | + auralog( |
| 48 | + ClientLogInputs( |
| 49 | + type="warn", |
| 50 | + message="typed input", |
| 51 | + location="tests/client", |
| 52 | + data={"ok": True}, |
| 53 | + ) |
| 54 | + ) |
| 55 | + mocked.assert_called_once_with("warn", "typed input", "tests/client", {"ok": True}) |
| 56 | + |
| 57 | + def test_client_log_sends_when_runtime_is_hydrated(self) -> None: |
| 58 | + ws = _FakeSocket() |
| 59 | + with patch( |
| 60 | + "auralogger.client.client_log._resolve_project_token_runtime", |
| 61 | + return_value="ptok", |
| 62 | + ), patch( |
| 63 | + "auralogger.client.client_log._merged_runtime_for_send", |
| 64 | + return_value={"project_id": "pid", "session": "sess", "styles": []}, |
| 65 | + ), patch( |
| 66 | + "auralogger.client.client_log._ensure_ws", |
| 67 | + return_value=ws, |
| 68 | + ), patch("auralogger.client.client_log.print_log"), patch( |
| 69 | + "auralogger.client.client_log._schedule_socket_idle_close" |
| 70 | + ): |
| 71 | + client_log("info", "hello", "tests/client", {"x": 1}) |
| 72 | + |
| 73 | + self.assertEqual(len(ws.sent), 1) |
| 74 | + |
| 75 | + def test_close_socket_delegates(self) -> None: |
| 76 | + with patch("auralogger.client.client_log.close_client_log_socket") as mocked: |
| 77 | + AuraClient.close_socket() |
| 78 | + mocked.assert_called_once_with() |
| 79 | + |
| 80 | + def test_close_client_log_socket_closes_ws(self) -> None: |
| 81 | + ws = _FakeSocket() |
| 82 | + with patch("auralogger.client.client_log._ws", ws), patch( |
| 83 | + "auralogger.client.client_log._bound_url", "wss://x" |
| 84 | + ): |
| 85 | + close_client_log_socket() |
| 86 | + self.assertTrue(ws.closed) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + unittest.main() |
0 commit comments