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

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

import WorkflowSteps from '../WorkflowSteps.vue';

function createWorkflowNodes() {
  return [
    {
      key: 'node-a',
      label: '节点 A',
      original: {
        data: {},
        type: 'llmNode',
      },
    },
    {
      key: 'node-b',
      label: '节点 B',
      original: {
        data: {},
        type: 'llmNode',
      },
    },
  ];
}

function mountWorkflowSteps() {
  return mount(WorkflowSteps, {
    props: {
      initSignal: false,
      nodeJson: createWorkflowNodes(),
      pollingData: undefined,
      workflowId: 'workflow-1',
    },
    global: {
      stubs: {
        ConfirmItem: true,
        ConfirmItemMulti: true,
        ShowJson: true,
      },
    },
  });
}

function getCollapseItems(wrapper: ReturnType<typeof mountWorkflowSteps>) {
  return wrapper.findAll('.el-collapse-item');
}

describe('workflowSteps', () => {
  it('用户手动展开或关闭后，节点刷新和轮询不会覆盖当前选择', async () => {
    const wrapper = mountWorkflowSteps();

    await wrapper.setProps({
      pollingData: {
        nodes: {
          'node-a': { status: 1 },
        },
        status: 1,
      },
    });
    expect(getCollapseItems(wrapper)[0]?.classes()).toContain('is-active');

    await wrapper.setProps({
      pollingData: {
        nodes: {
          'node-a': { result: { text: 'A 完成' }, status: 20 },
          'node-b': { status: 1 },
        },
        status: 1,
      },
    });
    expect(getCollapseItems(wrapper)[0]?.classes()).not.toContain('is-active');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');

    await getCollapseItems(wrapper)[0]
      ?.get('.el-collapse-item__header')
      .trigger('click');
    expect(getCollapseItems(wrapper)[0]?.classes()).toContain('is-active');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');

    await wrapper.setProps({
      nodeJson: createWorkflowNodes(),
      pollingData: {
        nodes: {
          'node-a': { result: { text: 'A 完成' }, status: 20 },
          'node-b': { result: { text: 'B 完成' }, status: 20 },
        },
        status: 20,
      },
    });
    expect(getCollapseItems(wrapper)[0]?.classes()).toContain('is-active');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');

    await getCollapseItems(wrapper)[0]
      ?.get('.el-collapse-item__header')
      .trigger('click');
    expect(getCollapseItems(wrapper)[0]?.classes()).not.toContain('is-active');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');

    await wrapper.setProps({
      pollingData: {
        nodes: {
          'node-a': { message: '节点执行失败', status: 21 },
          'node-b': { result: { text: 'B 完成' }, status: 20 },
        },
        status: 21,
      },
    });
    expect(getCollapseItems(wrapper)[0]?.classes()).not.toContain('is-active');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');
  });

  it('新一轮试运行会清除手动选择并恢复自动跟随', async () => {
    const wrapper = mountWorkflowSteps();

    await getCollapseItems(wrapper)[1]
      ?.get('.el-collapse-item__header')
      .trigger('click');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');

    await wrapper.setProps({ initSignal: true });
    expect(
      getCollapseItems(wrapper).every((item) => !item.classes('is-active')),
    ).toBe(true);

    await wrapper.setProps({
      pollingData: {
        nodes: {
          'node-a': { status: 1 },
        },
        status: 1,
      },
    });
    expect(getCollapseItems(wrapper)[0]?.classes()).toContain('is-active');
  });

  it('工作流失败时优先展开失败节点并展示错误信息', async () => {
    const wrapper = mountWorkflowSteps();

    await wrapper.setProps({
      pollingData: {
        message: 'JavaScript 语法错误（第 2 行，第 3 列）：Unexpected token',
        nodes: {
          'node-a': { result: { text: 'A 完成' }, status: 20 },
          'node-b': {
            message:
              'JavaScript 语法错误（第 2 行，第 3 列）：Unexpected token',
            status: 21,
          },
        },
        status: 21,
      },
    });

    expect(getCollapseItems(wrapper)[0]?.classes()).not.toContain('is-active');
    expect(getCollapseItems(wrapper)[1]?.classes()).toContain('is-active');
    expect(wrapper.text()).toContain(
      'JavaScript 语法错误（第 2 行，第 3 列）：Unexpected token',
    );
  });
});
