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

import {
  filterRefOptionsByContentType,
  filterRefOptionsByDataType,
} from './refOptionFilter';

describe('filterRefOptionsByContentType', () => {
  const options = [
    {
      label: '开始节点',
      selectable: false,
      value: 'start',
      children: [
        {
          label: '图片',
          selectable: true,
          value: 'start.image',
          contentType: 'image',
        },
        {
          label: '问题',
          selectable: true,
          value: 'start.user_input',
          contentType: 'text',
        },
      ],
    },
  ];

  it('图片输入仅展示图片类型引用', () => {
    const filtered = filterRefOptionsByContentType(options, ['image']);

    expect(filtered[0]?.children).toHaveLength(1);
    expect(filtered[0]?.children[0]?.value).toBe('start.image');
  });

  it('保留当前已选择的旧版非图片引用', () => {
    const filtered = filterRefOptionsByContentType(
      options,
      ['image'],
      'start.user_input',
    );

    expect(filtered[0]?.children.map((item: any) => item.value)).toEqual([
      'start.image',
      'start.user_input',
    ]);
  });
});

describe('filterRefOptionsByDataType', () => {
  const options = [
    {
      label: '知识库',
      selectable: false,
      value: 'knowledge',
      children: [
        {
          label: '文档',
          selectable: true,
          value: 'knowledge.documents',
          dataType: 'Array<Object>',
        },
        {
          label: '数量',
          selectable: true,
          value: 'knowledge.count',
          dataType: 'Number',
        },
        {
          label: '标题',
          selectable: true,
          value: 'knowledge.title',
          dataType: 'String',
        },
      ],
    },
  ];

  it('数组输入接受 Array 及 Array 泛型', () => {
    const filtered = filterRefOptionsByDataType(options, ['Array']);

    expect(filtered[0]?.children.map((item: any) => item.value)).toEqual([
      'knowledge.documents',
    ]);
  });

  it('次数输入只展示 Number，并保留当前旧引用', () => {
    const filtered = filterRefOptionsByDataType(
      options,
      ['Number'],
      'knowledge.title',
    );

    expect(filtered[0]?.children.map((item: any) => item.value)).toEqual([
      'knowledge.count',
      'knowledge.title',
    ]);
  });
});
