module Main (main) where
import Control.Monad.ST (runST)
import System.Exit (exitFailure)
import Pykinos.Penelope.ConditionalTrail
( checkpointTrail
, newConditionalTrail
, readSlot
, rollbackTrail
, trailLogLength
, writeSlot
)
import Pykinos.Penelope.EffectProfile
( ReaderStrategy (..)
, StaticEffectProfile (..)
, defaultProfile
, selectReaderStrategy
)
import Pykinos.Penelope.ReaderTrail
( emptyPersistentEnv
, insertTrail
, lookupPersistent
, lookupTrail
, newReaderTrail
, withLocalBindings
, withPersistentBindings
)
import Pykinos.Penelope.WriterSlab
( censorSlice
, freezeSlab
, listenSlice
, markSlab
, newWriterSlab
, tellManySlab
, tellSlab
)
assert :: String -> Bool -> IO ()
assert desc True = putStrLn (" [PASS] " ++ desc)
assert desc False = do
putStrLn (" [FAIL] " ++ desc)
exitFailure
main :: IO ()
main = do
putStrLn "=== pykinos-penelope Test Suite ==="
putStrLn "--- Section 1: WriterSlab Chunk Boundaries & Listen/Censor ---"
testWriterSlab
putStrLn "--- Section 2: EffectProfile Strategy Selection ---"
testEffectProfile
putStrLn "--- Section 3: ReaderTrail Scoped Lexical Shadowing ---"
testReaderTrail
putStrLn "--- Section 4: ConditionalTrail WAM Epoch Guarding ---"
testConditionalTrail
putStrLn "=== All pykinos-penelope tests passed! ==="
testWriterSlab :: IO ()
testWriterSlab = do
-- Test 1: Tell > 200 items across 64-slot chunks
let (fullList, sliceList, censoredFull) = runST $ do
slab <- newWriterSlab
tellManySlab slab [1 .. 50 :: Int]
m1 <- markSlab slab
tellManySlab slab [51 .. 180 :: Int]
s1 <- listenSlice slab m1
censorSlice slab m1 (* 2)
tellManySlab slab [181 .. 250 :: Int]
f1 <- freezeSlab slab
pure ([1 .. 250 :: Int], s1, f1)
assert "listenSlice extracts exact middle slice [51..180]" $
sliceList == [51 .. 180]
let expectedCensored = [1 .. 50] ++ map (* 2) [51 .. 180] ++ [181 .. 250]
assert "censorSlice mutates only marked slice in-place across chunk boundaries" $
censoredFull == expectedCensored
testEffectProfile :: IO ()
testEffectProfile = do
assert "defaultProfile selects StrategyFrozenFast" $
selectReaderStrategy defaultProfile == StrategyFrozenFast
let localProf = defaultProfile { sepUsesLocal = True }
assert "Local-only profile selects StrategyShallowUndoTrail" $
selectReaderStrategy localProf == StrategyShallowUndoTrail
let multiShotProf = defaultProfile { sepUsesLocal = True, sepUsesMultiShot = True }
assert "MultiShot profile selects StrategyPersistentART" $
selectReaderStrategy multiShotProf == StrategyPersistentART
testReaderTrail :: IO ()
testReaderTrail = do
let (outerVal, innerVal, restoredVal) = runST $ do
trail <- newReaderTrail
insertTrail trail 1 ("global" :: String)
out0 <- lookupTrail trail 1
(inn0, rest0) <- withLocalBindings trail [(1, "local"), (2, "temp")] $ do
i <- lookupTrail trail 1
pure (i, ())
r <- lookupTrail trail 1
t <- lookupTrail trail 2
pure (out0, inn0, (r, t))
assert "Initial lookup finds global value" $
outerVal == Just "global"
assert "withLocalBindings shadows key with local value" $
innerVal == Just "local"
assert "Scope exit restores outer binding and removes ephemeral key" $
restoredVal == (Just "global", Nothing)
-- Persistent env test
let env0 = emptyPersistentEnv
env1 = withPersistentBindings [(1, "val1" :: String)] env0
env2 = withPersistentBindings [(1, "val2"), (2, "val3")] env1
assert "Persistent env lookup reflects latest bindings" $
lookupPersistent env2 1 == Just "val2"
&& lookupPersistent env1 1 == Just "val1"
&& lookupPersistent env2 2 == Just "val3"
testConditionalTrail :: IO ()
testConditionalTrail = do
let (logLen1, logLen2, valAtCp, valAfterRollback) = runST $ do
trail <- newConditionalTrail
writeSlot trail 10 (100 :: Int)
cp1 <- checkpointTrail trail
-- Write slot 10 once in epoch 1 -> logs 1 entry
writeSlot trail 10 200
l1 <- trailLogLength trail
-- Repeated write slot 10 in same epoch 1 -> NO additional log entry!
writeSlot trail 10 300
writeSlot trail 10 400
l2 <- trailLogLength trail
vBefore <- readSlot trail 10
-- Rollback to cp1
rollbackTrail trail cp1
vAfter <- readSlot trail 10
pure (l1, l2, vBefore, vAfter)
assert "Initial mutation in epoch 1 trails prior state (len 2)" $
logLen1 == 2 -- 1 for initial slot 10, 1 for epoch 1 mutation
assert "Repeated mutations within epoch 1 do NOT add to trail log" $
logLen2 == logLen1
assert "Value before rollback is 400" $
valAtCp == Just 400
assert "Rollback restores slot 10 back to 100" $
valAfterRollback == Just 100