11:0
     ˆÀ�þBQ6³Ž˜ÆB¶Ù^óÄƒuÐ»Ã¿ÂÁ %    # 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::SendSharedDraft, :aggregate_failures do
  let(:group)     { create(:group, email_address: create(:email_address), signature: create(:signature, body: '<p>Configured signature</p>')) }
  let(:agent)     { create(:agent, groups: [group]) }
  let(:colleague) { create(:agent, groups: [group]) }
  let(:token)     { create(:token, user: agent, preferences: { permission: %w[ticket.agent] }) }
  let(:ticket)    { create(:ticket, group:) }
  let(:body)      { '<p>Hello,</p><p>the fix ships tomorrow.</p>' }
  let(:article)   { { 'articleType' => 'email', 'type' => 'email', 'internal' => false, 'to' => 'customer@example.com', 'subject' => 'Your fix', 'body' => body } }
  let(:note)      { { 'type' => 'note', 'internal' => 'true', 'body' => '<p>Checked the logs.</p>' } }

  # Both interfaces keep the whole ticket form in a draft, changed or not, and form state besides.
  let(:form) { { 'id' => Gql::ZammadSchema.id_from_object(ticket), 'group_id' => group.id.to_s, 'state_id' => ticket.state_id, 'priority_id' => ticket.priority_id.to_s, 'owner_id' => '', 'pending_time' => nil, 'isDefaultFollowUpStateSet' => false } }

  def context(state: nil, response: nil)
    instance_double(MCP::ServerContext, modern?: true, client_capabilities: { elicitation: { form: {} } }, request_state: state,
                    input_responses: response && { confirm: response }, input_response: response, cancelled?: false)
  end

  def call(context = self.context, arguments: { ticket_number: ticket.number })
    Service::MCPServer::CallTool.execute(tool: described_class, arguments:, user: agent, token:, server_context: context).to_h
  end

  def accepted(state)
    context(state:, response: { action: 'accept', content: {} })
  end

  def send_confirmed(arguments = { ticket_number: ticket.number })
    call(accepted(call(arguments:).fetch(:requestState)), arguments:)
  end

  def save_draft(new_article = article, ticket_attributes: form)
    UserInfo.with_user_id(colleague.id) do
      ticket.create_shared_draft!(new_article:, ticket_attributes:)
    end
  end

  def text_of(result)
    result[:content].first[:text]
  end

  it "is offered to an agent's token over MCP only, as a destructive write of the open world" do
    expect(Service::AI::Tool::Runtime::Router.offered_tools(user: agent, surface: :mcp, token:)).to include(described_class)
    expect(Service::AI::Tool::Runtime::Router.offered_tools(surface: :harness, user: agent, audience: AI::Harness::Audience::HUMAN_AGENTS)).not_to include(described_class)

    annotations = Service::MCPServer::ToolDefinition.execute(tool: described_class, user: agent, token:).annotations
    expect(annotations.to_h).to include(destructiveHint: true, openWorldHint: true)
  end

  it 'sends a draft the interface saved once the user confirmed it, with the signature, and deletes the draft' do
    save_draft

    pending = call
    expect(pending.dig(:inputRequests, 'confirm', :params, :message)).to include('customer@example.com', 'subject: ', 'the fix ships tomorrow.', 'Configured signature')
    expect(ticket.articles).to be_empty

    result = call(accepted(pending.fetch(:requestState)))

    expect(result[:isError]).to be(false)
    expect(result[:structuredContent]).to include('type' => 'email', 'to' => 'customer@example.com', 'outcome' => 'queued')
    expect(ticket.articles.sole).to have_attributes(subject: include('Your fix'), body: include('the fix ships tomorrow.', 'Configured signature'), created_by: agent)
    expect(ticket.reload.shared_draft).to be_nil
  end

  it 'puts the signature where the editor left its place, before the quoted message' do
    save_draft(article.merge('body' => '<p>Hello,</p><div data-signature-placeholder="true"></div><blockquote type="cite">Earlier message</blockquote>'))

    send_confirmed

    sent = ticket.articles.sole.body
    expect