singularity-forge/packages/pi-coding-agent/src/core/fs-utils.test.ts
Mikael Hugo b62f7b20ec fix: convert node:test API calls to vitest equivalents
- t.after() → afterEach() with import injection
- t.before() → beforeEach() with import injection
- t.test() → test() (flatten subtests)
- t.skip() → return with skip comment
- Fix vitest.config.ts poolOptions deprecation for Vitest 4
- Run fix-vitest-api.mjs across 108 affected test files

💘 Generated with Crush

Assisted-by: GLM-5.1 via Crush <crush@charm.land>
2026-05-02 04:42:38 +02:00

54 lines
1.8 KiB
TypeScript

import assert from "node:assert/strict";
import { describe, it, afterEach } from 'vitest';
import { mkdtempSync, readFileSync, rmSync, existsSync } from "node:fs";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { atomicWriteFileSync } from "./fs-utils.js";
describe("atomicWriteFileSync", () => {
let dir: string;
afterEach(() => {
if (dir) {
rmSync(dir, { recursive: true, force: true });
}
});
it("writes file content atomically", () => {
dir = mkdtempSync(join(tmpdir(), "fs-utils-test-"));
const filePath = join(dir, "test.txt");
atomicWriteFileSync(filePath, "hello world");
assert.equal(readFileSync(filePath, "utf-8"), "hello world");
});
it("overwrites existing file atomically", () => {
dir = mkdtempSync(join(tmpdir(), "fs-utils-test-"));
const filePath = join(dir, "test.txt");
atomicWriteFileSync(filePath, "first");
atomicWriteFileSync(filePath, "second");
assert.equal(readFileSync(filePath, "utf-8"), "second");
});
it("does not leave .tmp file after successful write", () => {
dir = mkdtempSync(join(tmpdir(), "fs-utils-test-"));
const filePath = join(dir, "test.txt");
atomicWriteFileSync(filePath, "content");
assert.equal(existsSync(filePath + ".tmp"), false);
});
it("supports Buffer content", () => {
dir = mkdtempSync(join(tmpdir(), "fs-utils-test-"));
const filePath = join(dir, "test.bin");
const buf = Buffer.from([0x00, 0x01, 0x02, 0xff]);
atomicWriteFileSync(filePath, buf);
const result = readFileSync(filePath);
assert.deepEqual(result, buf);
});
it("supports encoding parameter", () => {
dir = mkdtempSync(join(tmpdir(), "fs-utils-test-"));
const filePath = join(dir, "test.txt");
atomicWriteFileSync(filePath, "utf8 content", "utf-8");
assert.equal(readFileSync(filePath, "utf-8"), "utf8 content");
});
});