packages feed

keiro-dsl-0.26.0.0: test/Keiro/Dsl/InputSourceSpec.hs

-- | Tests for declared input sources (candidate Language 6
-- @input <Name> from intake <Intake> event <Event>+ …@): parsing, printing,
-- predecessor refusal, the two checker diagnostics, and the proof that a
-- declared source changes no generated byte, fingerprint, ledger or diff.
module Keiro.Dsl.InputSourceSpec (inputSourceSpec) where

import Control.Exception (bracket)
import Control.Monad (filterM, forM, forM_)
import Data.ByteString qualified as BS
import Data.List (sort)
import Data.List.NonEmpty (NonEmpty (..))
import Data.List.NonEmpty qualified as NE
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as TE
import Keiro.Dsl.Diff (Change (..), diffSources)
import Keiro.Dsl.FoldFingerprint (aggregateFoldFingerprintForService)
import Keiro.Dsl.Grammar (InputDecl (..), InputSource (..), Node (..), ProcessBody (..), ProcessNode (..), ReactionBody (..), RouterNode (..))
import Keiro.Dsl.Grammar qualified as G
import Keiro.Dsl.LanguageVersion (ParsedSource (..))
import Keiro.Dsl.Parser (parseSourceDocument, renderParseFailure)
import Keiro.Dsl.PrettyPrint (renderSource)
import Keiro.Dsl.ProcessReaction (checkProcessReaction, processReactionFingerprint)
import Keiro.Dsl.ReplayImpact (ReplayImpact (..), replayImpactServices)
import Keiro.Dsl.RouterSelection (checkRouterSelection, routerSelectionFingerprint)
import Keiro.Dsl.SemanticContract (CheckedService, checkedLanguageContract, checkedSource, checkedSpec)
import Keiro.Dsl.SourceIndex (ParsedSourceDocument (..))
import Keiro.Dsl.TypeGraph (resolveTypeGraph)
import Keiro.Dsl.Validate (Diagnostic (..), DiagnosticCode (..), Severity (..), validateService)
import Keiro.Dsl.Workspace (WorkspaceDiagnostic (..), checkWorkspace, fileContentSource, loadWorkspace)
import System.Directory (createDirectory, doesDirectoryExist, doesFileExist, getTemporaryDirectory, listDirectory, removeFile, removePathForcibly)
import System.Environment (lookupEnv)
import System.Exit (ExitCode (..))
import System.FilePath (makeRelative, takeDirectory, (</>))
import System.IO (hClose, openTempFile)
import System.Process (CreateProcess (..), proc, readCreateProcessWithExitCode)
import Test.Hspec

inputSourceSpec :: Spec
inputSourceSpec = describe "declared input sources" $ do
  grammarTests
  checkerTests
  nonInterferenceTests

--------------------------------------------------------------------------------
-- Milestone 1: grammar, parser and printer
--------------------------------------------------------------------------------

