11from __future__ import annotations
22
3+ import re
34import subprocess
45from collections .abc import Generator
6+ from pathlib import Path
57from unittest .mock import patch
68
79import pytest
810from fastapi .testclient import TestClient
911
10- from app .main import create_app
12+ from app .main import SERVICE_VERSION , create_app
1113from app .services .executor_base import HealthCheck
1214from app .services .executor_docker import DockerExecutor
1315from app .services .executor_factory import get_executor
1416
17+ CHART_YAML = Path (__file__ ).resolve ().parents [3 ] / "kubernetes" / "code-interpreter" / "Chart.yaml"
18+
1519
1620@pytest .fixture (autouse = True )
1721def _clear_executor_cache () -> Generator [None , None , None ]:
@@ -29,6 +33,7 @@ def test_health_returns_ok_when_backend_healthy() -> None:
2933 body = response .json ()
3034 assert body ["status" ] == "ok"
3135 assert body ["message" ] is None
36+ assert body ["version" ] == SERVICE_VERSION
3237
3338
3439def test_health_returns_error_when_backend_unhealthy () -> None :
@@ -42,6 +47,32 @@ def test_health_returns_error_when_backend_unhealthy() -> None:
4247 body = response .json ()
4348 assert body ["status" ] == "error"
4449 assert body ["message" ] == "daemon down"
50+ assert body ["version" ] == SERVICE_VERSION
51+
52+
53+ def test_health_version_matches_package_metadata () -> None :
54+ """The version should come from the installed package, not be hardcoded."""
55+ from importlib .metadata import version as package_version
56+
57+ assert package_version ("code-interpreter" ) == SERVICE_VERSION
58+
59+
60+ def test_service_version_matches_helm_chart_version () -> None :
61+ """Guard against drift between the Python package and the Helm chart.
62+
63+ A version mismatch means clients calling /health to gate on capabilities
64+ would see one number while the deployment artifact reports another.
65+ """
66+ assert CHART_YAML .is_file (), f"Chart.yaml not found at { CHART_YAML } "
67+ text = CHART_YAML .read_text (encoding = "utf-8" )
68+ match = re .search (r"^version:\s*(\S+)\s*$" , text , re .MULTILINE )
69+ assert match is not None , f"could not find a top-level 'version:' line in { CHART_YAML } "
70+ chart_version = match .group (1 ).strip ("\" '" )
71+ assert chart_version == SERVICE_VERSION , (
72+ f"Helm chart version { chart_version !r} != Python package version "
73+ f"{ SERVICE_VERSION !r} . Bump both together so /health and the deployed "
74+ "chart report the same number."
75+ )
4576
4677
4778def _make_completed (returncode : int , stderr : bytes = b"" ) -> subprocess .CompletedProcess [bytes ]:
0 commit comments