none
     ˆÅÄÃ‚p ÂÁÀ b    ‹      Õ–]oÚ0†ïû+<_ìÚªmðÔ¤¡Q@-Ó4MSebC¬&vd(ýõspÚfSÑ¨Ô"vuŽÉ‹Ïûø;ü�º“ãú2¹ ñ·Ïƒ~áÀ÷V šDþC»Þ zCŒpbm~°Z­ê«V]é9L®!±YÚcµˆm�Y†ÉQXüFŽP˜pÊ\D¡6å¤/¿Gj†@-¬Œ·!×*›h'¬žÎ@+eÁ¬0¾17EnyÏ”^—!h´@*Æ�ív#_ÃU…²l8Ul]¸	Þ¹²+°�¤Ó��"Õ$´	ZÒTÌe[•cŠlŽŒŽ;D¬¤�iJå]}.fÑÔvðÏ~wôËéœ«âß$¤ÈÙ›uð§ngx1êD˜iÆC /k®œæ“5eŠ‰™àl«øÆ‹oÄÃö#¯‰¸‰µÈ­PòY
±Š«ÔäÔ±;‚DWU^Âþ54þc0Æ—×½á$ê_û!aEÏîÞn1SÍ¥E‘Ð<¶î[	é+~”S“_<6Q‰ Å<±˜ TC/*_Á=S)ãºJ¾�ùÑ¯�U—Ùj6š'µ ¨g(8=oœ ôŽþòNª•¬¸eñ"€’©�w h6QpvÞ:Ý
à§¯{È“µ¹õË
“Jcw”ãƒBÅ±X’lòC™“Ý7E®V\»Máã[øßó¦Ðî<Z7&Oé¡LÃ+0Œ¥Ö!lÂ¡Øß}™Å´¼%ÂSþ±¾tw&>¾Àž×QÑ»}P’L*�÷_S;>f |æ…°y^†à»¿GrFYO    7   ˆl–Ým_JJi?u8 ^¸Ë·â˜´o‘äÇò 8à
4Ë’¼)Ÿ¿ƒh¹ÄÂÁ      0
  1   ˆl–ß=¿JÕ5*i@»pO\ÊbÑ¿�þ@³‚hr¼'Ÿ# wùÀ0ÅÃÂ  E   ˆl–Ü4ý('ÜZÔâ€vàZ¸ÓJbÑ¿�þB F³�‘¹q±²EüÿÁƒh wÆ_�uÐb&=LyZËcõÅÄ  $   	H403239_•I|¥‰ÓMjqØ‚¦ ë<óo¬ÇÆ  A   ˆl–äY>”JC]Š
‚À5q°)‹Fÿ�þB7n9el/J!�à?Å‚hBÊ_‹uÐb&=LtAêÉÈ     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Gql::Subscriptions::KnowledgeBase::AnswerUpdates, type: :graphql do
  include_context 'basic Knowledge Base'

  let(:answer)       { published_answer }
  let(:variables)    { { answerId: gql.id(answer) } }
  let(:mock_channel) { build_mock_channel }
  let(:subscription) do
    <<~QUERY
      subscription knowledgeBaseAnswerUpdates($answerId: ID!, $locale: String, $initial: Boolean = false) {
        knowledgeBaseAnswerUpdates(answerId: $answerId, locale: $locale, initial: $initial) {
          answer {
            translation(locale: $locale) { title }
          }
        }
      }
    QUERY
  end

  before do
    gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  end

  shared_examples 'subscribes and receives updates' do
    it 'subscribes' do
      expect(gql.result.data).to eq({ 'answer' => nil })
    end

    context 'with initial data' do
      let(:variables) { { answerId: gql.id(answer), initial: true } }

      it 'subscribes with initial data' do
        expect(gql.result.data.dig(:answer, :translation, :title)).to eq(answer.translation_primary.title)
      end
    end

    it 'receives updates when the answer translation is edited' do
      answer.translation_primary.update!(title: 'A brand new title')

      expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'knowledgeBaseAnswerUpdates', 'answer', 'translation', 'title'))
        .to eq('A brand new title')
    end
  end

  context 'with an admin (editor)', authenticated_as: :admin do
    let(:admin) { create(:admin) }

    include_examples 'subscribes and receives updates'

    it_behaves_like 'graphql responds with error if unauthenticated'
  end

  context 'with an agent (reader)', authenticated_as: :agent do
    let(:agent) { create(:agent) }

    include_examples 'subscribes and receives updates'

    context 'with a draft answer' do
      let(:answer) { draft_answer }

      it 'is forbidden' do
        expect(gql.result.error_type).to eq(Exceptions::Forbidden)
      end
    end
  end

  context 'with a customer', authenticated_as: :customer do
    let(:customer) { create(:customer) }

    include_examples 'subscribes and receives updates'

    it 'stops the updates when the knowledge base becomes inactive' do
      knowledge_base.update!(active: false)
      answer.touch

      expect(mock_channel.mock_broadcasted_first.error_type).to eq(Exceptions::Forbidden)
    end

    context 'with a draft answer' do
      let(:answer) { draft_answer }

      it 'is forbidden' do
        expect(gql.result.error_type).to eq(Exceptions::Forbidden)
      end
    end
  end

  # Denied by KnowledgeBase::AnswerPolicy, which gates the read on the knowledge base being active
  #   â€” the answer argument is authorized before the resolver looks a knowledge base up.
  #   https://github.com/zammad/zammad/issues/6338
  context 'when the knowledge base is inactive', authenticated_as: :customer do
    let(:customer)       { create(:customer) 