mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 13:44:03 +00:00
refactor(test): share temp home env harness
This commit is contained in:
@@ -1,55 +1,11 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { DEFAULT_AGENT_MAX_CONCURRENT, DEFAULT_SUBAGENT_MAX_CONCURRENT } from "./agent-limits.js";
|
||||
import { loadConfig } from "./config.js";
|
||||
|
||||
type HomeEnvSnapshot = {
|
||||
home: string | undefined;
|
||||
userProfile: string | undefined;
|
||||
homeDrive: string | undefined;
|
||||
homePath: string | undefined;
|
||||
stateDir: string | undefined;
|
||||
};
|
||||
|
||||
function snapshotHomeEnv(): HomeEnvSnapshot {
|
||||
return {
|
||||
home: process.env.HOME,
|
||||
userProfile: process.env.USERPROFILE,
|
||||
homeDrive: process.env.HOMEDRIVE,
|
||||
homePath: process.env.HOMEPATH,
|
||||
stateDir: process.env.OPENCLAW_STATE_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function restoreHomeEnv(snapshot: HomeEnvSnapshot) {
|
||||
const restoreKey = (key: string, value: string | undefined) => {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
};
|
||||
restoreKey("HOME", snapshot.home);
|
||||
restoreKey("USERPROFILE", snapshot.userProfile);
|
||||
restoreKey("HOMEDRIVE", snapshot.homeDrive);
|
||||
restoreKey("HOMEPATH", snapshot.homePath);
|
||||
restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir);
|
||||
}
|
||||
import { withTempHome } from "./home-env.test-harness.js";
|
||||
|
||||
describe("config identity defaults", () => {
|
||||
let fixtureRoot = "";
|
||||
let fixtureCount = 0;
|
||||
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-identity-"));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const writeAndLoadConfig = async (home: string, config: Record<string, unknown>) => {
|
||||
const configDir = path.join(home, ".openclaw");
|
||||
await fs.mkdir(configDir, { recursive: true });
|
||||
@@ -61,32 +17,8 @@ describe("config identity defaults", () => {
|
||||
return loadConfig();
|
||||
};
|
||||
|
||||
const withTempHome = async <T>(fn: (home: string) => Promise<T>): Promise<T> => {
|
||||
const home = path.join(fixtureRoot, `home-${fixtureCount++}`);
|
||||
await fs.mkdir(path.join(home, ".openclaw"), { recursive: true });
|
||||
|
||||
const snapshot = snapshotHomeEnv();
|
||||
process.env.HOME = home;
|
||||
process.env.USERPROFILE = home;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw");
|
||||
|
||||
if (process.platform === "win32") {
|
||||
const match = home.match(/^([A-Za-z]:)(.*)$/);
|
||||
if (match) {
|
||||
process.env.HOMEDRIVE = match[1];
|
||||
process.env.HOMEPATH = match[2] || "\\";
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await fn(home);
|
||||
} finally {
|
||||
restoreHomeEnv(snapshot);
|
||||
}
|
||||
};
|
||||
|
||||
it("does not derive mention defaults and only sets ackReactionScope when identity is present", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, {
|
||||
agents: {
|
||||
list: [
|
||||
@@ -111,7 +43,7 @@ describe("config identity defaults", () => {
|
||||
});
|
||||
|
||||
it("keeps ackReaction unset and does not synthesize agent/session defaults when identity is missing", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, { messages: {} });
|
||||
|
||||
expect(cfg.messages?.ackReaction).toBeUndefined();
|
||||
@@ -126,7 +58,7 @@ describe("config identity defaults", () => {
|
||||
});
|
||||
|
||||
it("does not override explicit values", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, {
|
||||
agents: {
|
||||
list: [
|
||||
@@ -152,7 +84,7 @@ describe("config identity defaults", () => {
|
||||
});
|
||||
|
||||
it("supports provider textChunkLimit config", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, {
|
||||
messages: {
|
||||
messagePrefix: "[openclaw]",
|
||||
@@ -184,7 +116,7 @@ describe("config identity defaults", () => {
|
||||
});
|
||||
|
||||
it("accepts blank model provider apiKey values", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, {
|
||||
models: {
|
||||
mode: "merge",
|
||||
@@ -219,7 +151,7 @@ describe("config identity defaults", () => {
|
||||
});
|
||||
|
||||
it("respects empty responsePrefix to disable identity defaults", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, {
|
||||
agents: {
|
||||
list: [
|
||||
@@ -241,7 +173,7 @@ describe("config identity defaults", () => {
|
||||
});
|
||||
|
||||
it("does not derive responsePrefix from identity emoji", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-identity-", async (home) => {
|
||||
const cfg = await writeAndLoadConfig(home, {
|
||||
agents: {
|
||||
list: [
|
||||
|
||||
64
src/config/home-env.test-harness.ts
Normal file
64
src/config/home-env.test-harness.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
type HomeEnvSnapshot = {
|
||||
home: string | undefined;
|
||||
userProfile: string | undefined;
|
||||
homeDrive: string | undefined;
|
||||
homePath: string | undefined;
|
||||
stateDir: string | undefined;
|
||||
};
|
||||
|
||||
function snapshotHomeEnv(): HomeEnvSnapshot {
|
||||
return {
|
||||
home: process.env.HOME,
|
||||
userProfile: process.env.USERPROFILE,
|
||||
homeDrive: process.env.HOMEDRIVE,
|
||||
homePath: process.env.HOMEPATH,
|
||||
stateDir: process.env.OPENCLAW_STATE_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function restoreHomeEnv(snapshot: HomeEnvSnapshot) {
|
||||
const restoreKey = (key: string, value: string | undefined) => {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
};
|
||||
restoreKey("HOME", snapshot.home);
|
||||
restoreKey("USERPROFILE", snapshot.userProfile);
|
||||
restoreKey("HOMEDRIVE", snapshot.homeDrive);
|
||||
restoreKey("HOMEPATH", snapshot.homePath);
|
||||
restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir);
|
||||
}
|
||||
|
||||
export async function withTempHome<T>(
|
||||
prefix: string,
|
||||
fn: (home: string) => Promise<T>,
|
||||
): Promise<T> {
|
||||
const home = await fs.mkdtemp(path.join(os.tmpdir(), prefix));
|
||||
await fs.mkdir(path.join(home, ".openclaw"), { recursive: true });
|
||||
|
||||
const snapshot = snapshotHomeEnv();
|
||||
process.env.HOME = home;
|
||||
process.env.USERPROFILE = home;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw");
|
||||
|
||||
if (process.platform === "win32") {
|
||||
const match = home.match(/^([A-Za-z]:)(.*)$/);
|
||||
if (match) {
|
||||
process.env.HOMEDRIVE = match[1];
|
||||
process.env.HOMEPATH = match[2] || "\\";
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await fn(home);
|
||||
} finally {
|
||||
restoreHomeEnv(snapshot);
|
||||
await fs.rm(home, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
@@ -1,84 +1,17 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { withTempHome } from "./home-env.test-harness.js";
|
||||
import { createConfigIO } from "./io.js";
|
||||
|
||||
type HomeEnvSnapshot = {
|
||||
home: string | undefined;
|
||||
userProfile: string | undefined;
|
||||
homeDrive: string | undefined;
|
||||
homePath: string | undefined;
|
||||
stateDir: string | undefined;
|
||||
};
|
||||
|
||||
function snapshotHomeEnv(): HomeEnvSnapshot {
|
||||
return {
|
||||
home: process.env.HOME,
|
||||
userProfile: process.env.USERPROFILE,
|
||||
homeDrive: process.env.HOMEDRIVE,
|
||||
homePath: process.env.HOMEPATH,
|
||||
stateDir: process.env.OPENCLAW_STATE_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function restoreHomeEnv(snapshot: HomeEnvSnapshot) {
|
||||
const restoreKey = (key: string, value: string | undefined) => {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
};
|
||||
restoreKey("HOME", snapshot.home);
|
||||
restoreKey("USERPROFILE", snapshot.userProfile);
|
||||
restoreKey("HOMEDRIVE", snapshot.homeDrive);
|
||||
restoreKey("HOMEPATH", snapshot.homePath);
|
||||
restoreKey("OPENCLAW_STATE_DIR", snapshot.stateDir);
|
||||
}
|
||||
|
||||
describe("config io write", () => {
|
||||
let fixtureRoot = "";
|
||||
let fixtureCount = 0;
|
||||
const silentLogger = {
|
||||
warn: () => {},
|
||||
error: () => {},
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
fixtureRoot = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-config-io-"));
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await fs.rm(fixtureRoot, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const withTempHome = async <T>(fn: (home: string) => Promise<T>): Promise<T> => {
|
||||
const home = path.join(fixtureRoot, `home-${fixtureCount++}`);
|
||||
await fs.mkdir(path.join(home, ".openclaw"), { recursive: true });
|
||||
|
||||
const snapshot = snapshotHomeEnv();
|
||||
process.env.HOME = home;
|
||||
process.env.USERPROFILE = home;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(home, ".openclaw");
|
||||
|
||||
if (process.platform === "win32") {
|
||||
const match = home.match(/^([A-Za-z]:)(.*)$/);
|
||||
if (match) {
|
||||
process.env.HOMEDRIVE = match[1];
|
||||
process.env.HOMEPATH = match[2] || "\\";
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return await fn(home);
|
||||
} finally {
|
||||
restoreHomeEnv(snapshot);
|
||||
}
|
||||
};
|
||||
|
||||
it("persists caller changes onto resolved config without leaking runtime defaults", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
@@ -119,7 +52,7 @@ describe("config io write", () => {
|
||||
});
|
||||
|
||||
it("preserves env var references when writing", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
@@ -178,7 +111,7 @@ describe("config io write", () => {
|
||||
});
|
||||
|
||||
it("keeps env refs in arrays when appending entries", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
@@ -251,7 +184,7 @@ describe("config io write", () => {
|
||||
});
|
||||
|
||||
it("logs an overwrite audit entry when replacing an existing config file", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||
await fs.writeFile(
|
||||
@@ -290,7 +223,7 @@ describe("config io write", () => {
|
||||
});
|
||||
|
||||
it("does not log an overwrite audit entry when creating config for the first time", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const warn = vi.fn();
|
||||
const io = createConfigIO({
|
||||
env: {} as NodeJS.ProcessEnv,
|
||||
@@ -313,7 +246,7 @@ describe("config io write", () => {
|
||||
});
|
||||
|
||||
it("appends config write audit JSONL entries with forensic metadata", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||
const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl");
|
||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||
@@ -358,7 +291,7 @@ describe("config io write", () => {
|
||||
});
|
||||
|
||||
it("records gateway watch session markers in config audit entries", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
await withTempHome("openclaw-config-io-", async (home) => {
|
||||
const configPath = path.join(home, ".openclaw", "openclaw.json");
|
||||
const auditPath = path.join(home, ".openclaw", "logs", "config-audit.jsonl");
|
||||
await fs.mkdir(path.dirname(configPath), { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user