pastedFiles.test.ts1.8 KBView on GitHub
/**
 * Tests: normalizePastedFiles — filename synthesis for clipboard pastes.
 *
 * Clipboard images (e.g. a copied screenshot) arrive as a File with an empty
 * `name`. normalizePastedFiles gives those a real filename so the attachment
 * chip and the allowlist's extension fallback have something to work with,
 * while leaving already-named files untouched.
 */

import { normalizePastedFiles } from '@/modules/files/upload/pastedFiles';

describe('normalizePastedFiles', () => {
  it('synthesizes a filename for a nameless pasted image from its mime subtype', () => {
    const pasted = new File(['x'], '', { type: 'image/png' });

    const [out] = normalizePastedFiles([pasted]);

    expect(out.name).toBe('pasted-image.png');
    expect(out.type).toBe('image/png');
  });

  it('derives the extension from the mime subtype (e.g. jpeg)', () => {
    const pasted = new File(['x'], '', { type: 'image/jpeg' });

    expect(normalizePastedFiles([pasted])[0].name).toBe('pasted-image.jpeg');
  });

  it('falls back to png when the mime type has no subtype', () => {
    const pasted = new File(['x'], '', { type: '' });

    expect(normalizePastedFiles([pasted])[0].name).toBe('pasted-image.png');
  });

  it('leaves an already-named file unchanged', () => {
    const named = new File(['x'], 'report.pdf', { type: 'application/pdf' });

    const [out] = normalizePastedFiles([named]);

    expect(out).toBe(named);
    expect(out.name).toBe('report.pdf');
  });

  it('normalizes a mixed batch, touching only the nameless entries', () => {
    const named = new File(['x'], 'notes.md', { type: 'text/markdown' });
    const nameless = new File(['y'], '', { type: 'image/webp' });

    const out = normalizePastedFiles([named, nameless]);

    expect(out[0]).toBe(named);
    expect(out[1].name).toBe('pasted-image.webp');
  });
});