120000
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þ[ÁYÇÌc![l¯ùÂ„q¿Ç_�uÐb&=LyZËcõÆÅ ?÷     # 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::GetTicketHistory, :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)   { travel_to(3.days.ago) { create(:ticket, group:, customer:, title: 'Outlook sync fails after update') } }

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

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

  def add_history(time:, author: agent, **data)
    History.add(history_object: 'Ticket', o_id: ticket.id, created_by_id: author.id, created_at: time, **data)
  end

  def descriptions(result)
    result.content[:changes].flat_map { |change| change[:entries].pluck(:description) }
  end

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

  describe 'the entries' do
    before do
      ticket
      add_history(time: 2.days.ago, history_type: 'updated', history_attribute: 'group', value_from: 'Users', value_to: '2nd Level', id_from: 1, id_to: 2)
      add_history(time: 1.day.ago, history_type: 'updated', history_attribute: 'priority', value_from: '2 normal', value_to: '3 high', id_from: 2, id_to: 3)
    end

    it 'lists them newest first, with who, when, the field and its old and new value in words' do
      result = perform
      latest = result.content[:changes].first

      expect(latest).to include(time: Service::AI::Tool::Output.time(1.day.ago), issuer: { type: 'User', id: agent.id, name: 'Nina Agent' })
      expect(latest[:entries]).to eq([{ action: 'updated', description: 'Priority: 2 normal â†’ 3 high', field: 'Priority', field_key: 'priority_id', from: '2 normal', to: '3 high' }])
      expect(descriptions(result).first(2)).to eq(['Priority: 2 normal â†’ 3 high', 'Group: Users â†’ 2nd Level'])
      expect(descriptions(result)).to include('created the ticket')
    end

    it 'names an option by its label, also one removed since' do
      attribute = ObjectManager::Attribute.get(object: 'Ticket', name: 'type')
      attribute.data_option[:options] = { 'incident' => 'Incident' }
      attribute.data_option[:historical_options] = { 'problem' => 'Problem', 'incident' => 'Incident' }
      attribute.save!(validate: false)
      add_history(time: 1.hour.ago, history_type: 'updated', history_attribute: 'type', value_from: 'problem', value_to: 'incident')

      expect(perform.content[:changes].first[:entries].first).to include(field: attribute.display, from: 'Problem', to: 'Incident')
    end

    it 'decodes multi-select history before resolving current and removed labels' do
      attribute = ObjectManager::Attribute.get(object: 'Ticket', name: 'type')
      attribute.data_type = 'multiselect'
      attribute.data_option[:options] = { 'incident' => 'Incident' }
      attribute.data_option[:historical_options] = { 'problem' => 'Problem' }
      attribute.save!(validate: false)
      values = ticket.history_attribute_changes('type', [%w[problem], %w[problem incident]])
      add_history(time: 1.hour.ago, history_type: 'updated', history_attribute: 'type', **values)

      expect(perform.content[:changes].first[:entries].first).to include(field_key: 'type', from: 'Problem', to: 'Problem, Incident')
      expect(perform.text).to include('Problem â†’ Problem, Incident