     161        0     1288       48        8        0        0        2        0       52       50        0        0        0        0        0        0
     ?ˆÙ�þ@³Žàˆ�N ÏØ0ÝÀÚ  6   Aˆl–Ãa¾”ªi?u8 ¸Û71hß�þBür¬ã�f1�­´R�ùÙƒq·ÃÞÎÁÛ ¿    A# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'json_schemer'

# The rules every tool keeps, checked for each tool instead of a list kept by hand: a new tool needs no change here
#   unless it breaks one of them.
RSpec.describe 'Declarations of the AI tools' do # rubocop:disable RSpec/DescribeClass
  it 'knows every tool in the directory of the tool layer' do
    # Taken before the files are loaded below, so that the router has to find them on its own.
    known = Service::AI::Tool::Runtime::Router.tools

    tools = Rails.root.glob('app/services/service/ai/tool/**/*.rb')
      .map { |path| path.relative_path_from(Rails.root.join('app/services')).sub_ext('').to_s.camelize.constantize }
      .select { |klass| klass.is_a?(Class) && klass < Service::AI::Tool }

    expect(tools - known).to be_empty
  end

  it 'gives every tool a name of its own' do
    names = Service::AI::Tool::Runtime::Router.tools.map(&:tool_name)

    expect(names).to eq(names.uniq)
  end

  describe 'the tools always loaded' do
    def overfull_audiences(tools)
      AI::Harness::Audience::ALL.each_value.select do |audience|
        tools.count { |tool| tool.always_loaded? && tool.audience?(audience) } > Service::AI::Tool::ALWAYS_LOADED_PER_AUDIENCE
      end
    end

    it 'are at most five per audience' do
      expect(overfull_audiences(Service::AI::Tool::Runtime::Router.tools)).to be_empty
    end

    it 'counts a sixth of an audience as too many' do
      tools = Array.new(6) do
        Class.new(Service::AI::Tool) do
          audiences :human_agents
          always_loaded
        end
      end

      expect(overfull_audiences(tools).map(&:name)).to eq([:human_agents])
    end
  end

  describe 'a tool that needs the run of the harness', :aggregate_failures do
    def tool_offered_in(*surfaces)
      Class.new(Service::AI::Tool) do
        description 'A tool for the spec.'
        effect :read
        permission 'ticket.agent'
        offered_in(*surfaces)
        audiences :human_agents
        needs_run
      end
    end

    it 'is declared where it is offered in the harness only' do
      expect(tool_offered_in(:harness).declaration_errors).to be_empty
    end

    it 'is refused where it is offered anywhere else too' do
      expect(tool_offered_in(:harness, :mcp).declaration_errors).to eq(['needs the run, so it is offered in the harness only'])
      expect(tool_offered_in(:mcp).declaration_errors).to eq(['needs the run, so it is offered in the harness only'])
    end

    it 'is no need for a tool that does not say so' do
      expect(Class.new(tool_offered_in(:harness, :mcp)) { self.tool_needs_run = false }.declaration_errors).to be_empty
    end
  end

  describe 'a tool that works on the starting object of the conversation', :aggregate_failures do
    def tool_offered_in(*surfaces)
      Class.new(Service::AI::Tool) do
        description 'A tool for the spec.'
        effect :read
        permission 'ticket.agent'
        offered_in(*surfaces)
        audiences :human_agents
        loads :ticket, type: Ticket
      end
    end

    it 'is declared where it is offered in the harness only' do
      expect(tool_offered_in(:harness).declaration_errors).to be_empty
    end

    it 'is refused where it is offered over MCP, which has no conversation' do
      expect(tool_offered_in(:harness, :mcp).declaration_errors).to eq(['loads the starting object, so it is offered in the harness only'])
    end

    it 'is declared over MCP too where the caller may name the object' do
      named = Class.new(tool_offered_in(:harness, :mcp)) { loads :ticket, type: Ticket, named: true }

      expect(named.declaration_errors).to be_empty
    end
  end

  # Offering a tool over MCP makes it part of a public interface: it is added here 