import { mkdtempSync, readFileSync, rmSync, existsSync, statSync } from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' import { describe, expect, it } from 'vitest' import { writeJsonAtomic } from '../../src/commands/test/log-atomic.ts' describe('log atomic write', () => { it('replaces any existing file with the new payload and never leaves a 0-byte artifact', () => { const dir = mkdtempSync(join(tmpdir(), 'dacli-log-')) const target = join(dir, 'result.json') try { writeJsonAtomic(target, { attempt: 1, ok: false }) expect(statSync(target).size).toBeGreaterThan(0) writeJsonAtomic(target, { attempt: 2, ok: true, payload: { id: '852466054494945285', actionableTaskCreated: true, verifyExitCode: 0 } }) const body = readFileSync(target, 'utf-8') expect(body).toContain('"verifyExitCode": 0') expect(body).toContain('"actionableTaskCreated": true') expect(statSync(target).size).toBeGreaterThan(0) } finally { rmSync(dir, { recursive: true, force: true }) } }) it('creates the destination directory on first write', () => { const dir = mkdtempSync(join(tmpdir(), 'dacli-log-')) const target = join(dir, 'nested', 'deep', 'result.json') try { writeJsonAtomic(target, { ok: true }) expect(existsSync(target)).toBe(true) } finally { rmSync(dir, { recursive: true, force: true }) } }) })