keiro-dsl-0.12.0.0: test/conformance-scalar-expressions/Generated/AggregateScalarExpressions/ScalarAccount/Harness.hs
{-# LANGUAGE OverloadedLabels #-}
-- @generated by keiro-dsl 0.12.0.0 (language keiro-dsl 4) from aggregate ScalarAccount; do not edit.
module Generated.AggregateScalarExpressions.ScalarAccount.Harness (harnessAssertions) where
import Generated.AggregateScalarExpressions.ScalarAccount.Domain
import Generated.AggregateScalarExpressions.ScalarAccount.Codec (encodeScalarAccountEvent, parseScalarAccountEvent, scalarAccountCodec, encodeLimitsMapped, decodeLimitsMapped)
import Generated.AggregateScalarExpressions.ScalarAccount.Transducer (scalarAccountTransducer)
import Keiki.Core (applyEventsEither, defaultValidationOptions, step, validateTransducer, (!))
import Keiro.Codec (eventType)
import Generated.AggregateScalarExpressions.Nominals (AccountMode (..), RequestId, parseRequestId)
import Data.Aeson qualified as Aeson
import Data.Aeson.Key qualified as AesonKey
import Data.Aeson.KeyMap qualified as AesonKeyMap
import Data.Either (isLeft)
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text qualified as T
import Keiro.Codec.Structural (FixtureCases (..))
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (UTCTime (..), picosecondsToDiffTime)
import ScalarExpressions.Bindings qualified as Bindings
-- | (label, passed). A driver runs these and exits non-zero on any False,
-- naming the failing assertion. Filling a hole wrongly turns a specific
-- entry False; the scaffold cannot.
harnessAssertions :: [(String, Bool)]
harnessAssertions =
[ ("validateTransducer is empty", null (validateTransducer defaultValidationOptions scalarAccountTransducer))
-- clock-free: spec samples no wall clock (verified at scaffold time)
, ("golden round-trip: Adjusted", roundTrips sampleEventAdjusted)
, ("golden round-trip: ClosedEvent", roundTrips sampleEventClosedEvent)
, ("accepts Adjust from ScalarAccountOpen", acceptAdjust)
]
++ mappedConformanceAssertions
++ forwardReplayAdjust
roundTrips :: ScalarAccountEvent -> Bool
roundTrips e = parseScalarAccountEvent (eventType scalarAccountCodec e) (encodeScalarAccountEvent e) == Right e
sampleRequestId :: RequestId
sampleRequestId =
case parseRequestId "req_01h455vb4pex5vsknk084sn02q" of
Right parsed -> parsed
Left problem -> error (show problem)
sampleObservedAt :: UTCTime
sampleObservedAt = UTCTime (fromGregorian 2026 1 2) (picosecondsToDiffTime 11045123456789012)
sampleEventAdjusted :: ScalarAccountEvent
sampleEventAdjusted = Adjusted (AdjustedData 0 0 0 "sample-label" False Normal sampleRequestId sampleObservedAt (snd (NonEmpty.head (fixtureCases Bindings.limitsCases))))
sampleEventClosedEvent :: ScalarAccountEvent
sampleEventClosedEvent = ClosedEvent (ClosedEventData 0)
acceptAdjust :: Bool
acceptAdjust =
case step scalarAccountTransducer (ScalarAccountOpen, initialScalarAccountRegs) (Adjust (AdjustData 0 0 0 "sample-label" False Normal sampleRequestId sampleObservedAt (snd (NonEmpty.head (fixtureCases Bindings.limitsCases))))) of
Just (v, _, _) -> v == ScalarAccountReviewed
Nothing -> False
-- forward/replay equality (plan 147): cross the persisted codec boundary,
-- replay the emitted chain, and compare the final vertex and every register.
forwardReplayAdjust :: [(String, Bool)]
forwardReplayAdjust =
case step scalarAccountTransducer (ScalarAccountOpen, initialScalarAccountRegs) (Adjust (AdjustData 0 0 0 "sample-label" False Normal sampleRequestId sampleObservedAt (snd (NonEmpty.head (fixtureCases Bindings.limitsCases))))) of
Nothing -> [(prefix <> "forward step accepted", False)]
Just (forwardVertex, forwardRegs, emitted) ->
case mapM (\event -> parseScalarAccountEvent (eventType scalarAccountCodec event) (encodeScalarAccountEvent event)) emitted of
Left _ -> [(prefix <> "emitted chain decodes", False)]
Right decodedEvents ->
case applyEventsEither scalarAccountTransducer (ScalarAccountOpen, initialScalarAccountRegs) decodedEvents of
Left _ -> [(prefix <> "replay succeeds", False)]
Right (replayVertex, replayRegs) ->
[ (prefix <> "final vertex", replayVertex == forwardVertex)
, (prefix <> "register balance", (replayRegs ! #balance) == (forwardRegs ! #balance))
, (prefix <> "register reserved", (replayRegs ! #reserved) == (forwardRegs ! #reserved))
, (prefix <> "register capacity", (replayRegs ! #capacity) == (forwardRegs ! #capacity))
, (prefix <> "register machine", (replayRegs ! #machine) == (forwardRegs ! #machine))
, (prefix <> "register label", (replayRegs ! #label) == (forwardRegs ! #label))
, (prefix <> "register active", (replayRegs ! #active) == (forwardRegs ! #active))
, (prefix <> "register mode", (replayRegs ! #mode) == (forwardRegs ! #mode))
, (prefix <> "register requestId", (replayRegs ! #requestId) == (forwardRegs ! #requestId))
, (prefix <> "register openedAt", (replayRegs ! #openedAt) == (forwardRegs ! #openedAt))
, (prefix <> "register limits", (replayRegs ! #limits) == (forwardRegs ! #limits))
]
where
prefix = "forward/replay equality: Adjust from ScalarAccountOpen -- "
mappedConformanceAssertions :: [(String, Bool)]
mappedConformanceAssertions =
concat
[ adjustedLimitsAssertions
, structuralWirePolicyAssertions
]
adjustedLimitsAssertions :: [(String, Bool)]
adjustedLimitsAssertions =
[ ("mapped codec round-trip: Adjusted/limits/" <> T.unpack label, roundTrips (Adjusted (AdjustedData 0 0 0 "sample-label" False Normal sampleRequestId sampleObservedAt mappedValue)))
| (label, mappedValue) <- NonEmpty.toList (fixtureCases Bindings.limitsCases)
]
structuralWirePolicyAssertions :: [(String, Bool)]
structuralWirePolicyAssertions =
[ ("wire policy unknown fields: scalar-expressions.Limits.v1", all (\(_, value) -> isLeft (decodeLimitsMapped (insertObjectField "__keiro_unknown" (Aeson.Bool True) (encodeLimitsMapped value)))) (NonEmpty.toList (fixtureCases Bindings.limitsCases)))
]
insertObjectField :: T.Text -> Aeson.Value -> Aeson.Value -> Aeson.Value
insertObjectField key inserted (Aeson.Object objectValue) = Aeson.Object (AesonKeyMap.insert (AesonKey.fromText key) inserted objectValue)
insertObjectField _ _ value = value