refactor(test): replace ad-hoc env restore blocks with helpers

This commit is contained in:
Peter Steinberger
2026-02-21 18:28:49 +00:00
parent 63488eb981
commit fc43a16d43
3 changed files with 9 additions and 31 deletions

View File

@@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, vi } from "vitest"; import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../config/config.js"; import type { OpenClawConfig } from "../config/config.js";
import { withEnvAsync } from "../test-utils/env.js";
const mocks = vi.hoisted(() => ({ const mocks = vi.hoisted(() => ({
readCommand: vi.fn(), readCommand: vi.fn(),
@@ -139,9 +140,7 @@ describe("maybeRepairGatewayServiceConfig", () => {
}); });
it("uses OPENCLAW_GATEWAY_TOKEN when config token is missing", async () => { it("uses OPENCLAW_GATEWAY_TOKEN when config token is missing", async () => {
const previousToken = process.env.OPENCLAW_GATEWAY_TOKEN; await withEnvAsync({ OPENCLAW_GATEWAY_TOKEN: "env-token" }, async () => {
process.env.OPENCLAW_GATEWAY_TOKEN = "env-token";
try {
setupGatewayTokenRepairScenario("env-token"); setupGatewayTokenRepairScenario("env-token");
const cfg: OpenClawConfig = { const cfg: OpenClawConfig = {
@@ -161,12 +160,6 @@ describe("maybeRepairGatewayServiceConfig", () => {
}), }),
); );
expect(mocks.install).toHaveBeenCalledTimes(1); expect(mocks.install).toHaveBeenCalledTimes(1);
} finally { });
if (previousToken === undefined) {
delete process.env.OPENCLAW_GATEWAY_TOKEN;
} else {
process.env.OPENCLAW_GATEWAY_TOKEN = previousToken;
}
}
}); });
}); });

View File

@@ -3,6 +3,7 @@ import path from "node:path";
import { describe, expect, it, vi } from "vitest"; import { describe, expect, it, vi } from "vitest";
import { withTempHome } from "../../test/helpers/temp-home.js"; import { withTempHome } from "../../test/helpers/temp-home.js";
import { ensureAuthProfileStore, listProfilesForProvider } from "../agents/auth-profiles.js"; import { ensureAuthProfileStore, listProfilesForProvider } from "../agents/auth-profiles.js";
import { withEnvAsync } from "../test-utils/env.js";
import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js"; import { createProviderUsageFetch, makeResponse } from "../test-utils/provider-usage-fetch.js";
import { import {
formatUsageReportLines, formatUsageReportLines,
@@ -302,9 +303,7 @@ describe("provider usage loading", () => {
}); });
it("falls back to claude.ai web usage when OAuth scope is missing", async () => { it("falls back to claude.ai web usage when OAuth scope is missing", async () => {
const cookieSnapshot = process.env.CLAUDE_AI_SESSION_KEY; await withEnvAsync({ CLAUDE_AI_SESSION_KEY: "sk-ant-web-1" }, async () => {
process.env.CLAUDE_AI_SESSION_KEY = "sk-ant-web-1";
try {
const mockFetch = createProviderUsageFetch(async (url) => { const mockFetch = createProviderUsageFetch(async (url) => {
if (url.includes("api.anthropic.com/api/oauth/usage")) { if (url.includes("api.anthropic.com/api/oauth/usage")) {
return makeResponse(403, { return makeResponse(403, {
@@ -336,13 +335,7 @@ describe("provider usage loading", () => {
const claude = expectSingleAnthropicProvider(summary); const claude = expectSingleAnthropicProvider(summary);
expect(claude?.windows.some((w) => w.label === "5h")).toBe(true); expect(claude?.windows.some((w) => w.label === "5h")).toBe(true);
expect(claude?.windows.some((w) => w.label === "Week")).toBe(true); expect(claude?.windows.some((w) => w.label === "Week")).toBe(true);
} finally { });
if (cookieSnapshot === undefined) {
delete process.env.CLAUDE_AI_SESSION_KEY;
} else {
process.env.CLAUDE_AI_SESSION_KEY = cookieSnapshot;
}
}
}); });
it("loads snapshots for copilot antigravity gemini codex and xiaomi", async () => { it("loads snapshots for copilot antigravity gemini codex and xiaomi", async () => {

View File

@@ -2,6 +2,7 @@ import fs from "node:fs/promises";
import os from "node:os"; import os from "node:os";
import path from "node:path"; import path from "node:path";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { withEnvAsync } from "../test-utils/env.js";
import { pathExists } from "../utils.js"; import { pathExists } from "../utils.js";
import { runGatewayUpdate } from "./update-runner.js"; import { runGatewayUpdate } from "./update-runner.js";
@@ -421,11 +422,8 @@ describe("runGatewayUpdate", () => {
}); });
it("updates global bun installs when detected", async () => { it("updates global bun installs when detected", async () => {
const oldBunInstall = process.env.BUN_INSTALL;
const bunInstall = path.join(tempDir, "bun-install"); const bunInstall = path.join(tempDir, "bun-install");
process.env.BUN_INSTALL = bunInstall; await withEnvAsync({ BUN_INSTALL: bunInstall }, async () => {
try {
const bunGlobalRoot = path.join(bunInstall, "install", "global", "node_modules"); const bunGlobalRoot = path.join(bunInstall, "install", "global", "node_modules");
const pkgRoot = path.join(bunGlobalRoot, "openclaw"); const pkgRoot = path.join(bunGlobalRoot, "openclaw");
await seedGlobalPackageRoot(pkgRoot); await seedGlobalPackageRoot(pkgRoot);
@@ -449,13 +447,7 @@ describe("runGatewayUpdate", () => {
expect(result.before?.version).toBe("1.0.0"); expect(result.before?.version).toBe("1.0.0");
expect(result.after?.version).toBe("2.0.0"); expect(result.after?.version).toBe("2.0.0");
expect(calls.some((call) => call === "bun add -g openclaw@latest")).toBe(true); expect(calls.some((call) => call === "bun add -g openclaw@latest")).toBe(true);
} finally { });
if (oldBunInstall === undefined) {
delete process.env.BUN_INSTALL;
} else {
process.env.BUN_INSTALL = oldBunInstall;
}
}
}); });
it("rejects git roots that are not a openclaw checkout", async () => { it("rejects git roots that are not a openclaw checkout", async () => {