from __future__ import annotations

import uuid

from security import HoneypotResponder


def test_should_use_honeypot_threshold():
    responder = HoneypotResponder()
    assert responder.should_use_honeypot(79) is False
    assert responder.should_use_honeypot(80) is True
    assert responder.should_use_honeypot(100) is True


def test_generate_fake_response_email():
    responder = HoneypotResponder()
    resp = responder.generate_fake_response("/api/forgot-password")
    assert resp["success"] is True
    assert resp["message"] == "邮件已发送"


def test_generate_fake_response_register_contains_fake_uuid():
    responder = HoneypotResponder()
    resp = responder.generate_fake_response("/api/register")
    assert resp["success"] is True
    assert "user_id" in resp
    uuid.UUID(resp["user_id"])


def test_generate_fake_response_login():
    responder = HoneypotResponder()
    resp = responder.generate_fake_response("/api/login")
    assert resp == {"success": True}


def test_generate_fake_response_generic():
    responder = HoneypotResponder()
    resp = responder.generate_fake_response("/api/tasks/run")
    assert resp["success"] is True
    assert resp["message"] == "操作成功"


def test_delay_response_ranges():
    responder = HoneypotResponder()

    assert responder.delay_response(0) == 0
    assert responder.delay_response(20) == 0

    d = responder.delay_response(21)
    assert 0.5 <= d <= 1.0
    d = responder.delay_response(50)
    assert 0.5 <= d <= 1.0

    d = responder.delay_response(51)
    assert 1.0 <= d <= 3.0
    d = responder.delay_response(80)
    assert 1.0 <= d <= 3.0

    d = responder.delay_response(81)
    assert 3.0 <= d <= 8.0
    d = responder.delay_response(100)
    assert 3.0 <= d <= 8.0

