|
| 1 | +import io |
| 2 | +import unittest |
| 3 | +from contextlib import redirect_stdout |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | +from urllib.error import HTTPError, URLError |
| 6 | + |
| 7 | +from cas_evals.registry_check import RegistryCheckError, check_registry_urls, main |
| 8 | + |
| 9 | + |
| 10 | +def _fake_response(status_code): |
| 11 | + response = MagicMock() |
| 12 | + response.status = status_code |
| 13 | + response.read.return_value = b"" |
| 14 | + response.__enter__.return_value = response |
| 15 | + response.__exit__.return_value = False |
| 16 | + return response |
| 17 | + |
| 18 | + |
| 19 | +class RegistryCheckTests(unittest.TestCase): |
| 20 | + def test_all_paths_return_200(self): |
| 21 | + with patch("cas_evals.registry_check.urlopen", return_value=_fake_response(200)) as urlopen: |
| 22 | + results = check_registry_urls( |
| 23 | + "https://coding-autopilot-system.github.io/cas-contracts/registry", |
| 24 | + ["index.json", "v0.1/manifest.json"], |
| 25 | + ) |
| 26 | + self.assertEqual( |
| 27 | + results, |
| 28 | + [("index.json", 200), ("v0.1/manifest.json", 200)], |
| 29 | + ) |
| 30 | + self.assertEqual(urlopen.call_count, 2) |
| 31 | + |
| 32 | + def test_one_path_returns_404(self): |
| 33 | + responses = [_fake_response(200), _fake_response(404)] |
| 34 | + |
| 35 | + def side_effect(*args, **kwargs): |
| 36 | + return responses.pop(0) |
| 37 | + |
| 38 | + with patch("cas_evals.registry_check.urlopen", side_effect=side_effect): |
| 39 | + with self.assertRaises(RegistryCheckError) as ctx: |
| 40 | + check_registry_urls( |
| 41 | + "https://coding-autopilot-system.github.io/cas-contracts/registry", |
| 42 | + ["index.json", "v0.1/manifest.json"], |
| 43 | + ) |
| 44 | + self.assertIn("v0.1/manifest.json", str(ctx.exception)) |
| 45 | + self.assertIn("404", str(ctx.exception)) |
| 46 | + |
| 47 | + def test_network_unavailable_raises_distinguishable_error(self): |
| 48 | + with patch("cas_evals.registry_check.urlopen", side_effect=URLError("no route to host")): |
| 49 | + with self.assertRaises(RegistryCheckError) as ctx: |
| 50 | + check_registry_urls( |
| 51 | + "https://coding-autopilot-system.github.io/cas-contracts/registry", |
| 52 | + ["index.json"], |
| 53 | + ) |
| 54 | + self.assertIn("unavailable", str(ctx.exception).lower()) |
| 55 | + self.assertNotIn("404", str(ctx.exception)) |
| 56 | + |
| 57 | + def test_http_error_raises_registry_check_error(self): |
| 58 | + error = HTTPError("https://example.com/index.json", 500, "Internal Server Error", {}, None) |
| 59 | + with patch("cas_evals.registry_check.urlopen", side_effect=error): |
| 60 | + with self.assertRaises(RegistryCheckError) as ctx: |
| 61 | + check_registry_urls( |
| 62 | + "https://coding-autopilot-system.github.io/cas-contracts/registry", |
| 63 | + ["index.json"], |
| 64 | + ) |
| 65 | + self.assertIn("500", str(ctx.exception)) |
| 66 | + |
| 67 | + def test_main_exits_zero_on_success(self): |
| 68 | + with patch("cas_evals.registry_check.check_registry_urls") as mocked: |
| 69 | + mocked.return_value = [ |
| 70 | + ("index.json", 200), |
| 71 | + ("v0.1/manifest.json", 200), |
| 72 | + ("v0.1/common.schema.json", 200), |
| 73 | + ("v0.1/evaluation-result.schema.json", 200), |
| 74 | + ] |
| 75 | + buffer = io.StringIO() |
| 76 | + with redirect_stdout(buffer): |
| 77 | + exit_code = main([]) |
| 78 | + self.assertEqual(exit_code, 0) |
| 79 | + output = buffer.getvalue() |
| 80 | + self.assertIn("index.json", output) |
| 81 | + self.assertIn("200", output) |
| 82 | + |
| 83 | + def test_main_exits_nonzero_on_failure(self): |
| 84 | + with patch( |
| 85 | + "cas_evals.registry_check.check_registry_urls", |
| 86 | + side_effect=RegistryCheckError("v0.1/manifest.json returned HTTP 404"), |
| 87 | + ): |
| 88 | + buffer = io.StringIO() |
| 89 | + with redirect_stdout(buffer): |
| 90 | + exit_code = main([]) |
| 91 | + self.assertEqual(exit_code, 1) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + unittest.main() |
0 commit comments