fix: finalize durable Pi lifecycle

This commit is contained in:
2026-08-04 21:34:08 +02:00
parent 5ba2821a1b
commit a368889838
20 changed files with 1016 additions and 147 deletions
+29 -2
View File
@@ -1,8 +1,13 @@
import { test, expect } from "vitest";
import { closeSync, fsyncSync, mkdtempSync, openSync, rmSync, writeFileSync } from "node:fs";
import { closeSync, existsSync, fsyncSync, mkdtempSync, openSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { loadSettings, saveSettings } from "../src/settings/settings-store.js";
import {
captureSettingsSnapshot,
loadSettings,
restoreSettingsSnapshot,
saveSettings,
} from "../src/settings/settings-store.js";
import { loadConfig } from "../src/config.js";
function cfgWith(file: string) {
@@ -70,3 +75,25 @@ test("saveSettings restores the previous file when post-rename directory durabil
rmSync(dir, { recursive: true, force: true });
}
});
test("settings snapshots restore exact absent and empty-file states", () => {
const dir = mkdtempSync(join(tmpdir(), "tht-settings-snapshot-"));
try {
const file = join(dir, "settings.json");
const cfg = cfgWith(file);
const absent = captureSettingsSnapshot(cfg);
expect(absent).toEqual({ exists: false, rawBase64: "" });
saveSettings(cfg, { provider: "new", model: "model", thinking: "high" });
restoreSettingsSnapshot(cfg, absent);
expect(existsSync(file)).toBe(false);
writeFileSync(file, Buffer.alloc(0), { mode: 0o600 });
const empty = captureSettingsSnapshot(cfg);
expect(empty.exists).toBe(true);
saveSettings(cfg, { provider: "new", model: "model", thinking: "high" });
restoreSettingsSnapshot(cfg, empty);
expect(readFileSync(file)).toEqual(Buffer.alloc(0));
} finally {
rmSync(dir, { recursive: true, force: true });
}
});