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

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

import WorkflowFileInput from '../WorkflowFileInput.vue';

describe('workflow file input', () => {
  it('shows the upload area again after the uploaded file is deleted', async () => {
    const Host = defineComponent({
      components: { WorkflowFileInput },
      setup() {
        const value = ref([
          {
            fileName: '需求说明.pdf',
            filePath: '/files/requirements.pdf',
            size: 1024,
          },
        ]);
        return { value };
      },
      template: '<WorkflowFileInput v-model="value" />',
    });
    const wrapper = mount(Host);

    expect(wrapper.find('.workflow-file-input__dropzone').exists()).toBe(false);
    expect(wrapper.find('.workflow-file-input__summary').exists()).toBe(true);

    await wrapper.get('button[aria-label="删除文件"]').trigger('click');
    await nextTick();

    expect(wrapper.find('.workflow-file-input__summary').exists()).toBe(false);
    expect(wrapper.find('.workflow-file-input__dropzone').exists()).toBe(true);
  });

  it('hides the delete action and disables upload when parameters are locked', async () => {
    const wrapper = mount(WorkflowFileInput, {
      props: {
        disabled: true,
        modelValue: [
          {
            fileName: '需求说明.pdf',
            filePath: '/files/requirements.pdf',
            size: 1024,
          },
        ],
      },
    });

    expect(wrapper.find('button[aria-label="删除文件"]').exists()).toBe(false);
    expect(wrapper.find('.workflow-file-input__summary').exists()).toBe(true);

    await wrapper.setProps({ modelValue: [] });
    expect(
      wrapper.get('.workflow-file-input__upload-trigger').attributes(),
    ).toHaveProperty('disabled');
  });
});
