From 5ac3e7eb6ad232e6b418f3f26d48beea0571ae9c Mon Sep 17 00:00:00 2001 From: Bram Date: Sun, 24 Dec 2023 00:37:22 +0100 Subject: [PATCH] Fix Event test This commit is starting to show how the definition of the Envelope wrapper is better done on a different level --- src/Internal/Values/Envelope.elm | 2 +- src/Internal/Values/Event.elm | 1 + src/Matrix/Event.elm | 4 +-- tests/Envelope.elm | 42 ++++++++++++++++++++++++++++++++ tests/Event.elm | 31 +++++++++++++---------- 5 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 tests/Envelope.elm diff --git a/src/Internal/Values/Envelope.elm b/src/Internal/Values/Envelope.elm index 617973c..96d09cb 100644 --- a/src/Internal/Values/Envelope.elm +++ b/src/Internal/Values/Envelope.elm @@ -1,5 +1,5 @@ module Internal.Values.Envelope exposing - ( Envelope, init + ( Envelope(..), init , map, mapMaybe, mapList , Settings, mapSettings, extractSettings , mapContext diff --git a/src/Internal/Values/Event.elm b/src/Internal/Values/Event.elm index 954e935..a23a1d4 100644 --- a/src/Internal/Values/Event.elm +++ b/src/Internal/Values/Event.elm @@ -3,6 +3,7 @@ module Internal.Values.Event exposing , content, eventId, eventType, originServerTs, roomId, sender, stateKey , UnsignedData(..), age, prevContent, redactedBecause, transactionId , encode, decoder + , IEvent ) {-| diff --git a/src/Matrix/Event.elm b/src/Matrix/Event.elm index 016df69..5d9190f 100644 --- a/src/Matrix/Event.elm +++ b/src/Matrix/Event.elm @@ -1,6 +1,6 @@ module Matrix.Event exposing ( Event, content, eventType, stateKey - , eventId, originServerTs, roomId, sender + , eventId, roomId, sender, originServerTs , previousContent, redactedBecause ) @@ -20,7 +20,7 @@ events. ## Metadata -@docs eventId, originServerTs, roomId, sender +@docs eventId, roomId, sender, originServerTs ## Optional data diff --git a/tests/Envelope.elm b/tests/Envelope.elm new file mode 100644 index 0000000..85dcdb1 --- /dev/null +++ b/tests/Envelope.elm @@ -0,0 +1,42 @@ +module Envelope exposing (..) + +import Context as TestContext +import Fuzz exposing (Fuzzer) +import Internal.Config.Default as Default +import Internal.Values.Envelope exposing (Envelope(..), Settings) +import Test exposing (..) + + +fuzzer : Fuzzer a -> Fuzzer (Envelope a) +fuzzer fuzz = + Fuzz.map3 + (\content context settings -> + Envelope + { content = content + , context = context + , settings = settings + } + ) + fuzz + TestContext.fuzzer + fuzzerSettings + + +fuzzerSettings : Fuzzer Settings +fuzzerSettings = + Fuzz.map3 Settings + (Fuzz.oneOf + [ Fuzz.constant Default.currentVersion + , Fuzz.string + ] + ) + (Fuzz.oneOf + [ Fuzz.constant Default.deviceName + , Fuzz.string + ] + ) + (Fuzz.oneOf + [ Fuzz.constant Default.syncTime + , Fuzz.int + ] + ) diff --git a/tests/Event.elm b/tests/Event.elm index f7be57c..e7cd84d 100644 --- a/tests/Event.elm +++ b/tests/Event.elm @@ -1,5 +1,6 @@ module Event exposing (..) +import Envelope as TestEnvelope import Expect import Fuzz exposing (Fuzzer) import Iddict as TestIddict @@ -24,24 +25,23 @@ valueFuzzer = , Fuzz.map E.string Fuzz.string , Fuzz.map (E.list E.int) (Fuzz.list Fuzz.int) , Fuzz.map (E.list E.string) (Fuzz.list Fuzz.string) - , Fuzz.map Event.encode (Fuzz.lazy (\_ -> fuzzer)) + , Fuzz.map Event.encode (Fuzz.lazy (\_ -> TestEnvelope.fuzzer fuzzer)) ] -fuzzer : Fuzzer Event.Event +fuzzer : Fuzzer Event.IEvent fuzzer = Fuzz.map8 (\c ei et o r se sk u -> - Envelope.init - { content = c - , eventId = ei - , eventType = et - , originServerTs = o - , roomId = r - , sender = se - , stateKey = sk - , unsigned = u - } + { content = c + , eventId = ei + , eventType = et + , originServerTs = o + , roomId = r + , sender = se + , stateKey = sk + , unsigned = u + } ) valueFuzzer Fuzz.string @@ -53,6 +53,11 @@ fuzzer = (Fuzz.maybe unsignedDataFuzzer) +fuzzerFull : Fuzzer Event.Event +fuzzerFull = + TestEnvelope.fuzzer fuzzer + + unsignedDataFuzzer : Fuzzer Event.UnsignedData unsignedDataFuzzer = Fuzz.map4 @@ -73,7 +78,7 @@ unsignedDataFuzzer = json : Test json = describe "JSON tests" - [ fuzz fuzzer + [ fuzz fuzzerFull "JSON encode + JSON decode" (\event -> event