Skip to content

Commit 9c28cc3

Browse files
committed
test(gateway): isolate live auth discovery state
1 parent bbd102a commit 9c28cc3

1 file changed

Lines changed: 74 additions & 2 deletions

File tree

‎src/gateway/gateway-models.profiles.live.test.ts‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
type Model,
2020
type ModelThinkingLevel,
2121
} from "openclaw/plugin-sdk/llm";
22-
import { afterEach, describe, expect, it } from "vitest";
22+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
2323
import { renderCatNoncePngBase64 } from "../../test/helpers/live-image-probe.js";
2424
import { discoverAuthStorage, discoverModels } from "../agents/agent-model-discovery.js";
2525
import { resolveAgentWorkspaceDir, resolveDefaultAgentDir } from "../agents/agent-scope.js";
@@ -2923,6 +2923,20 @@ function resolveGatewayLivePreparedProfileId(
29232923
: undefined;
29242924
}
29252925

2926+
async function enterIsolatedGatewayLiveDiscoveryState(): Promise<() => Promise<void>> {
2927+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
2928+
const tempStateDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-live-discovery-state-"));
2929+
setTestEnvValue("OPENCLAW_STATE_DIR", tempStateDir);
2930+
return async () => {
2931+
if (previousStateDir === undefined) {
2932+
delete process.env.OPENCLAW_STATE_DIR;
2933+
} else {
2934+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
2935+
}
2936+
await fs.rm(tempStateDir, { recursive: true, force: true, maxRetries: 5, retryDelay: 50 });
2937+
};
2938+
}
2939+
29262940
function createGatewayLiveModelSession(params: {
29272941
agentId: string;
29282942
credentialAttempt: number;
@@ -3127,6 +3141,49 @@ describe("buildLiveGatewayAuthProfileStore", () => {
31273141
expect(resolveGatewayLivePreparedProfileId(store, "anthropic")).toBeUndefined();
31283142
});
31293143

3144+
it("keeps prepared discovery credentials out of the ambient auth store", async () => {
3145+
const previousStateDir = process.env.OPENCLAW_STATE_DIR;
3146+
const ambientStateDir = await fs.mkdtemp(
3147+
path.join(os.tmpdir(), "openclaw-live-ambient-state-"),
3148+
);
3149+
process.env.OPENCLAW_STATE_DIR = ambientStateDir;
3150+
const ambientAgentDir = resolveDefaultAgentDir({});
3151+
const ambientStore: AuthProfileStore = {
3152+
version: 1,
3153+
profiles: {
3154+
"openai:ambient": { type: "api_key", provider: "openai", key: "ambient-test-key" },
3155+
},
3156+
};
3157+
saveAuthProfileStore(ambientStore, ambientAgentDir);
3158+
3159+
try {
3160+
const leaveDiscoveryState = await enterIsolatedGatewayLiveDiscoveryState();
3161+
try {
3162+
const discoveryAgentDir = resolveDefaultAgentDir({});
3163+
expect(discoveryAgentDir).not.toBe(ambientAgentDir);
3164+
const prepared = materializeGatewayLiveDiscoveryAuth({
3165+
env: { OPENAI_API_KEY: "prepared-openai-test-key" },
3166+
providerList: ["openai"],
3167+
store: ensureAuthProfileStore(discoveryAgentDir, { allowKeychainPrompt: false }),
3168+
});
3169+
saveAuthProfileStore(prepared, discoveryAgentDir);
3170+
} finally {
3171+
await leaveDiscoveryState();
3172+
}
3173+
3174+
expect(
3175+
ensureAuthProfileStore(ambientAgentDir, { allowKeychainPrompt: false }).profiles,
3176+
).toEqual(ambientStore.profiles);
3177+
} finally {
3178+
if (previousStateDir === undefined) {
3179+
delete process.env.OPENCLAW_STATE_DIR;
3180+
} else {
3181+
process.env.OPENCLAW_STATE_DIR = previousStateDir;
3182+
}
3183+
await fs.rm(ambientStateDir, { recursive: true, force: true });
3184+
}
3185+
});
3186+
31303187
it("keeps an env-first provider on its prepared direct credential", () => {
31313188
const store: AuthProfileStore = {
31323189
version: 1,
@@ -5828,6 +5885,17 @@ async function runGatewayModelSuite(params: GatewayModelSuiteParams) {
58285885
}
58295886

58305887
describeLive("gateway live (dev agent, profile keys)", () => {
5888+
let leaveDiscoveryState: (() => Promise<void>) | undefined;
5889+
5890+
beforeEach(async () => {
5891+
leaveDiscoveryState = await enterIsolatedGatewayLiveDiscoveryState();
5892+
});
5893+
5894+
afterEach(async () => {
5895+
await leaveDiscoveryState?.();
5896+
leaveDiscoveryState = undefined;
5897+
});
5898+
58315899
it(
58325900
"runs meaningful prompts across models with available keys",
58335901
async () =>
@@ -5927,7 +5995,11 @@ describeLive("gateway live (dev agent, profile keys)", () => {
59275995
}
59285996
} else {
59295997
logProgress("[all-models] loading auth profiles");
5930-
const authBacked = await loadAuthBackedLiveModelRegistry({ agentDir, cfg, providerList });
5998+
const authBacked = await loadAuthBackedLiveModelRegistry({
5999+
agentDir,
6000+
cfg,
6001+
providerList,
6002+
});
59316003
authProfileStore = authBacked.authProfileStore;
59326004
modelRegistry = authBacked.modelRegistry;
59336005
all = authBacked.all;

0 commit comments

Comments
 (0)