# retoor <retoor@molodetz.nl>

import re

import devplacepy.main as m
from starlette.testclient import TestClient
def _patch_limits(monkeypatch, limit, window=60):
    def fake_get_int_setting(key, default):
        return {
            "rate_limit_per_minute": limit,
            "rate_limit_window_seconds": window,
        }.get(key, default)

    monkeypatch.setattr(m, "get_int_setting", fake_get_int_setting)
    # conftest disables the limiter suite-wide; re-enable it for these tests only.
    monkeypatch.setattr(m, "RATE_LIMIT_DISABLED", False)
    m._rate_limit_store.clear()
    # _hot_settings caches the limit for HOT_SETTINGS_TTL; force a re-read of the
    # patched get_int_setting so a stale value from a prior test cannot leak in.
    m._hot_settings_value = None
    m._hot_settings_at = 0.0


def test_rate_limit_blocks_excess(monkeypatch):
    _patch_limits(monkeypatch, 3)
    client = TestClient(m.app)
    codes = [
        client.post("/", headers={"X-Real-IP": "9.9.9.9"}).status_code for _ in range(6)
    ]
    assert 429 in codes, codes
    assert codes[-1] == 429


def test_rate_limit_is_per_ip(monkeypatch):
    _patch_limits(monkeypatch, 2)
    client = TestClient(m.app)
    for _ in range(2):
        client.post("/", headers={"X-Real-IP": "1.1.1.1"})
    blocked = client.post("/", headers={"X-Real-IP": "1.1.1.1"}).status_code
    other_ip = client.post("/", headers={"X-Real-IP": "2.2.2.2"}).status_code
    assert blocked == 429
    assert other_ip != 429


def test_rate_limit_reads_configured_setting(monkeypatch):
    _patch_limits(monkeypatch, 1)
    client = TestClient(m.app)
    first = client.post("/", headers={"X-Real-IP": "7.7.7.7"}).status_code
    second = client.post("/", headers={"X-Real-IP": "7.7.7.7"}).status_code
    assert first != 429
    assert second == 429


def test_response_time_header_present():
    client = TestClient(m.app)
    response = client.get("/")
    assert response.status_code == 200
    value = response.headers.get("X-Response-Time")
    assert value is not None and value.endswith("ms")
    assert float(value[:-2]) >= 0


def test_response_time_indicator_rendered_on_html():
    client = TestClient(m.app)
    match = re.search(
        r'<div class="response-time-indicator"[^>]*>([\d.]+) ms</div>',
        client.get("/").text,
    )
    assert match is not None
    assert float(match.group(1)) >= 0


def test_response_time_indicator_absent_from_json():
    client = TestClient(m.app)
    response = client.get("/feed", headers={"Accept": "application/json"})
    assert "response-time-indicator" not in response.text
    assert response.headers.get("X-Response-Time", "").endswith("ms")
