import { test } from 'node:test' import assert from 'node:assert/strict' import { mkdtemp, readFile, readdir, writeFile as plainWrite, rm } from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import { writeFile } from '../server/atomic-write.js' test('replaces file contents and leaves no temp files', async () => { const dir = await mkdtemp(path.join(os.tmpdir(), 'atomic-')) const file = path.join(dir, 'a.json') await plainWrite(file, 'old') await writeFile(file, '{"ok":true}', 'utf8') assert.equal(await readFile(file, 'utf8'), '{"ok":true}') assert.deepEqual(await readdir(dir), ['a.json']) await rm(dir, { recursive: true }) }) test('a failed write keeps the old file and cleans up', async () => { const dir = await mkdtemp(path.join(os.tmpdir(), 'atomic-')) const file = path.join(dir, 'a.json') await plainWrite(file, 'old') await assert.rejects(() => writeFile(file, { not: 'a string' })) assert.equal(await readFile(file, 'utf8'), 'old') assert.deepEqual(await readdir(dir), ['a.json']) await rm(dir, { recursive: true }) })