feat: add sync relay guard fanout gating and env config
Some checks failed
CI / Test (OTP 27.2 / Elixir 1.18.2) (push) Failing after 0s
CI / Test (OTP 28.4 / Elixir 1.19.4 + E2E) (push) Failing after 0s

This commit is contained in:
2026-03-26 00:36:00 +01:00
parent 8309a89ba7
commit b402d95e47
7 changed files with 91 additions and 6 deletions

View File

@@ -30,6 +30,45 @@ defmodule Parrhesia.API.EventsTest do
assert second_result.message == "duplicate: event already stored"
end
test "publish fanout includes sync-originated events when relay guard is disabled" do
with_sync_relay_guard(false)
join_multi_node_group!()
event = valid_event()
event_id = event["id"]
assert {:ok, %{accepted: true}} =
Events.publish(event, context: %RequestContext{caller: :sync})
assert_receive {:remote_fanout_event, %{"id" => ^event_id}}, 200
end
test "publish fanout skips sync-originated events when relay guard is enabled" do
with_sync_relay_guard(true)
join_multi_node_group!()
event = valid_event()
event_id = event["id"]
assert {:ok, %{accepted: true}} =
Events.publish(event, context: %RequestContext{caller: :sync})
refute_receive {:remote_fanout_event, %{"id" => ^event_id}}, 200
end
test "publish fanout still includes local-originated events when relay guard is enabled" do
with_sync_relay_guard(true)
join_multi_node_group!()
event = valid_event()
event_id = event["id"]
assert {:ok, %{accepted: true}} =
Events.publish(event, context: %RequestContext{caller: :local})
assert_receive {:remote_fanout_event, %{"id" => ^event_id}}, 200
end
test "query and count preserve read semantics through the shared API" do
now = System.system_time(:second)
first = valid_event(%{"content" => "first", "created_at" => now})
@@ -53,6 +92,36 @@ defmodule Parrhesia.API.EventsTest do
)
end
defp with_sync_relay_guard(enabled?) when is_boolean(enabled?) do
[{:config, previous}] = :ets.lookup(Parrhesia.Config, :config)
sync =
previous
|> Map.get(:sync, [])
|> Keyword.put(:relay_guard, enabled?)
:ets.insert(Parrhesia.Config, {:config, Map.put(previous, :sync, sync)})
on_exit(fn ->
:ets.insert(Parrhesia.Config, {:config, previous})
end)
end
defp join_multi_node_group! do
case Process.whereis(:pg) do
nil ->
case :pg.start_link() do
{:ok, _pid} -> :ok
{:error, {:already_started, _pid}} -> :ok
end
_pid ->
:ok
end
:ok = :pg.join(Parrhesia.Fanout.MultiNode, self())
end
defp valid_event(overrides \\ %{}) do
base_event = %{
"pubkey" => String.duplicate("1", 64),

View File

@@ -22,6 +22,7 @@ defmodule Parrhesia.ConfigTest do
assert Parrhesia.Config.get([:limits, :max_filter_limit]) == 500
assert Parrhesia.Config.get([:database, :separate_read_pool?]) == false
assert Parrhesia.Config.get([:relay_url]) == "ws://localhost:4413/relay"
assert Parrhesia.Config.get([:sync, :relay_guard]) == false
assert Parrhesia.Config.get([:policies, :auth_required_for_writes]) == false
assert Parrhesia.Config.get([:policies, :marmot_media_max_imeta_tags_per_event]) == 8
assert Parrhesia.Config.get([:policies, :marmot_media_reject_mip04_v1]) == true