{-# LANGUAGE LambdaCase #-}

{- |
Module      : Conformance.EvidencePage
Description : The published evidence page, computed from receipts
License     : Apache-2.0

The conformance evidence page is a function of four inputs and nothing
else: the requirement inventory (@rows.json@), the application model
revision (@conformance/model-revision@), and one snapshot of run output
for one stated code revision — the conformance run receipts and the
backend contract suite's per-adapter results, copied from the CI runs
that produced them.

No state is typed into the page. A requirement reads executed or
partial only through 'effectiveState', from a receipt whose base is the
snapshot's stated base; every other requirement is listed by its
requirement text under the plan @rows.json@ declares. A snapshot whose
receipts or results name another base, were recorded on a dirty tree,
name an unknown requirement, or report a contract case list that is not
the same for every adapter is refused rather than rendered.

The documentation check renders the page again from the committed
inputs and compares it byte for byte with the committed page; any difference fails the check.
-}
module Conformance.EvidencePage
    ( -- * Snapshot
      Snapshot (..)
    , Source (..)
    , ContractResult (..)
    , ContractEvidence (..)
    , ContractOutcome (..)
    , loadSnapshot

      -- * Rendering
    , RenderedPage (..)
    , renderEvidencePage
    , renderConsumerSummary

      -- * The command
    , runEvidencePage
    ) where

import Control.Monad (unless)
import Data.Aeson
    ( FromJSON (..)
    , eitherDecode
    , eitherDecodeStrict
    , withObject
    , withText
    , (.:)
    , (.:?)
    )
import Data.ByteString qualified as BS
import Data.ByteString.Char8 qualified as BS8
import Data.ByteString.Lazy qualified as BSL
import Data.List (nub, sort, sortOn)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.IO qualified as TIO
import System.Directory
    ( doesDirectoryExist
    , doesFileExist
    , listDirectory
    )
import System.Exit (exitFailure)
import System.FilePath (takeExtension, (</>))
import System.IO (hPutStrLn, stderr)

import Conformance.Receipt
    ( Receipt (..)
    , Verdict (..)
    , loadReceipts
    )
import Conformance.Rows
    ( Row (..)
    , RowState (..)
    , ShownState (..)
    , effectiveState
    , loadRows
    )

-- | Where the snapshot's files were copied from: one CI run's artifact.
data Source = Source
    { Source -> Text
sourceWorkflow :: !Text
    , Source -> Text
sourceRun :: !Text
    , Source -> Text
sourceArtifact :: !Text
    }
    deriving stock (Int -> Source -> ShowS
[Source] -> ShowS
Source -> String
(Int -> Source -> ShowS)
-> (Source -> String) -> ([Source] -> ShowS) -> Show Source
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Source -> ShowS
showsPrec :: Int -> Source -> ShowS
$cshow :: Source -> String
show :: Source -> String
$cshowList :: [Source] -> ShowS
showList :: [Source] -> ShowS
Show, Source -> Source -> Bool
(Source -> Source -> Bool)
-> (Source -> Source -> Bool) -> Eq Source
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Source -> Source -> Bool
== :: Source -> Source -> Bool
$c/= :: Source -> Source -> Bool
/= :: Source -> Source -> Bool
Eq)

instance FromJSON Source where
    parseJSON :: Value -> Parser Source
parseJSON = String -> (Object -> Parser Source) -> Value -> Parser Source
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Source" ((Object -> Parser Source) -> Value -> Parser Source)
-> (Object -> Parser Source) -> Value -> Parser Source
forall a b. (a -> b) -> a -> b
$ \Object
o ->
        Text -> Text -> Text -> Source
Source (Text -> Text -> Text -> Source)
-> Parser Text -> Parser (Text -> Text -> Source)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"workflow" Parser (Text -> Text -> Source)
-> Parser Text -> Parser (Text -> Source)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"run" Parser (Text -> Source) -> Parser Text -> Parser Source
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"artifact"

-- | The evidence class of one contract-suite run. There is no public chain.
data ContractEvidence = ContractTestAdapter | ContractDevNet
    deriving stock (Int -> ContractEvidence -> ShowS
[ContractEvidence] -> ShowS
ContractEvidence -> String
(Int -> ContractEvidence -> ShowS)
-> (ContractEvidence -> String)
-> ([ContractEvidence] -> ShowS)
-> Show ContractEvidence
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ContractEvidence -> ShowS
showsPrec :: Int -> ContractEvidence -> ShowS
$cshow :: ContractEvidence -> String
show :: ContractEvidence -> String
$cshowList :: [ContractEvidence] -> ShowS
showList :: [ContractEvidence] -> ShowS
Show, ContractEvidence -> ContractEvidence -> Bool
(ContractEvidence -> ContractEvidence -> Bool)
-> (ContractEvidence -> ContractEvidence -> Bool)
-> Eq ContractEvidence
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ContractEvidence -> ContractEvidence -> Bool
== :: ContractEvidence -> ContractEvidence -> Bool
$c/= :: ContractEvidence -> ContractEvidence -> Bool
/= :: ContractEvidence -> ContractEvidence -> Bool
Eq, Eq ContractEvidence
Eq ContractEvidence =>
(ContractEvidence -> ContractEvidence -> Ordering)
-> (ContractEvidence -> ContractEvidence -> Bool)
-> (ContractEvidence -> ContractEvidence -> Bool)
-> (ContractEvidence -> ContractEvidence -> Bool)
-> (ContractEvidence -> ContractEvidence -> Bool)
-> (ContractEvidence -> ContractEvidence -> ContractEvidence)
-> (ContractEvidence -> ContractEvidence -> ContractEvidence)
-> Ord ContractEvidence
ContractEvidence -> ContractEvidence -> Bool
ContractEvidence -> ContractEvidence -> Ordering
ContractEvidence -> ContractEvidence -> ContractEvidence
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ContractEvidence -> ContractEvidence -> Ordering
compare :: ContractEvidence -> ContractEvidence -> Ordering
$c< :: ContractEvidence -> ContractEvidence -> Bool
< :: ContractEvidence -> ContractEvidence -> Bool
$c<= :: ContractEvidence -> ContractEvidence -> Bool
<= :: ContractEvidence -> ContractEvidence -> Bool
$c> :: ContractEvidence -> ContractEvidence -> Bool
> :: ContractEvidence -> ContractEvidence -> Bool
$c>= :: ContractEvidence -> ContractEvidence -> Bool
>= :: ContractEvidence -> ContractEvidence -> Bool
$cmax :: ContractEvidence -> ContractEvidence -> ContractEvidence
max :: ContractEvidence -> ContractEvidence -> ContractEvidence
$cmin :: ContractEvidence -> ContractEvidence -> ContractEvidence
min :: ContractEvidence -> ContractEvidence -> ContractEvidence
Ord)

instance FromJSON ContractEvidence where
    parseJSON :: Value -> Parser ContractEvidence
parseJSON = String
-> (Text -> Parser ContractEvidence)
-> Value
-> Parser ContractEvidence
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"ContractEvidence" ((Text -> Parser ContractEvidence)
 -> Value -> Parser ContractEvidence)
-> (Text -> Parser ContractEvidence)
-> Value
-> Parser ContractEvidence
forall a b. (a -> b) -> a -> b
$ \case
        Text
"test-adapter" -> ContractEvidence -> Parser ContractEvidence
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ContractEvidence
ContractTestAdapter
        Text
"devnet" -> ContractEvidence -> Parser ContractEvidence
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ContractEvidence
ContractDevNet
        Text
other ->
            String -> Parser ContractEvidence
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail
                ( String
"evidence class "
                    String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
other
                    String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" is not one the contract suite runs: \
                       \test-adapter or devnet"
                )

-- | One contract case's result on one adapter.
data ContractOutcome
    = ContractPassed
    | ContractFailed !Text
    | ContractNotSupported !Text
    deriving stock (Int -> ContractOutcome -> ShowS
[ContractOutcome] -> ShowS
ContractOutcome -> String
(Int -> ContractOutcome -> ShowS)
-> (ContractOutcome -> String)
-> ([ContractOutcome] -> ShowS)
-> Show ContractOutcome
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ContractOutcome -> ShowS
showsPrec :: Int -> ContractOutcome -> ShowS
$cshow :: ContractOutcome -> String
show :: ContractOutcome -> String
$cshowList :: [ContractOutcome] -> ShowS
showList :: [ContractOutcome] -> ShowS
Show, ContractOutcome -> ContractOutcome -> Bool
(ContractOutcome -> ContractOutcome -> Bool)
-> (ContractOutcome -> ContractOutcome -> Bool)
-> Eq ContractOutcome
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ContractOutcome -> ContractOutcome -> Bool
== :: ContractOutcome -> ContractOutcome -> Bool
$c/= :: ContractOutcome -> ContractOutcome -> Bool
/= :: ContractOutcome -> ContractOutcome -> Bool
Eq)

-- | One line of the contract suite's results file.
data ContractResult = ContractResult
    { ContractResult -> Text
crBase :: !Text
    , ContractResult -> Bool
crDirty :: !Bool
    , ContractResult -> Text
crAdapter :: !Text
    , ContractResult -> ContractEvidence
crEvidence :: !ContractEvidence
    , ContractResult -> Text
crCase :: !Text
    , ContractResult -> Text
crKind :: !Text
    , ContractResult -> Text
crRequirement :: !Text
    , ContractResult -> ContractOutcome
crOutcome :: !ContractOutcome
    }
    deriving stock (Int -> ContractResult -> ShowS
[ContractResult] -> ShowS
ContractResult -> String
(Int -> ContractResult -> ShowS)
-> (ContractResult -> String)
-> ([ContractResult] -> ShowS)
-> Show ContractResult
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ContractResult -> ShowS
showsPrec :: Int -> ContractResult -> ShowS
$cshow :: ContractResult -> String
show :: ContractResult -> String
$cshowList :: [ContractResult] -> ShowS
showList :: [ContractResult] -> ShowS
Show, ContractResult -> ContractResult -> Bool
(ContractResult -> ContractResult -> Bool)
-> (ContractResult -> ContractResult -> Bool) -> Eq ContractResult
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ContractResult -> ContractResult -> Bool
== :: ContractResult -> ContractResult -> Bool
$c/= :: ContractResult -> ContractResult -> Bool
/= :: ContractResult -> ContractResult -> Bool
Eq)

instance FromJSON ContractResult where
    parseJSON :: Value -> Parser ContractResult
parseJSON = String
-> (Object -> Parser ContractResult)
-> Value
-> Parser ContractResult
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"ContractResult" ((Object -> Parser ContractResult)
 -> Value -> Parser ContractResult)
-> (Object -> Parser ContractResult)
-> Value
-> Parser ContractResult
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
        outcome <- Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"outcome"
        reason <- o .:? "reason"
        result <- case (outcome :: Text, reason) of
            (Text
"passed", Maybe Text
Nothing) -> ContractOutcome -> Parser ContractOutcome
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ContractOutcome
ContractPassed
            (Text
"failed", Just Text
r) -> ContractOutcome -> Parser ContractOutcome
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> ContractOutcome
ContractFailed Text
r)
            (Text
"not-supported", Just Text
r) -> ContractOutcome -> Parser ContractOutcome
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> ContractOutcome
ContractNotSupported Text
r)
            (Text
"passed", Just Text
_) -> String -> Parser ContractOutcome
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"a passed case carries no reason"
            (Text
_, Maybe Text
Nothing) -> String -> Parser ContractOutcome
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"a " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
outcome String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" case names its reason")
            (Text, Maybe Text)
