
  .   £ˆò�þ]#Ì¬ã�f1�­¸QùÉ„d/:ßÎ_�uÐb&=LyZËcõÁÌ ?÷     £# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

# Covers Service::ContentTranslation::Base and its AI backend through their first subclass.
RSpec.describe Service::ContentTranslation::TicketArticle, performs_jobs: true do
  let(:target_locale) { 'de-de' }
  let(:content_type)  { 'text/html' }
  let(:body)          { '<p>Hello <strong>world</strong>.</p>' }
  let(:article)       { create(:ticket_article, body:, content_type:) }
  let(:llm_response)  { "{s1}\nHallo **Welt**." }
  let(:translated)    { '<p>Hallo <strong>Welt</strong>.</p>' }

  # Every prompt pair the provider was asked with â€” the size doubles as the call count.
  let(:provider_calls) { [] }

  # Most examples are about what is translated rather than about deferring, so they ask for an
  # answer in place; the deferral itself has its own describe block.
  def translate(**args)
    described_class.execute(object: article, target_locale:, background: false, **args)
  end

  before do
    setup_ai_provider('zammad_ai')
    setup_content_translation

    allow_any_instance_of(AI::Provider::ZammadAI).to receive(:ask) do |_provider, **prompts|
      provider_calls << prompts
      llm_response
    end
  end

  it 'returns the translation of the article' do
    result = translate

    expect(result).to have_attributes(
      content:    translated,
      backend:    'ai',
      translated: true,
      fresh:      true,
    )
  end

  it 'returns the analytics run for the feedback widget' do
    expect(translate.analytics_run).to be_a(AI::Analytics::Run)
  end

  # Not to its ticket: that would freeze which ticket authorizes the run, and a merge moves the
  #   article to another one.
  it 'relates the analytics run to the article' do
    expect(translate.analytics_run.related_object).to eq(article)
  end

  it 'serves a second request from the stored translation' do
    translate

    expect(translate)
      .to have_attributes(content: translated, fresh: false)
  end

  context 'when the AI provider is not configured' do
    before { unset_ai_provider }

    it 'still serves a translation that is already stored' do
      setup_ai_provider('zammad_ai')
      translate
      unset_ai_provider

      expect(translate)
        .to have_attributes(content: translated, fresh: false)
    end

    it 'raises an error' do
      expect { translate }
        .to raise_error(Service::CheckFeatureEnabled::FeatureDisabledError, 'AI provider is not configured.')
    end
  end

  describe 'honouring the feature toggles' do
    # A translation is stored first: a switched-off feature must not serve it either.
    before { translate }

    context 'when no translation service is configured' do
      before { Setting.set('content_translation_service', false) }

      it 'refuses although a translation is stored' do
        expect { translate }
          .to raise_error(Service::CheckFeatureEnabled::FeatureDisabledError, 'No translation service is configured.')
      end

      it 'asks no provider' do
        expect { translate }
          .to raise_error(Service::CheckFeatureEnabled::FeatureDisabledError)
          .and not_change(provider_calls, :size)
      end

      it 'consults no store' do
        allow(Service::AI::Feature::Translate).to receive(:execute)

        suppress(Service::CheckFeatureEnabled::FeatureDisabledError) { translate }

        expect(Service::AI::Feature::Translate).not_to have_received(:execute)
      end
    end

    context 'when article translation is switched off' do
      before { Setting.set('content_translation_ticket_article', false) }

      it 'refuses although a translation is stored' do
        expect { translate }
          .to raise_error(Service::CheckFeatureEnabled::FeatureDisabledError, 'Ticket article translation is not enabled.')
      end

      it 'asks no provider' do
        expect { translate }
          .to raise_error(Service::CheckFeatureEnabled::FeatureDisabledError)
          .and not_change(provider_calls, 