from time import sleep
from uuid import uuid4

import requests

from cancontech_tracing import init_tracing, traceable


@traceable()
def tracing_target_func(x, y):
    return x + y


def test_trace_creates_span():
    project_name = f"test-project-{uuid4()}"

    init_tracing(
        endpoint_url="http://127.0.0.1:6006/v1/traces",
        project_name=project_name,
        exporter_user_agent="x-omni-agent",
        processor_type="simple",  # so requests are not buffered in tests
        instrument_openai=False,
    )

    tracing_target_func(1, 2)
    sleep(0.1)

    url = f"http://127.0.0.1:6006/v1/projects/{project_name}/spans?limit=100"
    print("accessing " + url)
    response = requests.get(url)
    assert response.status_code == 200
    response_data = response.json()["data"]
    assert len(response_data) == 1
    assert response_data[0]["name"] == "tracing_target_func"
