返回 oh-my-ppt
thumbnail-database-recovery.test.ts
根目录 / tests / unit / thumbnail / thumbnail-database-recovery.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 vi.mock('electron', () => ({ app: { getPath: vi.fn(() => os.tmpdir()) } }))
7 vi.mock('@electron-toolkit/utils', () => ({ is: { dev: true } }))
8
9 describe('thumbnail database recovery', () => {
10 const roots: string[] = []
11
12 afterEach(() => {
13 for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true })
14 })
15
16 it('marks interrupted queued and running tasks as failed', async () => {
17 const root = fs.mkdtempSync(path.join(os.tmpdir(), 'ohmyppt-thumbnail-db-'))
18 roots.push(root)
19 const { PPTDatabase } = await import('../../../src/main/db/database')
20 const db = new PPTDatabase(path.join(root, 'test.db'))
21 await db.init()
22 try {
23 for (const [resourceId, status] of [
24 ['queued-id', 'queued'],
25 ['running-id', 'running'],
26 ['completed-id', 'completed']
27 ] as const) {
28 await db.upsertThumbnailRecord({
29 resourceType: 'style',
30 resourceId,
31 variant: 'default',
32 sourcePath: `/styles/${resourceId}.html`,
33 sourceMtimeMs: 1,
34 signature: resourceId,
35 thumbnailPath: status === 'completed' ? `/thumbs/${resourceId}.png` : '',
36 status
37 })
38 }
39
40 await db.failInterruptedThumbnailTasks()
41
42 await expect(db.getThumbnailRecord('style', 'queued-id')).resolves.toMatchObject({
43 status: 'failed',
44 error: '应用退出时任务尚未完成'
45 })
46 await expect(db.getThumbnailRecord('style', 'running-id')).resolves.toMatchObject({
47 status: 'failed',
48 error: '应用退出时任务尚未完成'
49 })
50 await expect(db.getThumbnailRecord('style', 'completed-id')).resolves.toMatchObject({
51 status: 'completed'
52 })
53 } finally {
54 await db.close()
55 }
56 })
57 })
58
58 lines TYPESCRIPT