import fs from "node:fs/promises"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { ensureAuthProfileStore } from "../agents/auth-profiles.js"; import { withTempHome } from "../config/home-env.test-harness.js"; import { asConfig, beginSecretsRuntimeIsolationForTest, EMPTY_LOADABLE_PLUGIN_ORIGINS, endSecretsRuntimeIsolationForTest, OPENAI_ENV_KEY_REF, type SecretsRuntimeEnvSnapshot, } from "./runtime-auth.integration.test-helpers.js"; import { activateSecretsRuntimeSnapshot, prepareSecretsRuntimeSnapshot } from "./runtime.js"; vi.unmock("../version.js"); describe("secrets runtime snapshot auth integration", () => { let envSnapshot: SecretsRuntimeEnvSnapshot; beforeEach(() => { envSnapshot = beginSecretsRuntimeIsolationForTest(); }); afterEach(() => { endSecretsRuntimeIsolationForTest(envSnapshot); }); it("recomputes config-derived agent dirs when refreshing active secrets runtime snapshots", async () => { await withTempHome("openclaw-secrets-runtime-agent-dirs-", async (home) => { const mainAgentDir = path.join(home, ".openclaw", "agents", "main", "agent"); const opsAgentDir = path.join(home, ".openclaw", "agents", "ops", "agent"); await fs.mkdir(mainAgentDir, { recursive: true }); await fs.mkdir(opsAgentDir, { recursive: true }); await fs.writeFile( path.join(mainAgentDir, "auth-profiles.json"), `${JSON.stringify( { version: 1, profiles: { "openai:default": { type: "api_key", provider: "openai", keyRef: OPENAI_ENV_KEY_REF, }, }, }, null, 2, )}\n`, { encoding: "utf8", mode: 0o600 }, ); await fs.writeFile( path.join(opsAgentDir, "auth-profiles.json"), `${JSON.stringify( { version: 1, profiles: { "anthropic:ops": { type: "api_key", provider: "anthropic", keyRef: { source: "env", provider: "default", id: "ANTHROPIC_API_KEY" }, }, }, }, null, 2, )}\n`, { encoding: "utf8", mode: 0o600 }, ); const prepared = await prepareSecretsRuntimeSnapshot({ config: asConfig({}), env: { OPENAI_API_KEY: "sk-main-runtime", ANTHROPIC_API_KEY: "sk-ops-runtime", }, loadablePluginOrigins: EMPTY_LOADABLE_PLUGIN_ORIGINS, }); activateSecretsRuntimeSnapshot(prepared); expect(ensureAuthProfileStore(opsAgentDir).profiles["anthropic:ops"]).toBeUndefined(); const refreshed = await prepareSecretsRuntimeSnapshot({ config: asConfig({ agents: { list: [{ id: "ops", agentDir: opsAgentDir }], }, }), env: { OPENAI_API_KEY: "sk-main-runtime", ANTHROPIC_API_KEY: "sk-ops-runtime", }, loadablePluginOrigins: EMPTY_LOADABLE_PLUGIN_ORIGINS, }); activateSecretsRuntimeSnapshot(refreshed); expect(ensureAuthProfileStore(opsAgentDir).profiles["anthropic:ops"]).toMatchObject({ type: "api_key", key: "sk-ops-runtime", keyRef: { source: "env", provider: "default", id: "ANTHROPIC_API_KEY" }, }); }); }); });