"""Integration tests for multi-session terminal WebSocket and REST API."""

import pytest
from fastapi.testclient import TestClient
from src.main import app


@pytest.fixture
def client():
    return TestClient(app)


class TestTerminalWebSocketMultiSession:
    """Tests for multi-session WebSocket routing."""

    def test_specific_session_websocket_route_exists(self, client):
        """The specific session WebSocket route should be registered."""
        # We can't easily test WebSocket without auth, but we can verify
        # the route exists by checking for a 403 (no auth cookie)
        response = client.get("/ws/tool-instances/test-instance/terminal/test-session")
        # WebSocket endpoint returns 403 when accessed via HTTP GET
        assert response.status_code in (403, 404)

    def test_default_session_alias_route_exists(self, client):
        """The default session alias route should still exist."""
        response = client.get("/ws/tool-instances/test-instance/terminal")
        assert response.status_code in (403, 404)


class TestTerminalRestApi:
    """Tests for REST API endpoints."""

    def test_list_sessions_requires_auth(self, client):
        """List sessions endpoint requires authentication."""
        response = client.get("/instances/test/terminal/sessions")
        assert response.status_code == 401

    def test_create_session_requires_auth(self, client):
        """Create session endpoint requires authentication."""
        response = client.post(
            "/instances/test/terminal/sessions",
            json={},
        )
        assert response.status_code == 401

    def test_close_session_requires_auth(self, client):
        """Close session endpoint requires authentication."""
        response = client.delete("/instances/test/terminal/sessions/test-session")
        assert response.status_code == 401

    def test_reset_session_requires_auth(self, client):
        """Reset session endpoint requires authentication."""
        response = client.post("/instances/test/terminal/sessions/test-session/reset")
        assert response.status_code == 401

    def test_rename_session_requires_auth(self, client):
        """Rename session endpoint requires authentication."""
        response = client.post(
            "/instances/test/terminal/sessions/test-session/rename",
            json={"name": "New Name"},
        )
        assert response.status_code == 401

    def test_legacy_reset_alias_requires_auth(self, client):
        """Legacy reset endpoint still requires auth."""
        response = client.post("/instances/test/terminal/reset")
        assert response.status_code == 401
