- Docs: paragraph writing direction (LTR/RTL) toggle with auto-detection - Docs: hardened file lifecycle, docx fidelity, and editor performance - PDF: Export as Word via cloud conversion, non-destructive Save As, and an in-progress export dialog - Slides: treat full-page background shapes as the page background - Sheets: faster recalculation with a session-resident engine; better xlsx compatibility (builtin number formats, auto-fit row heights, sheet-name resolution) - AI: surface stream and gateway errors instead of finishing empty turns; parse non-SSE JSON gateway replies; stall watchdogs - Fixes from alpha feedback batches 3 and 4 (TOC levels, Windows menus, honest AI applies, login flow diagnostics, credits-exhausted notice) - CI: sheets save round-trip E2E and xlsx compatibility corpus gate - Start the 0.5 release train (v0.5.0/v0.5.1) Co-authored-by: GenOffice <merrick-2002@users.noreply.github.com>
74 lines
2.9 KiB
TypeScript
74 lines
2.9 KiB
TypeScript
import { mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
import { rename } from 'node:fs/promises'
|
|
import { tmpdir } from 'node:os'
|
|
import { join } from 'node:path'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { atomicWriteFile, looksLikeZip } from '../src/main/atomic-write'
|
|
|
|
vi.mock('node:fs/promises', async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import('node:fs/promises')>()
|
|
return { ...actual, rename: vi.fn(actual.rename) }
|
|
})
|
|
|
|
let dir: string
|
|
|
|
afterEach(() => {
|
|
rmSync(dir, { recursive: true, force: true })
|
|
vi.mocked(rename).mockClear()
|
|
})
|
|
|
|
const epermError = () =>
|
|
Object.assign(new Error('EPERM: operation not permitted'), { code: 'EPERM' })
|
|
|
|
describe('atomicWriteFile', () => {
|
|
it('replaces the target and leaves no temp file behind', async () => {
|
|
dir = mkdtempSync(join(tmpdir(), 'aw-'))
|
|
const target = join(dir, 'a.docx')
|
|
writeFileSync(target, 'old')
|
|
await atomicWriteFile(target, Buffer.from('new'))
|
|
expect(readFileSync(target, 'utf-8')).toBe('new')
|
|
expect(readdirSync(dir)).toEqual(['a.docx'])
|
|
})
|
|
|
|
it('keeps the original intact and cleans the temp file when the write fails', async () => {
|
|
dir = mkdtempSync(join(tmpdir(), 'aw-'))
|
|
const target = join(dir, 'a.docx')
|
|
writeFileSync(target, 'old')
|
|
const bad = { byteLength: -1 } as unknown as Buffer
|
|
await expect(atomicWriteFile(target, bad)).rejects.toThrow()
|
|
expect(readFileSync(target, 'utf-8')).toBe('old')
|
|
expect(readdirSync(dir)).toEqual(['a.docx'])
|
|
})
|
|
|
|
// Windows: Defender/indexer briefly locks the target and rename throws EPERM
|
|
it('retries a transiently locked rename and still lands atomically', async () => {
|
|
dir = mkdtempSync(join(tmpdir(), 'aw-'))
|
|
const target = join(dir, 'a.docx')
|
|
writeFileSync(target, 'old')
|
|
vi.mocked(rename).mockRejectedValueOnce(epermError()).mockRejectedValueOnce(epermError())
|
|
await atomicWriteFile(target, Buffer.from('new'))
|
|
expect(readFileSync(target, 'utf-8')).toBe('new')
|
|
expect(readdirSync(dir)).toEqual(['a.docx'])
|
|
})
|
|
|
|
it('falls back to an in-place write when the rename stays locked', async () => {
|
|
dir = mkdtempSync(join(tmpdir(), 'aw-'))
|
|
const target = join(dir, 'a.docx')
|
|
writeFileSync(target, 'old')
|
|
for (let i = 0; i < 5; i++) vi.mocked(rename).mockRejectedValueOnce(epermError())
|
|
await atomicWriteFile(target, Buffer.from('new'))
|
|
expect(readFileSync(target, 'utf-8')).toBe('new')
|
|
expect(readdirSync(dir)).toEqual(['a.docx'])
|
|
})
|
|
})
|
|
|
|
describe('looksLikeZip', () => {
|
|
it('accepts a zip local-file header and rejects truncated/garbage bytes', () => {
|
|
dir = mkdtempSync(join(tmpdir(), 'aw-'))
|
|
expect(looksLikeZip(Buffer.from([0x50, 0x4b, 0x03, 0x04, 0x00]))).toBe(true)
|
|
expect(looksLikeZip(Buffer.from([0x50, 0x4b]))).toBe(false)
|
|
expect(looksLikeZip(Buffer.from('not a zip'))).toBe(false)
|
|
expect(looksLikeZip(Buffer.alloc(0))).toBe(false)
|
|
})
|
|
})
|