| 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('HTML editor message history', () => { |
| 19 | const roots: string[] = [] |
| 20 | |
| 21 | afterEach(async () => { |
| 22 | for (const root of roots.splice(0)) { |
| 23 | await rm(root, { recursive: true, force: true }) |
| 24 | } |
| 25 | }) |
| 26 | |
| 27 | it('persists messages and plan metadata by document id', async () => { |
| 28 | const root = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-html-editor-messages-')) |
| 29 | roots.push(root) |
| 30 | const db = new PPTDatabase(path.join(root, 'test.db')) |
| 31 | await db.init() |
| 32 | |
| 33 | try { |
| 34 | await db.createHtmlEditDocument({ |
| 35 | id: 'hedit-1', |
| 36 | title: 'Demo', |
| 37 | htmlPath: path.join(root, 'current.html'), |
| 38 | designWidth: 1280, |
| 39 | createdAt: 1, |
| 40 | updatedAt: 1 |
| 41 | }) |
| 42 | await db.createHtmlEditMessage({ |
| 43 | id: 'message-1', |
| 44 | docId: 'hedit-1', |
| 45 | role: 'user', |
| 46 | content: '把这个改为蓝色', |
| 47 | selectedElement: { |
| 48 | selector: 'body[data-page-id="hedit-1"] [data-block-id="title"]', |
| 49 | label: '页面标题', |
| 50 | elementTag: 'h1', |
| 51 | elementText: 'Demo' |
| 52 | }, |
| 53 | createdAt: 2 |
| 54 | }) |
| 55 | await db.createHtmlEditMessage({ |
| 56 | id: 'message-2', |
| 57 | docId: 'hedit-1', |
| 58 | role: 'assistant', |
| 59 | content: '已完成 HTML 改造。', |
| 60 | intent: 'style', |
| 61 | planJson: '{"intent":"style"}', |
| 62 | requiresConfirmation: false, |
| 63 | createdAt: 3 |
| 64 | }) |
| 65 | |
| 66 | await expect(db.listHtmlEditMessages('hedit-1')).resolves.toMatchObject([ |
| 67 | { |
| 68 | id: 'message-1', |
| 69 | role: 'user', |
| 70 | content: '把这个改为蓝色', |
| 71 | selectedSelector: 'body[data-page-id="hedit-1"] [data-block-id="title"]', |
| 72 | selectedLabel: '页面标题', |
| 73 | selectedElementTag: 'h1' |
| 74 | }, |
| 75 | { |
| 76 | id: 'message-2', |
| 77 | role: 'assistant', |
| 78 | intent: 'style', |
| 79 | planJson: '{"intent":"style"}' |
| 80 | } |
| 81 | ]) |
| 82 | } finally { |
| 83 | await db.close() |
| 84 | } |
| 85 | }) |
| 86 | |
| 87 | it('clears messages without affecting the document', async () => { |
| 88 | const root = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-html-editor-messages-')) |
| 89 | roots.push(root) |
| 90 | const db = new PPTDatabase(path.join(root, 'test.db')) |
| 91 | await db.init() |
| 92 | |
| 93 | try { |
| 94 | await db.createHtmlEditDocument({ |
| 95 | id: 'hedit-2', |
| 96 | title: 'Demo', |
| 97 | htmlPath: path.join(root, 'current.html'), |
| 98 | designWidth: 1280, |
| 99 | createdAt: 1, |
| 100 | updatedAt: 1 |
| 101 | }) |
| 102 | await db.createHtmlEditMessage({ |
| 103 | id: 'message-3', |
| 104 | docId: 'hedit-2', |
| 105 | role: 'user', |
| 106 | content: '测试', |
| 107 | createdAt: 2 |
| 108 | }) |
| 109 | |
| 110 | await db.clearHtmlEditMessages('hedit-2') |
| 111 | |
| 112 | await expect(db.listHtmlEditMessages('hedit-2')).resolves.toEqual([]) |
| 113 | await expect(db.getHtmlEditDocument('hedit-2')).resolves.toMatchObject({ title: 'Demo' }) |
| 114 | } finally { |
| 115 | await db.close() |
| 116 | } |
| 117 | }) |
| 118 | }) |
| 119 |