defmodule AiDebug.Test.MockLiveView do @moduledoc """ Mock LiveView process for testing that properly handles :sys messages. """ use GenServer def start_link(opts \\ []) do GenServer.start_link(__MODULE__, opts) end def init(opts) do view_module = Keyword.get(opts, :view, TestLiveView) socket = %Phoenix.LiveView.Socket{ id: "test-#{System.unique_integer()}", endpoint: AiDebugWeb.Endpoint, view: view_module, assigns: Keyword.get(opts, :assigns, %{__changed__: %{}, count: 0, message: "Hello"}), transport_pid: Keyword.get(opts, :transport_pid, self()), root_pid: self(), private: %{ __components__: %{} } } {:ok, %{socket: socket}} end # Handle :sys messages properly def handle_call(:get_state, _from, state) do {:reply, state, state} end def handle_call({:update_assigns, updates}, _from, state) do new_assigns = Map.merge(state.socket.assigns, updates) new_socket = %{state.socket | assigns: new_assigns} new_state = %{state | socket: new_socket} {:reply, {:ok, new_assigns}, new_state} end def handle_info({:phoenix_live_view, {:event, _event, _value, _meta}}, state) do # Just acknowledge the event {:noreply, state} end def handle_info(_msg, state) do {:noreply, state} end # Support :sys callbacks def system_continue(parent, debug, state) do :gen_server.system_continue(parent, debug, state) end def system_terminate(reason, parent, debug, state) do :gen_server.system_terminate(reason, parent, debug, state) end def system_get_state(state) do {:ok, state} end def system_replace_state(state_fun, state) do {:ok, state_fun.(state), state_fun.(state)} end end