_ -> String -> Parser ContractOutcome
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"unknown contract outcome: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
outcome)
        ContractResult
            <$> o .: "base"
            <*> o .: "dirty"
            <*> o .: "adapter"
            <*> o .: "evidence"
            <*> o .: "case"
            <*> o .: "kind"
            <*> o .: "requirement"
            <*> pure result

-- | Everything one snapshot directory holds.
data Snapshot = Snapshot
    { Snapshot -> Text
snapBase :: !Text
    -- ^ The code revision every receipt and result must name
    , Snapshot -> [Source]
snapSources :: ![Source]
    , Snapshot -> [Receipt]
snapReceipts :: ![Receipt]
    , Snapshot -> [String]
snapReceiptFiles :: ![FilePath]
    , Snapshot -> [ContractResult]
snapContract :: ![ContractResult]
    , Snapshot -> [String]
snapContractFiles :: ![FilePath]
    }
    deriving stock (Int -> Snapshot -> ShowS
[Snapshot] -> ShowS
Snapshot -> String
(Int -> Snapshot -> ShowS)
-> (Snapshot -> String) -> ([Snapshot] -> ShowS) -> Show Snapshot
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Snapshot -> ShowS
showsPrec :: Int -> Snapshot -> ShowS
$cshow :: Snapshot -> String
show :: Snapshot -> String
$cshowList :: [Snapshot] -> ShowS
showList :: [Snapshot] -> ShowS
Show, Snapshot -> Snapshot -> Bool
(Snapshot -> Snapshot -> Bool)
-> (Snapshot -> Snapshot -> Bool) -> Eq Snapshot
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Snapshot -> Snapshot -> Bool
== :: Snapshot -> Snapshot -> Bool
$c/= :: Snapshot -> Snapshot -> Bool
/= :: Snapshot -> Snapshot -> Bool
Eq)

