0
      ©ˆã�þZ eYÇÌc![l¯ùÍ„í<ïÌé¾É ?÷     ©# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'services/service/ai/tool/loads_context_object_examples'

RSpec.describe Service::AI::Tool::GetTicketArticles, :aggregate_failures do
  let(:group)    { create(:group) }
  let(:agent)    { create(:agent, firstname: 'Nina', lastname: 'Agent', groups: [group]) }
  let(:customer) { create(:customer, firstname: 'Anna', lastname: 'Example') }
  let(:ticket)   { create(:ticket, group:, customer:, title: 'Outlook sync fails after update') }

  # 30 messages the agent sees â€” the customer's first one, then the agent and the customer in turn, an internal note as
  #   the fifth â€” and a system notification between them, which no position counts.
  let!(:articles) do
    (1..30).map do |position|
      if position == 4
        create(:ticket_article, ticket:, sender_name: 'System', type_name: 'email', internal: true, body: 'Notification.',
                                preferences: { 'perform_origin' => 'trigger', 'notification' => true })
      end

      trait = if position == 5 then :internal_note
              elsif position.odd? then :inbound_email
              else :outbound_email
              end
      create(:ticket_article, trait, ticket:, body: "Message #{position}: #{'word ' * (position == 12 ? 2_000 : 3)}end", content_type: 'text/plain',
                                     created_by_id: (position.odd? && position != 5 ? customer : agent).id)
    end
  end

  def perform(arguments = {}, context_object: Gql::ZammadSchema.id_from_object(ticket))
    tool_call = AI::Provider::Completion::ToolCall.new(id: 'call_1', name: 'get_ticket_articles', arguments:)

    Service::AI::Tool::Runtime::PerformCall.execute(surface: :harness, user: agent, tool_call:, audience: AI::Harness::Audience::HUMAN_AGENTS, context_object:)
  end

  def positions(result)
    result.content[:messages].pluck(:position)
  end

  def cursor_at(offset)
    Service::AI::Tool::Runtime::Cursor.encode(offset:, scope: { ticket: ticket.id })
  end

  it 'is offered to an agent in the harness' do
    expect(Service::AI::Tool::Runtime::Router.offered_tools(surface: :harness, user: agent, audience: AI::Harness::Audience::HUMAN_AGENTS)).to include(described_class)
  end

  it 'says over MCP that files are counted in the index and listed with their IDs for read_attachment, as the harness prompt says it in the chat' do
    expect(described_class.description_for(:mcp)).to include('The index says how many files a message has; with positions, its files are listed with their IDs, by which read_attachment reads them')
    expect(described_class.description_for(:harness)).not_to include('attachments')
  end

  describe 'the index' do
    it 'lists the first page, each message with the start of its text, without the system notification' do
      result = perform

      expect(result).to have_attributes(status: 'ok')
      expect(result.content).to include(message_count: 30, has_more: true, next_cursor: cursor_at(20))
      expect(positions(result)).to eq((1..20).to_a)
      expect(result.content[:messages].first).to include(id: articles.first.id, position: 1, sender: 'Customer', from: 'Anna Example', internal: false, text: 'Message 1: word word word end')
      expect(result.content[:messages].first).not_to have_key(:subject)
      expect(result.content[:messages].fifth).to include(position: 5, internal: true)
      expect(result.text).not_to include('Notification.')
    end

    it 'says how many files each listed message has, and lists them with their IDs where a message is read with its text' do
      files = Array.new(2) { |index| create(:store, object: 'Ticket::Article', o_id: articles.third.id, filename: "sync-#{index}.log") }

      index = perform.content[:messages].find { |message| message[:position] == 3 }
      full  = perform({ 'positions' => [3] })

      expect(index).to include(attachment_count: 2).and not_include(:attachments)
      expect(perform.text).to include('message="3" type="email" from