Files
reverse-engineering-anything/tests/boundary/userConfig.test.ts
T
8d6552e928 feat(config): persist provider settings in a user config file (#3023)
* feat(config): persist provider settings in a user config file

Providers currently rediscover install paths from scratch in every
process, so a path set once (for example GHIDRA_INSTALL_DIR) silently
reverts on the next run. Introduce ~/.config/rea/rea.json as the
user-owned persistence layer and merge it into the environment
snapshot before configuration parsing, keeping env > config file >
auto-detect precedence on both the CLI and MCP entry points.

Add a rea config command (list/get/set/unset) as the write surface,
reporting the effective value and where it came from.

* fix(config): address review findings on persistence safety and CLI surface

- stage config writes beside the file and rename into place so an
  interrupted write cannot leave partial JSON (greptile #4, codex #11)
- convert unreadable config files into configuration errors on the CLI
  seam instead of escaping raw filesystem errors (greptile #5, codex #15)
- keep injected runtimes without a config root isolated from the host
  account's rea.json (codex #18)
- surface the config file error on stderr when doctor falls back to the
  raw environment (greptile #3, codex #16)
- route the config command through logCliCommand and return documents so
  --json stays parseable and failures exit non-zero (greptile #2,
  codex #10)
- reject empty --set values, unknown --set/--unset/--get names, and --get
  combined with mutations (greptile #1/#7, codex #9/#12/#13/#17)
- cover the new behavior in the boundary tests and stop leaking one
  temporary home per run (greptile #8)

* fix(config): validate and back up persisted provider settings

* fix(config): keep saved provider defaults out of client registrations

* fix(config): preserve provider discovery and budgets for saved settings

* fix(config): check only settings needed by the IDA deadline lookup

* fix(config): share effective provider settings and verify config file reads

* fix(doctor): share selected settings with Hopper and ILSpy probes

* fix(config): resolve the destination of first-time saves

* fix(doctor): preserve unresolved provider location inputs

* fix(doctor): honor inherited IDA deadline overrides

* fix(config): show empty environment overrides

* fix(setup): preserve client-selected saved configuration roots

---------

Co-authored-by: Jackzigen <zigenhan@gmail.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-21 22:02:35 +00:00

384 lines
12 KiB
TypeScript

import {
mkdir,
readdir,
readFile,
rm,
symlink,
writeFile,
} from "node:fs/promises";
import { mkdtempSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, posix, win32 } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
applyUserConfigurationDefaults,
parseUserConfig,
} from "../../src/config/userConfig.js";
import { userConfigFilePath } from "../../src/application/UserConfigurationLocation.js";
import {
loadUserConfiguration,
loadUserConfigurationDefaults,
updateUserConfig,
} from "../../src/application/UserConfigurationStore.js";
const roots: string[] = [];
afterEach(async () => {
await Promise.all(
roots.splice(0).map((path) => rm(path, { recursive: true, force: true })),
);
});
describe("userConfigFilePath", () => {
it("prefers XDG_CONFIG_HOME when set to an absolute path", () => {
const path = userConfigFilePath(
{ XDG_CONFIG_HOME: "/custom/xdg" },
"darwin",
);
expect(path).toBe("/custom/xdg/rea/rea.json");
});
it("falls back to ~/.config when XDG_CONFIG_HOME is relative or empty", () => {
const home = "/home/custom";
expect(
userConfigFilePath({ HOME: home, XDG_CONFIG_HOME: "relative" }, "linux"),
).toBe(posix.join(home, ".config", "rea", "rea.json"));
expect(
userConfigFilePath({ HOME: home, XDG_CONFIG_HOME: "" }, "linux"),
).toBe(posix.join(home, ".config", "rea", "rea.json"));
});
it("resolves under USERPROFILE on Windows", () => {
const home = "C:\\Users\\custom";
expect(userConfigFilePath({ USERPROFILE: home }, "win32")).toBe(
win32.join(home, ".config", "rea", "rea.json"),
);
});
});
describe("parseUserConfig", () => {
it("accepts known provider settings with string values", () => {
const parsed = parseUserConfig({
GHIDRA_INSTALL_DIR: "/opt/ghidra",
JAVA_HOME: "/usr/lib/jvm",
});
expect(parsed).toEqual({
ok: true,
value: { GHIDRA_INSTALL_DIR: "/opt/ghidra", JAVA_HOME: "/usr/lib/jvm" },
});
});
it("rejects unknown keys so a typo'd edit fails loudly", () => {
const parsed = parseUserConfig({ GHIDRA_INSTALLL_DIR: "/opt/ghidra" });
expect(parsed.ok).toBe(false);
});
it("rejects non-string values", () => {
const parsed = parseUserConfig({ GHIDRA_INSTALL_DIR: 42 });
expect(parsed.ok).toBe(false);
});
it("rejects empty-string values so a saved empty setting cannot block startup", () => {
const parsed = parseUserConfig({ GHIDRA_INSTALL_DIR: "" });
expect(parsed.ok).toBe(false);
});
it("rejects non-object documents", () => {
expect(parseUserConfig(["not", "an object"]).ok).toBe(false);
expect(parseUserConfig(null).ok).toBe(false);
expect(parseUserConfig("string").ok).toBe(false);
});
});
describe("applyUserConfigurationDefaults", () => {
it("keeps explicit values and records only the defaults that filled a gap", () => {
const environment = { HOME: "/home/user", JAVA_HOME: "/from-env" };
const merged = applyUserConfigurationDefaults(environment, {
GHIDRA_INSTALL_DIR: "/from-config",
JAVA_HOME: "/from-config",
});
expect(merged.environment).toEqual({
...environment,
GHIDRA_INSTALL_DIR: "/from-config",
});
expect([...merged.savedDefaults]).toEqual(["GHIDRA_INSTALL_DIR"]);
expect(Object.isFrozen(merged.environment)).toBe(true);
});
});
describe("loadUserConfigurationDefaults", () => {
it("loads validated defaults from the selected config file", async () => {
const root = tempHome();
const path = userConfigFilePath({ HOME: root }, "darwin");
await mkdir(join(root, ".config", "rea"), { recursive: true });
await writeFile(
path,
JSON.stringify({ GHIDRA_INSTALL_DIR: "/opt/ghidra" }),
);
expect(
await loadUserConfigurationDefaults({ HOME: root }, "darwin"),
).toEqual({
ok: true,
value: { GHIDRA_INSTALL_DIR: "/opt/ghidra" },
});
});
it("loads no defaults when the file is missing", async () => {
expect(
await loadUserConfigurationDefaults({ HOME: tempHome() }, "darwin"),
).toEqual({
ok: true,
value: {},
});
});
it.skipIf(process.platform === "win32")(
"follows a regular config symlink for startup and refuses to edit the link",
async () => {
const root = tempHome();
const path = userConfigFilePath({ HOME: root }, "darwin");
const target = join(root, "selected.json");
const text = '{ "JAVA_HOME": "/linked" }\n';
await mkdir(join(root, ".config", "rea"), { recursive: true });
await writeFile(target, text);
await symlink(target, path);
expect(
await loadUserConfigurationDefaults({ HOME: root }, "darwin"),
).toEqual({
ok: true,
value: { JAVA_HOME: "/linked" },
});
await expect(
updateUserConfig({ HOME: root }, { JAVA_HOME: "/other" }, [], "darwin"),
).rejects.toThrow("regular JSON file");
expect(await readFile(target, "utf8")).toBe(text);
expect(await readdir(join(root, ".config", "rea"))).toEqual(["rea.json"]);
},
);
});
it.skipIf(process.platform === "win32")(
"edits and backs up the config selected through native symlink and parent traversal",
async () => {
const home = tempHome();
const lexical = join(home, "lexical");
const physical = join(home, "physical");
await mkdir(join(lexical, "rea"), { recursive: true });
await mkdir(join(physical, "child"), { recursive: true });
await mkdir(join(physical, "rea"), { recursive: true });
await symlink(join(physical, "child"), join(lexical, "alias"));
const selectedConfigHome = `${lexical}/alias/..`;
const selected = join(physical, "rea", "rea.json");
const decoy = join(lexical, "rea", "rea.json");
const selectedBytes = '{ "JAVA_HOME": "/selected-java" }\n';
const decoyBytes = '{ "JAVA_HOME": "/decoy-java" }\n';
await writeFile(selected, selectedBytes);
await writeFile(decoy, decoyBytes);
const environment = { HOME: home, XDG_CONFIG_HOME: selectedConfigHome };
expect(await loadUserConfigurationDefaults(environment)).toEqual({
ok: true,
value: { JAVA_HOME: "/selected-java" },
});
await updateUserConfig(environment, { JAVA_HOME: "/updated-java" }, []);
expect(JSON.parse(await readFile(selected, "utf8"))).toEqual({
JAVA_HOME: "/updated-java",
});
expect(await readFile(`${selected}.rea.backup`, "utf8")).toBe(
selectedBytes,
);
expect(await readFile(decoy, "utf8")).toBe(decoyBytes);
expect(await readdir(join(lexical, "rea"))).toEqual(["rea.json"]);
expect((await readdir(join(physical, "rea"))).sort()).toEqual([
"rea.json",
"rea.json.rea.backup",
]);
},
);
it
.skipIf(process.platform === "win32")
.for(["default", "native-parent"] as const)(
"creates the first saved config through a parent directory alias (%s)",
async (layout) => {
const home = tempHome();
const physical = join(home, "physical");
const lexical = join(home, "lexical");
const nested = layout === "native-parent";
const link = nested ? join(lexical, "alias") : join(home, ".config");
const target = nested ? join(physical, "child") : physical;
await mkdir(target, { recursive: true });
await mkdir(lexical);
await symlink(target, link);
const environment: NodeJS.ProcessEnv = nested
? { HOME: home, XDG_CONFIG_HOME: `${link}/..` }
: { HOME: home };
const settings = { JAVA_HOME: "/initial-java" };
const selectedDirectory = join(physical, "rea");
await expect(
readFile(join(selectedDirectory, "rea.json")),
).rejects.toMatchObject({
code: "ENOENT",
});
expect(await updateUserConfig(environment, settings, [])).toEqual({
settings,
removed: [],
});
expect(
JSON.parse(await readFile(join(selectedDirectory, "rea.json"), "utf8")),
).toEqual(settings);
expect(await readdir(selectedDirectory)).toEqual(["rea.json"]);
expect(await readdir(lexical)).toEqual(nested ? ["alias"] : []);
},
);
describe("updateUserConfig", () => {
it("creates the config file with sorted settings and a trailing newline", async () => {
const root = tempHome();
const saved = await updateUserConfig(
{ HOME: root },
{ JAVA_HOME: "/usr/lib/jvm", GHIDRA_INSTALL_DIR: "/opt/ghidra" },
[],
"darwin",
);
expect(saved.settings).toEqual({
GHIDRA_INSTALL_DIR: "/opt/ghidra",
JAVA_HOME: "/usr/lib/jvm",
});
const onDisk = await readFile(
join(root, ".config", "rea", "rea.json"),
"utf8",
);
expect(onDisk).toBe(
'{\n "GHIDRA_INSTALL_DIR": "/opt/ghidra",\n "JAVA_HOME": "/usr/lib/jvm"\n}\n',
);
});
it("merges into an existing config without dropping other keys", async () => {
const root = tempHome();
await updateUserConfig(
{ HOME: root },
{ GHIDRA_INSTALL_DIR: "/opt/ghidra" },
[],
"darwin",
);
const saved = await updateUserConfig(
{ HOME: root },
{ JAVA_HOME: "/usr/lib/jvm" },
[],
"darwin",
);
expect(saved.settings).toEqual({
GHIDRA_INSTALL_DIR: "/opt/ghidra",
JAVA_HOME: "/usr/lib/jvm",
});
});
it("unset removes only the named keys and reports what was removed", async () => {
const root = tempHome();
await updateUserConfig(
{ HOME: root },
{ GHIDRA_INSTALL_DIR: "/opt/ghidra", JAVA_HOME: "/usr/lib/jvm" },
[],
"darwin",
);
const { removed } = await updateUserConfig(
{ HOME: root },
{},
["GHIDRA_INSTALL_DIR"],
"darwin",
);
expect(removed).toEqual(["GHIDRA_INSTALL_DIR"]);
const onDisk = JSON.parse(
await readFile(join(root, ".config", "rea", "rea.json"), "utf8"),
);
expect(onDisk).toEqual({ JAVA_HOME: "/usr/lib/jvm" });
});
it("unset on a missing file is a no-op", async () => {
const { removed } = await updateUserConfig(
{ HOME: tempHome() },
{},
["JAVA_HOME"],
"darwin",
);
expect(removed).toEqual([]);
});
it("keeps the exact first snapshot and leaves no lock or staging entries", async () => {
const root = tempHome();
const path = userConfigFilePath({ HOME: root }, "darwin");
const configDir = join(root, ".config", "rea");
const original = Buffer.from(
'{ \t"GHIDRA_INSTALL_DIR": "/opt/ghidra" }\r\n',
);
await mkdir(configDir, { recursive: true });
await writeFile(path, original);
await updateUserConfig(
{ HOME: root },
{},
["GHIDRA_INSTALL_DIR"],
"darwin",
);
await updateUserConfig(
{ HOME: root },
{ JAVA_HOME: "/other" },
[],
"darwin",
);
expect((await readdir(configDir)).sort()).toEqual([
"rea.json",
"rea.json.rea.backup",
]);
expect((await readFile(`${path}.rea.backup`)).equals(original)).toBe(true);
expect(JSON.parse(await readFile(path, "utf8"))).toEqual({
JAVA_HOME: "/other",
});
});
it("rejects a native-invalid value and a second writer without changing the current file", async () => {
const root = tempHome();
const environment = { HOME: root };
const path = userConfigFilePath(environment, "darwin");
await updateUserConfig(
environment,
{ JAVA_HOME: "/original" },
[],
"darwin",
);
const original = await readFile(path, "utf8");
await expect(
updateUserConfig(environment, { JAVA_HOME: "relative" }, [], "darwin"),
).rejects.toMatchObject({ _tag: "ConfigurationError" });
await mkdir(`${path}.lock`);
await expect(
updateUserConfig(environment, { JAVA_HOME: "/other" }, [], "darwin"),
).rejects.toMatchObject({ _tag: "ConfigurationError" });
expect(await readFile(path, "utf8")).toBe(original);
await expect(readFile(`${path}.rea.backup`, "utf8")).rejects.toMatchObject({
code: "ENOENT",
});
});
});
describe("loadUserConfiguration", () => {
it("reports an unreadable config file as a configuration error, not a crash", async () => {
const root = tempHome();
await mkdir(join(root, ".config", "rea", "rea.json"), { recursive: true });
const result = await loadUserConfiguration({ HOME: root });
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.error.message).toContain("rea.json");
expect(result.error.message).toContain("could not be read");
}
});
});
function tempHome(): string {
const path = mkdtempSync(join(tmpdir(), "rea-user-config-"));
roots.push(path);
return path;
}