import assert from 'node:assert/strict';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';

import { replaceFileAtomic } from './atomicReplace';

void test('replaceFileAtomic: replaces existing file without deleting before rename succeeds', async () => {
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dnd-replace-'));
  const finalPath = path.join(dir, 'out.bin');
  const tmpPath = path.join(dir, 'out.bin.tmp');
  await fs.writeFile(finalPath, 'previous', 'utf8');
  await fs.writeFile(tmpPath, 'updated-content', 'utf8');
  await replaceFileAtomic(tmpPath, finalPath);
  assert.equal(await fs.readFile(finalPath, 'utf8'), 'updated-content');
  await assert.rejects(() => fs.stat(tmpPath));
});

void test('replaceFileAtomic: rejects empty source', async () => {
  const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'dnd-replace-'));
  const finalPath = path.join(dir, 'out.bin');
  const tmpPath = path.join(dir, 'out.bin.tmp');
  await fs.writeFile(finalPath, 'x', 'utf8');
  await fs.writeFile(tmpPath, '', 'utf8');
  await assert.rejects(() => replaceFileAtomic(tmpPath, finalPath));
  assert.equal(await fs.readFile(finalPath, 'utf8'), 'x');
});
