Tests: isolate full-suite state leaks

This commit is contained in:
Peter Steinberger
2026-04-07 14:14:43 +01:00
parent c8e290fe22
commit 47563305a2
3 changed files with 10 additions and 7 deletions

View File

@@ -1,6 +1,6 @@
import http from "node:http";
import https from "node:https";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
getDirectAgentForCdp,
hasProxyEnv,
@@ -10,6 +10,10 @@ import {
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
beforeEach(() => {
vi.useRealTimers();
});
async function withIsolatedNoProxyEnv(fn: () => Promise<void>) {
const origNoProxy = process.env.NO_PROXY;
const origNoProxyLower = process.env.no_proxy;

View File

@@ -1,11 +1,11 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { MODEL_CONTEXT_TOKEN_CACHE } from "../../agents/context-cache.js";
import { loadModelCatalog } from "../../agents/model-catalog.js";
import { loadModelCatalog } from "../../agents/model-catalog.runtime.js";
import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions.js";
import { createModelSelectionState, resolveContextTokens } from "./model-selection.js";
vi.mock("../../agents/model-catalog.js", () => ({
vi.mock("../../agents/model-catalog.runtime.js", () => ({
loadModelCatalog: vi.fn(async () => [
{ provider: "anthropic", id: "claude-opus-4-6", name: "Claude Opus 4.5" },
{ provider: "inferencer", id: "deepseek-v3-4bit-mlx", name: "DeepSeek V3" },
@@ -19,9 +19,7 @@ vi.mock("../../agents/model-catalog.js", () => ({
vi.mock("../../channels/plugins/session-conversation.js", () => ({
resolveSessionParentSessionKey: (sessionKey?: string) =>
sessionKey
?.replace(/:thread:[^:]+$/, "")
.replace(/:topic:[^:]+$/, "") ?? null,
sessionKey?.replace(/:thread:[^:]+$/, "").replace(/:topic:[^:]+$/, "") ?? null,
}));
afterEach(() => {
@@ -533,7 +531,7 @@ describe("createModelSelectionState respects session model override", () => {
describe("createModelSelectionState resolveDefaultReasoningLevel", () => {
it("returns on when catalog model has reasoning true", async () => {
const { loadModelCatalog } = await import("../../agents/model-catalog.js");
const { loadModelCatalog } = await import("../../agents/model-catalog.runtime.js");
vi.mocked(loadModelCatalog).mockResolvedValueOnce([
{ provider: "openrouter", id: "x-ai/grok-4.1-fast", name: "Grok", reasoning: true },
]);

View File

@@ -415,6 +415,7 @@ describe("GatewayClient close handling", () => {
describe("GatewayClient connect auth payload", () => {
beforeEach(() => {
vi.useRealTimers();
wsInstances.length = 0;
loadDeviceAuthTokenMock.mockReset();
storeDeviceAuthTokenMock.mockReset();