"""Tests for web/utils/request_helpers.py.

Locks in parse_bool_arg's behavior contract: it MUST behave identically to
the inline pattern ``request.args.get(name, "false_or_true").lower() == "true"``
that it replaces. In particular: no whitespace stripping, no widening of the
truthy set beyond the literal string ``"true"`` (case-insensitive).
"""

from flask import Flask

from local_deep_research.web.utils.request_helpers import parse_bool_arg


def _make_app():
    """Create a minimal Flask test app."""
    app = Flask(__name__)
    app.config["TESTING"] = True
    return app


class TestParseBoolArg:
    """Tests for parse_bool_arg."""

    def test_missing_param_returns_default_false(self):
        """Absent param returns the default (False)."""
        app = _make_app()
        with app.test_request_context("/?other=value"):
            assert parse_bool_arg("flag") is False

    def test_missing_param_returns_default_true(self):
        """Absent param returns the default (True) when default=True."""
        app = _make_app()
        with app.test_request_context("/?other=value"):
            assert parse_bool_arg("flag", default=True) is True

    def test_value_true_lowercase(self):
        """?flag=true returns True."""
        app = _make_app()
        with app.test_request_context("/?flag=true"):
            assert parse_bool_arg("flag") is True

    def test_value_true_uppercase(self):
        """?flag=TRUE returns True (case-insensitive)."""
        app = _make_app()
        with app.test_request_context("/?flag=TRUE"):
            assert parse_bool_arg("flag") is True

    def test_value_true_mixed_case(self):
        """?flag=True returns True (case-insensitive)."""
        app = _make_app()
        with app.test_request_context("/?flag=True"):
            assert parse_bool_arg("flag") is True

    def test_value_false(self):
        """?flag=false returns False."""
        app = _make_app()
        with app.test_request_context("/?flag=false"):
            assert parse_bool_arg("flag", default=True) is False

    def test_empty_value_returns_false(self):
        """?flag= (empty string) returns False, NOT the default.

        This matches the inline pattern: ``request.args.get("flag", "false")``
        returns the empty string when the param is present-but-empty, not the
        default. The empty string lowercases to "" which != "true", so False.
        """
        app = _make_app()
        with app.test_request_context("/?flag="):
            # Even with default=True, an empty value still returns False.
            assert parse_bool_arg("flag", default=True) is False

    def test_value_yes_returns_false(self):
        """?flag=yes returns False — only "true" is truthy."""
        app = _make_app()
        with app.test_request_context("/?flag=yes"):
            assert parse_bool_arg("flag") is False

    def test_value_one_returns_false(self):
        """?flag=1 returns False — only "true" is truthy."""
        app = _make_app()
        with app.test_request_context("/?flag=1"):
            assert parse_bool_arg("flag") is False

    def test_value_on_returns_false(self):
        """?flag=on returns False — only "true" is truthy."""
        app = _make_app()
        with app.test_request_context("/?flag=on"):
            assert parse_bool_arg("flag") is False

    def test_whitespace_padded_true_returns_false(self):
        """?flag=%20true%20 returns False — whitespace is NOT stripped.

        This locks in the no-strip behavior: a stripping helper would return
        True here, which would diverge from the original inline pattern.
        """
        app = _make_app()
        with app.test_request_context("/?flag=%20true%20"):
            assert parse_bool_arg("flag") is False
