|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Tests for libs.application.service_config (LLM service configuration).""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +from libs.application.service_config import ServiceConfig |
| 9 | + |
| 10 | +# ── TestServiceConfig ─────────────────────────────────────────────────── |
| 11 | + |
| 12 | + |
| 13 | +class TestServiceConfig: |
| 14 | + """Construction, validation, and serialisation of ServiceConfig.""" |
| 15 | + |
| 16 | + def _make_env(self, **overrides): |
| 17 | + base = { |
| 18 | + "AZURE_OPENAI_API_VERSION": "2024-02-01", |
| 19 | + "AZURE_OPENAI_CHAT_DEPLOYMENT_NAME": "gpt-4", |
| 20 | + "AZURE_OPENAI_ENDPOINT": "https://myoai.openai.azure.com", |
| 21 | + "AZURE_OPENAI_API_KEY": "secret-key", |
| 22 | + } |
| 23 | + base.update(overrides) |
| 24 | + return base |
| 25 | + |
| 26 | + def test_construction_from_env_vars(self): |
| 27 | + env = self._make_env() |
| 28 | + cfg = ServiceConfig("default", "AZURE_OPENAI", env) |
| 29 | + assert cfg.service_id == "default" |
| 30 | + assert cfg.api_version == "2024-02-01" |
| 31 | + assert cfg.chat_deployment_name == "gpt-4" |
| 32 | + assert cfg.endpoint == "https://myoai.openai.azure.com" |
| 33 | + |
| 34 | + def test_is_valid_with_entra_id(self): |
| 35 | + env = self._make_env() |
| 36 | + cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=True) |
| 37 | + assert cfg.is_valid() is True |
| 38 | + |
| 39 | + def test_is_valid_without_entra_id_requires_api_key(self): |
| 40 | + env = self._make_env() |
| 41 | + cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=False) |
| 42 | + assert cfg.is_valid() is True |
| 43 | + |
| 44 | + def test_is_invalid_missing_endpoint(self): |
| 45 | + env = self._make_env() |
| 46 | + del env["AZURE_OPENAI_ENDPOINT"] |
| 47 | + cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=True) |
| 48 | + assert cfg.is_valid() is False |
| 49 | + |
| 50 | + def test_is_invalid_missing_deployment(self): |
| 51 | + env = self._make_env() |
| 52 | + del env["AZURE_OPENAI_CHAT_DEPLOYMENT_NAME"] |
| 53 | + cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=True) |
| 54 | + assert cfg.is_valid() is False |
| 55 | + |
| 56 | + def test_is_invalid_no_entra_no_key(self): |
| 57 | + env = self._make_env() |
| 58 | + del env["AZURE_OPENAI_API_KEY"] |
| 59 | + cfg = ServiceConfig("svc", "AZURE_OPENAI", env, use_entra_id=False) |
| 60 | + assert cfg.is_valid() is False |
| 61 | + |
| 62 | + def test_to_dict_keys(self): |
| 63 | + env = self._make_env() |
| 64 | + cfg = ServiceConfig("svc", "AZURE_OPENAI", env) |
| 65 | + d = cfg.to_dict() |
| 66 | + assert d["endpoint"] == "https://myoai.openai.azure.com" |
| 67 | + assert d["chat_deployment_name"] == "gpt-4" |
| 68 | + assert d["api_key"] == "secret-key" |
| 69 | + |
| 70 | + def test_to_dict_empty_fields_become_none(self): |
| 71 | + cfg = ServiceConfig("svc", "MISSING_PREFIX", {}) |
| 72 | + d = cfg.to_dict() |
| 73 | + assert d["endpoint"] is None |
| 74 | + assert d["chat_deployment_name"] is None |
| 75 | + |
| 76 | + def test_custom_prefix(self): |
| 77 | + env = { |
| 78 | + "MY_LLM_ENDPOINT": "https://custom.api", |
| 79 | + "MY_LLM_CHAT_DEPLOYMENT_NAME": "model-v2", |
| 80 | + } |
| 81 | + cfg = ServiceConfig("custom", "MY_LLM", env, use_entra_id=True) |
| 82 | + assert cfg.endpoint == "https://custom.api" |
| 83 | + assert cfg.chat_deployment_name == "model-v2" |
| 84 | + assert cfg.is_valid() is True |
0 commit comments