|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import fs from 'node:fs'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { afterEach, expect, test, vi } from 'vitest'; |
| 6 | +import { isAtomicPublishTemporaryPath, publishDurableFileSync } from './atomic-file.ts'; |
| 7 | + |
| 8 | +const roots: string[] = []; |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + vi.restoreAllMocks(); |
| 12 | + for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); |
| 13 | +}); |
| 14 | + |
| 15 | +test('fsyncs complete contents before publication and uses the requested mode', () => { |
| 16 | + const root = fixtureRoot('ordering'); |
| 17 | + const destination = path.join(root, 'record.json'); |
| 18 | + const events: string[] = []; |
| 19 | + const realFsync = fs.fsyncSync; |
| 20 | + const realRename = fs.renameSync; |
| 21 | + vi.spyOn(fs, 'fsyncSync').mockImplementation((descriptor) => { |
| 22 | + events.push('fsync'); |
| 23 | + return realFsync(descriptor); |
| 24 | + }); |
| 25 | + vi.spyOn(fs, 'renameSync').mockImplementation((source, target) => { |
| 26 | + events.push('publish'); |
| 27 | + return realRename(source, target); |
| 28 | + }); |
| 29 | + |
| 30 | + publishDurableFileSync({ destination, contents: 'durable\n', mode: 0o640 }); |
| 31 | + |
| 32 | + expect(fs.readFileSync(destination, 'utf8')).toBe('durable\n'); |
| 33 | + expect(fs.statSync(destination).mode & 0o777).toBe(0o640); |
| 34 | + expect(events.indexOf('fsync')).toBeGreaterThanOrEqual(0); |
| 35 | + expect(events.indexOf('fsync')).toBeLessThan(events.indexOf('publish')); |
| 36 | + expect(temporaryPaths(root, destination)).toEqual([]); |
| 37 | +}); |
| 38 | + |
| 39 | +test.each(['symbolic link', 'non-regular path'] as const)( |
| 40 | + 'refuses a final %s and removes the temporary file', |
| 41 | + (kind) => { |
| 42 | + const root = fixtureRoot(kind); |
| 43 | + const destination = path.join(root, 'record.json'); |
| 44 | + if (kind === 'symbolic link') { |
| 45 | + const outside = path.join(root, 'outside.json'); |
| 46 | + fs.writeFileSync(outside, 'outside'); |
| 47 | + fs.symlinkSync(outside, destination); |
| 48 | + } else { |
| 49 | + fs.mkdirSync(destination); |
| 50 | + } |
| 51 | + |
| 52 | + expect(() => publishDurableFileSync({ destination, contents: 'replacement' })).toThrow( |
| 53 | + kind === 'symbolic link' ? /symbolic link/ : /not a regular file/, |
| 54 | + ); |
| 55 | + expect(temporaryPaths(root, destination)).toEqual([]); |
| 56 | + }, |
| 57 | +); |
| 58 | + |
| 59 | +test('keeps an existing destination on link-exclusive publication failure', () => { |
| 60 | + const root = fixtureRoot('exclusive'); |
| 61 | + const destination = path.join(root, 'record.json'); |
| 62 | + fs.writeFileSync(destination, 'original'); |
| 63 | + |
| 64 | + expect(() => |
| 65 | + publishDurableFileSync({ |
| 66 | + destination, |
| 67 | + contents: 'replacement', |
| 68 | + publish: 'link-exclusive', |
| 69 | + }), |
| 70 | + ).toThrow(/EEXIST/); |
| 71 | + expect(fs.readFileSync(destination, 'utf8')).toBe('original'); |
| 72 | + expect(temporaryPaths(root, destination)).toEqual([]); |
| 73 | +}); |
| 74 | + |
| 75 | +test('preserves the publication error while cleaning the temporary file', () => { |
| 76 | + const root = fixtureRoot('publish-error'); |
| 77 | + const destination = path.join(root, 'record.json'); |
| 78 | + const primary = new Error('publication failed'); |
| 79 | + vi.spyOn(fs, 'renameSync').mockImplementation(() => { |
| 80 | + throw primary; |
| 81 | + }); |
| 82 | + |
| 83 | + assert.throws( |
| 84 | + () => publishDurableFileSync({ destination, contents: 'durable' }), |
| 85 | + (error: unknown) => error === primary, |
| 86 | + ); |
| 87 | + expect(temporaryPaths(root, destination)).toEqual([]); |
| 88 | +}); |
| 89 | + |
| 90 | +test('preserves a file fsync error when descriptor cleanup also fails', () => { |
| 91 | + const root = fixtureRoot('close-error'); |
| 92 | + const destination = path.join(root, 'record.json'); |
| 93 | + const primary = new Error('file fsync failed'); |
| 94 | + const secondary = new Error('descriptor close failed'); |
| 95 | + const realClose = fs.closeSync; |
| 96 | + vi.spyOn(fs, 'fsyncSync').mockImplementation(() => { |
| 97 | + throw primary; |
| 98 | + }); |
| 99 | + vi.spyOn(fs, 'closeSync').mockImplementation((descriptor) => { |
| 100 | + realClose(descriptor); |
| 101 | + throw secondary; |
| 102 | + }); |
| 103 | + |
| 104 | + assert.throws( |
| 105 | + () => publishDurableFileSync({ destination, contents: 'durable' }), |
| 106 | + (error: unknown) => error === primary, |
| 107 | + ); |
| 108 | + expect(temporaryPaths(root, destination)).toEqual([]); |
| 109 | +}); |
| 110 | + |
| 111 | +function fixtureRoot(label: string): string { |
| 112 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), `agent-device-durable-file-${label}-`)); |
| 113 | + roots.push(root); |
| 114 | + return root; |
| 115 | +} |
| 116 | + |
| 117 | +function temporaryPaths(root: string, destination: string): string[] { |
| 118 | + return fs |
| 119 | + .readdirSync(root) |
| 120 | + .map((name) => path.join(root, name)) |
| 121 | + .filter((pathname) => isAtomicPublishTemporaryPath(pathname, destination)); |
| 122 | +} |
0 commit comments