Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions webapi-test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
# Revision history for webapi-test

## 0.7.0.0 -- 2026-11-19 (zenbridge #2715: what a move's host is told, and what it may say)

Four things a scenario's host could not do, none of which names a host's
own ideas. All are breaking for a host that builds its `MoveHost` with the
constructor: build it from `defMoveHost` (`defMoveHost {settle = …,
observe = …}`), and a field added later breaks nothing.

* **The host is handed a move as it was played** (`MovePlayed`: its name,
where it was written, its own steps in order). `settle` and `observe`
take it as their first argument (breaking), and so does `verdict`.
* A step is a `MoveStep`: its kind, where it was written, its operation,
a call's request as it went out (`Sent`) and what the step gave
(`Answered`). These are the values the engine held, not the trace's
text: nothing is rendered, clipped or masked. A host reads them by a
cast to its own types, `sentAs @POST @SomeRoute step` and
`answeredAs @SomeAnswer step`.
* The steps are the move's calls, its effect steps (a foreign transform,
a host's own step: no request, the result as its value) and its fresh
values. Its begin and settle steps, a context switch, a step that only
moves the model and a model's extended action are not among them, nor
is any step outside the move.
* No recorder is needed. A step that fails ends its case before the
move's settle step, so every step handed over has its value.
* **The host has its word on a move once the checks have run**
(`verdict :: MovePlayed -> SomeObservation -> [CheckRecord] ->
WebApiSessions apps MoveVerdict`): it sees the move, what was observed
and every check's record, and answers a reason the move is inconclusive
and the evidence it noted.
* **The engine keeps the order.** A check that failed fails the case, and
the host's reason stays with the failure. When every check held and
the host gave a reason, the move is inconclusive: its case is
discarded, as one that did not settle is.
* The settle step of such a move keeps what its checks left (each check,
the labels, the model and the stage after it) and is marked
`inconclusive`. A move inconclusive before its checks (`settle` or
`observe` answered `Left`) has no check of its own, as before.
* A regression case that ends this way on replay reads `inconclusive`,
with the reason: never `passed`.
* **The host attaches evidence to a move** (`Evidence`: a `kind` of the
host's own, one line `what`, an optional JSON `detail` carried as it
is). Evidence changes no outcome.
* It is kept on the move's settle step (`StepRecord.evidence`, breaking:
a new field; `"evidence"` in its JSON), so it goes wherever the step
goes: a sink, the first failure, the narrowed case, a replay, a cut
case.
* A failing move's evidence is listed in the failure's text, under its
failed checks (`evidence, <kind>: <what>`). A reason the host gave
when a check failed is kept as one more item, of the engine's own kind
`otherwise inconclusive`.
* The run's trace counts it by kind over the generated cases that
counted (`RunTrace.evidence`, breaking: a new field): how many cases
and moves carried it, and the first three in full. An inconclusive or
discarded case counts nothing, as for the labels and the moves.
* The record bounds it (`boundEvidence`, `evidenceLimit`): a line is
clipped at 4,000 characters, a detail over 32,768 bytes as JSON is
replaced by `{"dropped": bytes}`, and a move keeps 100 items, the rest
counted in one last item of the kind `evidence not kept`.
* **The host is told that a case begins** (`caseBegins`): once before the
first step of every play, a generated case, a narrowing try and a
regression case replayed alike.
* `newWebApiSessionsCxt` replaces `initWebApiSessionsCxt` and `tracingCxt`
(breaking): a case's context holds the move it is in, so one is made for
each play.
* A run that uses none of this costs what it did. The 0.6.0.0 `moves`
suite, its host ported by two lines, allocates +0.09% at -O0 and -0.11%
at -O2 against 0.6.0.0.

## 0.6.0.0 -- 2026-11-18 (zenbridge #2741: a recorder's sink)

