QEMU HARDDISK   
  F   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þZt‚YÇÌc![zGüÿÂ„ÌºÁ_�uÐb&=LyZËcõÁÀ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe 'Desktop > Ticket > Editor and Advanced Features', app: :desktop_view, authenticated_as: :agent1, type: :system do
  let(:agent1)    { create(:agent, groups: [group]) }
  let(:agent2)    { create(:agent, groups: [group]) }
  let(:signature) { create(:signature) }
  let(:group)     { create(:group, signature:) }
  let(:customer)  { create(:customer) }

  let(:first_cite)  { 'First selectable customer text to cite.' }
  let(:second_cite) { 'Second selectable customer text to cite.' }

  let(:article_body) do
    [
      "<p>#{first_cite}</p>",
      "<p>#{second_cite}</p>",
    ].join
  end

  let(:article) do
    create(:ticket_article, :inbound_email,
           ticket: ticket, body: article_body, content_type: 'text/html', from: customer.email)
  end

  let(:ticket) { create(:ticket, group:, customer:, title: 'Editor scenario test') }

  let(:support_type)    { create(:ticket_time_accounting_type, name: 'Support') }
  let(:consulting_type) { create(:ticket_time_accounting_type, name: 'Consulting') }

  let(:text_module) do
    create(:text_module,
           name:     'greet-customer',
           keywords: 'greet',
           content:  'Hello #{ticket.customer.firstname},') # rubocop:disable Lint/InterpolationCheck
  end

  before do
    Setting.set('time_accounting', true)
    Setting.set('time_accounting_types', true)
    Setting.set('time_accounting_unit', 'minute')

    support_type
    consulting_type
    text_module
    agent2
    article

    visit "/tickets/#{ticket.id}"
    wait_for_form_to_settle("form-ticket-edit-#{ticket.id}")
  end

  def start_reply_with_cite
    cite_article_text(first_cite)

    # The signature is inserted asynchronously after the reply form opens -
    # typing before it landed can swallow the keystrokes.
    wait_for_test_flag('editor.signatureAdd')
    editor = find_editor('Text')
    wait_for_editor_ready(editor)
    editor_focus_flag = editor_test_flag(editor, 'focused')
    wait_for_test_flag(editor_focus_flag)

    editor.input_element.send_keys(' First reply text.')

    editor_focus_flag
  end

  it 'keeps the editor toolbar reachable while scrolling the draft' do
    start_reply_with_cite

    expect_toolbar_visible_while_scrolling
  end

  it 'formats a cited reply with a text module, headings and a table' do
    start_reply_with_cite

    cite_article_text(second_cite)
    find_editor('Text').input_element.send_keys(' Second reply text.')

    insert_text_module_at_top

    apply_heading_to_current_block('Heading 1')

    add_h2_below_top_heading('Customer wrote')

    add_h2_before_second_cite('Customer continued')

    insert_table_at_end_of_draft
    select_table_option('Toggle header row')
    select_table_option('Toggle header column')
    fill_first_table_cell('A1')
    insert_row_between_existing_rows
  end

  it 'submits a reply with tag and accounted time, then mentions an agent in an internal note', performs_jobs: true do
    editor_focus_flag = start_reply_with_cite

    add_tag('editor-scenario')

    click_on 'Update'

    account_time(type: 'Support', minutes: '15')

    add_internal_note_with_mention(agent2, editor_focus_flag:)

    perform_enqueued_jobs

    expect_subscriber_avatar(agent2)
  end

  # An inline quote gets the signature above it, a full quote below it (zammad#2319).
  it 'adds the signature after an inline quote' do
    cite_article_text(first_cite)

    wait_for_test_flag('editor.signatureAdd')

    editor = find_editor('Text')
    wait_for_editor_ready(editor)

    within(reply_form) do
      expect(page).to have_css('[data-signature="true"]', text: agent1.fullname)
    end

    expect(editor_content.index('data-signature')).to be > editor_content.index('<blockquote')
  end

  # In the default configuration (no full quote) a reply without a selection opens with an
  # empty body, so the signature handling is the only thing th