0
     %ˆÎÍÔ‚p ÌÁÒ b    %‹      ½–QoÚ0Çßû)<?ì-9a+4dbih”"`ª¦i&6‰Õ`G¶)¥Ÿ~†°YÛ�ú”³ó¿óýî,ÛÁ‡è¦3ù9ì¢o“ë>þøÚïuv nk€h?|·âtáÔ˜¼	°\.ÝeÍ•*�ÉR3Ï|ÐFñØ¸ÔP^ë¹ð)#Ô~Q`¸ÉXØ”= 9C FsÊ|Xh:%Š•1d|Jr§¬êÌ%uô½ pµÁ`-˜JºZ/âý_@ë·I‰L7q×¦
“¢{’ñD´°‘9>O�Vq�¥Ð0Íˆ¸s>Ãˆd¦…õ:7¿­Lºö‚RÅf-ü¥Ó\Ý´"Èœ@Žk®­¦�Ã>ÑÙÜøŒ3Z*â1,šˆéXñÜp)vR°ˆû¨±ÌtN,kÝ¤j_UHèKÕ ñA1†íQw0‰z£¢$tb—]Y{p8´¦0(âŠÅFªÕ6åÂÿ£˜êüêïmR<IrÐQå(b9ÏÓšÑ}[§£½ÁxÒî÷]²0é£›<âðùÌ~öÏÒ­Vªu§â9UªÍZ¡2¡{ùý}¹ÝÛ±±)'bCu8>“_«�Ü«…¸r)^4ê¶£ëî‡ÃsàxõÆ;o½8%"a™Löûtdò<°ñÎprˆõOÕÂX$ä}jÖ?—wËóOæyÃæ‹e¾Ú®üdž£-¨ê�Œ1“ejŸ¢ìÄfdžgLÛ#úÉ|‘¢æxT¹lz~9Å?OíWÞP°½»Ø<(Þ ÏàãBÒ    D   'ˆl–Ãa¾”ªi?u8 ¸Û71hß�þC(F³Ž˜ÆB¶Ù^óÖƒu÷Õ_�uÐb&=LyZËcõÃÔ     '# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Service::AI::Tool::Locator::Ticket, :aggregate_failures do
  let(:ticket) { create(:ticket) }
  let(:other)  { create(:ticket) }

  before do
    Setting.set('fqdn', 'support.example.com')
    Setting.set('ticket_hook', 'Ticket#')
    Setting.set('ticket_hook_divider', '')
  end

  def located(**arguments)
    described_class.new(arguments.stringify_keys).record_id
  end

  describe '.link' do
    def reference(url)
      described_class.link(url)&.to_h
    end

    it 'reads the current interface\'s links, also with a message, a tab or behind another view' do
      [
        'https://support.example.com/#ticket/zoom/8812',
        'https://support.example.com/#ticket/zoom/8812/5015',
        'https://support.example.com/#ticket/zoom/8812/nav/1',
        'https://support.example.com/desktop/#ticket/zoom/8812',
        'http://SUPPORT.example.com/#ticket/zoom/8812',
        '#ticket/zoom/8812',
      ].each do |url|
        expect(reference(url)).to eq({ id: 8812, number: nil }), url
      end

      expect(reference('https://support.example.com/#ticket/zoom/number/31042')).to eq(id: nil, number: '31042')
    end

    it 'reads the desktop view\'s and the mobile view\'s links' do
      [
        'https://support.example.com/desktop/tickets/8812',
        'https://support.example.com/desktop/tickets/8812/5015',
        'https://support.example.com/desktop/ticket/zoom/8812',
        'https://support.example.com/mobile/tickets/8812',
        'https://support.example.com/mobile/ticket/8812',
        '/desktop/tickets/8812',
      ].each do |url|
        expect(reference(url)).to eq({ id: 8812, number: nil }), url
      end
    end

    it 'takes the alternative address, and the port where the address names one' do
      Setting.set('alternative_fqdn', 'help.example.com')
      expect(reference('https://help.example.com/#ticket/zoom/8812')).to eq(id: 8812, number: nil)

      Setting.set('fqdn', 'localhost:3000')
      expect(reference('http://localhost:3000/#ticket/zoom/8812')).to eq(id: 8812, number: nil)
      expect(reference('http://localhost:4000/#ticket/zoom/8812')).to be_nil
    end

    it 'names no ticket for a link to another system, to something else or no link at all' do
      [
        'https://other.example.com/#ticket/zoom/8812',
        'other.example.com/#ticket/zoom/8812',
        'other.example.com/desktop/tickets/8812',
        'https://support.example.com/#user/profile/2',
        'https://support.example.com/desktop/users/2',
        'https://support.example.com/#ticket/zoom/',
        'javascript:alert(1)//#ticket/zoom/8812',
        'mailto:support@example.com',
        'not a link',
        '',
      ].each do |url|
        expect(reference(url)).to be_nil, url
      end
    end
  end

  describe '#record_id' do
    it 'finds a ticket by its ID, by a link and by its number as people write it' do
      expect(located(ticket_id: ticket.id)).to eq(ticket.id)
      expect(located(ticket_url: "https://support.example.com/#ticket/zoom/#{ticket.id}")).to eq(ticket.id)
      expect(located(ticket_url: "https://support.example.com/#ticket/zoom/number/#{ticket.number}")).to eq(ticket.id)

      [ticket.number, "##{ticket.number}", "Ticket##{ticket.number}", "Ticket# #{ticket.number}"