# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

RSpec.describe Service::KnowledgeBase::Search do
  include_context 'basic Knowledge Base'

  let(:user) { create(:admin) }

  # A term of its own per example group: searchindex_model_reload reindexes from the database but
  #   leaves the documents of rolled back records behind, and Elasticsearch answers with only the
  #   requested number of hits.
  let(:search_term) { 'ocarina' }

  let(:matching_answer) do
    create(:knowledge_base_answer, :published, category: category, translation_attributes: { title: 'Ocarina tuning' })
  end

  let(:subcategory_answer) do
    create(:knowledge_base_answer, :published, category: subcategory, translation_attributes: { title: 'Ocarina cleaning' })
  end

  let(:other_category_answer) do
    create(:knowledge_base_answer, :published, category: other_category, translation_attributes: { title: 'Ocarina storage' })
  end

  let(:matching_category) do
    create(:knowledge_base_category, knowledge_base: knowledge_base, parent: category).tap do |elem|
      elem.translations.first.update!(title: 'Ocarina department')
    end
  end

  # Title does not match, body does - so its body preview must come from the match rather than
  #   from the excerpt fallback.
  let(:body_only_answer) do
    create(:knowledge_base_answer, :published, category: category, translation_attributes: { title: 'Woodwind notes' }).tap do |elem|
      elem.translations.first.content.update!(body: 'Hold the ocarina with both hands.')
    end
  end

  def search_output(query = search_term, entity: :answer, scope: nil, for_user: user, **)
    described_class
      .with_current_user(for_user)
      .execute(query: query, knowledge_base: knowledge_base, entity: entity, scope: scope, locale: primary_locale, **)
  end

  def search(query = search_term, entity: :answer, scope: nil, for_user: user, **)
    search_output(query, entity: entity, scope: scope, for_user: for_user, **).results
  end

  def visibility_of(category)
    search_output(entity: :category).category_visibility[category.id]
  end

  # Looked up under the kind the record is, so an example only has to name the record - one run
  #   returns one kind of content.
  def result_for(item)
    entity = item.is_a?(KnowledgeBase::Category) ? :category : :answer

    search(entity: entity).find { |result| result.item == item }
  end

  def highlighted(segments)
    segments.select(&:highlight).map(&:text)
  end

  # The mode the quicksearch group asks for: answers alone, and none of the enrichment the search
  #   page's result list renders. Covered against both backends, because a hit means something
  #   different in each (Elasticsearch narrows to the browsed locale, the SQL fallback does not).
  def quick_search_output(query = search_term, limit: described_class::MAX_RESULTS)
    described_class
      .with_current_user(user)
      .execute(
        query:          query,
        knowledge_base: knowledge_base,
        locale:         primary_locale,
        entity:         :answer,
        limit:          limit,
        enriched:       false,
      )
  end

  def quick_search(query = search_term, limit: described_class::MAX_RESULTS)
    quick_search_output(query, limit: limit).results
  end

  shared_examples 'an answers-only, unenriched search' do
    it 'still finds answers' do
      expect(quick_search.map(&:item)).to include(matching_answer)
    end

    it 'returns answers alone, never a category' do
      expect(quick_search.map(&:item)).to all(be_a(KnowledgeBase::Answer))
    end

    # The searchable unit, and what Gql::Types::SearchResult::ItemType exposes for this model - so
    #   the quicksearch group is a list of these rather than of their answers.
    it 'carries the hit translation next to the answer' do
      result = quick_search.find { |elem| elem.item == matching_answer }

      expect(result.translation).to eq(matching_answer.translations.first)
    end

    it 'builds no title preview' do
      expect(quick_search.map(&:title_preview)).to all(be_empty)
    end

    it 'builds no body preview' do
      expect(quick_search.map(&:body_preview)).to all(be_empty)
    end

    # The preview being empty does not prove the body was never fetched: reading it loads the
    #   translation's content row and runs the whole HTML body through html2text. That is why the
    #   fallback is passed to #preview as a block rather than as an argument.
    it 'never loads the answer bodies' do
      expect(quick_search.map { |result| result.translation.association(:content).loaded? }).to all(be(false))
    end

    it 'builds no category path' do
      expect(quick_search.map(&:category_path)).to all(be_empty)
    end

    it 'hands out no batched category data' do
      output = quick_search_output

      expect([output.category_translations, output.category_visibility]).to all(be_empty)
    end

    # The two costs the mode exists to avoid, and the reason it exists at all: quicksearch fires on
    #   every debounced keystroke. Pinned on the calls rather than only on the empty output, because
    #   an output can be empty while the work was still done.
    it 'does not ask the backend for highlights' do
      allow(SearchKnowledgeBaseBackend).to receive(:new).and_call_original

      quick_search

      expect(SearchKnowledgeBaseBackend).to have_received(:new).with(hash_including(highlight_enabled: false))
    end

    it 'never loads the category tree' do
      allow(knowledge_base).to receive(:categories).and_call_original

      quick_search

      expect(knowledge_base).not_to have_received(:categories)
    end

    it 'honours a limit below the default cap' do
      expect(quick_search(limit: 1).size).to eq(1)
    end

    it 'returns nothing for a blank query' do
      expect(quick_search('')).to be_empty
    end
  end

  before do
    matching_answer
    subcategory_answer
    other_category_answer
    matching_category
    body_only_answer
  end

  context 'with Elasticsearch', searchindex: true do
    before do
      searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
    end

    it 'returns nothing for a blank query, so entering the page searches for nothing' do
      expect(search('')).to be_empty
    end

    it 'finds nothing where Elasticsearch did not run the search' do
      expect(search('laptop / email')).to be_empty
    end

    it 'raises where Elasticsearch did not run the search and the caller asks for it' do
      expect { search('laptop / email', raise_on_failure: true) }.to raise_error(SearchIndexBackend::SearchFailedError)
    end

    it 'finds answers' do
      expect(search.map(&:item)).to include(matching_answer)
    end

    it 'finds categories' do
      expect(search(entity: :category).map(&:item)).to include(matching_category)
    end

    # The knowledge base node itself is not one of the searchable kinds at all, so no run can
    #   return it - which is what ENTITY_MODELS leaves out.
    it 'returns answers alone when asked for answers' do
      expect(search.map(&:item)).to be_present.and(all(be_a(KnowledgeBase::Answer)))
    end

    it 'returns categories alone when asked for categories' do
      expect(search(entity: :category).map(&:item)).to be_present.and(all(be_a(KnowledgeBase::Category)))
    end

    it 'marks the matched run of the title' do
      expect(highlighted(result_for(matching_answer).title_preview)).to eq(['Ocarina'])
    end

    it 'leaves the unmatched runs of the title unmarked' do
      expect(result_for(matching_answer).title_preview.map(&:text).join).to eq('Ocarina tuning')
    end

    it 'marks the match inside the body' do
      expect(highlighted(result_for(body_only_answer).body_preview)).to eq(['ocarina'])
    end

    # The segments are plain text, so these characters have to survive indexing as themselves.
    context 'with HTML special characters in the title' do
      def title_of(answer, query)
        search(query).find { |result| result.item == answer }&.title_preview&.map(&:text)&.join
      end

      # A body of its own: the factory's Lorem body may contain `amplus`, which `amp` matches as
      #   the backend appends a wildcard to the query.
      let(:ampersand_answer) do
        create(:knowledge_base_answer, :published, category: category, translation_attributes: { title: 'Ocarina & order' }).tap do |answer|
          answer.translations.first.content.update!(body: 'Sheet music for both.')
        end
      end

      let(:nbsp_answer) do
        create(:knowledge_base_answer, :published, category: category, translation_attributes: { title: "Ocarina\u00A0<3" })
      end

      before do
        ampersand_answer
        nbsp_answer
        searchindex_model_reload([KnowledgeBase::Answer::Translation])
      end

      it 'previews an ampersand unescaped' do
        expect(title_of(ampersand_answer, 'ocarina')).to eq('Ocarina & order')
      end

      # https://github.com/zammad/zammad/issues/6369
      it 'previews an ampersand unescaped for a wildcard search' do
        expect(title_of(ampersand_answer, '*')).to eq('Ocarina & order')
      end

      it 'does not find the answer by the entity name of its ampersand' do
        expect(search('amp').map(&:item)).not_to include(ampersand_answer)
      end

      it 'previews a non-breaking space and an angle bracket unescaped' do
        expect(result_for(nbsp_answer).title_preview.map(&:text).join).to eq("Ocarina\u00A0<3")
      end
    end

    it 'still previews the body when only the title matched' do
      expect(result_for(matching_answer).body_preview.map(&:text).join).to be_present
    end

    it 'leaves a category without a body preview' do
      expect(result_for(matching_category).body_preview).to be_empty
    end

    it 'reports the category path of an answer, root first' do
      expect(result_for(subcategory_answer).category_path).to eq([category, subcategory])
    end

    it 'reports where a category sits, not the category itself' do
      expect(result_for(matching_category).category_path).to eq([category])
    end

    # How many hits a kind has is what its tab badge reads, and each kind is searched on its own -
    #   so the categories cannot be moved by how many answers the same term hit.
    it 'finds each kind of content independently of the other' do
      expect(search(entity: :category).size).to eq(1)
    end

    # The cap bounds one kind of content, because one run searches one index — a term with many
    #   answer hits must not leave the categories reading zero.
    context 'when the result cap is reached' do
      before { stub_const('Service::KnowledgeBase::Search::MAX_RESULTS', 2) }

      it 'caps the kind that was searched' do
        expect(search.size).to eq(2)
      end

      it 'leaves the other kind its own full count' do
        expect(search(entity: :category).size).to eq(1)
      end
    end

    # Filtered by permission before anything is counted: a draft is a hit for an editor and not one
    #   for a customer, and a tab badge has to say so.
    describe 'permission filtering' do
      let!(:draft_answer_hit) do
        create(:knowledge_base_answer, :draft, category: category, translation_attributes: { title: 'Ocarina repairs' })
      end
      let!(:internal_answer_hit) do
        create(:knowledge_base_answer, :internal, category: category, translation_attributes: { title: 'Ocarina rentals' })
      end

      before do
        searchindex_model_reload([KnowledgeBase::Answer::Translation])
      end

      it 'finds answers of every publication state for an editor' do
        expect(search(for_user: create(:admin)).map(&:item)).to include(matching_answer, internal_answer_hit, draft_answer_hit)
      end

      it 'finds internal answers but no drafts for a reader', :aggregate_failures do
        items = search(for_user: create(:agent)).map(&:item)

        expect(items).to include(matching_answer, internal_answer_hit)
        expect(items).not_to include(draft_answer_hit)
      end

      it 'finds published answers only for a customer', :aggregate_failures do
        items = search(for_user: create(:customer)).map(&:item)

        expect(items).to include(matching_answer)
        expect(items).not_to include(internal_answer_hit, draft_answer_hit)
      end

      it 'finds no drafts or archived answers for an editor either, where they are left out', :aggregate_failures do
        archived_answer_hit = create(:knowledge_base_answer, :archived, category: category, translation_attributes: { title: 'Ocarina history' })
        searchindex_model_reload([KnowledgeBase::Answer::Translation])

        items = search(for_user: create(:admin), include_drafts_and_archived: false).map(&:item)

        expect(items).to include(matching_answer, internal_answer_hit)
        expect(items).not_to include(draft_answer_hit, archived_answer_hit)
      end
    end

    # CategoryType#visibility renders the status icon of a result from this. It is batched here
    #   because the fallback, KnowledgeBase::Category#content_visibility, walks the subtree with a
    #   recursive query once per publication state — for every category on the page.
    describe 'category visibility' do
      it 'reads a category hit without content as draft' do
        expect(visibility_of(matching_category)).to eq(:draft)
      end

      it 'reports the publication state of the content in the category itself' do
        create(:knowledge_base_answer, :internal, category: matching_category)

        expect(visibility_of(matching_category)).to eq(:internal)
      end

      it 'covers the whole subtree, not the category alone' do
        child = create(:knowledge_base_category, knowledge_base: knowledge_base, parent: matching_category)
        create(:knowledge_base_answer, :published, category: child)

        expect(visibility_of(matching_category)).to eq(:published)
      end

      it 'reports the highest state when the subtree mixes them' do
        create(:knowledge_base_answer, :archived, category: matching_category)
        create(:knowledge_base_answer, :internal, category: matching_category)

        expect(visibility_of(matching_category)).to eq(:internal)
      end

      # Same rule as the browse grid, which derives it from the same
      #   Service::KnowledgeBase::Concerns::WalksCategoryTree: content the browsed locale has no
      #   translation for does not count.
      it 'ignores content that is not translated to the browsed locale' do
        create(:knowledge_base_answer, :published, category:               matching_category,
                                                   translation_attributes: { kb_locale: alternative_locale })

        expect(visibility_of(matching_category)).to eq(:draft)
      end

      # An answer search has no category hits, so it needs none of it.
      it 'is empty when answers were searched' do
        expect(search_output.category_visibility).to be_empty
      end
    end

    context 'when scoped to a category' do
      it 'includes answers of the category and its subcategories' do
        expect(search(scope: category).map(&:item)).to include(matching_answer, subcategory_answer)
      end

      it 'excludes answers of a sibling subtree' do
        expect(search(scope: category).map(&:item)).not_to include(other_category_answer)
      end
    end

    context 'when the indexed record is gone' do
      before { matching_answer.destroy! }

      it 'skips the stale hit instead of failing the whole search' do
        expect(search.map(&:item)).to include(subcategory_answer).and(not_include(matching_answer))
      end
    end

    # Granular permissions make KnowledgeBase.access_for_user :granular for anyone with a flat
    #   editor/reader permission, but this user has neither - they search as :public and find the
    #   published content they can browse, not nothing.
    context 'with granular permissions in effect, for a user without any knowledge base permission' do
      # Not the :agent factory - its default Agent role grants knowledge_base.reader (see
      #   db/seeds/permissions.rb), which is exactly the flat permission this example excludes.
      let(:user) { create(:agent, roles: [create(:role, permission_names: 'ticket.agent')]) }

      before do
        other_role = create(:role, permission_names: 'knowledge_base.editor')
        KnowledgeBase::PermissionsUpdate.new(other_category).update!(other_role => 'editor')
      end

      it 'takes the public path' do
        allow(SearchKnowledgeBaseBackend).to receive(:new).and_call_original

        search

        expect(SearchKnowledgeBaseBackend).to have_received(:new).with(hash_including(flavor: :public))
      end

      it 'still finds published answers outside the granted category' do
        expect(search.map(&:item)).to include(matching_answer)
      end
    end

    it_behaves_like 'an answers-only, unenriched search'
  end

  context 'without Elasticsearch' do
    before { Setting.set('es_url', nil) }

    it 'still finds answers' do
      expect(search.map(&:item)).to include(matching_answer)
    end

    it 'still finds categories' do
      expect(search(entity: :category).map(&:item)).to include(matching_category)
    end

    it 'returns the plain title as a single unmarked segment' do
      expect(result_for(matching_answer).title_preview.map { |segment| [segment.text, segment.highlight] })
        .to eq([['Ocarina tuning', false]])
    end

    it 'falls back to the body excerpt, unmarked' do
      preview = result_for(body_only_answer).body_preview

      expect(preview.map(&:highlight)).to eq([false])
    end

    # The fallback lists the newest hits first, here the draft and the archived answer.
    it 'leaves drafts and archived answers out where asked to, before it counts its hits' do
      create(:knowledge_base_answer, :draft, category: category, translation_attributes: { title: 'Ocarina repairs' })
      create(:knowledge_base_answer, :archived, category: category, translation_attributes: { title: 'Ocarina history' })

      expect(search(for_user: create(:admin), include_drafts_and_archived: false, limit: 1).map(&:item))
        .to contain_exactly(have_attributes(visibility: :published))
    end

    it_behaves_like 'an answers-only, unenriched search'
  end
end
