|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +module ActionMCP |
| 6 | + class McpAppsTest < ActiveSupport::TestCase |
| 7 | + def setup |
| 8 | + @original_tool_items = ActionMCP::ToolsRegistry.items.dup |
| 9 | + end |
| 10 | + |
| 11 | + def teardown |
| 12 | + ActionMCP::ToolsRegistry.instance_variable_set(:@items, @original_tool_items) |
| 13 | + end |
| 14 | + |
| 15 | + def build_tool(&block) |
| 16 | + klass = Class.new(ActionMCP::Tool) |
| 17 | + klass.define_singleton_method(:name) { "RendersUiTempTool#{SecureRandom.hex(4)}" } |
| 18 | + klass.abstract! |
| 19 | + klass.tool_name "renders_ui_temp_#{SecureRandom.hex(4)}" |
| 20 | + klass.description "temp" |
| 21 | + klass.class_eval(&block) if block |
| 22 | + klass |
| 23 | + end |
| 24 | + |
| 25 | + test "renders_ui serializes resourceUri under _meta.ui" do |
| 26 | + klass = build_tool { renders_ui "ui://widgets/panel", visibility: %i[model app] } |
| 27 | + |
| 28 | + assert_equal( |
| 29 | + { ui: { resourceUri: "ui://widgets/panel", visibility: [ "model", "app" ] } }, |
| 30 | + klass.to_h[:_meta] |
| 31 | + ) |
| 32 | + end |
| 33 | + |
| 34 | + test "renders_ui omits visibility key when not provided" do |
| 35 | + klass = build_tool { renders_ui "ui://widgets/panel" } |
| 36 | + |
| 37 | + assert_equal({ ui: { resourceUri: "ui://widgets/panel" } }, klass.to_h[:_meta]) |
| 38 | + end |
| 39 | + |
| 40 | + test "renders_ui rejects non-String URI" do |
| 41 | + assert_raises(ArgumentError) { build_tool { renders_ui :"ui://widgets/panel" } } |
| 42 | + assert_raises(ArgumentError) { build_tool { renders_ui nil } } |
| 43 | + end |
| 44 | + |
| 45 | + test "renders_ui rejects non-ui:// URI" do |
| 46 | + assert_raises(ArgumentError) { build_tool { renders_ui "http://widgets/panel" } } |
| 47 | + assert_raises(ArgumentError) { build_tool { renders_ui "" } } |
| 48 | + end |
| 49 | + |
| 50 | + test "renders_ui rejects unknown visibility values" do |
| 51 | + assert_raises(ArgumentError) do |
| 52 | + build_tool { renders_ui "ui://widgets/panel", visibility: %i[model agent] } |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + test "renders_ui composes with meta on orthogonal keys" do |
| 57 | + klass = build_tool do |
| 58 | + meta foo: "bar" |
| 59 | + renders_ui "ui://widgets/panel" |
| 60 | + end |
| 61 | + |
| 62 | + meta = klass.to_h[:_meta] |
| 63 | + assert_equal "bar", meta[:foo] |
| 64 | + assert_equal "ui://widgets/panel", meta[:ui][:resourceUri] |
| 65 | + end |
| 66 | + |
| 67 | + class StubSession |
| 68 | + attr_reader :client_capabilities |
| 69 | + |
| 70 | + def initialize(capabilities) |
| 71 | + @client_capabilities = capabilities |
| 72 | + end |
| 73 | + end |
| 74 | + |
| 75 | + class CapabilityProbe < ActionMCP::Capability |
| 76 | + end |
| 77 | + |
| 78 | + test "client_supports_ui? is true when the extension key is present" do |
| 79 | + caps = { "extensions" => { "io.modelcontextprotocol/ui" => {} } } |
| 80 | + instance = CapabilityProbe.new.with_context(session: StubSession.new(caps)) |
| 81 | + |
| 82 | + assert instance.client_supports_ui? |
| 83 | + end |
| 84 | + |
| 85 | + test "client_supports_ui? is false when the extension key is absent" do |
| 86 | + instance = CapabilityProbe.new.with_context(session: StubSession.new("tools" => {})) |
| 87 | + |
| 88 | + refute instance.client_supports_ui? |
| 89 | + end |
| 90 | + |
| 91 | + test "client_supports_ui? is false when there is no session" do |
| 92 | + refute CapabilityProbe.new.client_supports_ui? |
| 93 | + end |
| 94 | + |
| 95 | + test "client_supports_ui? is false when client_capabilities is nil" do |
| 96 | + instance = CapabilityProbe.new.with_context(session: StubSession.new(nil)) |
| 97 | + |
| 98 | + refute instance.client_supports_ui? |
| 99 | + end |
| 100 | + |
| 101 | + test "weather tool declares renders_ui pointing at the dashboard" do |
| 102 | + assert_equal "ui://weather/dashboard", WeatherTool.to_h.dig(:_meta, :ui, :resourceUri) |
| 103 | + end |
| 104 | + |
| 105 | + test "weather dashboard resolve returns HTML content with _meta" do |
| 106 | + instance = WeatherDashboardTemplate.new({}) |
| 107 | + response = instance.call |
| 108 | + |
| 109 | + refute response.error? |
| 110 | + content = response.contents.first |
| 111 | + assert_equal ActionMCP::MIME_TYPE_APP_HTML, content.mime_type |
| 112 | + refute_empty content.text |
| 113 | + assert content.meta[:ui][:prefersBorder] |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments