Files
ThothII/backend/test/settings-store.test.ts
T

48 lines
1.8 KiB
TypeScript

import { test, expect } from "vitest";
import { mkdtempSync, 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 { loadConfig } from "../src/config.js";
function cfgWith(file: string) {
return loadConfig({ THT_HARNESS_DIR: "../harness", SETTINGS_FILE: file });
}
test("loadSettings returns {} when the file does not exist", () => {
const dir = mkdtempSync(join(tmpdir(), "tht-set-"));
try {
expect(loadSettings(cfgWith(join(dir, "settings.json")))).toEqual({});
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("saveSettings writes the file and loadSettings reads it back", () => {
const dir = mkdtempSync(join(tmpdir(), "tht-set-"));
try {
const cfg = cfgWith(join(dir, "nested", "settings.json"));
const saved = saveSettings(cfg, { workspace: "psd", provider: "zai", model: "glm-5.2", thinking: "medium" });
expect(saved.model).toBe("glm-5.2");
expect(loadSettings(cfg)).toEqual({ workspace: "psd", provider: "zai", model: "glm-5.2", thinking: "medium" });
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("loadSettings returns {} on corrupt JSON (no throw)", () => {
const dir = mkdtempSync(join(tmpdir(), "tht-set-"));
try {
const file = join(dir, "settings.json");
writeFileSync(file, "{ not json");
expect(loadSettings(cfgWith(file))).toEqual({});
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("loadConfig sets settingsFile from SETTINGS_FILE, default data/settings.json", () => {
expect(loadConfig({}).settingsFile).toBe("data/settings.json");
expect(loadConfig({ SETTINGS_FILE: "/x/y.json" }).settingsFile).toBe("/x/y.json");
});