<commit_before>defmodule UsingAppsignalPhoenixChannel do
  import Appsignal.Phoenix.Channel, only: [channel_action: 5]
  use Appsignal.Instrumentation.Decorators

  @decorate channel_action()
  def handle_in("decorated", payload, socket) do
    {:reply, {:ok, payload}, socket}
  end

  def handle_in("instrumented" = action, payload, socket) do
    channel_action(__MODULE__, action, socket, payload, fn ->
      {:reply, {:ok, payload}, socket}
    end)
  end
end

defmodule Appsignal.Phoenix.ChannelTest do
  use ExUnit.Case
  alias Appsignal.FakeTransaction

  setup do
    Appsignal.FakeTransaction.start_link

    [
      socket: %Phoenix.Socket{
        channel: Elixir.PhoenixChatExampleWeb.RoomChannel,
        endpoint: Elixir.PhoenixChatExampleWeb.Endpoint,
        handler: Elixir.PhoenixChatExampleWeb.UserSocket,
        ref: 2,
        topic: "room:lobby",
        transport: Elixir.Phoenix.Transports.WebSocket,
        id: 1
      }
    ]
  end

  test "instruments a channel action with a decorator", %{socket: socket} do
    UsingAppsignalPhoenixChannel.handle_in("decorated", %{body: "Hello, world!"}, socket)

    assert [{"123", :channel}] == FakeTransaction.started_transactions
    assert "UsingAppsignalPhoenixChannel#decorated" == FakeTransaction.action
    assert %{
      "environment" => %{
        channel: PhoenixChatExampleWeb.RoomChannel,
        endpoint: PhoenixChatExampleWeb.Endpoint,
        handler: PhoenixChatExampleWeb.UserSocket,
        id: 1,
        ref: 2,
        topic: "room:lobby",
        transport: Phoenix.Transports.WebSocket
      },
      "params" => %{body: "Hello, world!"}
    } == FakeTransaction.sample_data
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.finished_transactions
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.completed_transactions
  end

  test "instruments a channel action with an instrumentation helper", %{socket: socket} do
    UsingAppsignalPhoenixChannel.handle_in("instrumented", %{body: "Hello, world!"}, socket)

    assert [{"123", :channel}] == FakeTransaction.started_transactions
    assert "UsingAppsignalPhoenixChannel#instrumented" == FakeTransaction.action
    assert %{
      "environment" => %{
        channel: PhoenixChatExampleWeb.RoomChannel,
        endpoint: PhoenixChatExampleWeb.Endpoint,
        handler: PhoenixChatExampleWeb.UserSocket,
        id: 1,
        ref: 2,
        topic: "room:lobby",
        transport: Phoenix.Transports.WebSocket
      },
      "params" => %{body: "Hello, world!"}
    } == FakeTransaction.sample_data
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.finished_transactions
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.completed_transactions
  end<commit_msg>Use string keys for params in ChannelTest<commit_after>defmodule UsingAppsignalPhoenixChannel do
  import Appsignal.Phoenix.Channel, only: [channel_action: 5]
  use Appsignal.Instrumentation.Decorators

  @decorate channel_action()
  def handle_in("decorated", payload, socket) do
    {:reply, {:ok, payload}, socket}
  end

  def handle_in("instrumented" = action, payload, socket) do
    channel_action(__MODULE__, action, socket, payload, fn ->
      {:reply, {:ok, payload}, socket}
    end)
  end
end

defmodule Appsignal.Phoenix.ChannelTest do
  use ExUnit.Case
  alias Appsignal.FakeTransaction

  setup do
    Appsignal.FakeTransaction.start_link

    [
      socket: %Phoenix.Socket{
        channel: Elixir.PhoenixChatExampleWeb.RoomChannel,
        endpoint: Elixir.PhoenixChatExampleWeb.Endpoint,
        handler: Elixir.PhoenixChatExampleWeb.UserSocket,
        ref: 2,
        topic: "room:lobby",
        transport: Elixir.Phoenix.Transports.WebSocket,
        id: 1
      }
    ]
  end

  test "instruments a channel action with a decorator", %{socket: socket} do
    UsingAppsignalPhoenixChannel.handle_in("decorated", %{"body" => "Hello, world!"}, socket)

    assert [{"123", :channel}] == FakeTransaction.started_transactions
    assert "UsingAppsignalPhoenixChannel#decorated" == FakeTransaction.action
    assert %{
      "environment" => %{
        channel: PhoenixChatExampleWeb.RoomChannel,
        endpoint: PhoenixChatExampleWeb.Endpoint,
        handler: PhoenixChatExampleWeb.UserSocket,
        id: 1,
        ref: 2,
        topic: "room:lobby",
        transport: Phoenix.Transports.WebSocket
      },
      "params" => %{"body" => "Hello, world!"}
    } == FakeTransaction.sample_data
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.finished_transactions
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.completed_transactions
  end

  test "instruments a channel action with an instrumentation helper", %{socket: socket} do
    UsingAppsignalPhoenixChannel.handle_in("instrumented", %{"body" => "Hello, world!"}, socket)

    assert [{"123", :channel}] == FakeTransaction.started_transactions
    assert "UsingAppsignalPhoenixChannel#instrumented" == FakeTransaction.action
    assert %{
      "environment" => %{
        channel: PhoenixChatExampleWeb.RoomChannel,
        endpoint: PhoenixChatExampleWeb.Endpoint,
        handler: PhoenixChatExampleWeb.UserSocket,
        id: 1,
        ref: 2,
        topic: "room:lobby",
        transport: Phoenix.Transports.WebSocket
      },
      "params" => %{"body" => "Hello, world!"}
    } == FakeTransaction.sample_data
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.finished_transactions
    assert [%Appsignal.Transaction{id: "123"}] = FakeTransaction.completed_transactions
  end
{'fraction_non_alphanumeric': 0.11242391693519513, 'fraction_numerical': 0.00895094880057286, 'mean_word_length': 3.7068239258635214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2336734', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1631, 'n_words': 353}