"""Tests for {{ interface_name }} REST API client interface.

Generated by: madsci new comm --template comm/rest
Template version: {{ template_version }}
"""

import pytest

from {{ interface_name }}_fake_interface import {{ interface_name | pascal_case }}FakeInterface


class Test{{ interface_name | pascal_case }}FakeInterface:
    """Test cases for the fake REST client interface."""

    @pytest.fixture
    def interface(self) -> {{ interface_name | pascal_case }}FakeInterface:
        """Create a test interface instance with zero latency."""
        return {{ interface_name | pascal_case }}FakeInterface(simulated_latency=0.0)

    def test_initial_state(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that the interface starts disconnected."""
        assert not interface.is_connected
        assert interface.operation_count == 0
        assert interface.request_history == []

    def test_connect_disconnect(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test connecting and disconnecting."""
        interface.connect()
        assert interface.is_connected
        interface.disconnect()
        assert not interface.is_connected

    def test_base_url(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that base_url is accessible and trailing slash is stripped."""
        iface = {{ interface_name | pascal_case }}FakeInterface(base_url="http://example.com:9090/")
        assert iface.base_url == "http://example.com:9090"

    def test_get(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test GET request."""
        interface.set_response("GET", "/api/status", {"status": "ok"})
        interface.connect()
        result = interface.get("/api/status")
        assert result == {"status": "ok"}
        assert interface.operation_count == 1

    def test_get_requires_connection(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that GET without connection raises an error."""
        with pytest.raises(RuntimeError, match="Not connected"):
            interface.get("/api/status")

    def test_post(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test POST request."""
        interface.set_response("POST", "/api/command", {"id": "123"})
        interface.connect()
        result = interface.post("/api/command", data={"action": "run"})
        assert result == {"id": "123"}
        assert interface.request_history[-1]["data"] == {"action": "run"}

    def test_post_requires_connection(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that POST without connection raises an error."""
        with pytest.raises(RuntimeError, match="Not connected"):
            interface.post("/api/command")

    def test_put(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test PUT request."""
        interface.set_response("PUT", "/api/config", {"updated": True})
        interface.connect()
        result = interface.put("/api/config", data={"key": "value"})
        assert result == {"updated": True}
        assert interface.request_history[-1]["data"] == {"key": "value"}

    def test_put_requires_connection(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that PUT without connection raises an error."""
        with pytest.raises(RuntimeError, match="Not connected"):
            interface.put("/api/config")

    def test_delete(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test DELETE request."""
        interface.set_response("DELETE", "/api/resource/1", {"deleted": True})
        interface.connect()
        result = interface.delete("/api/resource/1")
        assert result == {"deleted": True}

    def test_delete_requires_connection(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that DELETE without connection raises an error."""
        with pytest.raises(RuntimeError, match="Not connected"):
            interface.delete("/api/resource/1")

    def test_unregistered_endpoint_returns_empty(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that an unregistered endpoint returns an empty dict."""
        interface.connect()
        result = interface.get("/api/unknown")
        assert result == {}

    def test_request_history_tracks_all_methods(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that request history tracks all HTTP methods."""
        interface.connect()
        interface.get("/a")
        interface.post("/b", data={"x": 1})
        interface.put("/c", data={"y": 2})
        interface.delete("/d")

        assert len(interface.request_history) == 4
        assert interface.request_history[0]["method"] == "GET"
        assert interface.request_history[1]["method"] == "POST"
        assert interface.request_history[2]["method"] == "PUT"
        assert interface.request_history[3]["method"] == "DELETE"
        assert interface.operation_count == 4

    def test_reset(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that reset clears all internal state."""
        interface.set_response("GET", "/api/status", {"status": "ok"})
        interface.connect()
        interface.get("/api/status")
        interface.reset()
        assert interface.operation_count == 0
        assert interface.request_history == []
        # Responses are also cleared
        result = interface.get("/api/status")
        assert result == {}

    def test_response_is_a_copy(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that returned responses are copies, not references."""
        original = {"key": "value"}
        interface.set_response("GET", "/test", original)
        interface.connect()
        result = interface.get("/test")
        result["key"] = "modified"
        # Original configured response should be unchanged
        second = interface.get("/test")
        assert second["key"] == "value"
