0
      0
  5   ˆl–ßi~” T¾R( 	Å�àS�ÿ�þB gà‚l,�¯çÂƒh¹ÁÀ¿             0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0        0
     ˆÃ�þB gàá ÞŸÂƒh¹ÁÀ¿      0
  5   	ˆl–ßi~” T¾R( 	ÅŒ¹pM)‹Fÿ�þB gàá%|?ÏÃƒh¹ÂÁÀ      	0
     ˆÄ�þB gàá ÞŸÃƒh¹ÂÁÀ      0
  5   ˆ¾�þB gàá%|?ÏÃƒh¹Âa–ßi~” T¾R( 	Å�;q—ÔÅ£Á      0
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þ]cYÇÌc!ZŒCùÅ„âÄ_�uÐb&=LyZËcõÀÃ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Gql::Queries::KnowledgeBase::Answer, type: :graphql do
  include_context 'basic Knowledge Base'

  let(:query) do
    <<~GQL
      query knowledgeBaseAnswer($answerId: ID!, $locale: String) {
        knowledgeBaseAnswer(answerId: $answerId, locale: $locale) {
          id
          visibility
          internalAt
          publishedAt
          archivedAt
          visibilitySchedules {
            visibility
            scheduledAt
          }
          tags
          attachments { id internalId name size type preferences }
          translation {
            id
            title
            content { id bodyWithUrls bodyForEditing }
            editedAt
            editedBy { id fullname }
            kbLocale { id systemLocale { locale } }
            navigation {
              index
              totalCount
              previousAnswer { id translation { id title } }
              nextAnswer { id translation { id title } }
            }
          }
          category {
            id
            breadcrumb { id translation { title } }
          }
        }
      }
    GQL
  end
  let(:answer)    { published_answer }
  let(:answer_id) { gql.id(answer) }
  let(:locale)    { nil }
  let(:variables) { { answerId: answer_id, locale: }.compact }

  before do
    answer
    setup if defined?(setup)
    gql.execute(query, variables:)
  end

  # The locale of the field's own argument, which is what keeps one locale's translation from
  #   standing in for another's in a client that caches by object identity.
  context 'with a translation asked for in a specific locale', authenticated_as: :admin do
    let(:admin) { create(:admin) }
    let(:query) do
      <<~GQL
        query knowledgeBaseAnswer($answerId: ID!, $locale: String, $fieldLocale: String) {
          knowledgeBaseAnswer(answerId: $answerId, locale: $locale) {
            id
            translation(locale: $fieldLocale) {
              id
              title
              kbLocale { id systemLocale { locale } }
            }
          }
        }
      GQL
    end

    let(:alternative_title)                { 'Titel in der anderen Sprache' }
    let(:translated_in_alternative_locale) { true }

    before do
      if translated_in_alternative_locale
        create(:knowledge_base_answer_translation,
               answer:    answer,
               kb_locale: alternative_locale,
               title:     alternative_title)
      end

      gql.execute(query, variables: { answerId: answer_id, locale: primary_locale.system_locale.locale, fieldLocale: field_locale })
    end

    context 'when it names another locale than the query' do
      let(:field_locale) { alternative_locale.system_locale.locale }

      it 'answers with the locale of the argument, not the query', :aggregate_failures do
        expect(gql.result.data['translation']).to include('title' => alternative_title)
        expect(gql.result.data.dig('translation', 'kbLocale', 'systemLocale'))
          .to include('locale' => alternative_locale.system_locale.locale)
      end
    end

    context 'when it names a locale the answer has no translation in' do
      let(:translated_in_alternative_locale) { false }
      let(:field_locale)                     { alternative_locale.system_locale.locale }

      it 'falls back to the primary locale, which the caller sees on the translation', :aggregate_failures do
        expect(gql.result.d