"""The container's TTS engine must be env-driven, not hardcoded.

Regression for a bug where docker/jarvis-config.template.json hardcoded
`"tts_engine": "piper"`. The bridge reads the rendered /app/config/jarvis.json
*before* the environment, so a hardcoded "piper" overrode `TTS_ENGINE=melo` in
.env and the bot read Korean text with the English Piper voice ("foreign
accent"). The template must carry `${TTS_ENGINE}` so envsubst (entrypoint.sh)
renders whatever engine the deployment configured.
"""

import json
import string
from pathlib import Path

TEMPLATE = Path(__file__).resolve().parent.parent / "docker" / "jarvis-config.template.json"


def _render(**env) -> dict:
    """Mimic entrypoint.sh `envsubst < template`: substitute env vars, leaving
    any unset ones as literal text (valid JSON string values)."""
    raw = TEMPLATE.read_text(encoding="utf-8")
    return json.loads(string.Template(raw).safe_substitute(**env))


def test_template_does_not_hardcode_an_engine():
    raw = TEMPLATE.read_text(encoding="utf-8")
    assert '"tts_engine": "${TTS_ENGINE}"' in raw
    assert '"tts_engine": "piper"' not in raw
    assert '"tts_engine": "melo"' not in raw


def test_rendered_engine_follows_env():
    assert _render(TTS_ENGINE="melo")["tts_engine"] == "melo"
    assert _render(TTS_ENGINE="piper")["tts_engine"] == "piper"
    assert _render(TTS_ENGINE="xtts")["tts_engine"] == "xtts"
