64 lines
2.0 KiB
JavaScript
64 lines
2.0 KiB
JavaScript
/**
|
|
* @file config-write.test.js
|
|
* @description Tests for the shared safe-write primitives (atomicWriteFile,
|
|
* createBackup, assertUnder) that cc-mutate.js and the Codex config writer
|
|
* both depend on.
|
|
* @author Nguyễn Ngọc Trí Vĩ <vinnt@smartgift.vn>
|
|
*/
|
|
|
|
const { describe, it } = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const os = require("node:os");
|
|
|
|
const { atomicWriteFile, createBackup, assertUnder } = require("../lib/config-write");
|
|
|
|
describe("atomicWriteFile", () => {
|
|
it("leaves no temp file behind after a successful write", () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cw-"));
|
|
atomicWriteFile(path.join(dir, "a.md"), "body");
|
|
assert.equal(fs.readFileSync(path.join(dir, "a.md"), "utf8"), "body");
|
|
assert.deepEqual(fs.readdirSync(dir), ["a.md"]);
|
|
});
|
|
});
|
|
|
|
describe("createBackup", () => {
|
|
it("backs a directory up as a tree, not a single file", () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cw-"));
|
|
fs.mkdirSync(path.join(dir, "src", "sk"), { recursive: true });
|
|
fs.writeFileSync(path.join(dir, "src", "sk", "SKILL.md"), "x");
|
|
const out = createBackup({
|
|
source: path.join(dir, "src", "sk"),
|
|
backupRoot: path.join(dir, "bak"),
|
|
type: "skills",
|
|
name: "sk",
|
|
});
|
|
assert.ok(fs.existsSync(path.join(out, "SKILL.md")));
|
|
});
|
|
|
|
it("returns null when the source does not exist", () => {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "cw-"));
|
|
const out = createBackup({
|
|
source: path.join(dir, "missing"),
|
|
backupRoot: path.join(dir, "bak"),
|
|
type: "agents",
|
|
name: "missing",
|
|
});
|
|
assert.equal(out, null);
|
|
});
|
|
});
|
|
|
|
describe("assertUnder", () => {
|
|
it("rejects a target outside the root", () => {
|
|
assert.throws(
|
|
() => assertUnder("/a/b", "/a/c/x"),
|
|
(e) => e.code === "EOUTOFROOT"
|
|
);
|
|
});
|
|
|
|
it("allows a target under the root", () => {
|
|
assert.doesNotThrow(() => assertUnder("/a/b", "/a/b/c"));
|
|
});
|
|
});
|