| 1 | import fs from 'fs' |
| 2 | import os from 'os' |
| 3 | import path from 'path' |
| 4 | import { afterEach, describe, expect, it } from 'vitest' |
| 5 | import { |
| 6 | importHtmlEditorMedia, |
| 7 | isSupportedHtmlEditorMediaFile, |
| 8 | listHtmlEditorMedia |
| 9 | } from '../../../src/main/html-editor/html-editor-media' |
| 10 | |
| 11 | const roots: string[] = [] |
| 12 | |
| 13 | afterEach(async () => { |
| 14 | await Promise.all( |
| 15 | roots.splice(0).map((root) => fs.promises.rm(root, { recursive: true, force: true })) |
| 16 | ) |
| 17 | }) |
| 18 | |
| 19 | describe('HTML editor media import', () => { |
| 20 | it('accepts only supported media extensions', () => { |
| 21 | expect(isSupportedHtmlEditorMediaFile('image', 'photo.WEBP')).toBe(true) |
| 22 | expect(isSupportedHtmlEditorMediaFile('video', 'clip.webm')).toBe(true) |
| 23 | expect(isSupportedHtmlEditorMediaFile('image', 'clip.mp4')).toBe(false) |
| 24 | expect(isSupportedHtmlEditorMediaFile('video', 'document.pdf')).toBe(false) |
| 25 | }) |
| 26 | |
| 27 | it('copies the selected media into the current document workspace and returns a file URL', async () => { |
| 28 | const root = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'html-editor-media-')) |
| 29 | roots.push(root) |
| 30 | const sourcePath = path.join(root, 'source image.png') |
| 31 | const workspaceDir = path.join(root, 'document') |
| 32 | await fs.promises.writeFile(sourcePath, 'media-data') |
| 33 | |
| 34 | const result = await importHtmlEditorMedia({ |
| 35 | workspaceDir, |
| 36 | sourcePath, |
| 37 | mediaType: 'image' |
| 38 | }) |
| 39 | |
| 40 | expect(result.relativePath).toMatch(/^assets\/images\/source image-[\w-]+\.png$/) |
| 41 | expect(result.url).toMatch(/^file:\/\//) |
| 42 | expect(result.filePath).toContain(path.join('document', 'assets', 'images')) |
| 43 | await expect(fs.promises.readFile(result.filePath, 'utf-8')).resolves.toBe('media-data') |
| 44 | |
| 45 | const imageAssets = await listHtmlEditorMedia({ workspaceDir, mediaType: 'image' }) |
| 46 | const videoAssets = await listHtmlEditorMedia({ workspaceDir, mediaType: 'video' }) |
| 47 | expect(imageAssets).toEqual([ |
| 48 | expect.objectContaining({ |
| 49 | filePath: result.filePath, |
| 50 | relativePath: result.relativePath, |
| 51 | url: result.url |
| 52 | }) |
| 53 | ]) |
| 54 | expect(videoAssets).toEqual([]) |
| 55 | }) |
| 56 | }) |
| 57 |