Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217import { mkdtemp, readFile, readdir, rm, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, } from 'node:path';
import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';
import { overwrite, } from '../../dist/final/node/index.mjs';import { type AtomicTempFileWriter, writeFileAtomically,} from './write-atomic.ts';
//region Atomic write fixture helpers
/** Creates isolated temp directory for atomic write tests. @returns Temp directory path. @example ```ts const tempDir = await setup(); ``` */async function setup(): Promise<string> { return await mkdtemp(join( tmpdir(), 'file-enforcer-write-atomic-', ),);}
/** Removes isolated temp directory. @param tempDir - Directory returned by {@link setup}. @example ```ts await teardown(tempDir); ``` */async function teardown(tempDir: string,): Promise<void> { await rm( tempDir, { recursive: true, force: true, }, );}
/** Captures error thrown by atomic write fault injection. @param filePath - Destination path passed to atomic writer. @param content - Replacement content passed to atomic writer. @param tempFileWriter - Temp-file writer expected to throw. @returns Error thrown by atomic write. @throws When atomic write unexpectedly succeeds. @example ```ts const error = await captureAtomicWriteError({ filePath, content: 'new', tempFileWriter }); ``` */async function captureAtomicWriteError( { filePath, content, tempFileWriter, }: { readonly content: string; readonly filePath: string; readonly tempFileWriter: AtomicTempFileWriter; },): Promise<unknown> { try { await writeFileAtomically({ filePath, content, tempFileWriter, },); } catch (atomicWriteError: unknown) { return atomicWriteError; }
throw new Error('Atomic write unexpectedly succeeded',);}
/** Returns temp files left beside destination outputs. @param tempDir - Directory to inspect. @returns Directory entries ending with atomic temp suffix. @example ```ts const leftovers = await atomicTempEntries(tempDir); ``` */async function atomicTempEntries(tempDir: string,): Promise<readonly string[]> { /** Directory entries after attempted write. */ const entries = await readdir(tempDir,); return entries.filter(function isAtomicTempEntry(entry,): boolean { return entry.endsWith('.tmp',); },);}
//endregion Atomic write fixture helpers
await describe({ name: 'atomic destination writes', concurrency: 1, children: [ it({ name: 'preserves existing destination and removes temp file when temp write fails', fn: async function preservesExistingDestinationOnTempWriteFailure(): Promise<void> { const tempDir = await setup(); await using _cleanup = { [Symbol.asyncDispose](): Promise<void> { return teardown(tempDir,); }, }; const dest = join( tempDir, 'existing.txt', ); await writeFile( dest, 'old content', ); /** Fault-injection writer that leaves partial temp content before throwing. @param tempPath - Temp path provided by atomic writer. @example ```ts failAfterPartialTempWrite({ tempPath, content: 'new' }); ``` */ async function failAfterPartialTempWrite( { tempPath, }: Parameters<AtomicTempFileWriter>[0], ): Promise<void> { await writeFile( tempPath, 'partial new content', ); throw new Error('intentional atomic temp write failure',); }
const atomicWriteError = await captureAtomicWriteError({ filePath: dest, content: 'new content', tempFileWriter: failAfterPartialTempWrite, },);
expect(atomicWriteError,).toBeInstanceOf(Error,); expect(await readFile(dest, 'utf8',),).toBe('old content',); expect(await atomicTempEntries(tempDir,),).toEqual([],); }, },),
it({ name: 'public overwrite replaces content without leaving temp files', fn: async function publicOverwriteUsesAtomicDestinationReplacement(): Promise<void> { const tempDir = await setup(); await using _cleanup = { [Symbol.asyncDispose](): Promise<void> { return teardown(tempDir,); }, }; const dest = join( tempDir, 'public.txt', ); await writeFile( dest, 'old public content', );
await overwrite({ dest, content: 'new public content', manifestPath: join( tempDir, 'manifest.json', ), },);
expect(await readFile(dest, 'utf8',),).toBe('new public content',); expect(await atomicTempEntries(tempDir,),).toEqual([],); }, },), ],},);