defmodule Raxol.Terminal.Emulator.ResponseTest do
  use ExUnit.Case
  alias Raxol.Terminal.Emulator
  # Needed for cursor move before CPR
  alias Raxol.Terminal.Cursor.Manager

  describe "Terminal Responses" do
    # Tests focus on the output generated by Emulator.process_input
    # for specific reporting sequences.

    test ~c"Primary DA (Device Attributes - CSI c) returns correct response" do
      emulator = Emulator.new()
      input = "\e[c"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      # Expecting basic VT102 response
      assert output == "\e[?6c"
    end

    test ~c"Primary DA (Device Attributes - CSI 0 c) returns correct response" do
      emulator = Emulator.new()
      input = "\e[0c"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      # Expecting basic VT102 response
      assert output == "\e[?6c"
    end

    test ~c"Primary DA with non-zero param is ignored" do
      emulator = Emulator.new()
      input = "\e[1c"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      assert output == ""
    end

    test ~c"Secondary DA (Device Attributes - CSI > c) returns correct response" do
      emulator = Emulator.new()
      input = "\e[>c"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      # Expecting basic xterm-like response (version 0, patch 0, flags 0)
      assert output == "\e[>0;0;0c"
    end

    test ~c"Secondary DA (Device Attributes - CSI > 0 c) returns correct response" do
      emulator = Emulator.new()
      input = "\e[>0c"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      # Expecting basic xterm-like response
      assert output == "\e[>0;0;0c"
    end

    test ~c"Secondary DA with non-zero param is ignored" do
      emulator = Emulator.new()
      input = "\e[>1c"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      assert output == ""
    end

    # --- DSR - Device Status Report ---
    test ~c"DSR 5n (Status Report) returns OK response" do
      emulator = Emulator.new()
      input = "\e[5n"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      # Expecting "OK" response
      assert output == "\e[0n"
    end

    test ~c"DSR 6n (Report Cursor Position) returns correct CPR" do
      emulator = Emulator.new(80, 24)
      # Move cursor to row 5, col 10 (0-based)
      emulator = %{emulator | cursor: Manager.move_to(emulator.cursor, 5, 10)}
      input = "\e[6n"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      # Expecting CPR: ESC [ <row=6> ; <col=11> R (1-based)
      assert output == "\e[6;11R"
    end

    test ~c"DSR with unknown param is ignored" do
      emulator = Emulator.new()
      # Example unknown DSR
      input = "\e[7n"
      {_updated_emulator, output} = Emulator.process_input(emulator, input)
      assert output == ""
    end
  end
end
