dexcord/test/support/cache_probe_handler.ex

32 lines
1.1 KiB
Elixir

defmodule Dexcord.FakeGateway.CacheProbeHandler do
@moduledoc false
# A `Dexcord.Handler` used to prove ordering: the Dispatcher writes the cache
# INLINE before spawning the handler Task, so by the time this handler runs the
# cache must already reflect the event. For each event it reads the relevant
# cache entry and relays what it observed to the subscribed test process as
# `{:handler_saw, name, cache_result}`.
@behaviour Dexcord.Handler
@key {__MODULE__, :sink}
def subscribe(pid), do: :persistent_term.put(@key, pid)
@impl true
def handle_event({name, data}) do
case :persistent_term.get(@key, nil) do
nil -> :ok
pid -> send(pid, {:handler_saw, name, probe(name, data)})
end
end
defp probe(:GUILD_CREATE, %Dexcord.Guild{id: id}), do: Dexcord.Cache.guild(id)
defp probe(:MESSAGE_CREATE, %Dexcord.Message{author: %Dexcord.User{id: uid}}),
do: Dexcord.Cache.user(uid)
defp probe(:GUILD_MEMBERS_CHUNK, %Dexcord.Events.GuildMembersChunk{guild_id: gid}),
do: Dexcord.Cache.members(gid)
defp probe(_name, _data), do: :na
end