| 1 | import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' |
| 2 | import os from 'node:os' |
| 3 | import path from 'node:path' |
| 4 | import { afterEach, describe, expect, it } from 'vitest' |
| 5 | import { restorePageEditSnapshots } from '../../../src/main/edit-jobs/page-edit-rollback' |
| 6 | |
| 7 | describe('restorePageEditSnapshots', () => { |
| 8 | const roots: string[] = [] |
| 9 | |
| 10 | afterEach(async () => { |
| 11 | for (const root of roots.splice(0)) { |
| 12 | await rm(root, { recursive: true, force: true }) |
| 13 | } |
| 14 | }) |
| 15 | |
| 16 | it('continues restoring the remaining files when one rollback write fails', async () => { |
| 17 | const root = await mkdtemp(path.join(os.tmpdir(), 'ohmyppt-page-edit-rollback-')) |
| 18 | roots.push(root) |
| 19 | const restoredPath = path.join(root, 'page.html') |
| 20 | const missingParentPath = path.join(root, 'missing', 'index.html') |
| 21 | await writeFile(restoredPath, 'changed', 'utf-8') |
| 22 | |
| 23 | const failures = await restorePageEditSnapshots([ |
| 24 | { path: restoredPath, exists: true, content: 'original' }, |
| 25 | { path: missingParentPath, exists: true, content: 'original index' } |
| 26 | ]) |
| 27 | |
| 28 | await expect(readFile(restoredPath, 'utf-8')).resolves.toBe('original') |
| 29 | expect(failures).toHaveLength(1) |
| 30 | expect(failures[0]?.path).toBe(missingParentPath) |
| 31 | }) |
| 32 | }) |
| 33 |