diff options
| author | Arnaud Bailly <arnaud@pankzsoft.com> | 2025-11-05 07:55:45 +0100 |
|---|---|---|
| committer | Arnaud Bailly <arnaud@pankzsoft.com> | 2025-11-05 07:55:45 +0100 |
| commit | 72ed98c66d1585c94ee5383c19a1dec8e10bb050 (patch) | |
| tree | 44605042e4a49411edf401ff9d206939e971a8d7 /lambda-calcul/haskell/test/Minilang | |
| parent | 947940230f1b14bb883e5f5de2593c52e2438c1c (diff) | |
| download | lambda-nantes-72ed98c66d1585c94ee5383c19a1dec8e10bb050.tar.gz | |
record last success/failure in score
Diffstat (limited to 'lambda-calcul/haskell/test/Minilang')
| -rw-r--r-- | lambda-calcul/haskell/test/Minilang/ServerSpec.hs | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs index fdf1389..e109977 100644 --- a/lambda-calcul/haskell/test/Minilang/ServerSpec.hs +++ b/lambda-calcul/haskell/test/Minilang/ServerSpec.hs @@ -15,7 +15,7 @@ import Data.Data (Proxy (..)) import Data.String (fromString) import qualified Data.Text.Lazy as LT import Data.Text.Lazy.Encoding (decodeUtf8) -import Minilang.API (Challenge (..), Score, User (..), defaultChallenges, newToken) +import Minilang.API (Challenge (..), Score (..), User (..), defaultChallenges, newToken) import Minilang.App (makeEnv) import Minilang.Server (serveAPI) import Network.Wai (Application) @@ -121,6 +121,53 @@ spec = with (app testSeed) $ do get ("/api/challenge?token=" <> LBS.toStrict token) `shouldRespondWith` fromString expected + it "GET /api/score returns user current score" $ do + void $ postJSON "/api/register" alice + get ("/api/score?token=" <> LBS.toStrict token) + `shouldRespondWith` 200 + { matchBody = MatchBody $ \_ actual -> + case eitherDecode actual of + Right (score :: Score) -> + if score == Score {user = User "alice", grade = 0, lastTest = Nothing, lastResult = Nothing} + then Nothing + else Just $ "Expected score with grade 0, but got " ++ show score + Left err -> Just $ "Failed to decode JSON: " ++ err + } + + it "GET /api/score returns 400 given token is not registered" $ do + get ("/api/score?token=" <> LBS.toStrict token) + `shouldRespondWith` 400 + + it "GET /api/score returns score with success given last challenge succeeded" $ do + void $ postJSON "/api/register" alice + void $ postJSON ("/api/challenge?token=" <> LBS.toStrict token) "\"x\"" + + get ("/api/score?token=" <> LBS.toStrict token) + `shouldRespondWith` 200 + { matchBody = MatchBody $ \_ actual -> + case eitherDecode actual of + Right (score :: Score) -> + if lastResult score == Just True + then Nothing + else Just $ "Expected score with success, but got " ++ show score + Left err -> Just $ "Failed to decode JSON: " ++ err + } + + it "GET /api/score returns score with failure given last challenge failed" $ do + void $ postJSON "/api/register" alice + void $ postJSON ("/api/challenge?token=" <> LBS.toStrict token) "\"y\"" + + get ("/api/score?token=" <> LBS.toStrict token) + `shouldRespondWith` 200 + { matchBody = MatchBody $ \_ actual -> + case eitherDecode actual of + Right (score :: Score) -> + if lastResult score == Just False + then Nothing + else Just $ "Expected score with failure, but got " ++ show score + Left err -> Just $ "Failed to decode JSON: " ++ err + } + postJSON :: BS.ByteString -> ByteString -> WaiSession () SResponse postJSON url = request "POST" url [("Content-Type", "application/json")] |
