packages feed

keiro-dsl-0.11.0.0: test/conformance-v2/Generated/HospitalCapacity/Reservation/Harness.hs

{-# LANGUAGE OverloadedLabels #-}
-- @generated by keiro-dsl 0.11.0.0 (language keiro-dsl 4) from aggregate Reservation; do not edit.
module Generated.HospitalCapacity.Reservation.Harness (harnessAssertions) where

import Generated.HospitalCapacity.Reservation.Domain
import Generated.HospitalCapacity.Reservation.Codec (encodeReservationEvent, parseReservationEvent, reservationCodec)
import Generated.HospitalCapacity.Reservation.Transducer (reservationTransducer)
import Keiki.Core (applyEventsEither, defaultValidationOptions, step, validateTransducer, (!))
import Keiro.Codec (EventType (..), decodeRaw, eventType)
import Generated.HospitalCapacity.Nominals (CommandId, parseCommandId, DivertStatus (..), HospitalId, parseHospitalId, PatientAcuity (..), TransferReservationId, parseTransferReservationId)

-- | (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 reservationTransducer))
  -- clock-free: spec samples no wall clock (verified at scaffold time)
  , ("golden round-trip: TransferReservationCreated", roundTrips sampleEventTransferReservationCreated)
  , ("golden round-trip: TransferReservationConfirmed", roundTrips sampleEventTransferReservationConfirmed)
  , ("accepts RequestTransferReservation from ReservationUnrequested", acceptRequestTransferReservation)
  ]
  ++ forwardReplayRequestTransferReservation
  ++ [ ("upcast TransferReservationCreated chain wired (current-shape stand-in; add a golden payload)", upcastsTransferReservationCreated)
     ]

roundTrips :: ReservationEvent -> Bool
roundTrips e = parseReservationEvent (eventType reservationCodec e) (encodeReservationEvent e) == Right e

sampleCommandId :: CommandId
sampleCommandId =
  case parseCommandId "cmd_01h455vb4pex5vsknk084sn02q" of
    Right parsed -> parsed
    Left problem -> error (show problem)

sampleHospitalId :: HospitalId
sampleHospitalId =
  case parseHospitalId "hosp_01h455vb4pex5vsknk084sn02q" of
    Right parsed -> parsed
    Left problem -> error (show problem)

sampleTransferReservationId :: TransferReservationId
sampleTransferReservationId =
  case parseTransferReservationId "rsv_01h455vb4pex5vsknk084sn02q" of
    Right parsed -> parsed
    Left problem -> error (show problem)

sampleEventTransferReservationCreated :: ReservationEvent
sampleEventTransferReservationCreated = TransferReservationCreated (TransferReservationCreatedData sampleTransferReservationId sampleHospitalId sampleCommandId RedTag Open False "sample-triageNote")

sampleEventTransferReservationConfirmed :: ReservationEvent
sampleEventTransferReservationConfirmed = TransferReservationConfirmed (TransferReservationConfirmedData sampleTransferReservationId sampleHospitalId sampleCommandId)

acceptRequestTransferReservation :: Bool
acceptRequestTransferReservation =
  case step reservationTransducer (ReservationUnrequested, initialReservationRegs) (RequestTransferReservation (RequestTransferReservationData sampleTransferReservationId sampleHospitalId sampleCommandId RedTag Open False)) of
    Just (v, _, _) -> v == ReservationHeld
    Nothing -> False

-- forward/replay equality (plan 147): cross the persisted codec boundary,
-- replay the emitted chain, and compare the final vertex and every register.
forwardReplayRequestTransferReservation :: [(String, Bool)]
forwardReplayRequestTransferReservation =
  case step reservationTransducer (ReservationUnrequested, initialReservationRegs) (RequestTransferReservation (RequestTransferReservationData sampleTransferReservationId sampleHospitalId sampleCommandId RedTag Open False)) of
    Nothing -> [(prefix <> "forward step accepted", False)]
    Just (forwardVertex, forwardRegs, emitted) ->
      case mapM (\event -> parseReservationEvent (eventType reservationCodec event) (encodeReservationEvent event)) emitted of
        Left _ -> [(prefix <> "emitted chain decodes", False)]
        Right decodedEvents ->
          case applyEventsEither reservationTransducer (ReservationUnrequested, initialReservationRegs) decodedEvents of
            Left _ -> [(prefix <> "replay succeeds", False)]
            Right (replayVertex, replayRegs) ->
              [ (prefix <> "final vertex", replayVertex == forwardVertex)
              , (prefix <> "register reservationId", (replayRegs ! #reservationId) == (forwardRegs ! #reservationId))
              , (prefix <> "register hospitalId", (replayRegs ! #hospitalId) == (forwardRegs ! #hospitalId))
              , (prefix <> "register patientAcuity", (replayRegs ! #patientAcuity) == (forwardRegs ! #patientAcuity))
              ]
  where
    prefix = "forward/replay equality: RequestTransferReservation from ReservationUnrequested -- "

upcastsTransferReservationCreated :: Bool
upcastsTransferReservationCreated =
  either (const False) (const True)
    (decodeRaw reservationCodec (EventType "TransferReservationCreated") 1 (encodeReservationEvent sampleEventTransferReservationCreated))