From 5065ed05ffde049b1f41275920d9a56ae41a2d01 Mon Sep 17 00:00:00 2001 From: Bram Date: Fri, 22 Dec 2023 23:44:46 +0100 Subject: [PATCH] Add Event tests --- src/Internal/Values/Event.elm | 2 +- tests/Event.elm | 84 +++++++++++++++++++++++++++++++++++ tests/Timestamp.elm | 11 +++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Event.elm create mode 100644 tests/Timestamp.elm diff --git a/src/Internal/Values/Event.elm b/src/Internal/Values/Event.elm index 61414f9..e0ad27f 100644 --- a/src/Internal/Values/Event.elm +++ b/src/Internal/Values/Event.elm @@ -1,7 +1,7 @@ module Internal.Values.Event exposing ( Event , content, eventId, eventType, originServerTs, roomId, sender, stateKey - , UnsignedData, age, prevContent, redactedBecause, transactionId + , UnsignedData(..), age, prevContent, redactedBecause, transactionId , encode, decoder ) diff --git a/tests/Event.elm b/tests/Event.elm new file mode 100644 index 0000000..f7be57c --- /dev/null +++ b/tests/Event.elm @@ -0,0 +1,84 @@ +module Event exposing (..) + +import Expect +import Fuzz exposing (Fuzzer) +import Iddict as TestIddict +import Internal.Tools.Iddict as Iddict +import Internal.Tools.Timestamp as Timestamp +import Internal.Values.Envelope as Envelope +import Internal.Values.Event as Event +import Json.Decode as D +import Json.Encode as E +import Test exposing (..) +import Timestamp as TestTimestamp + + +{-| Example values that can be used for arbitrary JSON values +-} +valueFuzzer : Fuzzer E.Value +valueFuzzer = + Fuzz.oneOf + [ Fuzz.map (Iddict.encode E.int) (TestIddict.fuzzer Fuzz.int) + , Fuzz.map Timestamp.encode TestTimestamp.fuzzer + , Fuzz.map E.int Fuzz.int + , 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)) + ] + + +fuzzer : Fuzzer Event.Event +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 + } + ) + valueFuzzer + Fuzz.string + Fuzz.string + TestTimestamp.fuzzer + Fuzz.string + Fuzz.string + (Fuzz.maybe Fuzz.string) + (Fuzz.maybe unsignedDataFuzzer) + + +unsignedDataFuzzer : Fuzzer Event.UnsignedData +unsignedDataFuzzer = + Fuzz.map4 + (\age prev redact trans -> + Event.UnsignedData + { age = age + , prevContent = prev + , redactedBecause = redact + , transactionId = trans + } + ) + (Fuzz.maybe Fuzz.int) + (Fuzz.maybe valueFuzzer) + (Fuzz.maybe <| Fuzz.lazy (\_ -> fuzzer)) + (Fuzz.maybe Fuzz.string) + + +json : Test +json = + describe "JSON tests" + [ fuzz fuzzer + "JSON encode + JSON decode" + (\event -> + event + |> Event.encode + |> D.decodeValue Event.decoder + |> Expect.equal (Ok event) + ) + ] diff --git a/tests/Timestamp.elm b/tests/Timestamp.elm new file mode 100644 index 0000000..f8b7dd4 --- /dev/null +++ b/tests/Timestamp.elm @@ -0,0 +1,11 @@ +module Timestamp exposing (..) + +import Fuzz exposing (Fuzzer) +import Internal.Tools.Timestamp exposing (Timestamp) +import Test exposing (..) +import Time + + +fuzzer : Fuzzer Timestamp +fuzzer = + Fuzz.map Time.millisToPosix Fuzz.int