返回 oh-my-ppt
style-thumbnail-warmup.test.ts
根目录 / tests / unit / styles / style-thumbnail-warmup.test.ts
1 import fs from 'node:fs'
2 import os from 'node:os'
3 import path from 'node:path'
4 import { afterEach, describe, expect, it, vi } from 'vitest'
5
6 const state = vi.hoisted(() => ({
7 enqueueHtmlThumbnails: vi.fn(async () => [])
8 }))
9
10 vi.mock('../../../src/main/io/thumbnails/html-thumbnail-service', () => ({
11 enqueueHtmlThumbnails: state.enqueueHtmlThumbnails
12 }))
13
14 describe('warmStyleThumbnails', () => {
15 const roots: string[] = []
16
17 afterEach(() => {
18 state.enqueueHtmlThumbnails.mockClear()
19 for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true })
20 })
21
22 it('resolves builtin, user, and explicit package preview paths', async () => {
23 const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ohmyppt-style-warmup-'))
24 roots.push(root)
25 const previews = [
26 path.join(root, 'system', 'builtin-key', 'preview.html'),
27 path.join(root, 'user', 'custom-id', 'preview.html'),
28 path.join(root, 'overrides', 'special', 'preview.html')
29 ]
30 for (const previewPath of previews) {
31 fs.mkdirSync(path.dirname(previewPath), { recursive: true })
32 fs.writeFileSync(previewPath, '<!doctype html>')
33 }
34
35 const { warmStyleThumbnails } = await import('../../../src/main/styles/style-thumbnail-warmup')
36 await warmStyleThumbnails(
37 root,
38 [
39 { id: 'builtin-id', style: 'builtin-key', source: 'builtin' },
40 { id: 'custom-id', style: 'custom-key', source: 'custom' },
41 {
42 id: 'override-id',
43 style: 'override-key',
44 source: 'override',
45 packageDir: 'overrides/special'
46 },
47 { id: 'missing-id', style: 'missing-key', source: 'custom' }
48 ],
49 500
50 )
51
52 expect(state.enqueueHtmlThumbnails).toHaveBeenCalledWith(
53 [
54 { resourceType: 'style', resourceId: 'builtin-id', sourcePath: previews[0] },
55 { resourceType: 'style', resourceId: 'custom-id', sourcePath: previews[1] },
56 { resourceType: 'style', resourceId: 'override-id', sourcePath: previews[2] }
57 ],
58 { delayMs: 500 }
59 )
60 })
61 })
62
62 lines TYPESCRIPT