import pytest
import asyncio
from app.core.observability import benchmark, time_it


def test_benchmark_context_manager(caplog):
    import logging

    with caplog.at_level(logging.INFO):
        with benchmark("Test Task"):
            # simulate work
            pass

    assert any(
        "[BENCHMARK] Test Task completed in" in record.message
        for record in caplog.records
    )


def test_time_it_decorator_sync(caplog):
    import logging

    @time_it("Sync Helper")
    def my_sync_fn(x, y):
        return x + y

    with caplog.at_level(logging.INFO):
        res = my_sync_fn(10, 20)

    assert res == 30
    assert any(
        "[BENCHMARK] Sync Helper completed in" in record.message
        for record in caplog.records
    )


@pytest.mark.asyncio
async def test_time_it_decorator_async(caplog):
    import logging

    @time_it("Async Helper")
    async def my_async_fn():
        await asyncio.sleep(0.01)
        return "done"

    with caplog.at_level(logging.INFO):
        res = await my_async_fn()

    assert res == "done"
    assert any(
        "[BENCHMARK] Async Helper completed in" in record.message
        for record in caplog.records
    )
