# allow: no-sut-import — black-box HTTP test; drives real routes through the Flask test client
"""
Tests for chat page routes (GET /chat/ and GET /chat/<session_id>).

These tests verify:
- Page routes require login
- Page renders with and without session_id
- Invalid session_id is handled gracefully
"""

import json


class TestChatPageRoutes:
    """Tests for chat page rendering endpoints."""

    def test_chat_page_requires_login(self, client):
        """Test that GET /chat/ requires authentication."""
        response = client.get("/chat/", follow_redirects=False)
        # Should redirect to login page
        assert response.status_code in (302, 303)
        assert "/login" in response.location or response.status_code == 401

    def test_chat_page_renders_without_session_id(self, authenticated_client):
        """Test that GET /chat/ renders successfully for authenticated users."""
        response = authenticated_client.get("/chat/")
        assert response.status_code == 200
        # The response should be HTML (from render_template)
        assert b"<!DOCTYPE html>" in response.data or b"<html" in response.data

    def test_chat_page_renders_with_valid_session_id(
        self, authenticated_client
    ):
        """Test that GET /chat/<session_id> renders with a session ID."""
        # First create a session
        create_response = authenticated_client.post(
            "/api/chat/sessions",
            json={"initial_query": "Test query"},
            content_type="application/json",
        )
        session_id = json.loads(create_response.data)["session_id"]

        # Now load the chat page with that session
        response = authenticated_client.get(f"/chat/{session_id}")
        assert response.status_code == 200
        assert b"<!DOCTYPE html>" in response.data or b"<html" in response.data

    def test_chat_page_handles_invalid_session_id(self, authenticated_client):
        """Test that GET /chat/<session_id> with invalid session_id still renders.

        The page should render (status 200) because session validation happens
        client-side via JavaScript API calls, not server-side during page load.
        """
        invalid_session_id = "non-existent-session-id-12345"
        response = authenticated_client.get(f"/chat/{invalid_session_id}")
        # Page should still render - session validation is client-side
        assert response.status_code == 200

    def test_chat_page_with_session_requires_login(self, client):
        """Test that GET /chat/<session_id> requires authentication."""
        response = client.get("/chat/some-session-id", follow_redirects=False)
        # Should redirect to login page
        assert response.status_code in (302, 303)
        assert "/login" in response.location or response.status_code == 401
