from __future__ import annotations

import pytest

import oh_my_linear.server as server


class _FakeOfficial:
    def __init__(self):
        self.reconnect_calls: list[bool] = []

    def reconnect(self, *, ignore_cooldown: bool) -> dict[str, bool | int]:
        self.reconnect_calls.append(ignore_cooldown)
        return {"connected": True, "authRetryRemainingSeconds": 0}


def test_server_exports_main_and_mcp() -> None:
    assert callable(server.main)
    assert server.mcp is not None


def test_reauth_tool_is_not_exposed() -> None:
    assert not hasattr(server, "reauth")


def test_reauth_tool_not_registered_in_mcp_tool_registry() -> None:
    tool_names = set(server.mcp._tool_manager._tools.keys())
    assert "reconnect_official" in tool_names
    assert "reauth" not in tool_names


def test_reconnect_official_defaults_ignore_cooldown_false(monkeypatch: pytest.MonkeyPatch) -> None:
    fake_official = _FakeOfficial()
    monkeypatch.setattr(server, "_official", fake_official)

    result = server.reconnect_official()

    assert result == {"connected": True, "authRetryRemainingSeconds": 0}
    assert fake_official.reconnect_calls == [False]


def test_reconnect_official_forwards_ignore_cooldown_true(monkeypatch: pytest.MonkeyPatch) -> None:
    fake_official = _FakeOfficial()
    monkeypatch.setattr(server, "_official", fake_official)

    result = server.reconnect_official(ignoreCooldown=True)

    assert result == {"connected": True, "authRetryRemainingSeconds": 0}
    assert fake_official.reconnect_calls == [True]
