storage: add initial postgres event persistence and schema migration
This commit is contained in:
52
test/parrhesia/storage/adapters/postgres/events_test.exs
Normal file
52
test/parrhesia/storage/adapters/postgres/events_test.exs
Normal 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
|
||||
Reference in New Issue
Block a user