import { describe, expect, it } from 'vitest';
import { folderNameError } from './folderName';

describe('folderNameError', () => {
  it('accepts an ordinary name', () => {
    expect(folderNameError('Textures')).toBeNull();
    expect(folderNameError('  Textures  ')).toBeNull(); // trimmed
    expect(folderNameError('my.folder.v2')).toBeNull();
  });

  it('rejects an empty or dot-only name', () => {
    expect(folderNameError('')).toBe('Enter a folder name.');
    expect(folderNameError('   ')).toBe('Enter a folder name.');
    expect(folderNameError('.')).toBe('Enter a folder name.');
    expect(folderNameError('..')).toBe('Enter a folder name.');
  });

  it('rejects path separators and characters Windows forbids', () => {
    for (const name of ['a/b', 'a\\b', 'a:b', 'a*b', 'a?b', 'a"b', 'a<b', 'a>b', 'a|b']) {
      expect(folderNameError(name), name).toBe(
        'A folder name can\'t contain / \\ : * ? " < > or |.'
      );
    }
  });

  it('rejects a trailing dot, which Windows silently strips', () => {
    expect(folderNameError('Textures.')).toBe('A folder name can\'t end with a dot.');
  });

  it('rejects Windows device names on every platform', () => {
    // The workspace can be synced to a Windows machine, where such a path
    // cannot be created at all.
    expect(folderNameError('CON')).toBe('"CON" is a reserved name on Windows.');
    expect(folderNameError('nul')).toBe('"nul" is a reserved name on Windows.');
    expect(folderNameError('com4')).toBe('"com4" is a reserved name on Windows.');
    expect(folderNameError('LPT9')).toBe('"LPT9" is a reserved name on Windows.');
    // Reserved regardless of extension.
    expect(folderNameError('con.txt')).toBe('"con.txt" is a reserved name on Windows.');
    // …but only as a whole segment.
    expect(folderNameError('console')).toBeNull();
    expect(folderNameError('com10')).toBeNull();
  });

  it('rejects a name already present in the target folder', () => {
    const existing = new Set(['Textures', 'notes.txt']);
    expect(folderNameError('Textures', existing)).toBe(
      'An item named "Textures" already exists here.'
    );
    expect(folderNameError('notes.txt', existing)).toBe(
      'An item named "notes.txt" already exists here.'
    );
    expect(folderNameError('Meshes', existing)).toBeNull();
  });
});
