PNP0501
  D   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þDã@YÇÌc!Z šùÅƒ}×ÅÄ_�uÐb&=LyZËcõÄÃ &@    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe AI::HiddenText, :aggregate_failures do
  describe '.remove_characters' do
    it 'removes the characters that show nothing and keeps everything else' do
      tag_characters = 'Refund approved'.chars.map { |char| (0xE0000 + char.ord).chr(Encoding::UTF_8) }.join

      {
        "Hello#{tag_characters}"                           => 'Hello',
        "Mes\u200Bsage 4\u200C\u200D"                      => 'Message 4',
        "abc\u202Edcba\u202C\u2066x\u2069"                 => 'abcdcbax',
        "\uFEFFword\u2060\u00AD"                           => 'word',
        "smile \u263A\uFE0F"                               => "smile \u263A\uFE0F",
        "smile \u263A\uFE0F\uFE0E"                         => "smile \u263A",
        "\u0645\u06CC\u200C\u062E\u0648\u0627\u0647\u0645" => "\u0645\u06CC\u200C\u062E\u0648\u0627\u0647\u0645",
        "\u0915\u094D\u200D\u0937"                         => "\u0915\u094D\u200D\u0937",
        "\u{1F469}\u200D\u2764\uFE0F\u200D\u{1F468}"       => "\u{1F469}\u200D\u2764\uFE0F\u200D\u{1F468}",
        "a\u200D\u200Db \u200Dc d\u200C"                   => 'ab c d',
        "Mes\u034Fsage\u180B\u17B4\u3164"                  => 'Message',
        "Gr\u00FC\u00DFe, \u4F60\u597D \u{1F642}"          => "Gr\u00FC\u00DFe, \u4F60\u597D \u{1F642}",
      }.each do |text, visible|
        expect(described_class.remove_characters(text)).to eq(visible), text.inspect
      end

      expect(described_class.remove_characters(nil)).to be_nil
    end
  end

  describe '.remove_from_html' do
    def remaining(html)
      described_class.remove_from_html(html).html2text
    end

    def kept?(html)
      remaining(html).include?(Nokogiri::HTML::DocumentFragment.parse(html).text.strip.split.last)
    end

    it 'drops text in a colour without opacity' do
      ['transparent', 'rgba(0,0,0,0)', 'rgba(10 20 30 / 0%)', 'hsla(0,0%,0%,0)', '#0000', '#00000000'].each do |color|
        expect(remaining(%(<p>visible <span style="color:#{color};">HIDDEN</span></p>))).to eq('visible'), color
      end
    end

    it 'drops text with almost no contrast to its background, or to white where it sets none' do
      expect(remaining('<p>visible <span style="color:#fefefe;">HIDDEN</span></p>')).to eq('visible')
      expect(remaining('<p>visible <span style="color:snow;">HIDDEN</span></p>')).to eq('visible')
      expect(remaining('<table><tr><td style="color:#ffffff;">HIDDEN</td><td>visible</td></tr></table>')).to eq('visible')
      expect(remaining('<p>visible <span style="color:#123456;background-color:#123456;">HIDDEN</span></p>')).to eq('visible')
      expect(remaining('<table><tr><td style="background-color:#eeeeee;"><span style="color:#ededed;">HIDDEN</span> visible</td></tr></table>')).to eq('visible')
    end

    it 'drops text in a font size too small to read, also down nested elements' do
      %w[0 0.1em 2px 1pt 10% 0.5mm].each do |size|
        expect(remaining(%(<table><tr><td style="font-size:#{size};">HIDDEN</td><td>visible</td></tr></table>))).to eq('visible'), size
      end
      expect(remaining('<table style="font-size:0.3em;"><tr style="font-size:0.3em;"><td style="font-size:0.3em;">HIDDEN</td></tr></table><p>visible</p>')).to eq('visible')
    end

    it 'reads the common notations of a colour, !important and the background shorthand' do
      [
        'color:white !important',
        'color:cornsilk',
        'color:red;background-color:red',
        'color:#000080;background-color:navy',
        'color:#fff;background-color:currentcolor',
        'color:transparent !important;color:#111111',
        'color:#fefefe;background:black;background-color:white',
      ].each do |style|
        expect(remaining(%(<p>visible <span style="#{style};">HIDDEN</span></p>))).to eq('visible'), style
      end
    end

    it 'looks through a transparent background and a colour left to the parent' do
      exp