packages feed

baikai-mcp-0.1.0.0: test/JsonSchemaSpec.hs

module JsonSchemaSpec (tests) where

import Baikai.Mcp.JsonSchema
import Data.Aeson (Value (..), object, (.=))
import Data.Aeson qualified as Aeson
import Data.Text (Text)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, assertBool, assertFailure, testCase, (@?=))

tests :: TestTree
tests =
  testGroup
    "JSON Schema 2020-12 (bounded)"
    [ testCase "types, integers written as 1.0, and boolean schemas" $ do
        conforms (object ["type" .= ("integer" :: Text)]) (Number 1.0)
        violates (object ["type" .= ("integer" :: Text)]) (Number 1.5)
        conforms (object ["type" .= ["string", "null" :: Text]]) Null
        conforms (Bool True) (String "x")
        violates (Bool False) (String "x"),
      testCase "objects: required, properties, additionalProperties, patternProperties" $ do
        let s =
              object
                [ "type" .= ("object" :: Text),
                  "properties" .= object ["name" .= object ["type" .= ("string" :: Text)]],
                  "patternProperties" .= object ["^x-" .= object ["type" .= ("integer" :: Text)]],
                  "additionalProperties" .= False,
                  "required" .= ["name" :: Text]
                ]
        conforms s (object ["name" .= ("a" :: Text), "x-n" .= (1 :: Int)])
        violates s (object ["name" .= ("a" :: Text), "other" .= (1 :: Int)])
        violates s (object ["x-n" .= (1 :: Int)])
        violates s (object ["name" .= ("a" :: Text), "x-n" .= ("no" :: Text)]),
      testCase "local references into $defs and composition" $ do
        let s =
              object
                [ "$defs" .= object ["pos" .= object ["type" .= ("integer" :: Text), "minimum" .= (1 :: Int)]],
                  "allOf" .= [object ["$ref" .= ("#/$defs/pos" :: Text)], object ["maximum" .= (10 :: Int)]],
                  "not" .= object ["const" .= (5 :: Int)]
                ]
        conforms s (Number 3)
        violates s (Number 0)
        violates s (Number 11)
        violates s (Number 5),
      testCase "$ref applies alongside sibling keywords (2020-12, not draft-07)" $ do
        let s = object ["$defs" .= object ["s" .= object ["type" .= ("string" :: Text)]], "$ref" .= ("#/$defs/s" :: Text), "maxLength" .= (2 :: Int)]
        conforms s (String "ab")
        violates s (String "abc"),
      testCase "anyOf, oneOf and if/then/else" $ do
        let one = object ["oneOf" .= [object ["type" .= ("integer" :: Text)], object ["minimum" .= (2 :: Int)]]]
        conforms one (Number 1)
        violates one (Number 3)
        let cond = object ["if" .= object ["type" .= ("string" :: Text)], "then" .= object ["minLength" .= (2 :: Int)], "else" .= object ["type" .= ("null" :: Text)]]
        conforms cond (String "ab")
        violates cond (String "a")
        violates cond (Number 1)
        conforms (object ["anyOf" .= [object ["type" .= ("null" :: Text)], object ["type" .= ("array" :: Text)]]]) Null,
      testCase "arrays: prefixItems, items, contains, uniqueItems" $ do
        let s = object ["prefixItems" .= [object ["type" .= ("string" :: Text)]], "items" .= object ["type" .= ("integer" :: Text)], "contains" .= object ["const" .= (7 :: Int)], "maxContains" .= (1 :: Int)]
        conforms s (Aeson.toJSON [String "a", Number 1, Number 7])
        violates s (Aeson.toJSON [String "a", Number 1])
        violates s (Aeson.toJSON [String "a", Number 7, Number 7])
        violates s (Aeson.toJSON [Number 1, Number 7])
        violates (object ["uniqueItems" .= True]) (Aeson.toJSON [Number 1, Number 1.0]),
      testCase "numbers and strings" $ do
        conforms (object ["multipleOf" .= (0.01 :: Double)]) (Number 0.1)
        violates (object ["multipleOf" .= (0.03 :: Double)]) (Number 0.1)
        conforms (object ["maxLength" .= (2 :: Int)]) (String "世界")
        conforms (object ["pattern" .= ("^[a-z]+\\d{2}$" :: Text)]) (String "abc12")
        violates (object ["pattern" .= ("^[a-z]+\\d{2}$" :: Text)]) (String "abc123")
        conforms (object ["pattern" .= ("b.c" :: Text)]) (String "xxbac")
        violates (object ["pattern" .= ("b.c" :: Text)]) (String "b\nc")
        violates (object ["pattern" .= ("^a$" :: Text)]) (String "x\na"),
      testCase "annotations and extension keywords do not assert" $
        conforms (object ["format" .= ("email" :: Text), "x-mcp-header" .= ("H" :: Text), "description" .= ("d" :: Text)]) (String "not an email"),
      testCase "unsupported contracts are unverifiable, never satisfied" $ do
        unverifiable (object ["$ref" .= ("https://example.com/schema.json" :: Text)]) Null
        unverifiable (object ["$schema" .= ("http://json-schema.org/draft-07/schema#" :: Text)]) Null
        unverifiable (object ["unevaluatedProperties" .= False]) (object [])
        unverifiable (object ["pattern" .= ("^(?=a)" :: Text)]) (String "a")
        unverifiable (object ["pattern" .= ("(a)\\1" :: Text)]) (String "aa")
        unverifiable (object ["$ref" .= ("#anchor" :: Text)]) Null,
      testCase "a cyclic reference exhausts the budget instead of looping" $
        unverifiable (object ["$ref" .= ("#" :: Text)]) Null,
      testCase "exponential composition stops at the step budget" $ do
        let nest :: Int -> Value
            nest 0 = object ["type" .= ("string" :: Text)]
            nest n = object ["anyOf" .= [nest (n - 1), nest (n - 1), nest (n - 1)]]
        case validateJson defaultSchemaBudget {maxSteps = 5000} (nest 12) (Number 1) of
          Unverifiable _ -> pure ()
          other -> assertFailure (show other),
      testCase "violations name the instance location" $
        case validateJson defaultSchemaBudget (object ["properties" .= object ["a/b" .= object ["items" .= object ["type" .= ("string" :: Text)]]]]) (object ["a/b" .= [Number 1]]) of
          Violates [v] -> v.instancePath @?= "/a~1b/0"
          other -> assertFailure (show other)
    ]

conforms :: Value -> Value -> Assertion
conforms s v = validateJson defaultSchemaBudget s v @?= Conforms

violates :: Value -> Value -> Assertion
violates s v = case validateJson defaultSchemaBudget s v of
  Violates _ -> pure ()
  other -> assertFailure ("expected a violation, got " <> show other)

unverifiable :: Value -> Value -> Assertion
unverifiable s v = case validateJson defaultSchemaBudget s v of
  Unverifiable _ -> pure ()
  other -> assertBool ("expected unverifiable, got " <> show other) False