packages feed

keiro-dsl-0.12.0.0: test/conformance-projection-catalog/Generated/CatalogDemo/Shipments/Harness.hs

{-# LANGUAGE OverloadedLabels #-}
-- @generated by keiro-dsl 0.12.0.0 (language keiro-dsl 5) from aggregate Shipments; do not edit.
module Generated.CatalogDemo.Shipments.Harness (harnessAssertions) where

import Generated.CatalogDemo.Shipments.Domain
import Generated.CatalogDemo.Shipments.Codec (encodeShipmentsEvent, parseShipmentsEvent, shipmentsCodec)
import Generated.CatalogDemo.Shipments.Transducer (shipmentsTransducer)
import Keiki.Core (applyEventsEither, defaultValidationOptions, step, validateTransducer, (!))
import Keiro.Codec (eventType)
import Data.List.NonEmpty qualified as NonEmpty
import Data.Text qualified as T
import Keiro.Codec.Structural (FixtureCases (..))
import CatalogDemo.MappedBindings qualified as MappedBindings

-- | (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 shipmentsTransducer))
  -- clock-free: spec samples no wall clock (verified at scaffold time)
  , ("golden round-trip: ShipmentRecorded", roundTrips sampleEventShipmentRecorded)
  , ("accepts RecordShipment from ShipmentsEmpty", acceptRecordShipment)
  ]
  ++ mappedConformanceAssertions
  ++ forwardReplayRecordShipment

roundTrips :: ShipmentsEvent -> Bool
roundTrips e = parseShipmentsEvent (eventType shipmentsCodec e) (encodeShipmentsEvent e) == Right e

sampleEventShipmentRecorded :: ShipmentsEvent
sampleEventShipmentRecorded = ShipmentRecorded (ShipmentRecordedData (snd (NonEmpty.head (fixtureCases MappedBindings.sharedReferenceCases))) False)

acceptRecordShipment :: Bool
acceptRecordShipment =
  case step shipmentsTransducer (ShipmentsEmpty, initialShipmentsRegs) (RecordShipment (RecordShipmentData (snd (NonEmpty.head (fixtureCases MappedBindings.sharedReferenceCases))) False)) of
    Just (v, _, _) -> v == ShipmentsRecorded
    Nothing -> False

-- forward/replay equality (plan 147): cross the persisted codec boundary,
-- replay the emitted chain, and compare the final vertex and every register.
forwardReplayRecordShipment :: [(String, Bool)]
forwardReplayRecordShipment =
  case step shipmentsTransducer (ShipmentsEmpty, initialShipmentsRegs) (RecordShipment (RecordShipmentData (snd (NonEmpty.head (fixtureCases MappedBindings.sharedReferenceCases))) False)) of
    Nothing -> [(prefix <> "forward step accepted", False)]
    Just (forwardVertex, forwardRegs, emitted) ->
      case mapM (\event -> parseShipmentsEvent (eventType shipmentsCodec event) (encodeShipmentsEvent event)) emitted of
        Left _ -> [(prefix <> "emitted chain decodes", False)]
        Right decodedEvents ->
          case applyEventsEither shipmentsTransducer (ShipmentsEmpty, initialShipmentsRegs) decodedEvents of
            Left _ -> [(prefix <> "replay succeeds", False)]
            Right (replayVertex, replayRegs) ->
              [ (prefix <> "final vertex", replayVertex == forwardVertex)
              , (prefix <> "register accepted", (replayRegs ! #accepted) == (forwardRegs ! #accepted))
              ]
  where
    prefix = "forward/replay equality: RecordShipment from ShipmentsEmpty -- "

mappedConformanceAssertions :: [(String, Bool)]
mappedConformanceAssertions =
  concat
    [ shipmentRecordedSharedReferenceAssertions
    ]

shipmentRecordedSharedReferenceAssertions :: [(String, Bool)]
shipmentRecordedSharedReferenceAssertions =
  [ ("mapped codec round-trip: ShipmentRecorded/sharedReference/" <> T.unpack label, roundTrips (ShipmentRecorded (ShipmentRecordedData mappedValue False)))
  | (label, mappedValue) <- NonEmpty.toList (fixtureCases MappedBindings.sharedReferenceCases)
  ]