from __future__ import annotations

import terminaluse.lib.client as client_module


def test_terminaluse_forwards_logging_config(monkeypatch) -> None:
    captured: dict[str, object] = {}

    def _fake_init(self, **kwargs):  # type: ignore[no-untyped-def]
        captured.update(kwargs)

    monkeypatch.setattr(client_module.TerminalUseBase, "__init__", _fake_init)

    logging_config = {"level": "debug", "silent": True}
    client_module.TerminalUse(
        base_url="https://api.terminaluse.test",
        token="test-token",
        agent_api_key="test-agent-key",
        auto_refresh_token=False,
        logging=logging_config,
    )

    assert captured["logging"] == logging_config


def test_async_terminaluse_forwards_logging_config(monkeypatch) -> None:
    captured: dict[str, object] = {}

    def _fake_init(self, **kwargs):  # type: ignore[no-untyped-def]
        captured.update(kwargs)

    monkeypatch.setattr(client_module.AsyncTerminalUseBase, "__init__", _fake_init)

    logging_config = {"level": "debug", "silent": True}
    client_module.AsyncTerminalUse(
        base_url="https://api.terminaluse.test",
        token="test-token",
        agent_api_key="test-agent-key",
        auto_refresh_token=False,
        logging=logging_config,
    )

    assert captured["logging"] == logging_config


def test_terminaluse_normalizes_sdk_logging_level(monkeypatch) -> None:
    captured: dict[str, object] = {}

    def _fake_init(self, **kwargs):  # type: ignore[no-untyped-def]
        captured.update(kwargs)

    monkeypatch.setattr(client_module.TerminalUseBase, "__init__", _fake_init)

    client_module.TerminalUse(
        base_url="https://api.terminaluse.test",
        token="test-token",
        agent_api_key="test-agent-key",
        auto_refresh_token=False,
        logging={"level": "WARNING", "silent": True},
    )

    assert captured["logging"] == {"level": "warn", "silent": True}


def test_terminaluse_rejects_invalid_logging_level() -> None:
    try:
        client_module.TerminalUse(
            base_url="https://api.terminaluse.test",
            token="test-token",
            agent_api_key="test-agent-key",
            auto_refresh_token=False,
            logging={"level": "verbose", "silent": True},
        )
    except ValueError as exc:
        assert str(exc) == "logging.level must be one of: debug, info, warn, error"
    else:  # pragma: no cover - defensive failure path
        raise AssertionError("Expected ValueError for invalid logging.level")
