"""Phase 54 — get_host_label / get_provider_tag 測試覆蓋(P53 新增 fn). P53 commit 7a10d27 加了 110 Nginx Proxy 路由判斷(192.168.0.110:11435/11436) 但無單測,未來改 IP / 加新 provider 容易破而不自知。 """ from __future__ import annotations import pytest from services.ollama_service import get_host_label, get_provider_tag # ────────────────────────────────────────────────────────────────────────── # get_host_label — 觀測台 / 報表顯示用人類可讀標籤 # ────────────────────────────────────────────────────────────────────────── class TestGetHostLabel: def test_empty_returns_unknown(self): assert get_host_label('') == '未知' assert get_host_label(None) == '未知' # type: ignore[arg-type] def test_gcp_primary_direct(self): """docker-compose 環境直連 GCP-A""" assert get_host_label('http://34.87.90.216:11434') == 'GCP-SSD' def test_gcp_secondary_direct(self): """docker-compose 環境直連 GCP-B""" assert get_host_label('http://34.21.145.224:11434') == 'GCP-SSD-2' def test_nginx_proxy_gcp_primary(self): """經 110 跳板 Nginx 轉發 GCP-A""" assert get_host_label('http://192.168.0.110:11435') == 'GCP-SSD(via Nginx 110)' def test_nginx_proxy_gcp_secondary(self): """經 110 跳板 Nginx 轉發 GCP-B""" assert get_host_label('http://192.168.0.110:11436') == 'GCP-SSD-2(via Nginx 110)' def test_111_fallback(self): assert get_host_label('http://192.168.0.111:11434') == '111 備援' def test_188_local(self): assert get_host_label('http://192.168.0.188:11434') == '188 本地' def test_localhost(self): assert get_host_label('http://localhost:11434') == '188 本地' def test_unknown_falls_back_to_hostname(self): """未匹配的 host 應回 host:port 中的 hostname 部分。""" assert get_host_label('http://10.0.0.50:11434') == '10.0.0.50' # ────────────────────────────────────────────────────────────────────────── # get_provider_tag — ai_calls.provider 白名單對應 # ────────────────────────────────────────────────────────────────────────── class TestGetProviderTag: def test_empty_returns_other(self): assert get_provider_tag('') == 'ollama_other' assert get_provider_tag(None) == 'ollama_other' # type: ignore[arg-type] def test_gcp_primary_direct_or_nginx(self): """直連 + Nginx 11435 都歸 gcp_ollama""" assert get_provider_tag('http://34.87.90.216:11434') == 'gcp_ollama' assert get_provider_tag('http://192.168.0.110:11435') == 'gcp_ollama' def test_gcp_secondary_direct_or_nginx(self): """直連 + Nginx 11436 都歸 ollama_secondary""" assert get_provider_tag('http://34.21.145.224:11434') == 'ollama_secondary' assert get_provider_tag('http://192.168.0.110:11436') == 'ollama_secondary' def test_111_returns_ollama_111(self): assert get_provider_tag('http://192.168.0.111:11434') == 'ollama_111' def test_unknown_returns_other(self): assert get_provider_tag('http://10.0.0.50:11434') == 'ollama_other' assert get_provider_tag('http://192.168.0.188:11434') == 'ollama_other' @pytest.mark.parametrize('host,expected', [ # 對齊 ai_calls 表 CHECK constraint 白名單(migration 043 補 ollama_other) ('http://34.87.90.216:11434', 'gcp_ollama'), ('http://192.168.0.110:11435', 'gcp_ollama'), ('http://34.21.145.224:11434', 'ollama_secondary'), ('http://192.168.0.110:11436', 'ollama_secondary'), ('http://192.168.0.111:11434', 'ollama_111'), ('http://10.0.0.99:11434', 'ollama_other'), ]) def test_provider_tag_white_list_alignment(self, host, expected): """每個 provider tag 必對應 migration 024 的 CHECK 白名單。""" assert get_provider_tag(host) == expected