0x0
  F   eˆl–Ãa¾”ªi?u8 ¸Û71hß�þYhM,ã�f1�­�?üÿè„,´÷í_�uÐb&=LyZËcõÁë 4$    e# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

# What is translated, and who may, is covered by
# spec/services/service/content_translation/ticket_article/translate_many_spec.rb.
RSpec.describe Gql::Mutations::Ticket::Article::TranslateMany, :aggregate_failures, performs_jobs: true, type: :graphql do
  let(:ticket)        { create(:ticket) }
  let!(:articles)     { create_list(:ticket_article, 2, ticket:, body: '<p>Hello world.</p>', content_type: 'text/html') }
  let(:agent)         { create(:agent, groups: [ticket.group]) }
  let(:target_locale) { 'de-de' }
  let(:locale)        { Locale.find_by(locale: target_locale) }

  let(:query) do
    <<~MUTATION
      mutation ticketArticleTranslateMany(
        $ticketId: ID!, $targetLocale: String!, $pageSize: Int, $firstArticlesCount: Int,
        $loadFirstArticles: Boolean, $beforeCursor: String, $afterCursor: String,
        $generateMissing: Boolean = false, $includeContent: Boolean! = true
      ) {
        ticketArticleTranslateMany(
          ticketId: $ticketId, targetLocale: $targetLocale, pageSize: $pageSize,
          firstArticlesCount: $firstArticlesCount, loadFirstArticles: $loadFirstArticles,
          beforeCursor: $beforeCursor, afterCursor: $afterCursor, generateMissing: $generateMissing
        ) {
          pendingArticleIds
          pendingBackend
          results {
            article {
              id
              translationAvailable(targetLocale: $targetLocale)
              translation(targetLocale: $targetLocale) @include(if: $includeContent) { content backend translated }
            }
            translated
            skipReason
            analytics @include(if: $includeContent) {
              run {
                id
              }
              usage {
                userHasProvidedFeedback
              }
            }
          }
        }
      }
    MUTATION
  end

  let(:variables) do
    {
      ticketId:        gql.id(ticket),
      pageSize:        2,
      targetLocale:    target_locale,
      generateMissing: true,
      includeContent:  true,
    }
  end

  def store_translation(article, translation, analytics_run: nil)
    Service::ContentTranslation::StoredTranslation.save(
      object:        article,
      locale:,
      content:       article.body,
      html:          true,
      backend:       'ai',
      translation:,
      analytics_run:,
    )
  end

  context 'when logged in as an agent', authenticated_as: :agent do
    before do
      setup_ai_provider
      setup_content_translation
      Setting.set('content_translation_ticket_article_auto', true)
    end

    context 'with stored, skipped and pending translations' do
      let(:variables) { super().merge(pageSize: 3) }
      let!(:skipped_article) { create(:ticket_article, ticket:, detected_language: 'de', content_type: 'text/html') }

      before do
        store_translation(articles.first, '<p>Hallo Welt.</p>')
        store_translation(skipped_article, '<p>Manuell</p>')
      end

      it 'returns completed outcomes and leaves pending articles to the subscription' do
        allow(Service::ContentTranslation::TicketArticle).to receive(:stored_translations).and_call_original
        gql.execute(query, variables:)

        expect(gql.result.data[:pendingArticleIds]).to eq([gql.id(articles.last)])
        expect(gql.result.data[:pendingBackend]).to eq('ai')
        expect(Service::ContentTranslation::TicketArticle).to have_received(:stored_translations).with([skipped_article], target_locale).once
        expect(gql.result.data[:results]).to contain_exactly(
          {
            'article'    => { 'id' => gql.id(articles.first), 'translationAvailable' => true, 'translation' => { 'content' => '<p>Hallo Welt.</p>', 'backend' => 'ai', 'translated' => true } },
            'translated' => true,
            'skipReason' => nil,
            'analytics'  => { 'run' => nil, 'usage' => nil },
          },
          {
    