返回 oh-my-ppt
session-slide-size.test.ts
根目录 / tests / unit / db / session-slide-size.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 import { resolveSlideSize } from '../../../src/shared/slide-size'
18
19 describe('session slide size persistence', () => {
20 const roots: string[] = []
21
22 afterEach(async () => {
23 for (const root of roots.splice(0)) {
24 await rm(root, { recursive: true, force: true })
25 }
26 })
27
28 it('persists square 1:1 sessions with explicit dimensions', async () => {
29 const root = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-session-slide-size-'))
30 roots.push(root)
31 const db = new PPTDatabase(path.join(root, 'test.db'))
32 await db.init()
33
34 try {
35 const slideSize = resolveSlideSize({ id: 'square-1-1' })
36 const sessionId = await db.createSession({
37 title: 'Square card',
38 topic: 'Square card',
39 styleId: 'minimal-white',
40 pageCount: 1,
41 slideSizeId: slideSize.id,
42 slideWidth: slideSize.width,
43 slideHeight: slideSize.height,
44 provider: 'test',
45 model: 'test-model'
46 })
47
48 await expect(db.getSession(sessionId)).resolves.toMatchObject({
49 slideSizeId: 'square-1-1',
50 slideWidth: 1200,
51 slideHeight: 1200
52 })
53 } finally {
54 await db.close()
55 }
56 })
57
58 it('rejects session creation without explicit slide size fields', async () => {
59 const root = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-session-slide-size-'))
60 roots.push(root)
61 const db = new PPTDatabase(path.join(root, 'test.db'))
62 await db.init()
63
64 try {
65 await expect(
66 db.createSession({
67 title: 'Missing size',
68 topic: 'Missing size',
69 styleId: 'minimal-white',
70 pageCount: 1,
71 provider: 'test',
72 model: 'test-model'
73 })
74 ).rejects.toThrow('Invalid slide size id:')
75 } finally {
76 await db.close()
77 }
78 })
79 })
80
80 lines TYPESCRIPT