* **A recorder hands what it records to a sink, as it happens**
Expand Down
7 changes: 6 additions & 1 deletion webapi-test/quickcheck-dynamic/Test/WebApi/DynamicLogic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,15 +153,20 @@ propApiWithCase :: forall c xstate apps e s.
propApiWithCase rec kept webapiRunner s =
-- a move that did not settle stops the case: neither passed nor failed,
-- so QuickCheck discards it (the trace says it was inconclusive)
monadic (\m -> ioProperty (webapiRunner (runReaderT m (maybe initWebApiSessionsCxt tracingCxt rec)) `catch` inconclusive)) $ do
-- each play of the case has a context of its own: the move it is in is
-- never another play's
monadic (\m -> ioProperty (newWebApiSessionsCxt rec >>= \cxt -> webapiRunner (runReaderT m cxt) `catch` inconclusive)) $ do
mapM_ (\r -> QC.run (liftIO (beginCaseWith r kept))) rec
-- the host is told a play begins, before its first step
QC.run (lift caseBegins)
monitor $ counterexample "\nExecution\n"
(_anonSt, _env) <- runActions s
-- let
-- newApiState = resolveNamedEntities env $ underlyingState anonSt
-- liftIO $ writeIORef newStRef (Just newApiState)
QC.assert True
where
WebApiGlobalStateModel {appMoveHost = MoveHost {caseBegins}} = reflect (Proxy @s)
inconclusive MoveInconclusive {move = mv, why} =
pure (QC.counterexample ("move " ++ T.unpack mv ++ " is inconclusive: " ++ T.unpack why) (QC.property QC.Discard))

Expand Down
144 changes: 131 additions & 13 deletions webapi-test/quickcheck-dynamic/Test/WebApi/Prediction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,20 @@ module Test.WebApi.Prediction
, ThingCheck (..)
, PredictionCheck (..)
, SomeObservation (..)
-- * A move as it was played
, MovePlayed (..)
, MoveStep (..)
, Sent (..)
, Answered (..)
, sentAs
, answeredAs
-- * The host
, MoveHost (..)
, defMoveHost
, MoveVerdict (..)
, noVerdict
, Evidence (..)
, CheckRecord (..)
, MoveInconclusive (..)
) where

Expand All @@ -50,10 +61,10 @@ import qualified Data.Map.Strict as M
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as T
import Data.Typeable (Typeable)
import Data.Typeable (Typeable, cast)
import Test.QuickCheck.StateModel (Any, LookUp, Var)
import Test.WebApi (WebApiSessions)
import Test.WebApi.Trace (StepInfo)
import Test.WebApi (ClientRequest, WebApiSessions)
import Test.WebApi.Trace (CheckRecord (..), Evidence (..), StepInfo, StepKind)
import Test.WebApi.Val (AnyVal)

-- ---------------------------------------------------------------------------
Expand Down Expand Up @@ -198,32 +209,139 @@ data SomeObservation where
instance Show SomeObservation where
show (SomeObservation o) = show o

-- ---------------------------------------------------------------------------
-- A move as it was played

