|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import io |
| 4 | +import unittest |
| 5 | +from contextlib import redirect_stdout |
| 6 | +from unittest.mock import patch |
| 7 | + |
| 8 | +from auralogger.cli_auth import CliProjectContext, resolve_project_context_for_cli_checks |
| 9 | +from auralogger.commands.client_check import run_client_check |
| 10 | +from auralogger.commands.init import run_init |
| 11 | +from auralogger.commands.server_check import run_server_check |
| 12 | + |
| 13 | + |
| 14 | +class _FakeSocket: |
| 15 | + def __init__(self) -> None: |
| 16 | + self.sent: list[str] = [] |
| 17 | + self.closed = False |
| 18 | + |
| 19 | + def send(self, body: str) -> None: |
| 20 | + self.sent.append(body) |
| 21 | + |
| 22 | + def close(self) -> None: |
| 23 | + self.closed = True |
| 24 | + |
| 25 | + |
| 26 | +class CliContextResolverTests(unittest.TestCase): |
| 27 | + def test_resolve_project_context_for_cli_checks_success(self) -> None: |
| 28 | + with patch( |
| 29 | + "auralogger.cli_auth.resolve_project_token_for_init", return_value="ptok" |
| 30 | + ), patch( |
| 31 | + "auralogger.cli_auth.resolve_user_secret_for_init", return_value="usec" |
| 32 | + ), patch( |
| 33 | + "auralogger.cli_auth.fetch_proj_auth_payload", |
| 34 | + return_value={"project_id": "pid-1", "project_name": "proj", "session": "sess-1"}, |
| 35 | + ): |
| 36 | + ctx = resolve_project_context_for_cli_checks() |
| 37 | + |
| 38 | + self.assertEqual( |
| 39 | + ctx, |
| 40 | + CliProjectContext( |
| 41 | + project_token="ptok", |
| 42 | + user_secret="usec", |
| 43 | + project_id="pid-1", |
| 44 | + project_name="proj", |
| 45 | + session="sess-1", |
| 46 | + ), |
| 47 | + ) |
| 48 | + |
| 49 | + def test_resolve_project_context_for_cli_checks_validates_required_fields(self) -> None: |
| 50 | + with patch( |
| 51 | + "auralogger.cli_auth.resolve_project_token_for_init", return_value="ptok" |
| 52 | + ), patch( |
| 53 | + "auralogger.cli_auth.resolve_user_secret_for_init", return_value="usec" |
| 54 | + ), patch( |
| 55 | + "auralogger.cli_auth.fetch_proj_auth_payload", |
| 56 | + return_value={"project_id": "pid-1", "session": ""}, |
| 57 | + ): |
| 58 | + with self.assertRaises(ValueError): |
| 59 | + resolve_project_context_for_cli_checks() |
| 60 | + |
| 61 | + |
| 62 | +class CliCommandWiringTests(unittest.TestCase): |
| 63 | + def test_server_check_uses_shared_context_resolver(self) -> None: |
| 64 | + context = CliProjectContext( |
| 65 | + project_token="ptok", |
| 66 | + user_secret="usec", |
| 67 | + project_id="pid-1", |
| 68 | + project_name="proj", |
| 69 | + session="sess-1", |
| 70 | + ) |
| 71 | + fake_ws = _FakeSocket() |
| 72 | + |
| 73 | + with patch( |
| 74 | + "auralogger.commands.server_check.resolve_project_context_for_cli_checks", |
| 75 | + return_value=context, |
| 76 | + ) as resolver, patch( |
| 77 | + "auralogger.commands.server_check.resolve_ws_base_url", |
| 78 | + return_value="wss://api.auralogger.com", |
| 79 | + ), patch( |
| 80 | + "auralogger.commands.server_check.create_connection", return_value=fake_ws |
| 81 | + ) as create_conn: |
| 82 | + run_server_check() |
| 83 | + |
| 84 | + resolver.assert_called_once_with() |
| 85 | + create_conn.assert_called_once() |
| 86 | + kwargs = create_conn.call_args.kwargs |
| 87 | + self.assertEqual(kwargs["header"], ["Authorization: Bearer usec"]) |
| 88 | + self.assertEqual(len(fake_ws.sent), 1) |
| 89 | + self.assertTrue(fake_ws.closed) |
| 90 | + |
| 91 | + def test_client_check_uses_shared_context_resolver(self) -> None: |
| 92 | + context = CliProjectContext( |
| 93 | + project_token="ptok", |
| 94 | + user_secret="usec", |
| 95 | + project_id="pid-1", |
| 96 | + project_name="proj", |
| 97 | + session="sess-1", |
| 98 | + ) |
| 99 | + fake_ws = _FakeSocket() |
| 100 | + |
| 101 | + with patch( |
| 102 | + "auralogger.commands.client_check.resolve_project_context_for_cli_checks", |
| 103 | + return_value=context, |
| 104 | + ) as resolver, patch( |
| 105 | + "auralogger.commands.client_check.resolve_ws_base_url", |
| 106 | + return_value="wss://api.auralogger.com", |
| 107 | + ), patch( |
| 108 | + "auralogger.commands.client_check.create_connection", return_value=fake_ws |
| 109 | + ) as create_conn: |
| 110 | + run_client_check() |
| 111 | + |
| 112 | + resolver.assert_called_once_with() |
| 113 | + create_conn.assert_called_once_with( |
| 114 | + "wss://api.auralogger.com/ptok/create_browser_logs", |
| 115 | + timeout=5, |
| 116 | + ) |
| 117 | + self.assertEqual(len(fake_ws.sent), 1) |
| 118 | + self.assertTrue(fake_ws.closed) |
| 119 | + |
| 120 | + |
| 121 | +class InitParityTests(unittest.TestCase): |
| 122 | + def test_run_init_already_configured_skips_proj_auth(self) -> None: |
| 123 | + output = io.StringIO() |
| 124 | + with patch( |
| 125 | + "auralogger.commands.init.is_full_runtime_env_configured", return_value=True |
| 126 | + ), patch( |
| 127 | + "auralogger.commands.init.fetch_proj_auth_payload" |
| 128 | + ) as fetch_proj_auth, redirect_stdout(output): |
| 129 | + run_init() |
| 130 | + |
| 131 | + fetch_proj_auth.assert_not_called() |
| 132 | + rendered = output.getvalue() |
| 133 | + self.assertIn("already configured", rendered) |
| 134 | + self.assertIn("Server integration snippet (Python)", rendered) |
| 135 | + |
| 136 | + def test_run_init_normal_path_prints_integration_help(self) -> None: |
| 137 | + output = io.StringIO() |
| 138 | + with patch( |
| 139 | + "auralogger.commands.init.is_full_runtime_env_configured", return_value=False |
| 140 | + ), patch( |
| 141 | + "auralogger.commands.init.get_resolved_project_token", return_value=None |
| 142 | + ), patch( |
| 143 | + "auralogger.commands.init._user_secret_explicitly_in_env", return_value=False |
| 144 | + ), patch( |
| 145 | + "auralogger.commands.init.get_resolved_session", return_value=None |
| 146 | + ), patch( |
| 147 | + "auralogger.commands.init.resolve_project_token_for_init", return_value="ptok" |
| 148 | + ), patch( |
| 149 | + "auralogger.commands.init.resolve_user_secret_for_init", return_value="usec" |
| 150 | + ), patch( |
| 151 | + "auralogger.commands.init.fetch_proj_auth_payload", |
| 152 | + return_value={"project_id": "pid-1", "session": "sess-1", "styles": []}, |
| 153 | + ), redirect_stdout(output): |
| 154 | + run_init() |
| 155 | + |
| 156 | + rendered = output.getvalue() |
| 157 | + self.assertIn("Copy-paste env block", rendered) |
| 158 | + self.assertIn("Server integration snippet (Python)", rendered) |
| 159 | + self.assertIn("auralogger-cli/client", rendered) |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == "__main__": |
| 163 | + unittest.main() |
0 commit comments