| 1 | import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | |
| 3 | const state = vi.hoisted(() => ({ |
| 4 | enqueueHtmlThumbnail: vi.fn(), |
| 5 | enqueueHtmlThumbnails: vi.fn(), |
| 6 | getFreshHtmlThumbnailPaths: vi.fn() |
| 7 | })) |
| 8 | |
| 9 | vi.mock('../../../src/main/io/thumbnails/html-thumbnail-service', () => ({ |
| 10 | enqueueHtmlThumbnail: state.enqueueHtmlThumbnail, |
| 11 | enqueueHtmlThumbnails: state.enqueueHtmlThumbnails, |
| 12 | getFreshHtmlThumbnailPaths: state.getFreshHtmlThumbnailPaths |
| 13 | })) |
| 14 | |
| 15 | describe('HTML editor cover thumbnails', () => { |
| 16 | afterEach(() => vi.clearAllMocks()) |
| 17 | |
| 18 | it('returns cached covers and queues only documents without one', async () => { |
| 19 | state.getFreshHtmlThumbnailPaths.mockResolvedValue(new Map([['doc-1', '/cache/doc-1.png']])) |
| 20 | state.enqueueHtmlThumbnails.mockResolvedValue([]) |
| 21 | |
| 22 | const { warmHtmlEditorCoverThumbnails } = |
| 23 | await import('../../../src/main/html-editor/html-editor-thumbnail') |
| 24 | const result = await warmHtmlEditorCoverThumbnails([ |
| 25 | { id: 'doc-1', htmlPath: '/tmp/doc-1.html', designWidth: 1280 }, |
| 26 | { id: 'doc-2', htmlPath: '/tmp/doc-2.html', designWidth: 1440 }, |
| 27 | { id: ' ', htmlPath: '/tmp/doc-3.html', designWidth: 1280 } |
| 28 | ]) |
| 29 | |
| 30 | expect(result.get('doc-1')).toBe('/cache/doc-1.png') |
| 31 | expect(state.getFreshHtmlThumbnailPaths).toHaveBeenCalledWith([ |
| 32 | { |
| 33 | resourceType: 'html-editor', |
| 34 | resourceId: 'doc-1', |
| 35 | variant: 'cover', |
| 36 | sourcePath: '/tmp/doc-1.html', |
| 37 | captureWidth: 1280, |
| 38 | captureHeight: 720, |
| 39 | thumbnailWidth: 640, |
| 40 | thumbnailHeight: 360 |
| 41 | }, |
| 42 | { |
| 43 | resourceType: 'html-editor', |
| 44 | resourceId: 'doc-2', |
| 45 | variant: 'cover', |
| 46 | sourcePath: '/tmp/doc-2.html', |
| 47 | captureWidth: 1440, |
| 48 | captureHeight: 810, |
| 49 | thumbnailWidth: 640, |
| 50 | thumbnailHeight: 360 |
| 51 | } |
| 52 | ]) |
| 53 | expect(state.enqueueHtmlThumbnails).toHaveBeenCalledWith([ |
| 54 | { |
| 55 | resourceType: 'html-editor', |
| 56 | resourceId: 'doc-2', |
| 57 | variant: 'cover', |
| 58 | sourcePath: '/tmp/doc-2.html', |
| 59 | captureWidth: 1440, |
| 60 | captureHeight: 810, |
| 61 | thumbnailWidth: 640, |
| 62 | thumbnailHeight: 360 |
| 63 | } |
| 64 | ]) |
| 65 | }) |
| 66 | |
| 67 | it('forces a new capture after document content changes', async () => { |
| 68 | state.enqueueHtmlThumbnail.mockResolvedValue({ status: 'queued' }) |
| 69 | |
| 70 | const { refreshHtmlEditorCoverThumbnail } = |
| 71 | await import('../../../src/main/html-editor/html-editor-thumbnail') |
| 72 | refreshHtmlEditorCoverThumbnail({ |
| 73 | id: 'doc-1', |
| 74 | htmlPath: '/tmp/doc-1.html', |
| 75 | designWidth: 1280 |
| 76 | }) |
| 77 | |
| 78 | expect(state.enqueueHtmlThumbnail).toHaveBeenCalledWith( |
| 79 | { |
| 80 | resourceType: 'html-editor', |
| 81 | resourceId: 'doc-1', |
| 82 | variant: 'cover', |
| 83 | sourcePath: '/tmp/doc-1.html', |
| 84 | captureWidth: 1280, |
| 85 | captureHeight: 720, |
| 86 | thumbnailWidth: 640, |
| 87 | thumbnailHeight: 360 |
| 88 | }, |
| 89 | { force: true } |
| 90 | ) |
| 91 | }) |
| 92 | }) |
| 93 |