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

require 'rails_helper'

RSpec.describe Gql::Queries::Link::List, type: :graphql do
  %w[child parent normal].each do |link_type|
    context "when fetching link list with link type #{link_type}" do
      let(:type) { link_type }

      let(:from_group)    { create(:group) }
      let(:from)          { create(:ticket, group: from_group) }
      let(:to_group)      { create(:group) }
      let(:to)            { create(:ticket, group: to_group) }
      let(:link)          { create(:link, from:, to:, link_type: type) }

      let(:variables) { { objectId: gql.id(from), targetType: 'Ticket' } }

      let(:query) do
        <<~QUERY
          query linkList($objectId: ID!, $targetType: String!) {
            linkList(objectId: $objectId, targetType: $targetType) {
              type
              item {
                ... on Ticket {
                  id
                  title
                }
              }
            }
          }
        QUERY
      end

      before do
        link
        gql.execute(query, variables: variables)
      end

      context 'with authenticated session', authenticated_as: :authenticated do
        let(:authenticated) { create(:agent, groups: [from_group, to_group]) }

        it 'returns link list' do
          link_type = if type == 'normal'
                        type
                      else
                        type == 'parent' ? 'child' : 'parent'
                      end

          expect(gql.result.data.first).to eq(
            {
              'item' => { 'id' => gql.id(to), 'title' => to.title },
              'type' => link_type
            }
          )
        end

        context 'when source is not accessible' do
          let(:authenticated) { create(:agent, groups: [to_group]) }

          it 'raises an error' do
            expect(gql.result.error_type).to eq(Pundit::NotAuthorizedError)
          end
        end

        context 'when target is not accessible' do
          before do
            create(:link, from: from, to: create(:ticket), link_type: type)
            gql.execute(query, variables: variables)
          end

          it 'returns link list without the related link', :aggregate_failures do
            expect(Link.count).to eq(2)
            expect(gql.result.data.size).to eq(1)
          end
        end
      end
    end
  end

  context 'when fetching linked knowledge base answers' do
    let(:group)       { create(:group) }
    let(:ticket)      { create(:ticket, group:) }
    let(:answer)      { create(:knowledge_base_answer, :published) }
    let(:translation) { answer.translation }

    let(:variables) { { objectId: gql.id(ticket), targetType: 'KnowledgeBase::Answer::Translation' } }

    let(:query) do
      <<~QUERY
        query linkList($objectId: ID!, $targetType: String!) {
          linkList(objectId: $objectId, targetType: $targetType) {
            type
            item {
              ... on KnowledgeBaseAnswerTranslation {
                id
                title
                categoryTreeTranslation {
                  id
                  title
                }
              }
            }
          }
        }
      QUERY
    end

    before do
      create(:link, from: ticket, to: translation, link_type: 'normal')
      gql.execute(query, variables: variables)
    end

    # An agent without knowledge base permissions still sees published answers,
    #   so their category tree must resolve as well.
    context 'with an agent without knowledge base permissions', authenticated_as: :authenticated do
      let(:role)          { create(:role, permissions: Permission.where(name: 'ticket.agent')) }
      let(:authenticated) { create(:agent, roles: [role], groups: [group]) }

      let(:category_translation) { answer.category.translation_preferred(translation.kb_locale) }

      it 'returns the answer including its category tree' do
        expect(gql.result.data.first).to eq(
          {
            'item' => {
              'id'                      => gql.id(translation),
              'title'                   => translation.title,
              'categoryTreeTranslation' => [
                { 'id' => gql.id(category_translation), 'title' => category_translation.title },
              ],
            },
            'type' => 'normal'
          }
        )
      end
    end
  end
end
