packages feed

unkai-core-0.1.0.0: test/Main.hs

module Main (main) where

import Data.Aeson qualified as JSON
import Data.Aeson.Types (parseMaybe)
import Data.Text.Lazy.Builder (toLazyText)
import Test.Tasty
import Test.Tasty.HUnit
import Unkai.Prelude

main :: IO ()
main =
  defaultMain $
    testGroup
      "core"
      [ testCase "a simple capture encodes a single UTF-8 segment" $ do
          let client = buildClient (Proxy @("b" :> Capture "object" Text :> Get '[JSON] ())) mempty "x/y%20 á?"
          toLazyText (_rqPath (_cliRequest (client service))) @?= "/b/x%2Fy%2520%20%C3%A1%3F",
        testCase "a repeated capture encodes each segment and inserts one separator" $ do
          let client = buildClient (Proxy @("b" :> Captures "object" Text :> Get '[JSON] ())) mempty ["x/y", "a b"]
          toLazyText (_rqPath (_cliRequest (client service))) @?= "/b/x%2Fy/a%20b"
          toLazyText (_rqPath (appendPaths (appendPath mempty "b") ([] :: [Text]))) @?= "/b",
        testCase "a mode suffix remains literal and reserved expansions retain slashes" $ do
          let simple = buildClient (Proxy @(CaptureMode "name" "move" Text :> Get '[JSON] ())) mempty "x/y"
              reserved = buildClient (Proxy @(CaptureReserved "name" Text :> Get '[JSON] ())) mempty "x/y%20"
          toLazyText (_rqPath (_cliRequest (simple service))) @?= "/x%2Fy:move"
          toLazyText (_rqPath (_cliRequest (reserved service))) @?= "/x/y%2520",
        testCase "patch fields distinguish absence, assignment and null" $ do
          let states = [Omit, Set ("new" :: Text), Clear]
              encode state = object (maybeToList (patchField toJSON "field" state))
              decode = withObject "test" (parsePatchField parseJSON "field")
          map encode states @?= [object [], object ["field" .= ("new" :: Text)], object ["field" .= JSON.Null]]
          map (parseMaybe decode . encode) states @?= map Just states
      ]
  where
    service = defaultService "test" "example.test"