|
| 1 | +// Covers `models auth order set/clear`: store writes and running-gateway refresh. |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import type { AuthProfileStore } from "../../agents/auth-profiles.js"; |
| 4 | +import type { OpenClawConfig } from "../../config/types.openclaw.js"; |
| 5 | +import type { RuntimeEnv } from "../../runtime.js"; |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + ensureAuthProfileStore: vi.fn(), |
| 9 | + setAuthProfileOrder: vi.fn(), |
| 10 | + loadModelsConfig: vi.fn(), |
| 11 | + refreshRunningGatewayAuthState: vi.fn(async () => undefined), |
| 12 | +})); |
| 13 | + |
| 14 | +vi.mock("../../agents/auth-profiles.js", () => ({ |
| 15 | + ensureAuthProfileStore: mocks.ensureAuthProfileStore, |
| 16 | + setAuthProfileOrder: mocks.setAuthProfileOrder, |
| 17 | + externalCliDiscoveryForProviderAuth: () => undefined, |
| 18 | + resolveAuthStatePathForDisplay: (agentDir: string) => `${agentDir}/auth-profiles.json`, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("./load-config.js", () => ({ |
| 22 | + loadModelsConfig: mocks.loadModelsConfig, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock("./shared.js", async (importOriginal) => { |
| 26 | + const actual = await importOriginal<typeof import("./shared.js")>(); |
| 27 | + return { |
| 28 | + ...actual, |
| 29 | + resolveModelsTargetAgent: (_cfg: OpenClawConfig, rawAgentId?: string) => ({ |
| 30 | + agentId: rawAgentId ?? "main", |
| 31 | + agentDir: `/tmp/agent-${rawAgentId ?? "main"}`, |
| 32 | + }), |
| 33 | + }; |
| 34 | +}); |
| 35 | + |
| 36 | +vi.mock("./auth-refresh.js", () => ({ |
| 37 | + refreshRunningGatewayAuthState: mocks.refreshRunningGatewayAuthState, |
| 38 | +})); |
| 39 | + |
| 40 | +const { modelsAuthOrderClearCommand, modelsAuthOrderSetCommand } = await import("./auth-order.js"); |
| 41 | + |
| 42 | +function createRuntime(): RuntimeEnv & { logs: string[] } { |
| 43 | + const logs: string[] = []; |
| 44 | + return { |
| 45 | + logs, |
| 46 | + log: (message: string) => { |
| 47 | + logs.push(message); |
| 48 | + }, |
| 49 | + error: () => {}, |
| 50 | + } as unknown as RuntimeEnv & { logs: string[] }; |
| 51 | +} |
| 52 | + |
| 53 | +function storeWith(profileIds: string[], order?: string[]): AuthProfileStore { |
| 54 | + return { |
| 55 | + version: 1, |
| 56 | + profiles: Object.fromEntries( |
| 57 | + profileIds.map((profileId) => [ |
| 58 | + profileId, |
| 59 | + { type: "oauth" as const, provider: profileId.split(":")[0] ?? "anthropic", access: "tok" }, |
| 60 | + ]), |
| 61 | + ), |
| 62 | + ...(order ? { order: { anthropic: order } } : {}), |
| 63 | + } as unknown as AuthProfileStore; |
| 64 | +} |
| 65 | + |
| 66 | +describe("models auth order", () => { |
| 67 | + beforeEach(() => { |
| 68 | + vi.clearAllMocks(); |
| 69 | + mocks.loadModelsConfig.mockResolvedValue({} as OpenClawConfig); |
| 70 | + mocks.ensureAuthProfileStore.mockReturnValue( |
| 71 | + storeWith(["anthropic:a", "anthropic:b"], ["anthropic:a"]), |
| 72 | + ); |
| 73 | + mocks.setAuthProfileOrder.mockResolvedValue( |
| 74 | + storeWith(["anthropic:a", "anthropic:b"], ["anthropic:b", "anthropic:a"]), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("set writes the store order and refreshes a running gateway", async () => { |
| 79 | + const runtime = createRuntime(); |
| 80 | + await modelsAuthOrderSetCommand( |
| 81 | + { provider: "anthropic", order: ["anthropic:b", "anthropic:a"] }, |
| 82 | + runtime, |
| 83 | + ); |
| 84 | + |
| 85 | + expect(mocks.setAuthProfileOrder).toHaveBeenCalledWith({ |
| 86 | + agentDir: "/tmp/agent-main", |
| 87 | + provider: "anthropic", |
| 88 | + order: ["anthropic:b", "anthropic:a"], |
| 89 | + }); |
| 90 | + expect(mocks.refreshRunningGatewayAuthState).toHaveBeenCalledTimes(1); |
| 91 | + expect(runtime.logs).toContain("Auth profile order override: anthropic:b, anthropic:a"); |
| 92 | + }); |
| 93 | + |
| 94 | + it("clear removes the store order and refreshes a running gateway", async () => { |
| 95 | + const runtime = createRuntime(); |
| 96 | + await modelsAuthOrderClearCommand({ provider: "anthropic" }, runtime); |
| 97 | + |
| 98 | + expect(mocks.setAuthProfileOrder).toHaveBeenCalledWith({ |
| 99 | + agentDir: "/tmp/agent-main", |
| 100 | + provider: "anthropic", |
| 101 | + order: null, |
| 102 | + }); |
| 103 | + expect(mocks.refreshRunningGatewayAuthState).toHaveBeenCalledTimes(1); |
| 104 | + expect(runtime.logs.some((line) => line.includes("Auth profile order override cleared"))).toBe( |
| 105 | + true, |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("does not refresh the gateway when the store update fails", async () => { |
| 110 | + mocks.setAuthProfileOrder.mockResolvedValue(null); |
| 111 | + |
| 112 | + await expect( |
| 113 | + modelsAuthOrderSetCommand({ provider: "anthropic", order: ["anthropic:a"] }, createRuntime()), |
| 114 | + ).rejects.toThrow("Failed to update auth state"); |
| 115 | + expect(mocks.refreshRunningGatewayAuthState).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments