# [DEF:backend.src.services.__tests__.test_llm_prompt_templates:Module]
# @TIER: STANDARD
# @SEMANTICS: tests, llm, prompts, templates, settings
# @PURPOSE: Validate normalization and rendering behavior for configurable LLM prompt templates.
# @LAYER: Domain Tests
# @RELATION: DEPENDS_ON -> backend.src.services.llm_prompt_templates
# @INVARIANT: All required prompt keys remain available after normalization.

from src.services.llm_prompt_templates import (
    DEFAULT_LLM_ASSISTANT_SETTINGS,
    DEFAULT_LLM_PROVIDER_BINDINGS,
    DEFAULT_LLM_PROMPTS,
    is_multimodal_model,
    normalize_llm_settings,
    resolve_bound_provider_id,
    render_prompt,
)


# [DEF:test_normalize_llm_settings_adds_default_prompts:Function]
# @TIER: STANDARD
# @PURPOSE: Ensure legacy/partial llm settings are expanded with all prompt defaults.
# @PRE: Input llm settings do not contain complete prompts object.
# @POST: Returned structure includes required prompt templates with fallback defaults.
def test_normalize_llm_settings_adds_default_prompts():
    normalized = normalize_llm_settings({"default_provider": "x"})

    assert "prompts" in normalized
    assert "provider_bindings" in normalized
    assert normalized["default_provider"] == "x"
    for key in DEFAULT_LLM_PROMPTS:
        assert key in normalized["prompts"]
        assert isinstance(normalized["prompts"][key], str)
    for key in DEFAULT_LLM_PROVIDER_BINDINGS:
        assert key in normalized["provider_bindings"]
    for key in DEFAULT_LLM_ASSISTANT_SETTINGS:
        assert key in normalized
# [/DEF:test_normalize_llm_settings_adds_default_prompts:Function]


# [DEF:test_normalize_llm_settings_keeps_custom_prompt_values:Function]
# @TIER: STANDARD
# @PURPOSE: Ensure user-customized prompt values are preserved during normalization.
# @PRE: Input llm settings contain custom prompt override.
# @POST: Custom prompt value remains unchanged in normalized output.
def test_normalize_llm_settings_keeps_custom_prompt_values():
    custom = "Doc for {dataset_name} using {columns_json}"
    normalized = normalize_llm_settings(
        {"prompts": {"documentation_prompt": custom}}
    )

    assert normalized["prompts"]["documentation_prompt"] == custom
# [/DEF:test_normalize_llm_settings_keeps_custom_prompt_values:Function]


# [DEF:test_render_prompt_replaces_known_placeholders:Function]
# @TIER: STANDARD
# @PURPOSE: Ensure template placeholders are deterministically replaced.
# @PRE: Template contains placeholders matching provided variables.
# @POST: Rendered prompt string contains substituted values.
def test_render_prompt_replaces_known_placeholders():
    rendered = render_prompt(
        "Hello {name}, diff={diff}",
        {"name": "bot", "diff": "A->B"},
    )

    assert rendered == "Hello bot, diff=A->B"
# [/DEF:test_render_prompt_replaces_known_placeholders:Function]


# [DEF:test_is_multimodal_model_detects_known_vision_models:Function]
# @TIER: STANDARD
# @PURPOSE: Ensure multimodal model detection recognizes common vision-capable model names.
def test_is_multimodal_model_detects_known_vision_models():
    assert is_multimodal_model("gpt-4o") is True
    assert is_multimodal_model("claude-3-5-sonnet") is True
    assert is_multimodal_model("stepfun/step-3.5-flash:free", "openrouter") is False
    assert is_multimodal_model("text-only-model") is False
# [/DEF:test_is_multimodal_model_detects_known_vision_models:Function]


# [DEF:test_resolve_bound_provider_id_prefers_binding_then_default:Function]
# @TIER: STANDARD
# @PURPOSE: Verify provider binding resolution priority.
def test_resolve_bound_provider_id_prefers_binding_then_default():
    settings = {
        "default_provider": "default-1",
        "provider_bindings": {"dashboard_validation": "vision-1"},
    }
    assert resolve_bound_provider_id(settings, "dashboard_validation") == "vision-1"
    assert resolve_bound_provider_id(settings, "documentation") == "default-1"
# [/DEF:test_resolve_bound_provider_id_prefers_binding_then_default:Function]


# [DEF:test_normalize_llm_settings_keeps_assistant_planner_settings:Function]
# @TIER: STANDARD
# @PURPOSE: Ensure assistant planner provider/model fields are preserved and normalized.
def test_normalize_llm_settings_keeps_assistant_planner_settings():
    normalized = normalize_llm_settings(
        {
            "assistant_planner_provider": "provider-a",
            "assistant_planner_model": "gpt-4.1-mini",
        }
    )
    assert normalized["assistant_planner_provider"] == "provider-a"
    assert normalized["assistant_planner_model"] == "gpt-4.1-mini"
# [/DEF:test_normalize_llm_settings_keeps_assistant_planner_settings:Function]


# [/DEF:backend.src.services.__tests__.test_llm_prompt_templates:Module]
