Skip to content

Commit 3850c20

Browse files
committed
test: isolate release validation from host state
1 parent c7eaf66 commit 3850c20

11 files changed

Lines changed: 37 additions & 9 deletions

‎src/agents/auth-profiles/oauth.openai-codex-refresh-fallback.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { expectDefined } from "@openclaw/normalization-core";
1010
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
1111
import { FILE_LOCK_TIMEOUT_ERROR_CODE, resetFileLockStateForTest } from "../../infra/file-lock.js";
1212
import { closeOpenClawAgentDatabasesForTest } from "../../state/openclaw-agent-db.js";
13-
import { captureEnv, setTestEnvValue } from "../../test-utils/env.js";
13+
import { captureEnv, deleteTestEnvValue, setTestEnvValue } from "../../test-utils/env.js";
1414
import { OAuthRefreshFailureError } from "./oauth-refresh-failure.js";
1515
import { buildRefreshContentionError } from "./oauth-refresh-lock-errors.js";
1616
import {
@@ -154,7 +154,7 @@ function requireOAuthContext(context: unknown): OAuthCredential {
154154
}
155155

156156
describe("resolveApiKeyForProfile openai refresh fallback", () => {
157-
const envSnapshot = captureEnv(OAUTH_AGENT_ENV_KEYS);
157+
const envSnapshot = captureEnv([...OAUTH_AGENT_ENV_KEYS, "OPENAI_API_KEY"]);
158158
let tempRoot = "";
159159
let agentDir = "";
160160
let caseIndex = 0;
@@ -186,6 +186,7 @@ describe("resolveApiKeyForProfile openai refresh fallback", () => {
186186
await fs.mkdir(agentDir, { recursive: true });
187187
setTestEnvValue("OPENCLAW_STATE_DIR", caseRoot);
188188
setTestEnvValue("OPENCLAW_AGENT_DIR", agentDir);
189+
deleteTestEnvValue("OPENAI_API_KEY");
189190
});
190191

191192
afterEach(async () => {

‎src/agents/bash-tools.exec-host-gateway.test.ts‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ describe("processGatewayAllowlist", () => {
948948
fs.writeFileSync(shadowGit, "#!/bin/sh\nexit 0\n", { mode: 0o755 });
949949
try {
950950
const command = "git status";
951+
const canonicalShadowGit = fs.realpathSync(shadowGit);
951952
await configurePlanBackedCommand({
952953
command,
953954
env: { PATH: tempDir },
@@ -961,9 +962,9 @@ describe("processGatewayAllowlist", () => {
961962
});
962963

963964
expect(defaultExecAutoReviewerMock).toHaveBeenCalledWith(
964-
expect.objectContaining({ resolvedPath: shadowGit }),
965+
expect.objectContaining({ resolvedPath: canonicalShadowGit }),
965966
);
966-
expect(result).toEqual({ execCommandOverride: `${shadowGit} status` });
967+
expect(result).toEqual({ execCommandOverride: `${canonicalShadowGit} status` });
967968
} finally {
968969
fs.rmSync(tempDir, { recursive: true, force: true });
969970
}
@@ -1157,6 +1158,15 @@ describe("processGatewayAllowlist", () => {
11571158
if (!authorizationPlan.ok) {
11581159
throw new Error(authorizationPlan.reason);
11591160
}
1161+
const enforced = buildAuthorizedShellCommandFromPlan({
1162+
plan: authorizationPlan,
1163+
mode: "enforced",
1164+
segmentSatisfiedBy: ["safeBuiltins"],
1165+
});
1166+
expect(enforced.ok).toBe(true);
1167+
if (!enforced.ok) {
1168+
throw new Error(enforced.reason);
1169+
}
11601170
hasDurableExecApprovalMock.mockReturnValue(true);
11611171
hasExactCommandDurableExecApprovalMock.mockReturnValue(true);
11621172
evaluateShellAllowlistWithAuthorizationMock.mockReturnValue({
@@ -1183,7 +1193,7 @@ describe("processGatewayAllowlist", () => {
11831193
const result = await runGatewayAllowlist({ command });
11841194

11851195
expect(createAndRegisterDefaultExecApprovalRequestMock).not.toHaveBeenCalled();
1186-
expect(result).toEqual({ execCommandOverride: command });
1196+
expect(result).toEqual({ execCommandOverride: enforced.command });
11871197
expect(commitExecAuthorizationMock).toHaveBeenCalledWith(
11881198
expect.objectContaining({
11891199
authorization: expect.objectContaining({

‎src/agents/btw.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { expectDefined } from "@openclaw/normalization-core";
44
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
5-
import { beforeEach, describe, expect, it, vi } from "vitest";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import type { SessionEntry } from "../config/sessions.js";
77
import {
88
looksLikeSecretSentinel,
@@ -571,6 +571,10 @@ function mockOpenAIPlatformProfile(): void {
571571
}
572572

573573
describe("runBtwSideQuestion", () => {
574+
afterEach(() => {
575+
vi.unstubAllEnvs();
576+
});
577+
574578
beforeEach(() => {
575579
streamSimpleMock.mockReset();
576580
readFileMock.mockReset();
@@ -1001,6 +1005,7 @@ describe("runBtwSideQuestion", () => {
10011005
});
10021006

10031007
it("lets native Codex bootstrap auth without a host profile", async () => {
1008+
vi.stubEnv("OPENAI_API_KEY", "");
10041009
const supports = vi.fn((ctx: Parameters<AgentHarness["supports"]>[0]) => {
10051010
if (ctx.modelProvider?.preparedAuth?.source !== "harness") {
10061011
return supportsPreparedOpenAIAuth(ctx);

‎src/agents/runtime-plan/prepare-auth.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ describe("prepareAgentRuntimeAuthPlan", () => {
13771377
});
13781378

13791379
it("resolves an env SecretRef on its prepared Platform route", async () => {
1380+
vi.stubEnv("OPENAI_API_KEY", "");
13801381
vi.stubEnv("OPENAI_PLATFORM_KEY", "secret-ref-platform-key");
13811382
try {
13821383
const config = {

‎src/agents/tools/model-config.helpers.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ const hasDirectOpenAiKey = (
107107
});
108108

109109
beforeEach(() => {
110+
vi.stubEnv("OPENAI_API_KEY", "");
110111
authMocks.resolveEnvApiKey.mockReset();
111112
authMocks.resolveEnvApiKey.mockImplementation(
112113
(provider: string, _env?: unknown, options?: { config?: unknown }) =>

‎src/cli/capability-cli.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ describe("capability cli", () => {
518518
});
519519

520520
beforeEach(() => {
521+
vi.stubEnv("OPENAI_API_KEY", "");
521522
mocks.runtime.log.mockClear();
522523
mocks.runtime.error.mockClear();
523524
mocks.runtime.writeJson.mockClear();

‎src/commands/auth-choice.model-check.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("warnIfModelConfigLooksOff", () => {
6767
},
6868
} as OpenClawConfig;
6969

70-
await warnIfModelConfigLooksOff(config, prompter, { validateCatalog: false });
70+
await warnIfModelConfigLooksOff(config, prompter, { env: {}, validateCatalog: false });
7171

7272
expect(loadModelCatalog).not.toHaveBeenCalled();
7373
expect(ensureAuthProfileStore).toHaveBeenCalledOnce();

‎src/gateway/server-methods/models-auth-status.test.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// credential cleanup, secret refresh, and provider run abort side effects.
33

44
import { expectDefined } from "@openclaw/normalization-core";
5-
import { beforeEach, describe, expect, it, vi } from "vitest";
5+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
66
import type { AuthHealthSummary } from "../../agents/auth-health.js";
77
import type { AuthProfileStore } from "../../agents/auth-profiles.js";
88
import { NON_ENV_SECRETREF_MARKER } from "../../agents/model-auth-markers.js";
@@ -233,6 +233,7 @@ async function readAuthStatus(params: Record<string, unknown> = {}) {
233233
}
234234

235235
function resetAuthStatusMocks(): void {
236+
vi.stubEnv("OPENAI_API_KEY", "");
236237
vi.clearAllMocks();
237238
invalidateModelAuthStatusCache();
238239
mocks.getRuntimeConfig.mockReturnValue({});
@@ -262,6 +263,10 @@ function resetAuthStatusMocks(): void {
262263
mocks.refreshActiveProviderAuthRuntimeSnapshot.mockResolvedValue(false);
263264
}
264265

266+
afterEach(() => {
267+
vi.unstubAllEnvs();
268+
});
269+
265270
function firstExternalCliAuthOption() {
266271
expect(mocks.ensureAuthProfileStore).toHaveBeenCalledTimes(1);
267272
expect(firstEnsureAuthProfileStoreCall()?.[0]).toBe("/tmp/agent");

‎src/gateway/server-methods/usage.sessions-usage.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,10 @@ describe("sessions.usage", () => {
839839
it("preserves JSONL detail lookup for storeless sessions", async () => {
840840
await withUsageState(async (writeSessionFile) => {
841841
const sessionFile = writeSessionFile("storeless.jsonl");
842+
const canonicalSessionFile = fs.realpathSync(sessionFile);
842843
await runSessionsUsageTimeseries({ key: "agent:opus:storeless" });
843844
expect(vi.mocked(loadSessionUsageTimeSeries)).toHaveBeenCalledWith(
844-
expect.objectContaining({ sessionFile, sessionEntry: undefined }),
845+
expect.objectContaining({ sessionFile: canonicalSessionFile, sessionEntry: undefined }),
845846
);
846847
});
847848
});

‎src/gateway/server-methods/usage.status-cache.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ describe("usage.status provider usage cache", () => {
8383
beforeEach(() => {
8484
now = 1_000;
8585
store = createStore();
86+
vi.stubEnv("OPENAI_API_KEY", "");
8687
vi.spyOn(Date, "now").mockImplementation(() => now);
8788
vi.clearAllMocks();
8889
clearModelAuthStatusUsageCache();
@@ -109,6 +110,7 @@ describe("usage.status provider usage cache", () => {
109110
});
110111

111112
afterEach(() => {
113+
vi.unstubAllEnvs();
112114
vi.restoreAllMocks();
113115
});
114116

0 commit comments

Comments
 (0)