Files
bDS/tests/renderer/plugins/dropImagePlugin.test.ts
Copilot b57e50f4a9 Drag-and-drop image insertion for Milkdown and Monaco editors (#47)
* Initial plan

* Implement drag-and-drop image insertion for both Milkdown and Monaco editors

Co-authored-by: rfc1437 <774975+rfc1437@users.noreply.github.com>

* Address code review: simplify Monaco type assertion, fix lint warning

Co-authored-by: rfc1437 <774975+rfc1437@users.noreply.github.com>

* feat: additional work on image drag-and-drop

* chore: updated documentation

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: rfc1437 <774975+rfc1437@users.noreply.github.com>
Co-authored-by: hugo <hugoms@me.com>
2026-03-12 21:04:45 +01:00

84 lines
3.2 KiB
TypeScript

/**
* Tests for the drop image plugin validation logic.
*/
import { describe, it, expect, vi } from 'vitest';
import { createImageImportPayload, hasImageFiles, SUPPORTED_IMAGE_EXTENSIONS } from '../../../src/renderer/plugins/dropImagePlugin';
function makeFile(name: string, type: string = 'application/octet-stream'): File {
return new File([''], name, { type });
}
describe('dropImagePlugin', () => {
describe('SUPPORTED_IMAGE_EXTENSIONS', () => {
it('should include common image formats', () => {
expect(SUPPORTED_IMAGE_EXTENSIONS.has('jpg')).toBe(true);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('jpeg')).toBe(true);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('png')).toBe(true);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('gif')).toBe(true);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('webp')).toBe(true);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('svg')).toBe(true);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('bmp')).toBe(true);
});
it('should not include non-image formats', () => {
expect(SUPPORTED_IMAGE_EXTENSIONS.has('pdf')).toBe(false);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('txt')).toBe(false);
expect(SUPPORTED_IMAGE_EXTENSIONS.has('mp4')).toBe(false);
});
});
describe('hasImageFiles', () => {
it('should return true for a single image file', () => {
expect(hasImageFiles([makeFile('photo.jpg')])).toBe(true);
expect(hasImageFiles([makeFile('image.PNG')])).toBe(true);
expect(hasImageFiles([makeFile('graphic.webp')])).toBe(true);
});
it('should return true for multiple image files', () => {
expect(hasImageFiles([makeFile('a.jpg'), makeFile('b.png'), makeFile('c.gif')])).toBe(true);
});
it('should return false for non-image files', () => {
expect(hasImageFiles([makeFile('document.pdf')])).toBe(false);
expect(hasImageFiles([makeFile('readme.txt')])).toBe(false);
});
it('should return false when mixed with non-image files', () => {
expect(hasImageFiles([makeFile('photo.jpg'), makeFile('doc.pdf')])).toBe(false);
});
it('should return true for screenshot-like clipboard files identified only by mime type', () => {
expect(hasImageFiles([makeFile('', 'image/png')])).toBe(true);
});
it('should return false for empty list', () => {
expect(hasImageFiles([])).toBe(false);
});
it('should return false for null-ish input', () => {
expect(hasImageFiles(null as unknown as File[])).toBe(false);
expect(hasImageFiles(undefined as unknown as File[])).toBe(false);
});
});
describe('createImageImportPayload', () => {
it('should build a buffer payload for screenshot-like clipboard images without a native path', async () => {
const screenshot = makeFile('', 'image/png');
const arrayBuffer = vi.fn().mockResolvedValue(new ArrayBuffer(0));
Object.defineProperty(screenshot, 'arrayBuffer', {
value: arrayBuffer,
});
const payload = await createImageImportPayload(screenshot);
expect(arrayBuffer).toHaveBeenCalledOnce();
expect(payload).toEqual({
kind: 'buffer',
fileName: 'pasted-image.png',
mimeType: 'image/png',
bytes: new Uint8Array(),
});
});
});
});