import logging

import pytest

from terminaluse.lib.sandbox.entrypoint import _log_timing_or_invocation


@pytest.mark.unit
def test_timing_logs_default_to_debug(monkeypatch, caplog):
    monkeypatch.delenv("TERMINALUSE_SANDBOX_TIMING_LOGS", raising=False)
    logger = logging.getLogger("test.entrypoint.default")

    with caplog.at_level(logging.DEBUG):
        _log_timing_or_invocation(logger, "[TIMING] payload_read: 10ms")

    assert any(
        record.levelno == logging.DEBUG and "[TIMING] payload_read: 10ms" in record.message for record in caplog.records
    )


@pytest.mark.unit
def test_timing_logs_can_be_promoted_to_info(monkeypatch, caplog):
    monkeypatch.setenv("TERMINALUSE_SANDBOX_TIMING_LOGS", "true")
    logger = logging.getLogger("test.entrypoint.info")

    with caplog.at_level(logging.INFO):
        _log_timing_or_invocation(logger, "[TIMING] payload_read: 10ms")

    assert any(
        record.levelno == logging.INFO and "[TIMING] payload_read: 10ms" in record.message for record in caplog.records
    )
