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

import {
  buildWorkflowImageValueFromResource,
  buildWorkflowImageValueFromUpload,
  buildWorkflowImageValueFromUrl,
  hasWorkflowImageValue,
  normalizeWorkflowImageValue,
  validateWorkflowImageFile,
} from '../workflowImageValue';

describe('workflowImageValue', () => {
  it('支持 URL、上传和素材图片值', () => {
    expect(buildWorkflowImageValueFromUrl('https://example.com/a.png')).toEqual({
      sourceType: 'url',
      url: 'https://example.com/a.png',
    });
    expect(
      buildWorkflowImageValueFromUpload(
        new File(['image'], 'a.png', { type: 'image/png' }),
        '/files/a.png',
      ),
    ).toMatchObject({ sourceType: 'upload', fileName: 'a.png' });
    expect(
      buildWorkflowImageValueFromResource({
        resourceName: 'asset',
        resourceType: 0,
        resourceUrl: '/files/asset.webp',
        suffix: 'webp',
      }),
    ).toMatchObject({ sourceType: 'resource', fileName: 'asset.webp' });
  });

  it('兼容旧值并校验必填', () => {
    expect(normalizeWorkflowImageValue('https://example.com/a.png')).toEqual({
      sourceType: 'url',
      url: 'https://example.com/a.png',
    });
    expect(
      hasWorkflowImageValue({
        fileName: 'legacy.jpg',
        filePath: '/files/legacy.jpg',
      }),
    ).toBe(true);
    expect(hasWorkflowImageValue(undefined)).toBe(false);
  });

  it('拒绝超限和非图片文件', () => {
    expect(() =>
      validateWorkflowImageFile(
        new File(
          [new Uint8Array(10 * 1024 * 1024 + 1)],
          'oversized.png',
          { type: 'image/png' },
        ),
      ),
    ).toThrow('单张图片不能超过 10 MiB');
    expect(() =>
      validateWorkflowImageFile(
        new File(['text'], 'note.txt', { type: 'text/plain' }),
      ),
    ).toThrow('仅支持 PNG、JPEG、WebP、GIF、BMP 图片');
  });
});
