# Copyright 2025 Softwell S.r.l.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tests for Login Page sys_app."""

from genro_asgi.sys_applications.login_page import Application


class TestLoginPageApplication:
    """Tests for login_page Application."""

    def _make_app(self, **kwargs: str) -> Application:
        """Create Application instance with optional kwargs."""
        return Application(**kwargs)

    def test_init_default_settings(self) -> None:
        """Default cookie and redirect settings are applied."""
        app = self._make_app()
        assert app._cookie_name == "session_id"
        assert app._cookie_path == "/"
        assert app._redirect_to == "/"
        assert app._login_endpoint == "/_server/login"

    def test_init_custom_settings(self) -> None:
        """Custom kwargs override default settings."""
        app = self._make_app(
            cookie_name="my_session",
            cookie_path="/app",
            redirect_to="/dashboard",
            login_endpoint="/api/login",
        )
        assert app._cookie_name == "my_session"
        assert app._cookie_path == "/app"
        assert app._redirect_to == "/dashboard"
        assert app._login_endpoint == "/api/login"

    def test_index_returns_html(self) -> None:
        """Index endpoint returns HTML content."""
        app = self._make_app()
        result = app.index()
        assert "<!DOCTYPE html>" in result
        assert "login-form" in result

    def test_settings_endpoint_default(self) -> None:
        """Settings endpoint returns default configuration."""
        app = self._make_app()
        result = app.settings()
        assert result == {
            "login_endpoint": "/_server/login",
            "cookie_name": "session_id",
            "cookie_path": "/",
            "redirect_to": "/",
        }

    def test_settings_endpoint_custom(self) -> None:
        """Settings endpoint reflects custom configuration."""
        app = self._make_app(cookie_name="tok", redirect_to="/home")
        result = app.settings()
        assert result["cookie_name"] == "tok"
        assert result["redirect_to"] == "/home"

    def test_creates_main_router(self) -> None:
        """Application creates main router."""
        app = self._make_app()
        assert app.main is not None
        assert app.main.name == "main"
