0
  E   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þDãO,ã�f1�­F!¿üÿÂƒ}àÁ_�uÐb&=LyZËcõÁÀ &H    # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'

# What the search finds, and for whom, is covered by spec/services/service/knowledge_base/search_spec.rb â€”
#   this covers the GraphQL surface only: arguments, authorization, the payload, and its query count.
RSpec.describe Gql::Queries::KnowledgeBase::Search, :searchindex, type: :graphql do
  include_context 'basic Knowledge Base'

  let(:gql_query) do
    <<~GQL
      query knowledgeBaseSearch($query: String!, $entity: EnumKnowledgeBaseSearchEntity!, $categoryId: ID, $locale: String, $first: Int) {
        knowledgeBaseSearch(query: $query, entity: $entity, categoryId: $categoryId, locale: $locale, first: $first) {
          totalCount
          edges {
            node {
              item {
                ... on KnowledgeBaseAnswer { id translation { title } }
                ... on KnowledgeBaseCategory { id translation { title } visibility }
              }
              titlePreview { text highlight }
              bodyPreview { text highlight }
              categoryPath { id title }
            }
          }
          pageInfo { endCursor hasNextPage }
        }
      }
    GQL
  end

  let(:search)      { 'ocarina' }
  # Always passed, so a context only has to switch it; that the argument may be *omitted* is
  #   covered on its own, with a document that leaves it out.
  let(:entity)      { 'answer' }
  let(:category_id) { nil }
  let(:locale)      { nil }
  let(:first)       { nil }
  let(:variables)   { { query: search, entity:, categoryId: category_id, locale:, first: }.compact }

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

  let(:internal_ocarina) do
    create(:knowledge_base_answer, :internal, category: category, translation_attributes: { title: 'Ocarina repair' })
  end

  let(:draft_ocarina) do
    create(:knowledge_base_answer, :draft, category: category, translation_attributes: { title: 'Ocarina draft' })
  end

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

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

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

  # Its answer is titled so that it does not match the search itself â€” the category is the hit
  #   here, and its published answer only gives the category something to be visible from.
  let(:ocarina_category_with_content) do
    create(:knowledge_base_category, knowledge_base: knowledge_base, parent: category).tap do |elem|
      elem.translations.first.update!(title: 'Ocarina archive')
      create(:knowledge_base_answer, :published, category: elem, translation_attributes: { title: 'Woodwind storage' })
    end
  end

  # Overridden where extra hits are needed in the index, which the shared setup below builds.
  let(:extra_ocarina_categories) { [] }

  def item_ids
    gql.result.nodes.map { |node| node.dig('item', 'id') }
  end

  def node_for(record)
    gql.result.nodes.find { |node| node.dig('item', 'id') == gql.id(record) }
  end

  before do
    published_ocarina
    internal_ocarina
    draft_ocarina
    subcategory_ocarina
    other_category_ocarina
    ocarina_category
    ocarina_category_with_content
    extra_ocarina_categories
    searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
    gql.execute(gql_query, variables:)
  end

  context 'with an admin (editor)', authenticated_as: :admin do
    let(:admin) { create(:admin) }

    it 'finds answers of every publication st