import type { ChatTimelineItem } from '../types';

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

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

import ChatTimeline from '../ChatTimeline.vue';

function thinkingMessage(options?: {
  content?: string;
  expanded?: boolean;
  messageStatus?: 'done' | 'streaming';
  thinkingStatus?: 'end' | 'thinking';
}): Extract<ChatTimelineItem, { type: 'message' }> {
  return {
    id: 'assistant-thinking',
    parts: [
      {
        id: 'thinking-part',
        content: options?.content ?? '分析问题',
        expanded: options?.expanded ?? true,
        status: options?.thinkingStatus ?? 'thinking',
        type: 'thinking',
      },
    ],
    role: 'assistant',
    status: options?.messageStatus ?? 'streaming',
    type: 'message',
  };
}

describe('chat timeline thinking interaction', () => {
  it('keeps the user expansion choice across streaming snapshots', async () => {
    const wrapper = mount(ChatTimeline, {
      props: {
        items: [thinkingMessage()],
      },
    });

    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(true);

    await wrapper.get('.chat-thinking-block__trigger').trigger('click');
    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(false);

    await wrapper.setProps({
      items: [
        thinkingMessage({
          content: '分析问题并继续接收思考分片',
          expanded: true,
        }),
      ],
    });
    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(false);

    await wrapper.get('.chat-thinking-block__trigger').trigger('click');
    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(true);

    await wrapper.setProps({
      items: [
        thinkingMessage({
          content: '分析问题并继续接收正文分片',
          expanded: false,
          thinkingStatus: 'end',
        }),
      ],
    });
    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(true);
  });

  it('uses the stream default when the user has not toggled thinking', async () => {
    const wrapper = mount(ChatTimeline, {
      props: {
        items: [thinkingMessage()],
      },
    });

    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(true);

    await wrapper.setProps({
      items: [
        thinkingMessage({
          expanded: false,
          messageStatus: 'done',
          thinkingStatus: 'end',
        }),
      ],
    });

    expect(wrapper.find('.chat-thinking-block__body').exists()).toBe(false);
  });
});
