Files
agentdock/console/server/settings-store.test.js

39 lines
2.1 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { SettingsStore } from "./settings-store.js";
test("settings encrypt secrets and never expose full values", async () => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "agentdock-settings-"));
const file = path.join(directory, "settings.enc.json");
const store = new SettingsStore(file, "a secure test encryption key with 32 chars");
await store.init();
await store.setSecret("OPENAI_API_KEY", "sk-test-1234567890");
const raw = await fs.readFile(file, "utf8");
assert.equal(raw.includes("sk-test-1234567890"), false);
assert.equal(store.publicView().secrets.find((item) => item.name === "OPENAI_API_KEY").masked, "sk-...7890");
});
test("CC Switch ownership suppresses competing provider environment variables", async () => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "agentdock-settings-"));
const store = new SettingsStore(path.join(directory, "settings.enc.json"), "a secure test encryption key with 32 chars");
await store.init();
await store.setSecret("OPENAI_API_KEY", "sk-test-1234567890");
assert.equal(store.environmentForTool("codex").OPENAI_API_KEY, "sk-test-1234567890");
await store.setPreferences({ models: {}, ccSwitchManaged: true });
assert.deepEqual(store.environmentForTool("codex"), {});
assert.equal(store.environmentForTool("dsh").DEEPSEEK_API_KEY, undefined);
});
test("partial preference updates preserve CC Switch ownership", async () => {
const directory = await fs.mkdtemp(path.join(os.tmpdir(), "agentdock-settings-"));
const store = new SettingsStore(path.join(directory, "settings.enc.json"), "a secure test encryption key with 32 chars");
await store.init();
await store.setPreferences({ models: { codex: "gpt-test" }, ccSwitchManaged: true });
await store.setPreferences({ models: { claude: "sonnet-test" } });
assert.equal(store.publicView().preferences.ccSwitchManaged, true);
assert.deepEqual(store.publicView().preferences.models, { claude: "sonnet-test" });
});