返回 oh-my-ppt
style-favorite.test.ts
根目录 / tests / unit / styles / style-favorite.test.ts
1 import { mkdtemp, rm } from 'node:fs/promises'
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', () => ({
7 app: {
8 getPath: vi.fn(() => path.join(os.tmpdir(), 'ohmyppt-test-user-data'))
9 }
10 }))
11
12 vi.mock('@electron-toolkit/utils', () => ({
13 is: { dev: true }
14 }))
15
16 import { PPTDatabase } from '../../../src/main/db/database'
17
18 describe('style favorites', () => {
19 afterEach(() => {
20 vi.useRealTimers()
21 })
22
23 it('stores favoriteAt without bumping updatedAt', async () => {
24 const tmp = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-style-favorite-'))
25 const db = new PPTDatabase(path.join(tmp, 'test.db'))
26 try {
27 await db.init()
28 vi.useFakeTimers()
29 vi.setSystemTime(new Date('2026-06-25T00:00:00Z'))
30 const styleId = await db.createStyleRow({
31 id: 'favorite-style',
32 style: 'favorite-style',
33 styleName: 'Favorite Style',
34 description: 'A style used to test favorites',
35 category: '测试',
36 aliases: [],
37 source: 'custom',
38 styleSkill: '# Skill',
39 styleCase: '测试'
40 })
41 const before = await db.getStyleRow(styleId)
42 expect(before).toBeTruthy()
43
44 vi.setSystemTime(new Date('2026-06-25T00:05:00Z'))
45 const favoriteAt = Math.floor(Date.now() / 1000)
46 await expect(db.setStyleFavorite(styleId, favoriteAt)).resolves.toBe(favoriteAt)
47
48 const favorited = await db.getStyleRow(styleId)
49 expect(favorited?.favoriteAt).toBe(favoriteAt)
50 expect(favorited?.updatedAt).toBe(before?.updatedAt)
51
52 await expect(db.setStyleFavorite(styleId, null)).resolves.toBeNull()
53 const unfavorited = await db.getStyleRow(styleId)
54 expect(unfavorited?.favoriteAt).toBeNull()
55 expect(unfavorited?.updatedAt).toBe(before?.updatedAt)
56 } finally {
57 await db.close()
58 await rm(tmp, { recursive: true, force: true })
59 }
60 })
61 })
62
62 lines TYPESCRIPT