File size: 2,320 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { afterEach, describe, expect, it } from "vitest";
import { resolveApiKeyForProfile } from "../../agents/auth-profiles/oauth.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import { clearSecretsRuntimeSnapshotState } from "../../secrets/runtime-state.js";
import { activateSecretsRuntimeSnapshot } from "../../secrets/runtime.js";
import {
  loadAuthStoreWithProfiles,
  setupSecretsRuntimeSnapshotTestHooks,
} from "../../secrets/runtime.test-support.ts";

const EMPTY_LOADABLE_PLUGIN_ORIGINS = new Map();
const { prepareSecretsRuntimeSnapshot } = setupSecretsRuntimeSnapshotTestHooks();

afterEach(() => {
  clearSecretsRuntimeSnapshotState();
});

describe("local model account secret isolation contract", () => {
  it("keeps a healthy selected profile usable while a sibling SecretRef is unavailable", async () => {
    const agentDir = "/tmp/openclaw-model-run-account-isolation";
    const coldProfileId = "openai:cold";
    const healthyProfileId = "anthropic:healthy";
    const cfg: OpenClawConfig = {};
    const store = loadAuthStoreWithProfiles({
      [coldProfileId]: {
        type: "api_key",
        provider: "openai",
        keyRef: { source: "env", provider: "default", id: "MISSING_OPENAI_PROFILE_KEY" },
      },
      [healthyProfileId]: {
        type: "api_key",
        provider: "anthropic",
        keyRef: { source: "env", provider: "default", id: "ANTHROPIC_PROFILE_KEY" },
      },
    });

    const snapshot = await prepareSecretsRuntimeSnapshot({
      config: cfg,
      env: { ANTHROPIC_PROFILE_KEY: "anthropic-runtime-key" },
      agentDirs: [agentDir],
      includeConfigRefs: false,
      allowUnavailableSecretOwners: true,
      loadablePluginOrigins: EMPTY_LOADABLE_PLUGIN_ORIGINS,
      loadAuthStore: () => store,
    });
    activateSecretsRuntimeSnapshot(snapshot);

    await expect(
      resolveApiKeyForProfile({
        cfg,
        store,
        profileId: healthyProfileId,
        agentDir,
      }),
    ).resolves.toMatchObject({
      apiKey: "anthropic-runtime-key",
      provider: "anthropic",
    });

    await expect(
      resolveApiKeyForProfile({
        cfg,
        store,
        profileId: coldProfileId,
        agentDir,
      }),
    ).rejects.toMatchObject({ code: "SECRET_SURFACE_UNAVAILABLE" });
  });
});