"""Test Tracer classes."""
from __future__ import annotations

import json
from datetime import datetime
from typing import Tuple
from unittest.mock import patch
from uuid import UUID, uuid4

import pytest
from freezegun import freeze_time

from langchain.callbacks.tracers.langchain import LangChainTracer
from langchain.callbacks.tracers.schemas import Run, RunTypeEnum, TracerSession
from langchain.schema import LLMResult

_SESSION_ID = UUID("4fbf7c55-2727-4711-8964-d821ed4d4e2a")
_TENANT_ID = UUID("57a08cc4-73d2-4236-8378-549099d07fad")


@pytest.fixture
def lang_chain_tracer_v2(monkeypatch: pytest.MonkeyPatch) -> LangChainTracer:
    monkeypatch.setenv("LANGCHAIN_TENANT_ID", "test-tenant-id")
    monkeypatch.setenv("LANGCHAIN_ENDPOINT", "http://test-endpoint.com")
    monkeypatch.setenv("LANGCHAIN_API_KEY", "foo")
    tracer = LangChainTracer()
    return tracer


# Mock a sample TracerSession object
@pytest.fixture
def sample_tracer_session_v2() -> TracerSession:
    return TracerSession(id=_SESSION_ID, name="Sample session", tenant_id=_TENANT_ID)


@freeze_time("2023-01-01")
@pytest.fixture
def sample_runs() -> Tuple[Run, Run, Run]:
    llm_run = Run(
        id="57a08cc4-73d2-4236-8370-549099d07fad",
        name="llm_run",
        execution_order=1,
        child_execution_order=1,
        parent_run_id="57a08cc4-73d2-4236-8371-549099d07fad",
        start_time=datetime.utcnow(),
        end_time=datetime.utcnow(),
        session_id=1,
        inputs={"prompts": []},
        outputs=LLMResult(generations=[[]]).dict(),
        serialized={},
        extra={},
        run_type=RunTypeEnum.llm,
    )
    chain_run = Run(
        id="57a08cc4-73d2-4236-8371-549099d07fad",
        name="chain_run",
        execution_order=1,
        start_time=datetime.utcnow(),
        end_time=datetime.utcnow(),
        child_execution_order=1,
        serialized={},
        inputs={},
        outputs={},
        child_runs=[llm_run],
        extra={},
        run_type=RunTypeEnum.chain,
    )

    tool_run = Run(
        id="57a08cc4-73d2-4236-8372-549099d07fad",
        name="tool_run",
        execution_order=1,
        child_execution_order=1,
        inputs={"input": "test"},
        start_time=datetime.utcnow(),
        end_time=datetime.utcnow(),
        outputs=None,
        serialized={},
        child_runs=[],
        extra={},
        run_type=RunTypeEnum.tool,
    )
    return llm_run, chain_run, tool_run


def test_persist_run(
    lang_chain_tracer_v2: LangChainTracer,
    sample_tracer_session_v2: TracerSession,
    sample_runs: Tuple[Run, Run, Run],
) -> None:
    """Test that persist_run method calls requests.post once per method call."""
    with patch("langchain.callbacks.tracers.langchain.requests.post") as post, patch(
        "langchain.callbacks.tracers.langchain.requests.get"
    ) as get:
        post.return_value.raise_for_status.return_value = None
        lang_chain_tracer_v2.session = sample_tracer_session_v2
        for run in sample_runs:
            lang_chain_tracer_v2.run_map[str(run.id)] = run
        for run in sample_runs:
            lang_chain_tracer_v2._end_trace(run)

        assert post.call_count == 3
        assert get.call_count == 0


def test_persist_run_with_example_id(
    lang_chain_tracer_v2: LangChainTracer,
    sample_tracer_session_v2: TracerSession,
    sample_runs: Tuple[Run, Run, Run],
) -> None:
    """Test the example ID is assigned only to the parent run and not the children."""
    example_id = uuid4()
    llm_run, chain_run, tool_run = sample_runs
    chain_run.child_runs = [tool_run]
    tool_run.child_runs = [llm_run]
    with patch("langchain.callbacks.tracers.langchain.requests.post") as post, patch(
        "langchain.callbacks.tracers.langchain.requests.get"
    ) as get:
        post.return_value.raise_for_status.return_value = None
        lang_chain_tracer_v2.session = sample_tracer_session_v2
        lang_chain_tracer_v2.example_id = example_id
        lang_chain_tracer_v2._persist_run(chain_run)

        assert post.call_count == 3
        assert get.call_count == 0
        posted_data = [
            json.loads(call_args[1]["data"]) for call_args in post.call_args_list
        ]
        assert posted_data[0]["id"] == str(chain_run.id)
        assert posted_data[0]["reference_example_id"] == str(example_id)
        assert posted_data[1]["id"] == str(tool_run.id)
        assert not posted_data[1].get("reference_example_id")
        assert posted_data[2]["id"] == str(llm_run.id)
        assert not posted_data[2].get("reference_example_id")