data Manifest = Manifest Text [Source]

instance FromJSON Manifest where
    parseJSON :: Value -> Parser Manifest
parseJSON = String -> (Object -> Parser Manifest) -> Value -> Parser Manifest
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"Snapshot" ((Object -> Parser Manifest) -> Value -> Parser Manifest)
-> (Object -> Parser Manifest) -> Value -> Parser Manifest
forall a b. (a -> b) -> a -> b
$ \Object
o ->
        Text -> [Source] -> Manifest
Manifest (Text -> [Source] -> Manifest)
-> Parser Text -> Parser ([Source] -> Manifest)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"base" Parser ([Source] -> Manifest) -> Parser [Source] -> Parser Manifest
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object
o Object -> Key -> Parser [Source]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"sources"

{- | Read a snapshot directory: @snapshot.json@ (the stated base and the
CI runs the files were copied from), @receipts/@ (the conformance run's
receipt artifact, as downloaded) and @contract/@ (the contract suite's
results files, one JSON object per line).
-}
loadSnapshot :: FilePath -> IO (Either String Snapshot)
loadSnapshot :: String -> IO (Either String Snapshot)
loadSnapshot String
dir = do
    manifest <- ByteString -> Either String Manifest
forall a. FromJSON a => ByteString -> Either String a
eitherDecode (ByteString -> Either String Manifest)
-> IO ByteString -> IO (Either String Manifest)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO ByteString
BSL.readFile (String
dir String -> ShowS
</> String
"snapshot.json")
    receipts <- loadReceipts (dir </> "receipts")
    receiptFiles <-
        filter
            ( \String
name ->
                ShowS
takeExtension String
name String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
".json" Bool -> Bool -> Bool
&& Text
"receipt-" Text -> Text -> Bool
`T.isPrefixOf` String -> Text
T.pack String
name
            )
            <$> listed (dir </> "receipts")
    contractFiles <-
        filter ((== ".jsonl") . takeExtension)
            <$> listed (dir </> "contract")
    contract <-
        mapM (readResults . ((dir </> "contract") </>)) contractFiles
    pure $ do
        Manifest base sources <-
            either (Left . ("snapshot.json: " <>)) Right manifest
        rs <- receipts
        cs <- concat <$> sequence contract
        pure
            Snapshot
                { snapBase = base
                , snapSources = sources
                , snapReceipts = rs
                , snapReceiptFiles = receiptFiles
                , snapContract = cs
                , snapContractFiles = contractFiles
                }
  where
    listed :: String -> IO [String]
listed String
d = do
        exists <- String -> IO Bool
doesDirectoryExist String
d
        if exists then sort <$> listDirectory d else pure []
    readResults :: String -> IO (Either String [b])
readResults String
path = do
        content <- String -> IO ByteString
BS.readFile String
path
        pure
            . traverse (decodeLine path)
            . zip [1 :: Int ..]
            . filter (not . BS.null)
            $ BS8.lines content
    decodeLine :: String -> (a, ByteString) -> Either String b
decodeLine String
path (a
n, ByteString
line) =
        (String -> Either String b)
-> (b -> Either String b) -> Either String b -> Either String b
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either
            (\String
e -> String -> Either String b
forall a b. a -> Either a b
Left (String
path String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
":" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> a -> String
forall a. Show a => a -> String
show a
n String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
e))
            b -> Either String b
forall a b. b -> Either a b
Right
            (ByteString -> Either String b
forall a. FromJSON a => ByteString -> Either String a
eitherDecodeStrict ByteString
line)

-- | The page as the site publishes it.
newtype RenderedPage = RenderedPage
    { RenderedPage -> Text
renderedPage :: Text
    }

{- | Render the evidence page, or refuse the snapshot naming why. The
first argument is the application model revision.
-}
renderEvidencePage
    :: Text -> [Row] -> Snapshot -> Either String RenderedPage
renderEvidencePage :: Text -> [Row] -> Snapshot -> Either String RenderedPage
renderEvidencePage Text
model [Row]
rows Snapshot
snap = do
    [Row] -> Snapshot -> Either String ()
validateSnapshot [Row]
rows Snapshot
snap
    RenderedPage -> Either String RenderedPage
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> [Row] -> Snapshot -> RenderedPage
layout Text
model [Row]
rows Snapshot
snap)

validateSnapshot :: [Row] -> Snapshot -> Either String ()
validateSnapshot :: [Row] -> Snapshot -> Either String ()
validateSnapshot [Row]
rows Snapshot
snap = do
    Bool -> String -> Either String ()
