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

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

import WorkflowFinalOutput from './WorkflowFinalOutput.vue';

vi.mock('#/utils/clipboard-feedback', () => ({
  copyTextWithFeedback: vi.fn(),
}));

describe('workflow final output', () => {
  it('uses one copy action and exposes array item hierarchy for a single field', () => {
    const wrapper = mount(WorkflowFinalOutput, {
      props: {
        output: {
          output: ['第一项', '第二项'],
        },
      },
    });

    expect(wrapper.text()).toContain('复制结果');
    expect(wrapper.text()).not.toContain('复制全部');
    expect(wrapper.findAll('button')).toHaveLength(1);
    expect(wrapper.get('.workflow-final-output__section-meta').text()).toBe(
      '数组 · 2 项',
    );
    expect(
      wrapper
        .findAll('.workflow-final-output__list-index')
        .map((item) => item.text()),
    ).toEqual(['01', '02']);
  });

  it('keeps scoped copy actions when the result contains multiple fields', () => {
    const wrapper = mount(WorkflowFinalOutput, {
      props: {
        output: {
          answer: '完成',
          items: ['A', 'B'],
        },
      },
    });

    expect(wrapper.text()).toContain('复制全部');
    expect(wrapper.findAll('button')).toHaveLength(3);
  });

  it('renders scalar line breaks in the workflow chat result', async () => {
    const wrapper = mount(WorkflowFinalOutput, {
      props: {
        output: {
          output: '问题：你是谁\n答案：你好',
        },
      },
    });

    await nextTick();

    const textOutput = wrapper.get('.workflow-final-output__text');
    expect(textOutput.text()).toContain('问题：你是谁');
    expect(textOutput.text()).toContain('答案：你好');
    expect(textOutput.find('br').exists()).toBe(true);
  });
});
