返回 oh-my-ppt
style-switch-job-flow.test.ts
根目录 / tests / unit / generation / style-switch-job-flow.test.ts
1 import fs from 'fs'
2 import os from 'os'
3 import path from 'path'
4 import { afterEach, describe, expect, it } from 'vitest'
5 import {
6 readStyleSwitchFileSnapshot,
7 restoreStyleSwitchFileSnapshot
8 } from '../../../src/main/edit-jobs/style-switch-job-files'
9
10 const tempDirs: string[] = []
11
12 const createTempDir = async (): Promise<string> => {
13 const tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'style-switch-job-'))
14 tempDirs.push(tempDir)
15 return tempDir
16 }
17
18 afterEach(async () => {
19 await Promise.all(
20 tempDirs.splice(0).map((tempDir) => fs.promises.rm(tempDir, { recursive: true }))
21 )
22 })
23
24 describe('style-switch job flow', () => {
25 it('restores only the captured page file after a failed isolated page attempt', async () => {
26 const tempDir = await createTempDir()
27 const pagePath = path.join(tempDir, 'page-1.html')
28 const otherPagePath = path.join(tempDir, 'page-2.html')
29 await fs.promises.writeFile(pagePath, '<main>before</main>', 'utf8')
30 await fs.promises.writeFile(otherPagePath, '<main>other</main>', 'utf8')
31
32 const snapshot = await readStyleSwitchFileSnapshot(pagePath)
33 await fs.promises.writeFile(pagePath, '<main>changed</main>', 'utf8')
34 await restoreStyleSwitchFileSnapshot(pagePath, snapshot)
35
36 await expect(fs.promises.readFile(pagePath, 'utf8')).resolves.toBe('<main>before</main>')
37 await expect(fs.promises.readFile(otherPagePath, 'utf8')).resolves.toBe('<main>other</main>')
38 })
39
40 it('removes a file that did not exist in the captured snapshot', async () => {
41 const tempDir = await createTempDir()
42 const pagePath = path.join(tempDir, 'page-3.html')
43 const snapshot = await readStyleSwitchFileSnapshot(pagePath)
44 await fs.promises.writeFile(pagePath, '<main>unexpected</main>', 'utf8')
45
46 await restoreStyleSwitchFileSnapshot(pagePath, snapshot)
47
48 await expect(fs.promises.stat(pagePath)).rejects.toMatchObject({ code: 'ENOENT' })
49 })
50 })
51
51 lines TYPESCRIPT