mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-25 11:55:47 -06:00
fix: reuse doctor env snapshot helper
This commit is contained in:
@@ -10,6 +10,7 @@ import {
|
||||
resolveSessionTranscriptsDirForAgent,
|
||||
} from "../config/sessions/paths.js";
|
||||
import type { SessionEntry } from "../config/sessions/types.js";
|
||||
import { captureEnv, deleteTestEnvValue, setTestEnvValue } from "../test-utils/env.js";
|
||||
import {
|
||||
clearTuiLastSessionPointers,
|
||||
moveHeartbeatMainSessionEntry,
|
||||
@@ -29,35 +30,6 @@ vi.mock("../channels/plugins/persisted-auth-state.js", () => ({
|
||||
|
||||
const noteMock = vi.fn();
|
||||
|
||||
type EnvSnapshot = {
|
||||
HOME?: string;
|
||||
OPENCLAW_HOME?: string;
|
||||
OPENCLAW_STATE_DIR?: string;
|
||||
OPENCLAW_OAUTH_DIR?: string;
|
||||
OPENCLAW_AGENT_DIR?: string;
|
||||
};
|
||||
|
||||
function captureEnv(): EnvSnapshot {
|
||||
return {
|
||||
HOME: process.env.HOME,
|
||||
OPENCLAW_HOME: process.env.OPENCLAW_HOME,
|
||||
OPENCLAW_STATE_DIR: process.env.OPENCLAW_STATE_DIR,
|
||||
OPENCLAW_OAUTH_DIR: process.env.OPENCLAW_OAUTH_DIR,
|
||||
OPENCLAW_AGENT_DIR: process.env.OPENCLAW_AGENT_DIR,
|
||||
};
|
||||
}
|
||||
|
||||
function restoreEnv(snapshot: EnvSnapshot) {
|
||||
for (const key of Object.keys(snapshot) as Array<keyof EnvSnapshot>) {
|
||||
const value = snapshot[key];
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function setupSessionState(cfg: OpenClawConfig, env: NodeJS.ProcessEnv, homeDir: string) {
|
||||
const agentId = "main";
|
||||
const sessionsDir = resolveSessionTranscriptsDirForAgent(agentId, env, () => homeDir);
|
||||
@@ -149,23 +121,30 @@ async function runOrphanTranscriptCheckWithQmdSessions(enabled: boolean, homeDir
|
||||
}
|
||||
|
||||
describe("doctor state integrity oauth dir checks", () => {
|
||||
let envSnapshot: EnvSnapshot;
|
||||
let envSnapshot: ReturnType<typeof captureEnv>;
|
||||
let tempHome = "";
|
||||
|
||||
beforeEach(() => {
|
||||
envSnapshot = captureEnv();
|
||||
envSnapshot = captureEnv([
|
||||
"HOME",
|
||||
"OPENCLAW_HOME",
|
||||
"OPENCLAW_STATE_DIR",
|
||||
"OPENCLAW_OAUTH_DIR",
|
||||
"OPENCLAW_AGENT_DIR",
|
||||
]);
|
||||
tempHome = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-doctor-state-integrity-"));
|
||||
process.env.HOME = tempHome;
|
||||
process.env.OPENCLAW_HOME = tempHome;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(tempHome, ".openclaw");
|
||||
delete process.env.OPENCLAW_OAUTH_DIR;
|
||||
delete process.env.OPENCLAW_AGENT_DIR;
|
||||
fs.mkdirSync(process.env.OPENCLAW_STATE_DIR, { recursive: true, mode: 0o700 });
|
||||
const stateDir = path.join(tempHome, ".openclaw");
|
||||
setTestEnvValue("HOME", tempHome);
|
||||
setTestEnvValue("OPENCLAW_HOME", tempHome);
|
||||
setTestEnvValue("OPENCLAW_STATE_DIR", stateDir);
|
||||
deleteTestEnvValue("OPENCLAW_OAUTH_DIR");
|
||||
deleteTestEnvValue("OPENCLAW_AGENT_DIR");
|
||||
fs.mkdirSync(stateDir, { recursive: true, mode: 0o700 });
|
||||
noteMock.mockClear();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
restoreEnv(envSnapshot);
|
||||
envSnapshot.restore();
|
||||
fs.rmSync(tempHome, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
@@ -274,7 +253,7 @@ describe("doctor state integrity oauth dir checks", () => {
|
||||
"legacy",
|
||||
"agent",
|
||||
);
|
||||
process.env.OPENCLAW_AGENT_DIR = legacyAgentDir;
|
||||
setTestEnvValue("OPENCLAW_AGENT_DIR", legacyAgentDir);
|
||||
|
||||
const text = await runStateIntegrityText({
|
||||
agents: {
|
||||
@@ -467,9 +446,10 @@ describe("doctor state integrity oauth dir checks", () => {
|
||||
);
|
||||
fs.symlinkSync(originalHome, symlinkHome, "dir");
|
||||
try {
|
||||
process.env.HOME = symlinkHome;
|
||||
process.env.OPENCLAW_HOME = symlinkHome;
|
||||
process.env.OPENCLAW_STATE_DIR = path.join(symlinkHome, ".openclaw");
|
||||
const symlinkStateDir = path.join(symlinkHome, ".openclaw");
|
||||
setTestEnvValue("HOME", symlinkHome);
|
||||
setTestEnvValue("OPENCLAW_HOME", symlinkHome);
|
||||
setTestEnvValue("OPENCLAW_STATE_DIR", symlinkStateDir);
|
||||
|
||||
setupSessionState(cfg, process.env, symlinkHome);
|
||||
const sessionsDir = resolveSessionTranscriptsDirForAgent(
|
||||
|
||||
Reference in New Issue
Block a user