"""IT-TERM-001 — WebSSH WebSocket rejects missing/invalid token."""

from __future__ import annotations

from unittest.mock import AsyncMock, MagicMock

import pytest

from server.api.webssh import terminal_ws

pytestmark = pytest.mark.integration


@pytest.mark.asyncio
async def test_terminal_ws_closes_without_token():
    ws = AsyncMock()
    ws.close = AsyncMock()
    ws.client = MagicMock()
    ws.client.host = "127.0.0.1"

    await terminal_ws(ws, server_id=1, token=None)

    ws.close.assert_awaited_once_with(code=4001, reason="Missing JWT token")
    ws.accept.assert_not_called()


@pytest.mark.asyncio
async def test_terminal_ws_closes_with_invalid_token():
    ws = AsyncMock()
    ws.close = AsyncMock()
    ws.client = MagicMock()
    ws.client.host = "127.0.0.1"

    await terminal_ws(ws, server_id=1, token="not-a-valid-jwt")

    ws.close.assert_awaited_once_with(code=4401, reason="Invalid or expired JWT token")