grammarTests :: Spec
grammarTests = do
  it "parses a source on a typed router input and on one of two reaction inputs" $ do
    parsed <- parseFixture "test/fixtures/input-source/sourced.keiro"
    routerSources parsed `shouldBe` [("HospitalTransferRouter", Just ("transferInbox", "TransferNeedAccepted" :| [], 77))]
    processSources parsed
      `shouldBe` [ ("TransferNeedTracking", "TransferNeedArrived", Just ("transferInbox", "TransferNeedAccepted" :| [], 167)),
                   ("TransferNeedTracking", "TransferNeedWithdrawnInput", Nothing)
                 ]

  it "parses a source on a braces router input" $ do
    parsed <- parseFixture "test/fixtures/topology/incident-command-sourced.keiro"
    lookup "CapacityChangeRouter" (routerSources parsed)
      `shouldBe` Just (Just ("hospitalInbox", "HospitalCapacitySnapshotPublished" :| [], 511))

  it "parses a source on a legacy process input" $ do
    original <- readFixtureText "test/fixtures/topology/hospital-capacity.keiro"
    let sourced =
          T.replace
            "  input SurgeInput {"
            "  input SurgeInput from intake incidentInbox event IncidentTransferNeedDeclared {"
            original
    sourced `shouldNotBe` original
    parsed <- either (fail . T.unpack) pure (parseText "hospital-capacity.keiro" sourced)
    [(processId, inputName, source) | (processId, inputName, source@(Just _)) <- processSources parsed]
      `shouldBe` [("HospitalSurge", "SurgeInput", Just ("incidentInbox", "IncidentTransferNeedDeclared" :| [], 497))]
    validationErrors parsed `shouldBe` []

  it "prints the clause where it was written, and the printed form parses back to the same sources" $
    forM_ ["test/fixtures/input-source/sourced.keiro", "test/fixtures/topology/incident-command-sourced.keiro"] $ \path -> do
      parsed <- parseFixture path
      let printed = renderSource parsed
      printed `shouldSatisfy` T.isInfixOf " from intake "
      reparsed <- either (fail . T.unpack) pure (parseText path printed)
      renderSource reparsed `shouldBe` printed
      map (fmap dropLine . snd) (routerSources reparsed) `shouldBe` map (fmap dropLine . snd) (routerSources parsed)
      [(p, i, fmap dropLine s) | (p, i, s) <- processSources reparsed] `shouldBe` [(p, i, fmap dropLine s) | (p, i, s) <- processSources parsed]

  it "Languages 1 to 5 refuse the clause with the version requirement" $
    forM_ [1 .. 5 :: Int] $ \version ->
      forM_ [legacyRouterProbe, legacyProcessProbe] $ \probe ->
        case parseText "probe.keiro" (probe version) of
          Right _ -> expectationFailure ("Language " <> show version <> " accepted an input source")
          Left message -> message `shouldSatisfy` T.isInfixOf "requires keiro-dsl language version 6"

  it "an input name followed directly by its body parses as before" $ do
    unsourced <- parseFixture "test/fixtures/input-source/unsourced.keiro"
    sourced <- parseFixture "test/fixtures/input-source/sourced.keiro"
    [source | (_, source) <- routerSources unsourced] `shouldBe` [Nothing]
    [source | (_, _, source) <- processSources unsourced] `shouldBe` [Nothing, Nothing]
    -- The two files differ only by the two clauses, on unchanged lines, so
    -- removing the parsed sources must give the unsourced specification exactly.
    stripSources sourced.spec `shouldBe` unsourced.spec

legacyRouterProbe :: Int -> Text
legacyRouterProbe version =
  T.unlines
    [ "language keiro-dsl " <> T.pack (show version),
      "context probe",
      "",
      "router ProbeRouter",
      "  name \"probe-router\"",
      "  input ProbeInput from intake probeInbox event ProbeEvent { probeId }"
    ]

legacyProcessProbe :: Int -> Text
legacyProcessProbe version =
  T.unlines
    [ "language keiro-dsl " <> T.pack (show version),
      "context probe",
      "",
      "process ProbeProcess",
      "  name \"probe-process\"",
      "  input ProbeInput from intake probeInbox event ProbeEvent { probeId }"
    ]

--------------------------------------------------------------------------------
-- Milestone 2: checker
--------------------------------------------------------------------------------

checkerTests :: Spec
checkerTests = do
  it "the sourced fixture checks clean" $ do
    parsed <- parseFixture "test/fixtures/input-source/sourced.keiro"
    validationErrors parsed `shouldBe` []

  it "reports an unknown intake and each unaccepted event, once per event" $ do
    original <- readFixtureText "test/fixtures/input-source/sourced.keiro"
    let broken =
          T.replace
            "from intake transferInbox event TransferNeedAccepted : TransferRouteInput"
            "from intake transferInbx event TransferNeedAccepted : TransferRouteInput"
            ( T.replace
                "from intake transferInbox event TransferNeedAccepted { transferNeedId:Text }"
                "from intake transferInbox event TransferNeedAccepted TransferNeedRejected TransferNeedRejected TransferNeedLost { transferNeedId:Text }"
                original
            )
    parsed <- either (fail . T.unpack) pure (parseText "sourced.keiro" broken)
    [(diagnostic.code, diagnostic.line) | diagnostic <- validationErrors parsed]
      `shouldBe` [ (InputSourceUnresolvedIntake, 77),
                   (InputSourceEventNotAccepted, 167),
                   (InputSourceEventNotAccepted, 167)
                 ]
    [diagnostic.message | diagnostic <- validationErrors parsed, diagnostic.code == InputSourceUnresolvedIntake]
      `shouldBe` ["input 'AcceptedHospitalTransferNeed' of router 'HospitalTransferRouter' names intake 'transferInbx', which is not declared in this service"]
    [diagnostic.message | diagnostic <- validationErrors parsed, diagnostic.code == InputSourceEventNotAccepted]
      `shouldBe` [ "input 'TransferNeedArrived' of process 'TransferNeedTracking' is fed by intake 'transferInbox' event 'TransferNeedRejected', which the intake does not accept",
                   "input 'TransferNeedArrived' of process 'TransferNeedTracking' is fed by intake 'transferInbox' event 'TransferNeedLost', which the intake does not accept"
                 ]

  it "resolves an intake declared in another workspace member" $ do
    manifest <- resolveTestPath "test/fixtures/input-source/workspace/service.keiro-workspace"
    loaded <- loadWorkspace (fileContentSource (takeDirectory manifest)) manifest
    case loaded of
      Left failure -> expectationFailure (show failure)
      Right workspace -> [diagnostic | diagnostic <- checkWorkspace workspace, diagnostic.severity == Error] `shouldBe` []

--------------------------------------------------------------------------------
-- Milestone 2: non-interference
--------------------------------------------------------------------------------

nonInterferenceTests :: Spec
nonInterferenceTests = do
  it "scaffolding a sourced specification writes byte-identical files to the unsourced one" $
    withTempDirectory "keiro-dsl-input-source-scaffold" $ \directory -> do
      outputs <- forM ["sourced", "unsourced"] $ \variant -> do
        original <- resolveTestPath ("test/fixtures/input-source/" <> variant <> ".keiro")
        let inputDirectory = directory </> variant
        createDirectory inputDirectory
        BS.readFile original >>= BS.writeFile (inputDirectory </> "service.keiro")
        (exitCode, _, stderrText) <-
          readCreateProcessWithExitCode ((proc "keiro-dsl" ["scaffold", "service.keiro", "--out", "out"]) {cwd = Just inputDirectory}) ""
        exitCode `shouldBe` ExitSuccess
        stderrText `shouldSatisfy` const True
        files <- listFilesRecursively (inputDirectory </> "out")
        contents <- forM files $ \file -> (,) (makeRelative (inputDirectory </> "out") file) <$> BS.readFile file
        pure (sort contents)
      case outputs of
        [sourcedFiles, unsourcedFiles] -> do
          map fst sourcedFiles `shouldBe` map fst unsourcedFiles
          map fst sourcedFiles `shouldSatisfy` any (T.isPrefixOf "keiro-dsl-ledger." . T.pack . lastSegment)
          map fst sourcedFiles `shouldSatisfy` any (T.isPrefixOf "keiro-dsl-cabal-fragment." . T.pack . lastSegment)
          forM_ (zip sourcedFiles unsourcedFiles) $ \((path, sourcedBytes), (_, unsourcedBytes)) ->
            (path, sourcedBytes == unsourcedBytes) `shouldBe` (path, True)
        _ -> expectationFailure "expected two scaffold outputs"

  it "declaring a source changes no fold, reaction or router-selection fingerprint" $ do
    sourced <- checkedSource <$> parseFixture "test/fixtures/input-source/sourced.keiro"
    unsourced <- checkedSource <$> parseFixture "test/fixtures/input-source/unsourced.keiro"
    sourcedFingerprints <- identities sourced
    unsourcedFingerprints <- identities unsourced
    let (folds, reactions, selections) = sourcedFingerprints
    folds `shouldSatisfy` (not . null)
    reactions `shouldSatisfy` (not . null)
    selections `shouldSatisfy` (not . null)
    sourcedFingerprints `shouldBe` unsourcedFingerprints

  it "diff reports no breaking or replay-affecting change for adding sources" $ do
    sourced <- parseFixture "test/fixtures/input-source/sourced.keiro"
    unsourced <- parseFixture "test/fixtures/input-source/unsourced.keiro"
    case diffSources unsourced sourced of
      Left failure -> expectationFailure (show failure)
      Right changes -> [change | change@(Breaking _) <- changes] `shouldBe` []
    replayImpactServices (checkedSource unsourced) (checkedSource sourced) `shouldBe` Right ReplayNeutral

-- | Fold fingerprints of every aggregate, the reaction fingerprint of every
-- reaction process, and the selection fingerprint of every declarative router.
identities :: CheckedService -> IO ([(Text, Text)], [(Text, Text)], [(Text, Text)])
identities service = do
  let spec = checkedSpec service
      contract = checkedLanguageContract service
  graph <- either (fail . show) pure (resolveTypeGraph spec)
  folds <- forM [aggregate | NAggregate aggregate <- spec.nodes] $ \aggregate ->
    either (fail . show) (pure . (,) aggregate.name) (aggregateFoldFingerprintForService service aggregate)
  reactions <- forM [process | NProcess process <- spec.nodes, ReactionProcessBody _ <- [process.body]] $ \process ->
    either (fail . show) (pure . (,) process.id . processReactionFingerprint) (checkProcessReaction contract graph spec process)
  selections <- forM [router | NRouter router <- spec.nodes] $ \router ->
    either (fail . show) (pure . (,) router.id . routerSelectionFingerprint) (checkRouterSelection contract graph spec router)
  pure (folds, reactions, selections)

--------------------------------------------------------------------------------
-- Helpers
--------------------------------------------------------------------------------

type SourceSummary = (Text, NonEmpty Text, Int)

summarize :: InputDecl -> Maybe SourceSummary
summarize input = fmap (\source -> (source.intake, source.events, G.unLoc source.loc)) input.source

dropLine :: SourceSummary -> (Text, NonEmpty Text)
dropLine (intake, events, _) = (intake, events)

routerSources :: ParsedSource -> [(Text, Maybe SourceSummary)]
routerSources parsed = [(router.id, summarize router.input) | NRouter router <- parsed.spec.nodes]

processSources :: ParsedSource -> [(Text, Text, Maybe SourceSummary)]
processSources parsed =
  [ (process.id, input.name, summarize input)
  | NProcess process <- parsed.spec.nodes,
    input <- case process.body of
      LegacyProcessBody input _ _ -> [input]
      ReactionProcessBody reaction -> NE.toList reaction.inputs
  ]

stripSources :: G.Spec -> G.Spec
stripSources spec = spec {G.nodes = map stripNode spec.nodes}
  where
    stripNode = \case
      NRouter router -> NRouter router {input = clear router.input}
      NProcess process -> NProcess process {body = stripBody process.body}
      other -> other
    stripBody = \case
      LegacyProcessBody input handle timer -> LegacyProcessBody (clear input) handle timer
      ReactionProcessBody reaction -> ReactionProcessBody reaction {inputs = NE.map clear reaction.inputs}
    clear :: InputDecl -> InputDecl
    clear input = input {source = Nothing}

validationErrors :: ParsedSource -> [Diagnostic]
validationErrors parsed = [diagnostic | diagnostic <- validateService (checkedSource parsed), diagnostic.severity == Error]

parseText :: FilePath -> Text -> Either Text ParsedSource
parseText path source = case parseSourceDocument path source of
  Left failure -> Left (renderParseFailure failure)
  Right document -> Right document.parsedSource

parseFixture :: FilePath -> IO ParsedSource
parseFixture path = do
  source <- readFixtureText path
  either (fail . T.unpack) pure (parseText path source)

readFixtureText :: FilePath -> IO Text
readFixtureText path = do
  resolved <- resolveTestPath path
  TE.decodeUtf8 <$> BS.readFile resolved

lastSegment :: FilePath -> FilePath
lastSegment = reverse . takeWhile (/= '/') . reverse

listFilesRecursively :: FilePath -> IO [FilePath]
listFilesRecursively directory = do
  entries <- map (directory </>) <$> listDirectory directory
  nested <- forM entries $ \entry -> do
    isDirectory <- doesDirectoryExist entry
    if isDirectory then listFilesRecursively entry else pure [entry]
  pure (concat nested)

-- | Locate a repo file regardless of the test process's current directory.
resolveTestPath :: FilePath -> IO FilePath
resolveTestPath rel = do
  override <- lookupEnv "KEIRO_DSL_TEST_ROOT"
  let candidates = [rel, "keiro-dsl" </> rel] <> maybe [] (\root -> [root </> rel]) override
  existing <- filterM doesFileExist candidates
  case existing of
    path : _ -> pure path
    [] -> fail ("unable to locate keiro-dsl test file " <> show rel <> "; tried " <> show candidates)

withTempDirectory :: String -> (FilePath -> IO a) -> IO a
withTempDirectory template = bracket acquire removePathForcibly
  where
    acquire = do
      base <- getTemporaryDirectory
      (path, handle) <- openTempFile base template
      hClose handle
      removeFile path
      createDirectory path
      pure path