"""Integration tests for the three-strike dashboard routes.

Refs: #962

Uses unique keys per test (uuid4) so parallel xdist workers and repeated
runs never collide on shared SQLite state.
"""

import uuid

import pytest


def _uid() -> str:
    """Return a short unique suffix for test keys."""
    return uuid.uuid4().hex[:8]


class TestThreeStrikeRoutes:
    @pytest.mark.unit
    def test_list_strikes_returns_200(self, client):
        response = client.get("/sovereignty/three-strike")
        assert response.status_code == 200
        data = response.json()
        assert "records" in data
        assert "categories" in data

    @pytest.mark.unit
    def test_list_blocked_returns_200(self, client):
        response = client.get("/sovereignty/three-strike/blocked")
        assert response.status_code == 200
        data = response.json()
        assert "blocked" in data

    @pytest.mark.unit
    def test_record_strike_first(self, client):
        key = f"test_btn_{_uid()}"
        response = client.post(
            "/sovereignty/three-strike/record",
            json={"category": "vlm_prompt_edit", "key": key},
        )
        assert response.status_code == 200
        data = response.json()
        assert data["count"] == 1
        assert data["blocked"] is False

    @pytest.mark.unit
    def test_record_invalid_category_returns_422(self, client):
        response = client.post(
            "/sovereignty/three-strike/record",
            json={"category": "not_a_real_category", "key": "x"},
        )
        assert response.status_code == 422

    @pytest.mark.unit
    def test_third_strike_returns_409(self, client):
        key = f"push_route_{_uid()}"
        for _ in range(2):
            client.post(
                "/sovereignty/three-strike/record",
                json={"category": "deployment_step", "key": key},
            )
        response = client.post(
            "/sovereignty/three-strike/record",
            json={"category": "deployment_step", "key": key},
        )
        assert response.status_code == 409
        data = response.json()
        assert data["detail"]["error"] == "three_strike_block"
        assert data["detail"]["count"] == 3

    @pytest.mark.unit
    def test_register_automation_returns_success(self, client):
        response = client.post(
            f"/sovereignty/three-strike/deployment_step/auto_{_uid()}/automation",
            json={"artifact_path": "scripts/auto.sh"},
        )
        assert response.status_code == 200
        assert response.json()["success"] is True

    @pytest.mark.unit
    def test_get_events_returns_200(self, client):
        key = f"events_{_uid()}"
        client.post(
            "/sovereignty/three-strike/record",
            json={"category": "vlm_prompt_edit", "key": key},
        )
        response = client.get(f"/sovereignty/three-strike/vlm_prompt_edit/{key}/events")
        assert response.status_code == 200
        data = response.json()
        assert data["category"] == "vlm_prompt_edit"
        assert data["key"] == key
        assert len(data["events"]) >= 1
