disabled
  Š        <   K   [   _   b   f   l   p   q   {   |   €   „   %z%c%T%s%b%e%r áŠ¢á‰µá‹®áŒµá‹« ETH ET ETH ETH   ç    áŒ�á‹•á‹�  gez gez UTF-8   L   ˆl–Ãa¾”J_)â€nà�pN©‹Fÿ�þB g	Ñ´9óÄƒh¹Ãa–Ãa¾”J_)â€nàƒp)‹FÿÂ      disabled
  5   ˆl–Ãa¾”J_)â€nà�p.©‹Fÿ�þB g	Á•¦YùÆƒh¹Å¿Ã  5   ˆl–Ãa¾”J_)â€nà�pO)‹Fÿ�þB g	ÑÊ)$—ùÇƒh¹ÆÀÄ  5   ˆl–Ãa¾”J_)â€nà�p/)‹Fÿ�þB g	Â|µóÈƒh¹ÇÁÅ      disabled
      disabled
      disabled
  7   	ˆl–Ãa¾”J_)â€nàƒp)‹Fÿ‘äÇò 8àHðVFðEüÿÉƒh¹ÈÂÆ      	disabled
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þYj1,ã�f1�­¸QùÊ„->É_�uÐb&=LyZËcõÄÈ 4²    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Service::Ticket::Create, current_user_id: -> { user.id } do
  subject(:service_result) { described_class.with_current_user(user).execute(ticket_data:) }

  let(:user)     { create(:agent, groups: [group]) }
  let(:group)    { create(:group) }
  let(:customer) { create(:customer) }

  describe '#execute' do
    let(:sample_title) { Faker::Lorem.sentence }

    let(:ticket_data) do
      {
        title:    sample_title,
        group:    group,
        customer: customer
      }
    end

    it 'creates a ticket with given metadata' do
      expect(service_result)
        .to have_attributes(
          title:    sample_title,
          group:    group,
          customer: customer
        )
    end

    it 'creates a ticket with customer email address' do
      test_email = Faker::Internet.unique.email
      ticket_data[:customer] = { email: test_email }

      expect(service_result.customer).to have_attributes(
        email:    test_email,
        role_ids: Role.signup_role_ids
      )
    end

    it 'takes an existing customer by email address' do
      ticket_data[:customer] = { email: customer.email }

      expect(service_result.customer).to eq(customer)
    end

    context 'when the customer is given as a phone number' do
      let(:phone_number) { '+49 30 609812345' }

      before { ticket_data[:customer] = { phone: phone_number } }

      it 'creates a customer with the number' do
        expect(service_result.customer).to have_attributes(
          phone:    phone_number,
          email:    '',
          role_ids: Role.signup_role_ids
        )
      end

      it 'indexes the number as the caller ID of the new customer, so the next call finds them' do
        expect(Cti::CallerId.where(user_id: service_result.customer.id, level: 'known').pluck(:caller_id))
          .to include('4930609812345')
      end

      it 'takes an existing customer whose phone is the number, however written' do
        existing_customer = create(:customer, phone: '030 / 609812345')

        expect(service_result.customer).to eq(existing_customer)
      end

      it 'takes an existing customer whose mobile is the number' do
        existing_customer = create(:customer, mobile: '0049 30 609812345')

        expect(service_result.customer).to eq(existing_customer)
      end

      it 'passes over a user who only mentions the number' do
        create(:customer, note: "Reachable at #{phone_number} in the morning.")

        expect(service_result.customer).to have_attributes(phone: phone_number, note: '')
      end

      it 'rejects a value that is no phone number' do
        ticket_data[:customer] = { phone: 'not a number' }

        expect { service_result }.to raise_error(Exceptions::InvalidAttribute, 'The phone number is invalid.')
      end

      it 'rejects a number too short to be one' do
        ticket_data[:customer] = { phone: '12345' }

        expect { service_result }.to raise_error(Exceptions::InvalidAttribute)
      end
    end

    it 'fails to create ticket without access' do
      allow_any_instance_of(TicketPolicy)
        .to receive(:create?).and_return(false)

      expect { service_result }
        .to raise_error(Pundit::NotAuthorizedError)
    end

    it 'adds article when present' do
      sample_body = Faker::Lorem.sentence
      ticket