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

import {
  getMakeFileNodeFormPatch,
  getMakeFileTargetFormatOptions,
  isMakeFileFormatCombinationAllowed,
  resolveMakeFileNodeTargetFormat,
  resolveMakeFileNodeTemplateStyle,
  sanitizeMakeFileNodeFileName,
} from '../makeFileNode';

describe('makeFileNode helpers', () => {
  it('清洗文件名中的非法字符与首尾空白', () => {
    expect(sanitizeMakeFileNodeFileName('  a/b\\\\c:*?<>|  ')).toBe('abc');
  });

  it('识别受支持与不受支持的格式组合', () => {
    expect(isMakeFileFormatCombinationAllowed('markdown', 'pdf')).toBe(true);
    expect(isMakeFileFormatCombinationAllowed('html', 'html')).toBe(true);
    expect(isMakeFileFormatCombinationAllowed('html', 'md')).toBe(false);
    expect(isMakeFileFormatCombinationAllowed('html', 'docx')).toBe(false);
  });

  it('按当前 targetFormat 回显目标格式', () => {
    expect(resolveMakeFileNodeTargetFormat({ targetFormat: 'pdf' })).toBe('pdf');
    expect(resolveMakeFileNodeTargetFormat({ targetFormat: 'html' })).toBe('html');
  });

  it('对未知 templateStyle 做默认回退', () => {
    expect(resolveMakeFileNodeTemplateStyle({ templateStyle: 'fancy' })).toBe('default');
  });

  it('在 html 源格式下仅暴露 html 与 pdf 目标格式', () => {
    expect(getMakeFileTargetFormatOptions('html').map((item) => item.value)).toEqual([
      'html',
      'pdf',
    ]);
  });

  it('在 sourceFormat 切换为 html 时自动修正非法目标格式', () => {
    expect(
      getMakeFileNodeFormPatch('sourceFormat', 'html', {
        sourceFormat: 'markdown',
        targetFormat: 'docx',
      }),
    ).toEqual({
      sourceFormat: 'html',
      targetFormat: 'html',
    });
  });
});