whenL (Text -> Bool
T.null (Snapshot -> Text
snapBase Snapshot
snap)) String
"the snapshot states no base"
    Bool -> String -> Either String ()
whenL
        ([ContractResult] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (Snapshot -> [ContractResult]
snapContract Snapshot
snap))
        String
"the snapshot holds no contract result"
    (Receipt -> Either String ()) -> [Receipt] -> Either String ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ Receipt -> Either String ()
receiptBound (Snapshot -> [Receipt]
snapReceipts Snapshot
snap)
    (ContractResult -> Either String ())
-> [ContractResult] -> Either String ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ ContractResult -> Either String ()
resultBound (Snapshot -> [ContractResult]
snapContract Snapshot
snap)
    Either String ()
sameCases
  where
    base :: Text
base = Snapshot -> Text
snapBase Snapshot
snap
    known :: [Text]
known = (Row -> Text) -> [Row] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Text
rowId [Row]
rows
    receiptBound :: Receipt -> Either String ()
receiptBound Receipt
r = do
        Bool -> String -> Either String ()
whenL
            (Receipt -> Text
receiptRow Receipt
r Text -> [Text] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Text]
known)
            (String
"a receipt names unknown requirement " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (Receipt -> Text
receiptRow Receipt
r))
        Bool -> String -> Either String ()
whenL
            (Receipt -> Text
receiptBase Receipt
r Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
base)
            ( String
"the receipt for "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (Receipt -> Text
receiptRow Receipt
r)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" names base "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (Receipt -> Text
receiptBase Receipt
r)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", not the stated base "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
base
            )
        Bool -> String -> Either String ()
whenL
            (Receipt -> Bool
receiptDirty Receipt
r)
            ( String
"the receipt for "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (Receipt -> Text
receiptRow Receipt
r)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" was recorded on a dirty tree"
            )
    resultBound :: ContractResult -> Either String ()
resultBound ContractResult
c = do
        Bool -> String -> Either String ()
whenL
            (ContractResult -> Text
crBase ContractResult
c Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
base)
            ( String
"the contract result for "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (ContractResult -> Text
crAdapter ContractResult
c)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" / "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (ContractResult -> Text
crCase ContractResult
c)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" names base "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (ContractResult -> Text
crBase ContractResult
c)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", not the stated base "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
base
            )
        Bool -> String -> Either String ()
whenL
            (ContractResult -> Bool
crDirty ContractResult
c)
            ( String
"the contract result for "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (ContractResult -> Text
crAdapter ContractResult
c)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" / "
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack (ContractResult -> Text
crCase ContractResult
c)
                String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" was recorded on a dirty tree"
            )
    sameCases :: Either String ()
sameCases = do
        let byAdapter :: [(Text, [ContractResult])]
byAdapter = [ContractResult] -> [(Text, [ContractResult])]
adapterCases (Snapshot -> [ContractResult]
snapContract Snapshot
snap)
            caseLists :: [[Text]]
caseLists = ((Text, [ContractResult]) -> [Text])
-> [(Text, [ContractResult])] -> [[Text]]
forall a b. (a -> b) -> [a] -> [b]
map ((ContractResult -> Text) -> [ContractResult] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ContractResult -> Text
crCase ([ContractResult] -> [Text])
-> ((Text, [ContractResult]) -> [ContractResult])
-> (Text, [ContractResult])
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, [ContractResult]) -> [ContractResult]
forall a b. (a, b) -> b
snd) [(Text, [ContractResult])]
byAdapter
        ((Text, [ContractResult]) -> Either String ())
-> [(Text, [ContractResult])] -> Either String ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_
            ( \(Text
a, [ContractResult]
cs) ->
                Bool -> String -> Either String ()
whenL
                    ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ((ContractResult -> Text) -> [ContractResult] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ContractResult -> Text
crCase [ContractResult]
cs)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= [ContractResult] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ContractResult]
cs)
                    (String
"adapter " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
T.unpack Text
a String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" reports a contract case twice")
            )
            [(Text, [ContractResult])]
byAdapter
        case [[Text]]
caseLists of
            [Text]
first : [[Text]]
rest ->
                Bool -> String -> Either String ()
whenL
                    (([Text] -> Bool) -> [[Text]] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (([Text] -> [Text] -> Bool
forall a. Eq a => a -> a -> Bool
/= [Text] -> [Text]
forall a. Ord a => [a] -> [a]
sort [Text]
first) ([Text] -> Bool) -> ([Text] -> [Text]) -> [Text] -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [Text]
forall a. Ord a => [a] -> [a]
sort) [[Text]]
rest)
                    String
"the adapters do not report the same contract case list"
            [] -> () -> Either String ()
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

whenL :: Bool -> String -> Either String ()
whenL :: Bool -> String -> Either String ()
whenL Bool
True String
e = String -> Either String ()
forall a b. a -> Either a b
Left String
e
whenL Bool
False String
_ = () -> Either String ()
forall a b. b -> Either a b
Right ()