langchain.callbacks.tracers.langchain.LangChainTracer langchain.schema.LLMResult langchain.callbacks.tracers.schemas.TracerSession
[((441, 485), 'uuid.UUID', 'UUID', (['"""4fbf7c55-2727-4711-8964-d821ed4d4e2a"""'], {}), "('4fbf7c55-2727-4711-8964-d821ed4d4e2a')\n", (445, 485), False, 'from uuid import UUID, uuid4\n'), ((499, 543), 'uuid.UUID', 'UUID', (['"""57a08cc4-73d2-4236-8378-549099d07fad"""'], {}), "('57a08cc4-73d2-4236-8378-549099d07fad')\n", (503, 543), False, 'from uuid import UUID, uuid4\n'), ((1070, 1095), 'freezegun.freeze_time', 'freeze_time', (['"""2023-01-01"""'], {}), "('2023-01-01')\n", (1081, 1095), False, 'from freezegun import freeze_time\n'), ((841, 858), 'langchain.callbacks.tracers.langchain.LangChainTracer', 'LangChainTracer', ([], {}), '()\n', (856, 858), False, 'from langchain.callbacks.tracers.langchain import LangChainTracer\n'), ((992, 1066), 'langchain.callbacks.tracers.schemas.TracerSession', 'TracerSession', ([], {'id': '_SESSION_ID', 'name': '"""Sample session"""', 'tenant_id': '_TENANT_ID'}), "(id=_SESSION_ID, name='Sample session', tenant_id=_TENANT_ID)\n", (1005, 1066), False, 'from langchain.callbacks.tracers.schemas import Run, RunTypeEnum, TracerSession\n'), ((3506, 3513), 'uuid.uuid4', 'uuid4', ([], {}), '()\n', (3511, 3513), False, 'from uuid import UUID, uuid4\n'), ((2700, 2760), 'unittest.mock.patch', 'patch', (['"""langchain.callbacks.tracers.langchain.requests.post"""'], {}), "('langchain.callbacks.tracers.langchain.requests.post')\n", (2705, 2760), False, 'from unittest.mock import patch\n'), ((2770, 2829), 'unittest.mock.patch', 'patch', (['"""langchain.callbacks.tracers.langchain.requests.get"""'], {}), "('langchain.callbacks.tracers.langchain.requests.get')\n", (2775, 2829), False, 'from unittest.mock import patch\n'), ((3644, 3704), 'unittest.mock.patch', 'patch', (['"""langchain.callbacks.tracers.langchain.requests.post"""'], {}), "('langchain.callbacks.tracers.langchain.requests.post')\n", (3649, 3704), False, 'from unittest.mock import patch\n'), ((3714, 3773), 'unittest.mock.patch', 'patch', (['"""langchain.callbacks.tracers.langchain.requests.get"""'], {}), "('langchain.callbacks.tracers.langchain.requests.get')\n", (3719, 3773), False, 'from unittest.mock import patch\n'), ((1390, 1407), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (1405, 1407), False, 'from datetime import datetime\n'), ((1426, 1443), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (1441, 1443), False, 'from datetime import datetime\n'), ((1776, 1793), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (1791, 1793), False, 'from datetime import datetime\n'), ((1812, 1829), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (1827, 1829), False, 'from datetime import datetime\n'), ((2226, 2243), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (2241, 2243), False, 'from datetime import datetime\n'), ((2262, 2279), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (2277, 2279), False, 'from datetime import datetime\n'), ((4137, 4169), 'json.loads', 'json.loads', (["call_args[1]['data']"], {}), "(call_args[1]['data'])\n", (4147, 4169), False, 'import json\n'), ((1515, 1542), 'langchain.schema.LLMResult', 'LLMResult', ([], {'generations': '[[]]'}), '(generations=[[]])\n', (1524, 1542), False, 'from langchain.schema import LLMResult\n')]