|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +RSpec.describe RubyLLM::Schema, "factory method (.create) approach" do |
| 6 | + include SchemaBuilders |
| 7 | + |
| 8 | + describe "schema attributes" do |
| 9 | + let(:base_schema) { build_factory_schema { string :title } } |
| 10 | + |
| 11 | + it "derives schema names from constants, instances, and defaults" do |
| 12 | + stub_const("NamedFactorySchema", build_factory_schema { string :title }) |
| 13 | + expect(NamedFactorySchema.new.to_json_schema[:name]).to eq("NamedFactorySchema") |
| 14 | + |
| 15 | + expect(base_schema.new.to_json_schema[:name]).to eq("Schema") |
| 16 | + expect(base_schema.new("CustomName").to_json_schema[:name]).to eq("CustomName") |
| 17 | + end |
| 18 | + |
| 19 | + it "honours description precedence" do |
| 20 | + schema_with_description = build_factory_schema do |
| 21 | + description "Factory description" |
| 22 | + string :title |
| 23 | + end |
| 24 | + |
| 25 | + anonymous_output = base_schema.new.to_json_schema |
| 26 | + expect(anonymous_output[:description]).to be_nil |
| 27 | + |
| 28 | + class_level_output = schema_with_description.new.to_json_schema |
| 29 | + expect(class_level_output[:description]).to eq("Factory description") |
| 30 | + |
| 31 | + instance_override = schema_with_description.new("NamedSchema", description: "Instance description").to_json_schema |
| 32 | + expect(instance_override[:description]).to eq("Instance description") |
| 33 | + end |
| 34 | + |
| 35 | + it "controls additional properties and strictness" do |
| 36 | + default_output = base_schema.new.to_json_schema |
| 37 | + expect(default_output[:schema][:additionalProperties]).to eq(false) |
| 38 | + expect(default_output[:schema][:strict]).to eq(true) |
| 39 | + |
| 40 | + configured_schema = build_factory_schema do |
| 41 | + additional_properties true |
| 42 | + strict false |
| 43 | + string :title |
| 44 | + end |
| 45 | + |
| 46 | + configured_output = configured_schema.new.to_json_schema |
| 47 | + expect(configured_output[:schema][:additionalProperties]).to eq(true) |
| 48 | + expect(configured_output[:schema][:strict]).to eq(false) |
| 49 | + end |
| 50 | + |
| 51 | + it "renders structured JSON schema" do |
| 52 | + configured_schema = build_factory_schema do |
| 53 | + description "Factory test description" |
| 54 | + additional_properties false |
| 55 | + string :title |
| 56 | + integer :count, required: false |
| 57 | + end |
| 58 | + |
| 59 | + output = configured_schema.new("FactoryConfiguredSchema").to_json_schema |
| 60 | + |
| 61 | + expect(output).to include( |
| 62 | + name: "FactoryConfiguredSchema", |
| 63 | + description: "Factory test description", |
| 64 | + schema: hash_including( |
| 65 | + type: "object", |
| 66 | + properties: { |
| 67 | + title: {type: "string"}, |
| 68 | + count: {type: "integer"} |
| 69 | + }, |
| 70 | + required: [:title], |
| 71 | + additionalProperties: false, |
| 72 | + strict: true |
| 73 | + ) |
| 74 | + ) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe "comprehensive scenario" do |
| 79 | + let(:schema_class) do |
| 80 | + build_factory_schema do |
| 81 | + description "Comprehensive factory schema" |
| 82 | + additional_properties true |
| 83 | + strict true |
| 84 | + |
| 85 | + string :name, description: "Name field" |
| 86 | + integer :count |
| 87 | + boolean :active, required: false |
| 88 | + |
| 89 | + object :config do |
| 90 | + string :setting |
| 91 | + end |
| 92 | + |
| 93 | + array :tags, of: :string |
| 94 | + |
| 95 | + any_of :status do |
| 96 | + string |
| 97 | + null |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + it "supports full-feature schemas" do |
| 103 | + json_output = schema_class.new("FactorySchema").to_json_schema |
| 104 | + |
| 105 | + expect(json_output[:name]).to eq("FactorySchema") |
| 106 | + expect(json_output[:schema][:additionalProperties]).to eq(true) |
| 107 | + expect(json_output[:schema][:strict]).to eq(true) |
| 108 | + expect(json_output[:schema][:properties].keys).to contain_exactly( |
| 109 | + :name, :count, :active, :config, :tags, :status |
| 110 | + ) |
| 111 | + expect(json_output[:schema][:required]).to contain_exactly(:name, :count, :config, :tags, :status) |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments