storage: add initial postgres event persistence and schema migration

This commit is contained in:
2026-03-13 20:29:58 +01:00
parent 7ec588805b
commit cd1adf94f0
6 changed files with 668 additions and 8 deletions

View File

@@ -0,0 +1,52 @@
defmodule Parrhesia.Storage.Adapters.Postgres.EventsTest do
use ExUnit.Case, async: true
alias Parrhesia.Protocol.EventValidator
alias Parrhesia.Storage.Adapters.Postgres.Events
test "normalize_event/1 decodes hex fields and extracts d_tag/expiration" do
pubkey = String.duplicate("a", 64)
event = %{
"pubkey" => pubkey,
"created_at" => 1_700_000_000,
"kind" => 30_023,
"tags" => [["d", "profile"], ["expiration", "1700001000"], ["p", String.duplicate("b", 64)]],
"content" => "hello",
"sig" => String.duplicate("c", 128)
}
id = EventValidator.compute_id(event)
event = Map.put(event, "id", id)
assert {:ok, normalized} = Events.normalize_event(event)
assert normalized.created_at == 1_700_000_000
assert normalized.kind == 30_023
assert normalized.d_tag == "profile"
assert normalized.expires_at == 1_700_001_000
assert normalized.id == Base.decode16!(id, case: :lower)
assert normalized.pubkey == Base.decode16!(pubkey, case: :lower)
end
test "candidate_wins_state?/2 uses created_at then lexical id tie-break" do
assert Events.candidate_wins_state?(
%{created_at: 11, id: <<2>>},
%{event_created_at: 10, event_id: <<255>>}
)
refute Events.candidate_wins_state?(
%{created_at: 9, id: <<0>>},
%{event_created_at: 10, event_id: <<255>>}
)
assert Events.candidate_wins_state?(
%{created_at: 10, id: <<1>>},
%{event_created_at: 10, event_id: <<2>>}
)
refute Events.candidate_wins_state?(
%{created_at: 10, id: <<3>>},
%{event_created_at: 10, event_id: <<2>>}
)
end
end