File size: 2,460 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
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/types.openclaw.js";

const mocks = vi.hoisted(() => ({
  resolveAgentDir: vi.fn((_cfg: OpenClawConfig, agentId: string) => `/tmp/agent-${agentId}`),
  getRuntimeConfigSourceSnapshot: vi.fn<() => OpenClawConfig | null>(() => null),
  getActiveSecretsRuntimeConfigSnapshot: vi.fn<
    typeof import("../../secrets/runtime-state.js").getActiveSecretsRuntimeConfigSnapshot
  >(() => null),
  prepareSecretsRuntimeSnapshot: vi.fn(async (_params: unknown) => ({ marker: "snapshot" })),
  activateSecretsRuntimeSnapshot: vi.fn(),
}));

vi.mock("../../agents/agent-scope.js", () => ({
  resolveAgentDir: mocks.resolveAgentDir,
}));

vi.mock("../../agents/auth-profiles/sqlite.js", () => ({
  assertAuthProfileStoreAgentOwner: vi.fn(),
}));

vi.mock("../../config/config.js", () => ({
  getRuntimeConfigSourceSnapshot: mocks.getRuntimeConfigSourceSnapshot,
}));

vi.mock("../../secrets/runtime-state.js", () => ({
  getActiveSecretsRuntimeConfigSnapshot: mocks.getActiveSecretsRuntimeConfigSnapshot,
}));

vi.mock("../../secrets/runtime.js", () => ({
  prepareSecretsRuntimeSnapshot: mocks.prepareSecretsRuntimeSnapshot,
  activateSecretsRuntimeSnapshot: mocks.activateSecretsRuntimeSnapshot,
}));

import { prepareLocalCapabilityAccountSecrets } from "./local-account-secrets.js";

describe("prepareLocalCapabilityAccountSecrets", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    mocks.getRuntimeConfigSourceSnapshot.mockReturnValue(null);
    mocks.getActiveSecretsRuntimeConfigSnapshot.mockReturnValue(null);
  });

  it("prepares only the selected agent auth store and preserves the source config", async () => {
    const cfg: OpenClawConfig = { agents: { defaults: { workspace: "/tmp/workspace" } } };
    const sourceConfig: OpenClawConfig = { secrets: { providers: {} } };
    mocks.getRuntimeConfigSourceSnapshot.mockReturnValue(sourceConfig);

    await prepareLocalCapabilityAccountSecrets({ cfg, agentId: "ops" });

    expect(mocks.resolveAgentDir).toHaveBeenCalledWith(cfg, "ops");
    expect(mocks.prepareSecretsRuntimeSnapshot).toHaveBeenCalledWith({
      config: sourceConfig,
      assignmentConfig: cfg,
      agentDirs: ["/tmp/agent-ops"],
      includeConfigRefs: false,
      allowUnavailableSecretOwners: true,
    });
    expect(mocks.activateSecretsRuntimeSnapshot).toHaveBeenCalledWith({ marker: "snapshot" });
  });
});