Raxol.PluginTestHelpers (Raxol v2.6.0)

View Source

Test helpers for Raxol plugins.

Provides utilities for testing plugin lifecycle, hook registration, event handling, and configuration management.

Usage

defmodule MyPluginTest do
  use ExUnit.Case
  import Raxol.PluginTestHelpers

  test "plugin initializes correctly" do
    plugin = create_test_plugin(MyPlugin)
    assert plugin.state.initialized == true
  end

  test "plugin handles events" do
    plugin = create_test_plugin(MyPlugin)
    {plugin, result} = call_hook(plugin, :on_key, %{key: :enter})
    assert result == :handled
  end
end

Summary

Functions

Assert that a hook was called with specific arguments.

Assert plugin state matches expected values.

Call a hook on a test plugin.

Clear the call log.

Create a test instance of a plugin.

Get plugin metadata if available.

Get the call count for a specific hook.

Assert that a hook was not called.

Simulate plugin lifecycle events.

Check if a plugin supports a specific hook.

Update plugin configuration.

Types

test_plugin()

@type test_plugin() :: %{
  module: atom(),
  state: term(),
  config: map(),
  hooks: list(),
  events: list(),
  call_log: list()
}

Functions

assert_hook_called(plugin, hook_name, expected_args \\ nil)

@spec assert_hook_called(map(), atom(), term()) :: :ok

Assert that a hook was called with specific arguments.

Example

assert_hook_called(plugin, :on_key)
assert_hook_called(plugin, :on_key, %{key: :enter})

assert_state(plugin, expected)

@spec assert_state(map(), map() | keyword()) :: :ok

Assert plugin state matches expected values.

Example

assert_state(plugin, %{enabled: true, count: 5})

call_hook(plugin, hook_name, args)

@spec call_hook(test_plugin(), atom(), term()) :: {test_plugin(), term()}

Call a hook on a test plugin.

Example

{plugin, result} = call_hook(plugin, :on_key, %{key: :enter})

clear_call_log(plugin)

@spec clear_call_log(test_plugin()) :: test_plugin()

Clear the call log.

Example

plugin = clear_call_log(plugin)

create_test_plugin(plugin_module, opts \\ [])

@spec create_test_plugin(
  module(),
  keyword()
) :: test_plugin()

Create a test instance of a plugin.

Options

  • :config - Plugin configuration
  • :state - Initial state override

Example

plugin = create_test_plugin(MyPlugin, config: %{enabled: true})

get_metadata(plugin)

@spec get_metadata(map()) :: map()

Get plugin metadata if available.

Example

metadata = get_metadata(plugin)

hook_call_count(plugin, hook_name)

@spec hook_call_count(map(), atom()) :: non_neg_integer()

Get the call count for a specific hook.

Example

count = hook_call_count(plugin, :on_key)

refute_hook_called(plugin, hook_name)

@spec refute_hook_called(map(), atom()) :: :ok

Assert that a hook was not called.

Example

refute_hook_called(plugin, :on_error)

simulate_lifecycle(plugin, event)

@spec simulate_lifecycle(map(), atom()) :: map()

Simulate plugin lifecycle events.

Example

plugin = simulate_lifecycle(plugin, :enable)
plugin = simulate_lifecycle(plugin, :disable)

supports_hook?(plugin, hook_name)

@spec supports_hook?(map(), atom()) :: boolean()

Check if a plugin supports a specific hook.

Example

supports_hook?(plugin, :on_key)

update_config(plugin, new_config)

@spec update_config(test_plugin(), map()) :: test_plugin()

Update plugin configuration.

Example

plugin = update_config(plugin, %{enabled: false})