-- | A move as it was played (zenbridge #2715): its name, where it was
-- written, and its own steps in the order they ran. The host is handed it
-- with every call it gets about the move ('MoveHost'), so it can tell what
-- the move did (what it wrote, and what came back) without keeping
-- anything between those calls.
data MovePlayed = MovePlayed
{ name :: Text
, info :: StepInfo
, steps :: [MoveStep]
-- ^ every call, every effect step (a foreign transform, a host's own
-- step) and every fresh value the move's body ran, oldest first. The
-- move's begin and settle steps are not among them, nor are the
-- engine's own steps (one that only moves the model, a context switch,
-- a model's extended action), nor any step outside the move
}

-- | One of a move's own steps, as the engine held it when it ran. Nothing
-- is rendered, clipped or masked: these are the values themselves, read by
-- a cast to the types the host knows ('sentAs', 'answeredAs'). A step that
-- fails ends its case before the move's settle step, so every step here
-- has its value.
data MoveStep = MoveStep
{ kind :: StepKind
-- ^ a call, an effect step (glue) or a fresh value
, info :: Maybe StepInfo
-- ^ where it was written, when a layer above the engine knows: its
-- rule, and the name that layer knows its operation by
, operation :: Text
-- ^ the operation id (a call), the effect's name, or the fresh value's
-- label, as the trace's record of the step names it
, sent :: Maybe Sent
-- ^ a call's request as it went out; none for any other step (the
-- engine never holds an effect step's input as a value)
, value :: Answered
-- ^ what the step gave: a call's result as its caller made it of the
-- answer (of the error, for a call that was expected to be refused),
-- an effect step's result, a fresh value's token
}

-- | A call's request as it went out: its variables looked up, its fresh
-- values and its files as the step made them.
data Sent where
Sent :: Typeable (ClientRequest meth r) => ClientRequest meth r -> Sent

-- | What a step gave, whatever its type.
data Answered where
Answered :: (Typeable a, Show a) => a -> Answered

instance Show Answered where
show (Answered a) = show a

-- | The step's request, when it is a call of this method and route:
-- @sentAs \@POST \@ItemsR step@.
sentAs :: forall meth r. Typeable (ClientRequest meth r) => MoveStep -> Maybe (ClientRequest meth r)
sentAs MoveStep {sent} = sent >>= \(Sent r) -> cast r

-- | The step's value, when it is of this type.
answeredAs :: forall a. Typeable a => MoveStep -> Maybe a
answeredAs MoveStep {value = Answered a} = cast a

-- ---------------------------------------------------------------------------
-- The host

-- | What the host does after a move: wait until what the move caused has
-- settled, within the move's budget; observe; and the checks it makes
-- itself on every flow.
-- settled, within the move's budget; observe; the checks it makes itself
-- on every flow; and, once every check has run, its own word on the move.
-- Each call about a move is handed the move as it was played, the same
-- value each time.
--
-- A move is inconclusive at one of two moments. /Before its checks:/ it
-- did not settle, or nothing could be observed ('settle' or 'observe'
-- answers 'Left'); no check runs. /After them:/ every check held and the
-- host's 'verdict' gives a reason. A check that fails always fails the
-- case: the host's reason is then kept with the failure.
data MoveHost apps = MoveHost
{ settle :: Int -> WebApiSessions apps (Either Text ())
{ settle :: MovePlayed -> Int -> WebApiSessions apps (Either Text ())
-- ^ 'Left' says why it did not settle within the budget (seconds):
-- the move is inconclusive
, observe :: WebApiSessions apps (Either Text SomeObservation)
, observe :: MovePlayed -> WebApiSessions apps (Either Text SomeObservation)
-- ^ 'Left': nothing could be observed, and the move is inconclusive
, hostChecks :: SomeObservation -> [(Text, Either Text ())]
, verdict :: MovePlayed -> SomeObservation -> [CheckRecord] -> WebApiSessions apps MoveVerdict
-- ^ the host's word once the checks have run, given the move, what was
-- observed and every check's record (the property's and the host's
-- own): see 'MoveVerdict'. Called once for every move that was
-- observed, whether its checks held or not
, caseBegins :: WebApiSessions apps ()
-- ^ called once before the first step of every play of a case: a
-- generated case, a narrowing try, a regression case replayed. A host
-- that scopes what it reads to the case takes its mark here
}

-- | A host whose moves settle at once, that observes nothing.
-- | A host whose moves settle at once, that observes nothing and has
-- nothing to say of a move. Build a host from it
-- (@defMoveHost {settle = …, observe = …}@), so that a field added later
-- breaks nothing.
defMoveHost :: MoveHost apps
defMoveHost = MoveHost
{ settle = \_ -> pure (Right ())
, observe = pure (Right (SomeObservation ()))
{ settle = \_ _ -> pure (Right ())
, observe = \_ -> pure (Right (SomeObservation ()))
, hostChecks = const []
, verdict = \_ _ _ -> pure noVerdict
, caseBegins = pure ()
}

-- | The host's word on a move once its checks have run (zenbridge #2715).
data MoveVerdict = MoveVerdict
{ inconclusive :: Maybe Text
-- ^ why the move proves nothing although nothing failed. The engine
-- applies it only when every check held: the move is then
-- inconclusive and its case discarded, the trace saying why. When a
-- check failed the case fails, and this reason is kept with the
-- failure, as evidence of the engine's own kind @otherwise inconclusive@
, evidence :: [Evidence]
-- ^ what the host noted about the move: kept on the move's settle
-- step in the trace, listed with the failure when a check failed, and
-- counted by kind over the run. It changes no outcome
}

-- | A move that did not settle within its budget, or could not be
-- observed: neither passed nor failed. The case runner discards the case
-- and the trace says so.
-- | Nothing to say: no reason, no evidence. A host with something to say
-- writes the constructor (@MoveVerdict {inconclusive = …, evidence = …}@):
-- the trace's step record has fields of the same names, so an update of
-- this value would not say which record it means.
noVerdict :: MoveVerdict
noVerdict = MoveVerdict {inconclusive = Nothing, evidence = []}

-- | A move that did not settle within its budget, could not be observed,
-- or was held inconclusive by its host once its checks had held: neither
-- passed nor failed. The case runner discards the case and the trace says
-- so.
data MoveInconclusive = MoveInconclusive
{ move :: Text
, why :: Text
Expand Down
Loading
Loading