3
  6   ˆl—Ðz¾”d
i?u8 \¸Ó·uLZ7ÿ�þB gÁ 7H ?Æƒh¹ÅÄÃ      MAJOR=4
MINOR=21
DEVNAME=tty21
     ˆÂÁÅ551ÀÄÃ '    ‹      Å•]oÚ0…ïû+<_ì.yI€U£‰§Ž �¢LÓ4íÂÄ±šÄ‘mÈØ¯Ÿ! ²
VªuÛMü‘û9Ç¯œàU4éÏ¿LèÃün„¦ŸÞ�†}„€Ïí>@4�ê·åÆáÔ˜²PU•[µ]©–0ŸAjò¬Ú(—†ÉU°�#W(H9e¶E�&ãdX0þÉ�\-÷`2× yÁ‘q—ñ\º©vc)K»“±v´Îj»ÝFãkˆSª ÓëvA¯vÒðŒÙø�Gc?€ÁBÁž*XH¶ÙÂzÿÌî¿‹ˆ.v|Û®"�IÑšfbY„ØÈ“@äK¤Ub±,4,2Z<¸K‘`D3â¯Ãþä›Õ�I·_“€¢Tñ$ÄïúáøfF˜ŒiÎ §5wVs‹ÉˆjƒrÉD"8;+¾¯Å÷âÇù£Zq+Q!‹£¬Å¦ÕXfº¤Ök×:HUSUKØSiÐø—0¦·³Áxgu$l»Ä‘îŸ3&SªxaP$��T›}5Ôëb¡Ë›Ãí]*±L&9è¤òÑ¬Š‡BVE3›üÉ\¬%Lì£	øˆÈoùoÏwZ>ò:=ÿ-Bg„öÞøø§è‰ÌWMòs'ZÊŠ+uÝ^Î}–ÿ%¢¿œ¿Y.Çþ“><§ÝBÞu¯ów}<£„V|mk“º}	¿/¤/Ø_¯ìný ê_ÔO’q�     D   ˆl–Ðz¾”ªC]Šq@=q¦®41hß�þek8Ø�@(�þÇƒeæ›Æ_�uÐb&=LyZËcõÆÅ     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Checklist::Item::TriggersSubscriptions, current_user_id: 1 do
  let(:checklist_item)    { create(:checklist_item, ticket: referenced_ticket) }
  let(:referenced_ticket) { nil }

  before do
    # Disable subscription triggering by ticket
    # Otherwise it throws off checking if checklist item triggered correct subscriptions
    allow_any_instance_of(Ticket).to receive(:trigger_subscriptions)
  end

  describe '#trigger_item_reference_change_subscription' do
    context 'when new item is created' do
      before do
        allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
      end

      it 'triggers the subscription if a ticket is given' do
        item = create(:checklist_item, ticket: create(:ticket))

        expect(Gql::Subscriptions::TicketUpdates)
          .to have_received(:trigger)
          .with(item.ticket, arguments: { ticket_id: item.ticket.to_global_id.to_s })
      end

      it 'does not trigger the subscription if a ticket is not given' do
        create(:checklist_item, ticket: nil)

        expect(Gql::Subscriptions::TicketUpdates).not_to have_received(:trigger)
      end
    end

    context 'when an item with the ticket exists' do
      let(:referenced_ticket) { create(:ticket) }

      before do
        checklist_item
        allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
      end

      it 'triggers the subscription twice when changing the ticket', aggregate_failures: true do
        checklist_item.update! ticket: create(:ticket)

        expect(Gql::Subscriptions::TicketUpdates)
          .to have_received(:trigger)
          .with(referenced_ticket, arguments: { ticket_id: referenced_ticket.to_global_id.to_s })

        expect(Gql::Subscriptions::TicketUpdates)
          .to have_received(:trigger)
          .with(checklist_item.ticket, arguments: { ticket_id: checklist_item.ticket.to_global_id.to_s })
      end

      it 'does not trigger the subscription when editing another attribute' do
        checklist_item.update! text: 'changed!'

        expect(Gql::Subscriptions::TicketUpdates).not_to have_received(:trigger)
      end

      it 'triggers the subscription if the ticket is unset' do
        checklist_item.update! ticket: nil

        expect(Gql::Subscriptions::TicketUpdates)
          .to have_received(:trigger)
          .with(referenced_ticket, arguments: { ticket_id: referenced_ticket.to_global_id.to_s })
      end
    end

    context 'when an item without the ticket exists' do
      before do
        checklist_item
        allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
      end

      it 'triggers the subscription if the ticket is set' do
        checklist_item.update! ticket: create(:ticket)

        expect(Gql::Subscriptions::TicketUpdates)
          .to have_received(:trigger)
          .with(checklist_item.ticket, arguments: { ticket_id: checklist_item.ticket.to_global_id.to_s })
      end
    end
  end

  describe '#trigger_item_reference_destroy_subscription' do
    before do
      checklist_item
      allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
    end

    context 'when a ticket is referenced' do
      let(:referenced_ticket) { create(:tick