"""Interaction language is durable at the public session CLI boundary.""" import json from types import SimpleNamespace import pytest from typer.testing import CliRunner from tht.cli.session_cmd import session_app from tht.config import DatabaseConfig def configure(monkeypatch, tmp_path, language="it"): from tht.cli import session_cmd cfg = SimpleNamespace( database=DatabaseConfig(database="testdb", user="u", password="p", schema="public"), paths=SimpleNamespace(sessions=tmp_path), language=language, ) monkeypatch.setattr(session_cmd, "_load_config_or_exit", lambda _: cfg) return cfg def invoke(*args): result = CliRunner().invoke(session_app, list(args)) assert result.exit_code == 0, result.output return json.loads(result.stdout) def test_new_pins_question_language_and_preserves_workspace_content(monkeypatch, tmp_path): configure(monkeypatch, tmp_path) question = "Elenca i pazienti con età superiore a 65 anni" session_id = invoke("new", question, "--interaction-language", "en", "--json")["id"] manifest = invoke("show", session_id, "--json") assert manifest["interaction_language"] == "it" assert manifest["question"] == question assert invoke("list", "--json")[0]["interaction_language"] == "it" @pytest.mark.parametrize("fallback", ["en", "it", None]) def test_english_question_controls_interaction_in_italian_workspace(monkeypatch, tmp_path, fallback): configure(monkeypatch, tmp_path) question = "How many patients were admitted to the hospital last year?" args = ["--interaction-language", fallback] if fallback else [] created = invoke("new", question, *args, "--json") assert created["interaction_language"] == "en" assert invoke("show", created["id"], "--json")["interaction_language"] == "en" @pytest.mark.parametrize("question", ["", "q", "123", "HbA1c", "SELECT * FROM patients"]) def test_ambiguous_or_code_question_keeps_fallback(monkeypatch, tmp_path, question): configure(monkeypatch, tmp_path) created = invoke("new", question, "--interaction-language", "fr-FR", "--json") assert invoke("show", created["id"], "--json")["interaction_language"] == "fr-FR" @pytest.mark.parametrize("language", ["", "en_US", "en