|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module MCP |
| 6 | + class Tool |
| 7 | + class SchemaTest < ActiveSupport::TestCase |
| 8 | + setup do |
| 9 | + Schema::VALIDATION_CACHE.clear |
| 10 | + end |
| 11 | + |
| 12 | + test "validates a schema once and reuses the result for identical schemas" do |
| 13 | + JSON::Validator.expects(:fully_validate).once.returns([]) |
| 14 | + |
| 15 | + schema = { properties: { validates_once: { type: "string" } } } |
| 16 | + InputSchema.new(schema) |
| 17 | + InputSchema.new(schema) |
| 18 | + end |
| 19 | + |
| 20 | + test "validates distinct schemas separately" do |
| 21 | + JSON::Validator.expects(:fully_validate).twice.returns([]) |
| 22 | + |
| 23 | + InputSchema.new(properties: { distinct_a: { type: "string" } }) |
| 24 | + InputSchema.new(properties: { distinct_b: { type: "string" } }) |
| 25 | + end |
| 26 | + |
| 27 | + test "a cache hit still yields a usable, validated schema" do |
| 28 | + schema = { properties: { cache_hit: { type: "string" } }, required: ["cache_hit"] } |
| 29 | + InputSchema.new(schema) |
| 30 | + cached = InputSchema.new(schema) |
| 31 | + |
| 32 | + assert_equal( |
| 33 | + { |
| 34 | + "$schema": "https://json-schema.org/draft/2020-12/schema", |
| 35 | + type: "object", |
| 36 | + properties: { cache_hit: { type: "string" } }, |
| 37 | + required: ["cache_hit"], |
| 38 | + }, |
| 39 | + cached.to_h, |
| 40 | + ) |
| 41 | + assert_nil(cached.validate_arguments(cache_hit: "value")) |
| 42 | + assert_raises(InputSchema::ValidationError) do |
| 43 | + cached.validate_arguments(cache_hit: 123) |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + test "an invalid schema raises every time and is not cached" do |
| 48 | + invalid = { properties: { not_cached: { type: "invalid_type" } } } |
| 49 | + |
| 50 | + assert_raises(ArgumentError) { InputSchema.new(invalid) } |
| 51 | + assert_raises(ArgumentError) { InputSchema.new(invalid) } |
| 52 | + end |
| 53 | + |
| 54 | + test "a schema at the normalization depth limit is cached without a nesting error" do |
| 55 | + # The deepest schema the initializer can still normalize via JSON.dump/parse. |
| 56 | + # The cache key must tolerate the same depth; the default JSON.generate |
| 57 | + # nesting limit (100) is stricter than normalization and would raise here. |
| 58 | + schema = { properties: { leaf: { type: "string" } } } |
| 59 | + loop do |
| 60 | + candidate = { properties: { child: schema } } |
| 61 | + JSON.parse(JSON.dump(candidate)) |
| 62 | + schema = candidate |
| 63 | + rescue JSON::NestingError |
| 64 | + break |
| 65 | + end |
| 66 | + |
| 67 | + JSON::Validator.stub(:fully_validate, []) do |
| 68 | + assert_nothing_raised do |
| 69 | + InputSchema.new(schema) |
| 70 | + InputSchema.new(schema) |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + test "ValidationCache evicts the oldest entry beyond its max size" do |
| 76 | + cache = Schema::ValidationCache.new(max_size: 2) |
| 77 | + cache.store("a") |
| 78 | + cache.store("b") |
| 79 | + cache.store("c") |
| 80 | + |
| 81 | + refute cache.validated?("a") |
| 82 | + assert cache.validated?("b") |
| 83 | + assert cache.validated?("c") |
| 84 | + end |
| 85 | + |
| 86 | + test "ValidationCache#clear empties the cache" do |
| 87 | + cache = Schema::ValidationCache.new |
| 88 | + cache.store("a") |
| 89 | + cache.clear |
| 90 | + |
| 91 | + refute cache.validated?("a") |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | +end |
0 commit comments