import {mount} from '@vue/test-utils';

import {describe, expect, it} from 'vitest';

import ChatKnowledgeCard from '../ChatKnowledgeCard.vue';

describe('ChatKnowledgeCard', () => {
  it('renders FAQ citation with knowledge name', () => {
    const wrapper = mount(ChatKnowledgeCard, {
      props: {
        items: [
          {
            id: 'faq-1',
            chunkContent: '暑假安排原文',
            faqCollection: true,
            knowledgeId: 'kb-faq',
            knowledgeName: '学生事务 FAQ',
            knowledgeType: 'FAQ',
          },
        ],
      },
    });

    expect(wrapper.find('.chat-knowledge-card__pill').text()).toContain(
      '学生事务 FAQ',
    );
  });

  it('renders document citation with document name', () => {
    const wrapper = mount(ChatKnowledgeCard, {
      props: {
        items: [
          {
            chunkContent: '文档 chunk 原文',
            documentId: 'doc-1',
            documentName: '数据引接与治理.pdf',
            knowledgeName: '治理知识库',
            knowledgeType: 'DOCUMENT',
          },
        ],
      },
    });

    expect(wrapper.find('.chat-knowledge-card__pill').text()).toContain(
      '数据引接与治理.pdf',
    );
  });

  it('falls back to sourceFileName for document citation', () => {
    const wrapper = mount(ChatKnowledgeCard, {
      props: {
        items: [
          {
            chunkContent: '文档 chunk 原文',
            documentId: 'doc-1',
            knowledgeName: '治理知识库',
            knowledgeType: 'DOCUMENT',
            metadata: {
              sourceFileName: '治理方案.docx',
            },
          },
        ],
      },
    });

    expect(wrapper.find('.chat-knowledge-card__pill').text()).toContain(
      '治理方案.docx',
    );
  });

  it('aggregates multiple chunks from the same source', () => {
    const wrapper = mount(ChatKnowledgeCard, {
      props: {
        items: [
          {
            chunkContent: '第一段',
            chunkId: 'chunk-1',
            documentId: 'doc-1',
            documentName: '治理方案.docx',
            knowledgeType: 'DOCUMENT',
          },
          {
            chunkContent: '第二段',
            chunkId: 'chunk-2',
            documentId: 'doc-1',
            documentName: '治理方案.docx',
            knowledgeType: 'DOCUMENT',
          },
        ],
      },
    });

    expect(wrapper.findAll('.chat-knowledge-card__pill')).toHaveLength(1);
    expect(wrapper.find('.chat-knowledge-card__count').text()).toContain('2');
  });

  it('shows raw chunk content after clicking citation', async () => {
    const wrapper = mount(ChatKnowledgeCard, {
      props: {
        items: [
          {
            chunkContent: '这里是命中片段原文',
            chunkId: 'chunk-1',
            documentId: 'doc-1',
            documentName: '治理方案.docx',
            knowledgeName: '治理知识库',
            score: 0.86,
          },
        ],
      },
    });

    await wrapper.find('.chat-knowledge-card__pill').trigger('click');

    expect(wrapper.find('.chat-knowledge-card__popover').exists()).toBe(true);
    expect(wrapper.text()).toContain('这里是命中片段原文');
    expect(wrapper.text()).toContain('86%');
  });
});
