test: share auth profile env fixture

This commit is contained in:
Peter Steinberger
2026-04-19 03:19:57 +01:00
parent 22d99ee9df
commit 314654bd0f

View File

@@ -28,14 +28,14 @@ async function loadFreshAuthProfilesModuleForTest() {
await import("./auth-profiles.js")); await import("./auth-profiles.js"));
} }
function withAgentDirEnv(prefix: string, run: (agentDir: string) => void | Promise<void>) { async function withAgentDirEnv(prefix: string, run: (agentDir: string) => void | Promise<void>) {
const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR; const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR; const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
try { try {
process.env.OPENCLAW_AGENT_DIR = agentDir; process.env.OPENCLAW_AGENT_DIR = agentDir;
process.env.PI_CODING_AGENT_DIR = agentDir; process.env.PI_CODING_AGENT_DIR = agentDir;
return run(agentDir); await run(agentDir);
} finally { } finally {
if (previousAgentDir === undefined) { if (previousAgentDir === undefined) {
delete process.env.OPENCLAW_AGENT_DIR; delete process.env.OPENCLAW_AGENT_DIR;
@@ -132,31 +132,14 @@ describe("auth profile store cache", () => {
}); });
}); });
it("keeps runtime-only external auth out of persisted auth-profiles.json files", () => { it("keeps runtime-only external auth out of persisted auth-profiles.json files", async () => {
const agentDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-auth-store-missing-"));
const previousAgentDir = process.env.OPENCLAW_AGENT_DIR;
const previousPiAgentDir = process.env.PI_CODING_AGENT_DIR;
mocks.resolveExternalCliAuthProfiles.mockReturnValue([createRuntimeOnlyOverlay("access-1")]); mocks.resolveExternalCliAuthProfiles.mockReturnValue([createRuntimeOnlyOverlay("access-1")]);
try {
process.env.OPENCLAW_AGENT_DIR = agentDir;
process.env.PI_CODING_AGENT_DIR = agentDir;
await withAgentDirEnv("openclaw-auth-store-missing-", (agentDir) => {
const store = ensureAuthProfileStore(agentDir); const store = ensureAuthProfileStore(agentDir);
expect(store.profiles["openai-codex:default"]).toMatchObject({ access: "access-1" }); expect(store.profiles["openai-codex:default"]).toMatchObject({ access: "access-1" });
expect(fs.existsSync(path.join(agentDir, "auth-profiles.json"))).toBe(false); expect(fs.existsSync(path.join(agentDir, "auth-profiles.json"))).toBe(false);
} finally { });
if (previousAgentDir === undefined) {
delete process.env.OPENCLAW_AGENT_DIR;
} else {
process.env.OPENCLAW_AGENT_DIR = previousAgentDir;
}
if (previousPiAgentDir === undefined) {
delete process.env.PI_CODING_AGENT_DIR;
} else {
process.env.PI_CODING_AGENT_DIR = previousPiAgentDir;
}
fs.rmSync(agentDir, { recursive: true, force: true });
}
}); });
}); });