"""Tests for {{ interface_name }} serial interface.

Generated by: madsci new comm --template comm/serial
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 serial 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 in a closed state."""
        assert not interface.is_open
        assert interface.operation_count == 0
        assert interface.write_history == []

    def test_open_close(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test opening and closing the serial port."""
        interface.open()
        assert interface.is_open
        interface.close()
        assert not interface.is_open

    def test_write(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test writing data to the serial port."""
        interface.open()
        n = interface.write(b"test data")
        assert n == 9
        assert interface.write_history == [b"test data"]
        assert interface.operation_count == 1

    def test_write_requires_open(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that writing to a closed port raises an error."""
        with pytest.raises(RuntimeError, match="not open"):
            interface.write(b"test")

    def test_read(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test reading data from the serial port."""
        interface.set_response(b"response data")
        interface.open()
        data = interface.read()
        assert data == b"response data"
        assert interface.operation_count == 1

    def test_read_requires_open(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that reading from a closed port raises an error."""
        with pytest.raises(RuntimeError, match="not open"):
            interface.read()

    def test_readline(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test reading a single line from the serial port."""
        interface.set_response(b"line1\nline2\n")
        interface.open()
        line = interface.readline()
        assert line == b"line1\n"

    def test_readline_no_newline(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test readline when buffer has no newline."""
        interface.set_response(b"partial")
        interface.open()
        line = interface.readline()
        assert line == b"partial"

    def test_query(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test the query convenience method."""
        interface.set_response(b"OK\n")
        interface.open()
        response = interface.query("*IDN?")
        assert response == "OK"
        assert b"*IDN?\n" in interface.write_history

    def test_flush_requires_open(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that flushing a closed port raises an error."""
        with pytest.raises(RuntimeError, match="not open"):
            interface.flush()

    def test_flush(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that flush succeeds when port is open."""
        interface.open()
        interface.flush()  # Should not raise

    def test_reset(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that reset clears all internal state."""
        interface.open()
        interface.write(b"data")
        interface.reset()
        assert interface.operation_count == 0
        assert interface.write_history == []

    def test_multiple_writes(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that multiple writes are tracked in order."""
        interface.open()
        interface.write(b"first")
        interface.write(b"second")
        assert interface.write_history == [b"first", b"second"]
        assert interface.operation_count == 2

    def test_read_consumes_buffer(self, interface: {{ interface_name | pascal_case }}FakeInterface) -> None:
        """Test that read consumes data from the buffer."""
        interface.set_response(b"abcdef")
        interface.open()
        first = interface.read(size=3)
        second = interface.read(size=3)
        assert first == b"abc"
        assert second == b"def"
