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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import { mkdtemp, readFile, rm, writeFile,} from 'node:fs/promises';import { tmpdir, } from 'node:os';import { join, resolve,} from 'node:path';import { reset, resetWriteTimestamps, writes, writeTimestamps,} from '../tracker.ts';import { overwriteTomlKey, } from './write-toml.ts';
/** Creates a fresh temp directory and resets tracker state */async function setup(prefix: string,): Promise<string> { const tempDir = await mkdtemp(join(tmpdir(), prefix,),); reset(); resetWriteTimestamps(); return tempDir;}
/** Removes the temp directory */async function teardown(tempDir: string,): Promise<void> { await rm(tempDir, { recursive: true, force: true, },);}
await describe({ name: overwriteTomlKey.name, children: [ it({ name: 'updates an existing scalar and preserves comments', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'pkg.toml',); /** Original TOML with comments and a value to update */ const original = '# Package metadata\nname = "old"\n# Tail comment\n'; await writeFile(dest, original,); await overwriteTomlKey({ dest, path: ['name',], value: 'new', },); const after = await readFile(dest, 'utf8',); expect(after.includes('name = "new"',),).toBe(true,); expect(after.includes('# Package metadata',),).toBe(true,); expect(after.includes('# Tail comment',),).toBe(true,); await teardown(tempDir,); }, },), it({ name: 'updates a value nested under a section header', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'config.toml',); await writeFile(dest, '[settings]\ntheme = "dark"\n',); await overwriteTomlKey({ dest, path: ['settings', 'theme',], value: 'light', },); const after = await readFile(dest, 'utf8',); expect(after.includes('theme = "light"',),).toBe(true,); expect(after.includes('[settings]',),).toBe(true,); await teardown(tempDir,); }, },), it({ name: 'creates a missing top-level key', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'add.toml',); await writeFile(dest, 'existing = "yes"\n',); await overwriteTomlKey({ dest, path: ['added',], value: 'value', },); const after = await readFile(dest, 'utf8',); expect(after.includes('added = "value"',),).toBe(true,); expect(after.includes('existing = "yes"',),).toBe(true,); await teardown(tempDir,); }, },), it({ name: 'updates an array-of-tables element', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'aot.toml',); await writeFile( dest, '[[fruits]]\nname = "apple"\n\n[[fruits]]\nname = "orange"\n', ); await overwriteTomlKey({ dest, path: ['fruits', 0, 'name',], value: 'banana', },); const after = await readFile(dest, 'utf8',); expect(after.includes('name = "banana"',),).toBe(true,); expect(after.includes('name = "orange"',),).toBe(true,); await teardown(tempDir,); }, },), it({ name: 'skips writeTimestamp when serialised text matches existing content', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'unchanged.toml',); /** Splice mode: writing the same value back yields identical bytes */ const source = 'name = "Alice"\n'; await writeFile(dest, source,); await overwriteTomlKey({ dest, path: ['name',], value: 'Alice', },); /** writeIfChanged should detect no diff and skip the write */ expect( writeTimestamps.has(resolve(dest,),), ).toBe(false,); await teardown(tempDir,); }, },), it({ name: 'registers dest in writes set even when the actual write is skipped', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'tracked.toml',); await writeFile(dest, 'name = "Alice"\n',); await overwriteTomlKey({ dest, path: ['name',], value: 'Alice', },); expect( writes.has(resolve(dest,),), ).toBe(true,); await teardown(tempDir,); }, },), it({ name: 'throws when the destination does not exist', fn: async () => { const tempDir = await setup('file-enforcer-toml-',); const dest = join(tempDir, 'missing.toml',); await expect(overwriteTomlKey({ dest, path: ['name',], value: 'value', },),) .rejects .toThrow(); await teardown(tempDir,); }, },), ],},);