|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { vi } from "vitest"; |
| 5 | + |
| 6 | +import { createSession, type Session, type SessionUpdates } from "../../../state/onboard-session"; |
| 7 | +import type { PoliciesStateOptions } from "./policies"; |
| 8 | + |
| 9 | +export type PolicyTestAgent = { name: string } | null; |
| 10 | +export type PolicyTestWebSearchConfig = { fetchEnabled: true }; |
| 11 | +type MessagingPlan = NonNullable<Session["messagingPlan"]>; |
| 12 | +type MessagingChannelId = MessagingPlan["channels"][number]["channelId"]; |
| 13 | + |
| 14 | +export function makeMessagingPlan( |
| 15 | + sandboxName: string, |
| 16 | + channels: readonly MessagingChannelId[], |
| 17 | + disabledChannels: readonly MessagingChannelId[] = [], |
| 18 | +): MessagingPlan { |
| 19 | + const disabled = new Set(disabledChannels); |
| 20 | + return { |
| 21 | + schemaVersion: 1, |
| 22 | + sandboxName, |
| 23 | + agent: "openclaw", |
| 24 | + workflow: "onboard", |
| 25 | + channels: channels.map((channelId) => ({ |
| 26 | + channelId, |
| 27 | + displayName: channelId, |
| 28 | + authMode: "token-paste", |
| 29 | + active: !disabled.has(channelId), |
| 30 | + selected: true, |
| 31 | + configured: true, |
| 32 | + disabled: disabled.has(channelId), |
| 33 | + inputs: [], |
| 34 | + hooks: [], |
| 35 | + })), |
| 36 | + disabledChannels, |
| 37 | + credentialBindings: [], |
| 38 | + networkPolicy: { presets: [], entries: [] }, |
| 39 | + agentRender: [], |
| 40 | + buildSteps: [], |
| 41 | + stateUpdates: [], |
| 42 | + healthChecks: [], |
| 43 | + }; |
| 44 | +} |
| 45 | + |
| 46 | +export function createPolicyHandlerDeps( |
| 47 | + overrides: Partial<PoliciesStateOptions<PolicyTestAgent, PolicyTestWebSearchConfig>["deps"]> = {}, |
| 48 | +) { |
| 49 | + let session = createSession(); |
| 50 | + const calls = { |
| 51 | + load: vi.fn(() => session), |
| 52 | + activeSandbox: vi.fn(() => ({ |
| 53 | + messaging: { plan: makeMessagingPlan("my-assistant", ["telegram"]) }, |
| 54 | + })), |
| 55 | + mergeChannels: vi.fn( |
| 56 | + (selected: string[], recorded: string[], active: string[] | null | undefined) => |
| 57 | + selected.length > 0 ? selected : (active ?? recorded), |
| 58 | + ), |
| 59 | + smoke: vi.fn(), |
| 60 | + prepareResume: vi.fn( |
| 61 | + ( |
| 62 | + _sandboxName: string, |
| 63 | + options: Parameters< |
| 64 | + PoliciesStateOptions< |
| 65 | + PolicyTestAgent, |
| 66 | + PolicyTestWebSearchConfig |
| 67 | + >["deps"]["preparePolicyPresetResumeSelection"] |
| 68 | + >[1], |
| 69 | + ) => ({ |
| 70 | + policyPresets: (options.recordedPolicyPresets ?? []).filter( |
| 71 | + (name) => name !== "unsupported", |
| 72 | + ), |
| 73 | + recordedPolicyPresetsNeedReconcile: (options.recordedPolicyPresets ?? []).includes( |
| 74 | + "unsupported", |
| 75 | + ), |
| 76 | + disabledMessagingPolicyPresetApplied: false, |
| 77 | + suppressedAgentRequiredPresetsLive: false, |
| 78 | + }), |
| 79 | + ), |
| 80 | + appliedCheck: vi.fn(() => false), |
| 81 | + skipped: vi.fn(), |
| 82 | + recordSkip: vi.fn(async () => session), |
| 83 | + startStep: vi.fn(async () => undefined), |
| 84 | + setupPolicies: vi.fn(async () => ["npm"]), |
| 85 | + updateSession: vi.fn((mutator: (value: Session) => Session | void) => { |
| 86 | + session = mutator(session) ?? session; |
| 87 | + return session; |
| 88 | + }), |
| 89 | + complete: vi.fn(async () => session), |
| 90 | + persistPolicies: vi.fn((_sandboxName: string, _appliedPolicyPresets: string[]) => undefined), |
| 91 | + }; |
| 92 | + return { |
| 93 | + calls, |
| 94 | + deps: { |
| 95 | + loadSession: calls.load, |
| 96 | + getActiveSandbox: calls.activeSandbox, |
| 97 | + mergePolicyMessagingChannels: calls.mergeChannels, |
| 98 | + verifyCompatibleEndpointSandboxSmoke: calls.smoke, |
| 99 | + preparePolicyPresetResumeSelection: calls.prepareResume, |
| 100 | + arePolicyPresetsApplied: calls.appliedCheck, |
| 101 | + skippedStepMessage: calls.skipped, |
| 102 | + recordStateSkipped: calls.recordSkip, |
| 103 | + startRecordedStep: calls.startStep, |
| 104 | + setupPoliciesWithSelection: calls.setupPolicies, |
| 105 | + updateSession: calls.updateSession, |
| 106 | + recordStepComplete: calls.complete, |
| 107 | + toSessionUpdates: (updates: Record<string, unknown>) => updates as SessionUpdates, |
| 108 | + persistAppliedPolicyPresets: calls.persistPolicies, |
| 109 | + ...overrides, |
| 110 | + }, |
| 111 | + setSession(next: Session) { |
| 112 | + session = next; |
| 113 | + }, |
| 114 | + getSession: () => session, |
| 115 | + }; |
| 116 | +} |
| 117 | + |
| 118 | +export function basePolicyHandlerOptions( |
| 119 | + deps: PoliciesStateOptions<PolicyTestAgent, PolicyTestWebSearchConfig>["deps"], |
| 120 | +): PoliciesStateOptions<PolicyTestAgent, PolicyTestWebSearchConfig> { |
| 121 | + return { |
| 122 | + resume: false, |
| 123 | + sandboxName: "my-assistant", |
| 124 | + provider: "provider", |
| 125 | + model: "model", |
| 126 | + endpointUrl: "https://example.com/v1", |
| 127 | + credentialEnv: "NVIDIA_INFERENCE_API_KEY", |
| 128 | + selectedMessagingChannels: [], |
| 129 | + webSearchConfig: null, |
| 130 | + webSearchSupported: true, |
| 131 | + hermesToolGateways: [], |
| 132 | + agent: null, |
| 133 | + deps, |
| 134 | + }; |
| 135 | +} |
0 commit comments