|
| 1 | +import os from "node:os"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { withTempHome } from "../config/home-env.test-harness.js"; |
| 4 | +import { |
| 5 | + beginSecretsRuntimeIsolationForTest, |
| 6 | + createOpenAIFileRuntimeConfig, |
| 7 | + createOpenAIFileRuntimeFixture, |
| 8 | + EMPTY_LOADABLE_PLUGIN_ORIGINS, |
| 9 | + endSecretsRuntimeIsolationForTest, |
| 10 | + expectResolvedOpenAIRuntime, |
| 11 | + loadAuthStoreWithProfiles, |
| 12 | + OPENAI_FILE_KEY_REF, |
| 13 | + type SecretsRuntimeEnvSnapshot, |
| 14 | +} from "./runtime-auth.integration.test-helpers.js"; |
| 15 | +import { |
| 16 | + activateSecretsRuntimeSnapshot, |
| 17 | + getActiveSecretsRuntimeSnapshot, |
| 18 | + prepareSecretsRuntimeSnapshot, |
| 19 | +} from "./runtime.js"; |
| 20 | + |
| 21 | +vi.unmock("../version.js"); |
| 22 | + |
| 23 | +describe("secrets runtime snapshot auth refresh failure", () => { |
| 24 | + let envSnapshot: SecretsRuntimeEnvSnapshot; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + envSnapshot = beginSecretsRuntimeIsolationForTest(); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + endSecretsRuntimeIsolationForTest(envSnapshot); |
| 32 | + }); |
| 33 | + |
| 34 | + it("keeps last-known-good runtime snapshot active when refresh preparation fails", async () => { |
| 35 | + if (os.platform() === "win32") { |
| 36 | + return; |
| 37 | + } |
| 38 | + await withTempHome("openclaw-secrets-runtime-refresh-fail-", async (home) => { |
| 39 | + const { secretFile, agentDir } = await createOpenAIFileRuntimeFixture(home); |
| 40 | + |
| 41 | + let loadAuthStoreCalls = 0; |
| 42 | + const loadAuthStore = () => { |
| 43 | + loadAuthStoreCalls += 1; |
| 44 | + if (loadAuthStoreCalls > 1) { |
| 45 | + throw new Error("simulated secrets runtime refresh failure"); |
| 46 | + } |
| 47 | + return loadAuthStoreWithProfiles({ |
| 48 | + "openai:default": { |
| 49 | + type: "api_key", |
| 50 | + provider: "openai", |
| 51 | + keyRef: OPENAI_FILE_KEY_REF, |
| 52 | + }, |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + const prepared = await prepareSecretsRuntimeSnapshot({ |
| 57 | + config: createOpenAIFileRuntimeConfig(secretFile), |
| 58 | + agentDirs: [agentDir], |
| 59 | + loadablePluginOrigins: EMPTY_LOADABLE_PLUGIN_ORIGINS, |
| 60 | + loadAuthStore, |
| 61 | + }); |
| 62 | + |
| 63 | + activateSecretsRuntimeSnapshot(prepared); |
| 64 | + expectResolvedOpenAIRuntime(agentDir); |
| 65 | + |
| 66 | + await expect( |
| 67 | + prepareSecretsRuntimeSnapshot({ |
| 68 | + config: { |
| 69 | + ...createOpenAIFileRuntimeConfig(secretFile), |
| 70 | + gateway: { auth: { mode: "token" } }, |
| 71 | + }, |
| 72 | + agentDirs: [agentDir], |
| 73 | + loadablePluginOrigins: EMPTY_LOADABLE_PLUGIN_ORIGINS, |
| 74 | + loadAuthStore, |
| 75 | + }), |
| 76 | + ).rejects.toThrow(/simulated secrets runtime refresh failure/i); |
| 77 | + |
| 78 | + const activeAfterFailure = getActiveSecretsRuntimeSnapshot(); |
| 79 | + expect(activeAfterFailure).not.toBeNull(); |
| 80 | + expectResolvedOpenAIRuntime(agentDir); |
| 81 | + expect(activeAfterFailure?.sourceConfig.models?.providers?.openai?.apiKey).toEqual( |
| 82 | + OPENAI_FILE_KEY_REF, |
| 83 | + ); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments