Hydra is an application framework for structuring testable, maintainable business logic in concurrent web servers and command-line applications. It organizes effects such as database access, logging, state handling, concurrency, and error processing through composable monadic approaches. section 9.0
Safe business logic makes failures explicit rather than calling uncontrolled error functions. Domain exceptions can be thrown with throwException and contained with runSafely, producing an explicit Either result while keeping native exception handling behind the framework. section 15.2.3
Safely loading a game session
A loading scenario retrieves a labyrinth and player position, runs each action with runSafely, converts failures into Left values, and creates mutable state only when both loads succeed.
Listing 15.15 Updated white-box test that uses a real runtime
spec :: Spec
spec
= around withAppRuntime
$ describe "Labyrinth KV DB tests"
$ it "Load player position should throw exception"
$ \appRt -> do
mocksVar <- newIORef
[ (0, RunRealInterpreter) #1
, (1, Mock (MockedKVDBConn "LabKVDB")) #2
, (2, Mock (Right testKVDBLabyrinth))
, (3, RunRealInterpreter) #3
, (4, Mock (MockedKVDBConn "LabKVDB")) #4
, (5, Mock (Left (DBError "KVDB Failure")))
, (6, RunRealInterpreter) #5
]
let testRt = TestRuntime (Just appRt) mocksVar
let action = loadGame testKVDBConfig 1
eGameState <- try (Test.runAppL testRt action)
case eGameState of
Left (e :: SomeException) ->
show e `shouldBe` "Failed to load the player pos"
Right _ -> fail "Test failed"
The updated white-box test combines a real application runtime with targeted database mocks, showing how Hydra tests logic across an interpreter hierarchy. section 15.2.3