-- | Results grouped by adapter, adapters in first-reported order.
adapterCases :: [ContractResult] -> [(Text, [ContractResult])]
adapterCases :: [ContractResult] -> [(Text, [ContractResult])]
adapterCases [ContractResult]
cs =
    [ (Text
a, [ContractResult
c | ContractResult
c <- [ContractResult]
cs, ContractResult -> Text
crAdapter ContractResult
c Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
a])
    | Text
a <- [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ((ContractResult -> Text) -> [ContractResult] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map ContractResult -> Text
crAdapter [ContractResult]
cs)
    ]

-- * Layout

data Section = Section
    { Section -> Int
secLevel :: Int
    , Section -> Text
secTitle :: Text
    , Section -> [Text]
secBody :: [Text]
    }

layout :: Text -> [Row] -> Snapshot -> RenderedPage
layout :: Text -> [Row] -> Snapshot -> RenderedPage
layout Text
model [Row]
rows Snapshot
snap =
    RenderedPage
        { renderedPage :: Text
renderedPage = [Text] -> Text
T.unlines ([Text]
preamble [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> (Section -> [Text]) -> [Section] -> [Text]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Section -> [Text]
sectionText [Section]
sections)
        }
  where
    base :: Text
base = Snapshot -> Text
snapBase Snapshot
snap
    shown :: [(Row, ShownState)]
shown =
        [ (Row
r, Text -> [Receipt] -> Row -> ShownState
effectiveState Text
base (Snapshot -> [Receipt]
snapReceipts Snapshot
snap) Row
r)
        | Row
r <- (Row -> Text) -> [Row] -> [Row]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn Row -> Text
rowId [Row]
rows
        ]
    inState :: (ShownState -> Bool) -> [Row]
inState ShownState -> Bool
p = [Row
r | (Row
r, ShownState
s) <- [(Row, ShownState)]
shown, ShownState -> Bool
p ShownState
s]
    executed :: [Row]
executed = (ShownState -> Bool) -> [Row]
inState (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== ShownState
ShownExecuted)
    partial :: [Row]
partial = (ShownState -> Bool) -> [Row]
inState (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== ShownState
ShownPartial)
    recorded :: [(Row, Verdict)]
recorded = [(Row
r, Verdict
v) | (Row
r, ShownRecorded Verdict
v) <- [(Row, ShownState)]
shown]
    planned :: RowState -> [Row]
planned RowState
st = (ShownState -> Bool) -> [Row]
inState (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== RowState -> ShownState
ShownPlanned RowState
st)
    uncovered :: [Row]
uncovered = RowState -> [Row]
planned RowState
Uncovered
    elsewhere :: [Row]
elsewhere = RowState -> [Row]
planned RowState
BoundElsewhere
    outOfScope :: [Row]
outOfScope = RowState -> [Row]
planned RowState
OutOfScope
    adapters :: [(Text, [ContractResult])]
adapters = [ContractResult] -> [(Text, [ContractResult])]
adapterCases (Snapshot -> [ContractResult]
snapContract Snapshot
snap)
    count :: [a] -> Text
count = String -> Text
T.pack (String -> Text) -> ([a] -> String) -> [a] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> String
forall a. Show a => a -> String
show (Int -> String) -> ([a] -> Int) -> [a] -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length

    preamble :: [Text]
preamble =
        [ Text
"# Conformance evidence"
        , Text
""
        , Text
"As an integrator deciding whether to build on Singular, I read which of the \
          \consumer requirements a run of one named code revision demonstrated, which \
          \it did not, and how every backend adapter answered the shared contract — \
          \and I can check that nothing on this page was typed by hand."
        , Text
""
        , Text
"This page is generated by `conformance evidence-page` from the requirement \
          \inventory `conformance/rows.json`, the application model revision in \
          \`conformance/model-revision`, and the receipts and contract results the CI \
          \runs named in the appendix produced for the code revision below. The \
          \documentation check regenerates it from those committed files and fails on \
          \any difference."
        , Text
""
        ]

    sections :: [Section]
sections =
        [ Int -> Text -> [Text] -> Section
Section
            Int
2
            Text
"What this evidence is bound to"
            [ Text
"| | |"
            , Text
"| --- | --- |"
            , Text
"| Code revision the runs checked out | `" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
base Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"` |"
            , Text
"| Application model revision | `" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
model Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"` |"
            , Text
"| Requirements in the inventory | "
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Row] -> Text
forall {a}. [a] -> Text
count [Row]
rows
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" ("
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Row] -> Text
forall {a}. [a] -> Text
count [Row]
outOfScope
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" out of scope) |"
            , Text
"| Demonstrated by a run | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Row] -> Text
forall {a}. [a] -> Text
count [Row]
executed Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
            , Text
"| Partly demonstrated by a run | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Row] -> Text
forall {a}. [a] -> Text
count [Row]
partial Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
            , Text
"| Run, result not a pass | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [(Row, Verdict)] -> Text
forall {a}. [a] -> Text
count [(Row, Verdict)]
recorded Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
            , Text
"| Bound to evidence elsewhere | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Row] -> Text
forall {a}. [a] -> Text
count [Row]
elsewhere Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
            , Text
"| Uncovered | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Row] -> Text
forall {a}. [a] -> Text
count [Row]
uncovered Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
            , Text
"| Backend adapters in the contract runs | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [(Text, [ContractResult])] -> Text
forall {a}. [a] -> Text
count [(Text, [ContractResult])]
adapters Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
            , Text
""
            , Text
"```mermaid"
            , Text
"flowchart TD"
            , Text
"    inputs[\"Inventory, model revision,<br/>receipts and backend results\"] -->|read and validate| render[\"Evidence reader\"]"
            , Text
"    render -->|computed state and dated appendix| page[\"Published page\"]"
            , Text
"```"
            ]
        , Int -> Text -> [Text] -> Section
Section
            Int
2
            Text
"Requirements a run demonstrated"
            ( [(Row, Text)] -> [Text]
requirementTable
                ( [(Row
r, Text
"demonstrated") | Row
r <- [Row]
executed]
                    [(Row, Text)] -> [(Row, Text)] -> [(Row, Text)]
forall a. Semigroup a => a -> a -> a
<> [(Row
r, Text
"partly demonstrated") | Row
r <- [Row]
partial]
                )
            )
        , Int -> Text -> [Text] -> Section
Section
            Int
2
            Text
"Requirements a run did not pass"
            ([(Row, Text)] -> [Text]
requirementTable [(Row
r, Text -> Verdict -> Text
verdictText (Row -> Text
rowId Row
r) Verdict
v) | (Row
r, Verdict
v) <- [(Row, Verdict)]
recorded])
        , Int -> Text -> [Text] -> Section
Section
            Int
2
            Text
"Requirements not demonstrated"
            [ Text
"Every requirement without a receipt for this code revision is listed with \
              \the plan the inventory declares for it."
            ]
        , Int -> Text -> [Text] -> Section
Section
            Int
3
            Text
"Uncovered"
            ([(Row, Text)] -> [Text]
requirementTable [(Row
r, Text
"uncovered") | Row
r <- [Row]
uncovered])
        , Int -> Text -> [Text] -> Section
Section
            Int
3
            Text
"Bound to evidence elsewhere"
            ( [(Row, Text)] -> [Text]
requirementTable
                [ (Row
r, Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"bound elsewhere" (Text
"bound elsewhere: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>) (Row -> Maybe Text
rowEvidence Row
r))
                | Row
r <- [Row]
elsewhere
                ]
            )
        , Int -> Text -> [Text] -> Section
Section
            Int
3
            Text
"Out of scope"
            ([(Row, Text)] -> [Text]
requirementTable [(Row
r, Text
"out of scope") | Row
r <- [Row]
outOfScope])
        , Int -> Text -> [Text] -> Section
Section
            Int
2
            Text
"Appendix: backend contract across adapters"
            ( [ Text
"One case list runs against every adapter. A case an adapter cannot \
                \exercise is reported for it as not supported, with the reason; it is \
                \never counted as passed."
              , Text
""
              , Text
"| Adapter | Evidence class | Passed | Failed | Not supported |"
              , Text
"| --- | --- | --- | --- | --- |"
              ]
                [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [ Text
"| "
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
a
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [ContractResult] -> Text
evidenceText [ContractResult]
cs
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (ContractOutcome -> Bool) -> [ContractResult] -> Text
outcomes ContractOutcome -> Bool
isPassed [ContractResult]
cs
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (ContractOutcome -> Bool) -> [ContractResult] -> Text
outcomes ContractOutcome -> Bool
isFailed [ContractResult]
cs
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (ContractOutcome -> Bool) -> [ContractResult] -> Text
outcomes ContractOutcome -> Bool
isNotSupported [ContractResult]
cs
                        Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
                   | (Text
a, [ContractResult]
cs) <- [(Text, [ContractResult])]
adapters
                   ]
            )
        ]
            [Section] -> [Section] -> [Section]
forall a. Semigroup a => a -> a -> a
<> [ Int -> Text -> [Text] -> Section
Section
                    Int
3
                    (Text
a Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" adapter")
                    ( [ Text
"Evidence class: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [ContractResult] -> Text
evidenceText [ContractResult]
cs Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"."
                      , Text
""
                      , Text
"| Contract case | Kind | Result |"
                      , Text
"| --- | --- | --- |"
                      ]
                        [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [ Text
"| "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
cell (ContractResult -> Text
crRequirement ContractResult
c)
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ContractResult -> Text
crKind ContractResult
c
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
cell (ContractOutcome -> Text
outcomeText (ContractResult -> ContractOutcome
crOutcome ContractResult
c))
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
                           | ContractResult
c <- [ContractResult]
cs
                           ]
                    )
               | (Text
a, [ContractResult]
cs) <- [(Text, [ContractResult])]
adapters
               ]
            [Section] -> [Section] -> [Section]
forall a. Semigroup a => a -> a -> a
<> [ Int -> Text -> [Text] -> Section
Section
                    Int
2
                    Text
"Appendix: how this page was produced"
                    ( [ Text
"This appendix is harness evidence: where the files behind this \
                        \page came from. It says nothing more about the product."
                      , Text
""
                      , Text
"| Workflow | Run | Artifact |"
                      , Text
"| --- | --- | --- |"
                      ]
                        [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [ Text
"| "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Source -> Text
sourceWorkflow Source
s
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Source -> Text
sourceRun Source
s
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Source -> Text
sourceArtifact Source
s
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
                           | Source
s <- Snapshot -> [Source]
snapSources Snapshot
snap
                           ]
                        [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [ Text
""
                           , Text
"The committed snapshot is `conformance/evidence/page/`: "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [String] -> Text
forall {a}. [a] -> Text
count (Snapshot -> [String]
snapReceiptFiles Snapshot
snap)
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" files of the receipt artifacts under `receipts/`, of which "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [Receipt] -> Text
forall {a}. [a] -> Text
count (Snapshot -> [Receipt]
snapReceipts Snapshot
snap)
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" are receipts, and "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [String] -> Text
forall {a}. [a] -> Text
count (Snapshot -> [String]
snapContractFiles Snapshot
snap)
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" contract results files under `contract/` holding "
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> [ContractResult] -> Text
forall {a}. [a] -> Text
count (Snapshot -> [ContractResult]
snapContract Snapshot
snap)
                                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" results."
                           , Text
""
                           , Text
"Regenerate the page with `nix run --quiet ./conformance#conformance -- \
                             \evidence-page --root . --write`; check it with the same command without \
                             \`--write`."
                           ]
                    )
               ]

    requirementTable :: [(Row, Text)] -> [Text]
requirementTable [(Row, Text)]
entries =
        if [(Row, Text)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(Row, Text)]
entries
            then [Text
"None."]
            else
                [ Text
"| Requirement | Expected outcome | State |"
                , Text
"| --- | --- | --- |"
                ]
                    [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [ Text
"| "
                            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
cell (Row -> Text
rowRequirement Row
r)
                            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
cell (Row -> Text
rowExpected Row
r)
                            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | "
                            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
cell Text
st
                            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"
                       | (Row
r, Text
st) <- [(Row, Text)]
entries
                       ]

    evidenceText :: [ContractResult] -> Text
evidenceText [ContractResult]
cs =
        Text -> [Text] -> Text
T.intercalate Text
", " ([Text] -> Text) -> ([Text] -> [Text]) -> [Text] -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$ (ContractResult -> Text) -> [ContractResult] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (ContractEvidence -> Text
contractEvidenceName (ContractEvidence -> Text)
-> (ContractResult -> ContractEvidence) -> ContractResult -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractResult -> ContractEvidence
crEvidence) [ContractResult]
cs
    outcomes :: (ContractOutcome -> Bool) -> [ContractResult] -> Text
outcomes ContractOutcome -> Bool
p = [ContractResult] -> Text
forall {a}. [a] -> Text
count ([ContractResult] -> Text)
-> ([ContractResult] -> [ContractResult])
-> [ContractResult]
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (ContractResult -> Bool) -> [ContractResult] -> [ContractResult]
forall a. (a -> Bool) -> [a] -> [a]
filter (ContractOutcome -> Bool
p (ContractOutcome -> Bool)
-> (ContractResult -> ContractOutcome) -> ContractResult -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractResult -> ContractOutcome
crOutcome)

sectionText :: Section -> [Text]
sectionText :: Section -> [Text]
sectionText Section
s =
    [Int -> Text -> Text
T.replicate (Section -> Int
secLevel Section
s) Text
"#" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Section -> Text
secTitle Section
s, Text
""]
        [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> Section -> [Text]
secBody Section
s
        [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
""]

cell :: Text -> Text
cell :: Text -> Text
cell = HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"|" Text
"\\|" (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\n" Text
" "

contractEvidenceName :: ContractEvidence -> Text
contractEvidenceName :: ContractEvidence -> Text
contractEvidenceName ContractEvidence
ContractTestAdapter = Text
"test-adapter"
contractEvidenceName ContractEvidence
ContractDevNet = Text
"devnet"

outcomeText :: ContractOutcome -> Text
outcomeText :: ContractOutcome -> Text
outcomeText = \case
    ContractOutcome
ContractPassed -> Text
"passed"
    ContractFailed Text
r -> Text
"failed: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
r
    ContractNotSupported Text
r -> Text
"not supported: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
r

isPassed, isFailed, isNotSupported :: ContractOutcome -> Bool
isPassed :: ContractOutcome -> Bool
isPassed ContractOutcome
ContractPassed = Bool
True
isPassed ContractOutcome
_ = Bool
False
isFailed :: ContractOutcome -> Bool
isFailed ContractFailed{} = Bool
True
isFailed ContractOutcome
_ = Bool
False
isNotSupported :: ContractOutcome -> Bool
isNotSupported ContractNotSupported{} = Bool
True
isNotSupported ContractOutcome
_ = Bool
False

verdictText :: Text -> Verdict -> Text
verdictText :: Text -> Verdict -> Text
verdictText Text
_ = \case
    Verdict
AgreesWithModel -> Text
"agrees with the model"
    Verdict
HeldQ002 ->
        Text
"held: the chain sided with Singular's model against the consumer's \
        \theorem, pending a ruling"
    Verdict
DivergesFromLean -> Text
"diverges: the chain contradicts Singular's model"
    Verdict
ResolvedByRuling -> Text
"resolved by a ruling: retained as history, not a pass"
    Verdict
UnmetByRuling -> Text
"unmet by a ruling"
    Verdict
Partial -> Text
"partial"

{- | The consumer page shares the evidence reader's validated inputs and
receipt-derived standings. Requirement families are discovered, not listed
in a second catalogue. Machine group keys are rendered as ordinary words.
-}
renderConsumerSummary :: [Row] -> Snapshot -> Either String Text
renderConsumerSummary :: [Row] -> Snapshot -> Either String Text
renderConsumerSummary [Row]
rows Snapshot
snap = do
    [Row] -> Snapshot -> Either String ()
validateSnapshot [Row]
rows Snapshot
snap
    Text -> Either String Text
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Either String Text) -> Text -> Either String Text
forall a b. (a -> b) -> a -> b
$
        [Text] -> Text
T.unlines ([Text] -> Text) -> [Text] -> Text
forall a b. (a -> b) -> a -> b
$
            [ Text
"<!-- consumer-summary:start -->"
            , Text
""
            , Text
"This summary is computed from the same inventory and validated receipts as the \
              \[evidence page](conformance-evidence.md), for code revision `"
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Snapshot -> Text
snapBase Snapshot
snap
                Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"`. Separate command journeys and measurements do not grant model-comparison credit."
            , Text
""
            , Text
"| Standing | Requirements |"
            , Text
"|---|---:|"
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line Text
"Total requirements" ([Row] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Row]
rows)
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line Text
"Demonstrated by a run" ((ShownState -> Bool) -> Int
count (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== ShownState
ShownExecuted))
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line Text
"Partly demonstrated by a run" ((ShownState -> Bool) -> Int
count (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== ShownState
ShownPartial))
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line Text
"Run, result not a pass" ((ShownState -> Bool) -> Int
count ShownState -> Bool
recorded)
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line
                Text
"Bound to evidence elsewhere"
                ((ShownState -> Bool) -> Int
count (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== RowState -> ShownState
ShownPlanned RowState
BoundElsewhere))
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line Text
"Uncovered" ((ShownState -> Bool) -> Int
count (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== RowState -> ShownState
ShownPlanned RowState
Uncovered))
            , Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line Text
"Out of scope" ((ShownState -> Bool) -> Int
count (ShownState -> ShownState -> Bool
forall a. Eq a => a -> a -> Bool
== RowState -> ShownState
ShownPlanned RowState
OutOfScope))
            , Text
""
            , Text
"| Requirement family | Requirements |"
            , Text
"|---|---:|"
            ]
                [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [ Text -> Int -> Text
forall {a}. Show a => Text -> a -> Text
line
                        (Text -> Text
T.toTitle (HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"-" Text
" " Text
group))
                        ([Row] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((Row -> Bool) -> [Row] -> [Row]
forall a. (a -> Bool) -> [a] -> [a]
filter ((Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
group) (Text -> Bool) -> (Row -> Text) -> Row -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Row -> Text
rowGroup) [Row]
rows))
                   | Text
group <- [Text] -> [Text]
forall a. Ord a => [a] -> [a]
sort ([Text] -> [Text]
forall a. Eq a => [a] -> [a]
nub ((Row -> Text) -> [Row] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Row -> Text
rowGroup [Row]
rows))
                   ]
                [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"", Text
"<!-- consumer-summary:end -->"]
  where
    states :: [ShownState]
states = (Row -> ShownState) -> [Row] -> [ShownState]
forall a b. (a -> b) -> [a] -> [b]
map (Text -> [Receipt] -> Row -> ShownState
effectiveState (Snapshot -> Text
snapBase Snapshot
snap) (Snapshot -> [Receipt]
snapReceipts Snapshot
snap)) [Row]
rows
    count :: (ShownState -> Bool) -> Int
count ShownState -> Bool
p = [ShownState] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ((ShownState -> Bool) -> [ShownState] -> [ShownState]
forall a. (a -> Bool) -> [a] -> [a]
filter ShownState -> Bool
p [ShownState]
states)
    recorded :: ShownState -> Bool
recorded ShownRecorded{} = Bool
True
    recorded ShownState
_ = Bool
False
    line :: Text -> a -> Text
line Text
label a
value = Text
"| " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
label Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" | " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
T.pack (a -> String
forall a. Show a => a -> String
show a
value) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" |"

-- | Replace exactly one generated section while preserving the page's prose.
consumerPage :: Text -> Text -> Either String Text
consumerPage :: Text -> Text -> Either String Text
consumerPage Text
summary Text
page = do
    Bool -> String -> Either String ()
whenL
        (HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
start Text
page Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1 Bool -> Bool -> Bool
|| HasCallStack => Text -> Text -> Int
Text -> Text -> Int
T.count Text
end Text
page Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
1)
        String
"the consumer page needs exactly one marked summary"
    let (Text
before, Text
rest) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
start Text
page
        (Text
_, Text
after) = HasCallStack => Text -> Text -> (Text, Text)
Text -> Text -> (Text, Text)
T.breakOn Text
end Text
rest
    Bool -> String -> Either String ()
whenL (Text -> Bool
T.null Text
after) String
"the consumer summary's end precedes its start"
    Text -> Either String Text
forall a. a -> Either String a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
before Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> Text
T.stripEnd Text
summary Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
T.drop (Text -> Int
T.length Text
end) Text
after)
  where
    start :: Text
start = Text
"<!-- consumer-summary:start -->"
    end :: Text
end = Text
"<!-- consumer-summary:end -->"

runEvidencePage :: FilePath -> Bool -> IO ()
runEvidencePage :: String -> Bool -> IO ()
runEvidencePage String
root Bool
write = do
    rows <-
        String -> IO (Either String [Row])
loadRows (String
root String -> ShowS
</> String
"conformance/rows.json") IO (Either String [Row])
-> (Either String [Row] -> IO [Row]) -> IO [Row]
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (String -> IO [Row])
-> ([Row] -> IO [Row]) -> Either String [Row] -> IO [Row]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> IO [Row]
forall a. String -> IO a
refuse [Row] -> IO [Row]
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
    model <-
        T.strip <$> TIO.readFile (root </> "conformance/model-revision")
    snap <-
        loadSnapshot (root </> "conformance/evidence/page")
            >>= either refuse pure
    page <- either refuse pure (renderEvidencePage model rows snap)
    summary <- either refuse pure (renderConsumerSummary rows snap)
    consumer <-
        TIO.readFile consumerPath
            >>= either refuse pure . consumerPage summary
    mapM_
        (publish write)
        [(pagePath, renderedPage page), (consumerPath, consumer)]
  where
    pagePath :: String
pagePath = String
root String -> ShowS
</> String
"docs/conformance-evidence.md"
    consumerPath :: String
consumerPath = String
root String -> ShowS
</> String
"docs/consumer-conformance.md"

publish :: Bool -> (FilePath, Text) -> IO ()
publish :: Bool -> (String, Text) -> IO ()
publish Bool
write (String
pagePath, Text
page) =
    if Bool
write
        then do
            String -> Text -> IO ()
TIO.writeFile String
pagePath Text
page
            String -> IO ()
putStrLn (String
"wrote " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
pagePath)
        else do
            exists <- String -> IO Bool
doesFileExist String
pagePath
            unless exists (refuse (pagePath <> " is missing"))
            committed <- TIO.readFile pagePath
            case firstDifference (T.lines page) (T.lines committed) of
                Just (Int
n, Text
want, Text
got) ->
                    String -> IO ()
forall a. String -> IO a
refuse
                        ( String
pagePath
                            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" disagrees with the receipts it is computed from at line "
                            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show Int
n
                            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
": rendered "
                            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
want
                            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
", published "
                            String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Text -> String
forall a. Show a => a -> String
show Text
got
                        )
                Maybe (Int, Text, Text)
Nothing
                    | Text
committed Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
page ->
                        String -> IO ()
forall a. String -> IO a
refuse (String
pagePath String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" differs from its rendering in line endings")
                    | Bool
otherwise -> () -> IO ()
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

refuse :: String -> IO a
refuse :: forall a. String -> IO a
refuse String
msg = do
    Handle -> String -> IO ()
hPutStrLn Handle
stderr (String
"conformance evidence-page: FAILED: " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
msg)
    IO a
forall a. IO a
exitFailure

-- | The first line (1-based) where two texts differ, with both sides.
firstDifference :: [Text] -> [Text] -> Maybe (Int, Text, Text)
firstDifference :: [Text] -> [Text] -> Maybe (Int, Text, Text)
firstDifference [Text]
want [Text]
got =
    case [ (Int
n, Text
w, Text
g)
         | (Int
n, Text
w, Text
g) <-
            [Int] -> [Text] -> [Text] -> [(Int, Text, Text)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3
                [Int
1 ..]
                ([Text] -> [Text]
forall {a}. IsString a => [a] -> [a]
pad [Text]
want)
                ([Text] -> [Text]
forall {a}. IsString a => [a] -> [a]
pad [Text]
got)
         , Text
w Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
g
         ] of
        (Int
n, Text
w, Text
g) : [(Int, Text, Text)]
_ -> (Int, Text, Text) -> Maybe (Int, Text, Text)
forall a. a -> Maybe a
Just (Int
n, Text
w, Text
g)
        [] -> Maybe (Int, Text, Text)
forall a. Maybe a
Nothing
  where
    width :: Int
width = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
want) ([Text] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
got)
    pad :: [a] -> [a]
pad [a]
xs = Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
take Int
width ([a]
xs [a] -> [a] -> [a]
forall a. Semigroup a => a -> a -> a
<> a -> [a]
forall a. a -> [a]
repeat a
"<end of page>")