import unittest from unittest.mock import patch import tuotuo def cluster_item(cluster_id): return { "clusterID": cluster_id, "clusterRegionName": "us-west-2", "tenantID": "tenant-1", "projectID": "project-1", "orgID": 1, "clusterCreatedAt": "2026-08-26T00:00:00Z", "clusterDeletedAt": None, "clusterProviderName": "aws", "clusterStatus": "NORMAL", "clusterDeployType": "Dedicated", "clusterShootName": "shoot-1", } class FakeResponse: ok = True status = 200 def __init__(self, payload): self.payload = payload async def __aenter__(self): return self async def __aexit__(self, exc_type, exc_value, traceback): return False async def json(self): return self.payload async def text(self): return "" class FakeClientSession: def __init__(self, responses): self.responses = iter(responses) self.requests = [] async def __aenter__(self): return self async def __aexit__(self, exc_type, exc_value, traceback): return False def get(self, url, *, params, headers): self.requests.append((url, dict(params), headers)) return FakeResponse(next(self.responses)) class ClinicClientTest(unittest.IsolatedAsyncioTestCase): async def test_list_clusters_uses_has_more_without_total(self): session = FakeClientSession([ { "items": [cluster_item("cluster-1")], "has_more": True, }, { "items": [cluster_item("cluster-2")], "has_more": False, }, ]) with patch.dict("os.environ", { "CLINIC_API_TOKEN": "test-token", "CLINIC_API_ENDPOINT": "https://clinic.example.com", }), patch("tuotuo.clinic.client.aiohttp.ClientSession", return_value=session): client = tuotuo.client("clinic") clusters = [cluster async for cluster in client.list_clusters()] self.assertEqual(["cluster-1", "cluster-2"], [cluster.id for cluster in clusters]) self.assertEqual(2, len(session.requests)) self.assertEqual({"limit": 100, "page": 1}, session.requests[0][1]) self.assertEqual({"limit": 100, "page": 2}, session.requests[1][1]) async def test_list_clusters_prefers_has_more_over_total(self): session = FakeClientSession([ { "items": [cluster_item("cluster-1")], "has_more": True, "total": 1, }, { "items": [cluster_item("cluster-2")], "has_more": False, "total": 101, }, ]) with patch.dict("os.environ", { "CLINIC_API_TOKEN": "test-token", "CLINIC_API_ENDPOINT": "https://clinic.example.com", }), patch("tuotuo.clinic.client.aiohttp.ClientSession", return_value=session): client = tuotuo.client("clinic") clusters = [cluster async for cluster in client.list_clusters()] self.assertEqual(["cluster-1", "cluster-2"], [cluster.id for cluster in clusters]) self.assertEqual([1, 2], [request[1]["page"] for request in session.requests]) async def test_list_clusters_falls_back_to_total(self): session = FakeClientSession([ { "items": [cluster_item("cluster-1")], "total": 101, }, { "items": [cluster_item("cluster-2")], "total": 101, }, ]) with patch.dict("os.environ", { "CLINIC_API_TOKEN": "test-token", "CLINIC_API_ENDPOINT": "https://clinic.example.com", }), patch("tuotuo.clinic.client.aiohttp.ClientSession", return_value=session): client = tuotuo.client("clinic") clusters = [cluster async for cluster in client.list_clusters()] self.assertEqual(["cluster-1", "cluster-2"], [cluster.id for cluster in clusters]) self.assertEqual([1, 2], [request[1]["page"] for request in session.requests]) if __name__ == "__main__": unittest.main()