"""Integration tests for GeminiModel internal conversions.

These tests avoid mocks while exercising internal transformations and
conversation handling without making external API calls.
"""

from kiss.core.models.gemini_model import GeminiModel


class TestGeminiModelConversationConversion:
    """Tests for GeminiModel conversation conversion and helpers."""

    def _model(self) -> GeminiModel:
        model = GeminiModel("gemini-3-flash-preview", api_key="test")
        model.model_config = {}
        return model

    def test_convert_conversation_with_tools_and_signatures(self):
        model = self._model()
        model._thought_signatures = {"call_1": b"sig-1"}
        model.conversation = [
            {"role": "user", "content": "Hello"},
            {"role": "assistant", "content": "Answer"},
            {
                "role": "assistant",
                "content": "",
                "tool_calls": [
                    {"id": "call_1", "function": {"name": "tool_a", "arguments": {"x": 1}}},
                    {"id": "call_2", "function": {"name": "tool_b", "arguments": {"y": 2}}},
                ],
            },
            {"role": "tool", "tool_call_id": "call_1", "content": '{"result": "ok"}'},
            {"role": "tool", "tool_call_id": "call_2", "content": {"result": "ok2"}},
            {"role": "tool", "tool_call_id": "call_missing", "content": "not json"},
            {"role": "system", "content": "ignored"},
        ]

        contents = model._convert_conversation_to_gemini_contents()
        assert len(contents) >= 4
        assert any(content.role == "model" for content in contents)
        assert any(content.role == "user" for content in contents)

    def test_extract_token_counts_no_usage(self):
        model = self._model()

        class Dummy:
            usage_metadata = None

        assert model.extract_input_output_token_counts_from_response(Dummy()) == (0, 0, 0, 0)
