from app.providers.llm.local_openai_compatible import LocalOpenAICompatibleLLM


def _llm(system_prompt="Sei kurz."):
    return LocalOpenAICompatibleLLM("http://x/v1", "k", "m", system_prompt=system_prompt)


def test_single_system_message_when_history_has_system():
    llm = _llm()
    history = [
        {"role": "system", "content": "Was du ueber den Nutzer weisst:\n- heisst Anna"},
        {"role": "user", "content": "Hallo"},
        {"role": "assistant", "content": "Hi Anna"},
    ]
    msgs = llm._build_messages("Wie geht es dir?", history)

    # Genau EINE System-Nachricht, ganz am Anfang (Qwen3-Template-Anforderung).
    assert sum(1 for m in msgs if m["role"] == "system") == 1
    assert msgs[0]["role"] == "system"
    assert "Sei kurz." in msgs[0]["content"]
    assert "heisst Anna" in msgs[0]["content"]
    # Reihenfolge der Nicht-System-Nachrichten bleibt erhalten, User zuletzt.
    assert [m["role"] for m in msgs] == ["system", "user", "assistant", "user"]
    assert msgs[-1] == {"role": "user", "content": "Wie geht es dir?"}


def test_no_system_message_without_prompt_or_history():
    llm = _llm(system_prompt="")
    msgs = llm._build_messages("Hallo", None)
    assert msgs == [{"role": "user", "content": "Hallo"}]
