|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { OpenClawConfig } from "../../config/config.js"; |
| 3 | + |
| 4 | +const hoisted = vi.hoisted(() => ({ |
| 5 | + resolveCommandSecretRefsViaGatewayMock: vi.fn(), |
| 6 | + getScopedChannelsCommandSecretTargetsMock: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +vi.mock("../../cli/command-secret-gateway.js", () => ({ |
| 10 | + resolveCommandSecretRefsViaGateway: (...args: unknown[]) => |
| 11 | + hoisted.resolveCommandSecretRefsViaGatewayMock(...args), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../../cli/command-secret-targets.js", () => ({ |
| 15 | + getAgentRuntimeCommandSecretTargetIds: () => new Set(["skills.entries.*.apiKey"]), |
| 16 | + getScopedChannelsCommandSecretTargets: (...args: unknown[]) => |
| 17 | + hoisted.getScopedChannelsCommandSecretTargetsMock(...args), |
| 18 | +})); |
| 19 | + |
| 20 | +const { resolveQueuedReplyExecutionConfig } = await import("./agent-runner-utils.js"); |
| 21 | +const { clearRuntimeConfigSnapshot, setRuntimeConfigSnapshot } = |
| 22 | + await import("../../config/config.js"); |
| 23 | + |
| 24 | +describe("resolveQueuedReplyExecutionConfig channel scope", () => { |
| 25 | + beforeEach(() => { |
| 26 | + clearRuntimeConfigSnapshot(); |
| 27 | + hoisted.resolveCommandSecretRefsViaGatewayMock |
| 28 | + .mockReset() |
| 29 | + .mockImplementation(async ({ config }) => ({ |
| 30 | + resolvedConfig: config, |
| 31 | + diagnostics: [], |
| 32 | + targetStatesByPath: {}, |
| 33 | + hadUnresolvedTargets: false, |
| 34 | + })); |
| 35 | + hoisted.getScopedChannelsCommandSecretTargetsMock.mockReset().mockReturnValue({ |
| 36 | + targetIds: new Set(["channels.discord.token"]), |
| 37 | + allowedPaths: new Set(["channels.discord.token", "channels.discord.accounts.work.token"]), |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + afterEach(() => { |
| 42 | + clearRuntimeConfigSnapshot(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("resolves base runtime targets, then active channel/account targets from originating context", async () => { |
| 46 | + const sourceConfig = { source: true } as unknown as OpenClawConfig; |
| 47 | + const baseResolved = { baseResolved: true } as unknown as OpenClawConfig; |
| 48 | + const scopedResolved = { scopedResolved: true } as unknown as OpenClawConfig; |
| 49 | + hoisted.resolveCommandSecretRefsViaGatewayMock |
| 50 | + .mockResolvedValueOnce({ |
| 51 | + resolvedConfig: baseResolved, |
| 52 | + diagnostics: [], |
| 53 | + targetStatesByPath: {}, |
| 54 | + hadUnresolvedTargets: false, |
| 55 | + }) |
| 56 | + .mockResolvedValueOnce({ |
| 57 | + resolvedConfig: scopedResolved, |
| 58 | + diagnostics: [], |
| 59 | + targetStatesByPath: {}, |
| 60 | + hadUnresolvedTargets: false, |
| 61 | + }); |
| 62 | + |
| 63 | + const resolved = await resolveQueuedReplyExecutionConfig(sourceConfig, { |
| 64 | + originatingChannel: "discord", |
| 65 | + messageProvider: "slack", |
| 66 | + originatingAccountId: "work", |
| 67 | + agentAccountId: "default", |
| 68 | + }); |
| 69 | + |
| 70 | + expect(resolved).toBe(scopedResolved); |
| 71 | + expect(hoisted.resolveCommandSecretRefsViaGatewayMock).toHaveBeenCalledTimes(2); |
| 72 | + const baseCall = hoisted.resolveCommandSecretRefsViaGatewayMock.mock.calls[0]?.[0] as { |
| 73 | + config: OpenClawConfig; |
| 74 | + commandName: string; |
| 75 | + targetIds: Set<string>; |
| 76 | + }; |
| 77 | + expect(baseCall.config).toBe(sourceConfig); |
| 78 | + expect(baseCall.commandName).toBe("reply"); |
| 79 | + expect(baseCall.targetIds).toEqual(new Set(["skills.entries.*.apiKey"])); |
| 80 | + expect(hoisted.getScopedChannelsCommandSecretTargetsMock).toHaveBeenCalledWith({ |
| 81 | + config: baseResolved, |
| 82 | + channel: "discord", |
| 83 | + accountId: "work", |
| 84 | + }); |
| 85 | + const scopedCall = hoisted.resolveCommandSecretRefsViaGatewayMock.mock.calls[1]?.[0] as { |
| 86 | + config: OpenClawConfig; |
| 87 | + commandName: string; |
| 88 | + targetIds: Set<string>; |
| 89 | + allowedPaths?: Set<string>; |
| 90 | + }; |
| 91 | + expect(scopedCall.config).toBe(baseResolved); |
| 92 | + expect(scopedCall.commandName).toBe("reply"); |
| 93 | + expect(scopedCall.targetIds).toEqual(new Set(["channels.discord.token"])); |
| 94 | + expect(scopedCall.allowedPaths).toEqual( |
| 95 | + new Set(["channels.discord.token", "channels.discord.accounts.work.token"]), |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it("falls back to messageProvider and agentAccountId when originating values are missing", async () => { |
| 100 | + const sourceConfig = { source: true } as unknown as OpenClawConfig; |
| 101 | + |
| 102 | + await resolveQueuedReplyExecutionConfig(sourceConfig, { |
| 103 | + messageProvider: "discord", |
| 104 | + agentAccountId: "ops", |
| 105 | + }); |
| 106 | + |
| 107 | + expect(hoisted.getScopedChannelsCommandSecretTargetsMock).toHaveBeenCalledWith({ |
| 108 | + config: sourceConfig, |
| 109 | + channel: "discord", |
| 110 | + accountId: "ops", |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it("skips scoped channel resolution when no active channel can be resolved", async () => { |
| 115 | + const sourceConfig = { source: true } as unknown as OpenClawConfig; |
| 116 | + |
| 117 | + const resolved = await resolveQueuedReplyExecutionConfig(sourceConfig); |
| 118 | + |
| 119 | + expect(resolved).toBe(sourceConfig); |
| 120 | + expect(hoisted.resolveCommandSecretRefsViaGatewayMock).toHaveBeenCalledTimes(1); |
| 121 | + expect(hoisted.getScopedChannelsCommandSecretTargetsMock).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | + |
| 124 | + it("prefers the runtime snapshot as the base config for secret resolution", async () => { |
| 125 | + const sourceConfig = { source: true } as unknown as OpenClawConfig; |
| 126 | + const runtimeConfig = { runtime: true } as unknown as OpenClawConfig; |
| 127 | + setRuntimeConfigSnapshot(runtimeConfig, sourceConfig); |
| 128 | + hoisted.getScopedChannelsCommandSecretTargetsMock.mockReturnValue({ |
| 129 | + targetIds: new Set<string>(), |
| 130 | + }); |
| 131 | + |
| 132 | + await resolveQueuedReplyExecutionConfig(sourceConfig, { |
| 133 | + messageProvider: "discord", |
| 134 | + }); |
| 135 | + |
| 136 | + const baseCall = hoisted.resolveCommandSecretRefsViaGatewayMock.mock.calls[0]?.[0] as { |
| 137 | + config: OpenClawConfig; |
| 138 | + commandName: string; |
| 139 | + }; |
| 140 | + expect(baseCall.config).toBe(runtimeConfig); |
| 141 | + expect(baseCall.commandName).toBe("reply"); |
| 142 | + expect(hoisted.getScopedChannelsCommandSecretTargetsMock).toHaveBeenCalledWith({ |
| 143 | + config: runtimeConfig, |
| 144 | + channel: "discord", |
| 145 | + accountId: undefined, |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments