test: speed up whatsapp and shared test suites

This commit is contained in:
Peter Steinberger
2026-03-24 15:15:31 +00:00
parent 83bb647238
commit d884676dd2
7 changed files with 42 additions and 48 deletions

View File

@@ -1,7 +1,7 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { expectChannelInboundContextContract as expectInboundContextContract } from "../../../../../src/channels/plugins/contracts/suites.js";
let capturedCtx: unknown;
@@ -111,9 +111,12 @@ import { updateLastRouteInBackground } from "./last-route.js";
let processMessage: typeof import("./process-message.js").processMessage;
describe("web processMessage inbound context", () => {
beforeEach(async () => {
beforeAll(async () => {
vi.resetModules();
({ processMessage } = await import("./process-message.js"));
});
beforeEach(async () => {
capturedCtx = undefined;
capturedDispatchParams = undefined;
backgroundTasks = new Set();

View File

@@ -1,11 +1,11 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, describe, expect, it, vi } from "vitest";
let extractLocationData: typeof import("./inbound.js").extractLocationData;
let extractMediaPlaceholder: typeof import("./inbound.js").extractMediaPlaceholder;
let extractText: typeof import("./inbound.js").extractText;
describe("web inbound helpers", () => {
beforeEach(async () => {
beforeAll(async () => {
vi.resetModules();
({ extractLocationData, extractMediaPlaceholder, extractText } = await import("./inbound.js"));
});

View File

@@ -137,12 +137,6 @@ afterEach(() => {
vi.clearAllMocks();
});
beforeEach(async () => {
vi.resetModules();
({ LocalMediaAccessError, loadWebMedia, loadWebMediaRaw, optimizeImageToJpeg } =
await import("./media.js"));
});
describe("web media loading", () => {
beforeAll(() => {
// Ensure state dir is stable and not influenced by other tests that stub OPENCLAW_STATE_DIR.

View File

@@ -2,7 +2,7 @@ import crypto from "node:crypto";
import fsSync from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../../src/config/config.js";
import { redactIdentifier } from "../../../src/logging/redact-identifier.js";
@@ -24,12 +24,15 @@ describe("web outbound", () => {
const sendPoll = vi.fn(async () => ({ messageId: "poll123" }));
const sendReaction = vi.fn(async () => {});
beforeEach(async () => {
beforeAll(async () => {
vi.resetModules();
vi.clearAllMocks();
({ sendMessageWhatsApp, sendPollWhatsApp, sendReactionWhatsApp } = await import("./send.js"));
({ setActiveWebListener } = await import("./active-listener.js"));
({ resetLogger, setLoggerOverride } = await import("../../../src/logging.js"));
});
beforeEach(() => {
vi.clearAllMocks();
setActiveWebListener({
sendComposingTo,
sendMessage,

View File

@@ -13,6 +13,10 @@ import {
const bootstrapAttempts = new Set<string>();
export function resetOutboundChannelResolutionStateForTest(): void {
bootstrapAttempts.clear();
}
export function normalizeDeliverableOutboundChannel(
raw?: string | null,
): DeliverableMessageChannel | undefined {

View File

@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
getChannelPlugin: vi.fn(),
@@ -46,9 +46,9 @@ vi.mock("../../config/plugin-auto-enable.js", () => ({
},
}));
import { setActivePluginRegistry } from "../../plugins/runtime.js";
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
let setActivePluginRegistry: typeof import("../../plugins/runtime.js").setActivePluginRegistry;
let createTestRegistry: typeof import("../../test-utils/channel-plugins.js").createTestRegistry;
let resetOutboundChannelResolutionStateForTest: typeof import("./channel-resolution.js").resetOutboundChannelResolutionStateForTest;
let resolveOutboundTarget: typeof import("./targets.js").resolveOutboundTarget;
describe("resolveOutboundTarget channel resolution", () => {
@@ -61,10 +61,17 @@ describe("resolveOutboundTarget channel resolution", () => {
mode: "explicit",
});
beforeEach(async () => {
beforeAll(async () => {
vi.resetModules();
({ setActivePluginRegistry } = await import("../../plugins/runtime.js"));
({ createTestRegistry } = await import("../../test-utils/channel-plugins.js"));
({ resetOutboundChannelResolutionStateForTest } = await import("./channel-resolution.js"));
({ resolveOutboundTarget } = await import("./targets.js"));
});
beforeEach(() => {
registrySeq += 1;
resetOutboundChannelResolutionStateForTest();
setActivePluginRegistry(createTestRegistry([]), `targets-test-${registrySeq}`);
mocks.getChannelPlugin.mockReset();
mocks.loadOpenClawPlugins.mockReset();

View File

@@ -1,5 +1,4 @@
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { TEST_UNDICI_RUNTIME_DEPS_KEY } from "../infra/net/undici-runtime.js";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
const undiciMocks = vi.hoisted(() => {
const createDispatcherCtor = <T extends Record<string, unknown> | string>() =>
@@ -15,14 +14,6 @@ const undiciMocks = vi.hoisted(() => {
};
});
const mockedModuleIds = [
"openclaw/plugin-sdk/infra-runtime",
"../../extensions/telegram/src/fetch.js",
"../../extensions/telegram/src/proxy.js",
"./fetch.js",
"undici",
] as const;
vi.mock("undici", () => ({
Agent: undiciMocks.agentCtor,
EnvHttpProxyAgent: undiciMocks.envHttpProxyAgentCtor,
@@ -31,27 +22,24 @@ vi.mock("undici", () => ({
}));
let fetchRemoteMedia: typeof import("./fetch.js").fetchRemoteMedia;
let makeProxyFetch: typeof import("../../extensions/telegram/src/proxy.js").makeProxyFetch;
let resolveTelegramTransport: typeof import("../../extensions/telegram/src/fetch.js").resolveTelegramTransport;
let shouldRetryTelegramTransportFallback: typeof import("../../extensions/telegram/src/fetch.js").shouldRetryTelegramTransportFallback;
async function loadTelegramNetworkModules(): Promise<void> {
vi.resetModules();
for (const id of mockedModuleIds) {
if (id !== "undici") {
vi.doUnmock(id);
}
}
({ fetchRemoteMedia } = await import("./fetch.js"));
({ makeProxyFetch } = await import("../../extensions/telegram/src/proxy.js"));
({ resolveTelegramTransport, shouldRetryTelegramTransportFallback } =
await import("../../extensions/telegram/src/fetch.js"));
}
let makeProxyFetch: typeof import("../../extensions/telegram/src/proxy.js").makeProxyFetch;
let TEST_UNDICI_RUNTIME_DEPS_KEY: typeof import("../infra/net/undici-runtime.js").TEST_UNDICI_RUNTIME_DEPS_KEY;
describe("fetchRemoteMedia telegram network policy", () => {
type LookupFn = NonNullable<Parameters<typeof fetchRemoteMedia>[0]["lookupFn"]>;
beforeEach(async () => {
beforeAll(async () => {
vi.resetModules();
({ TEST_UNDICI_RUNTIME_DEPS_KEY } = await import("../infra/net/undici-runtime.js"));
({ fetchRemoteMedia } = await import("./fetch.js"));
({ resolveTelegramTransport, shouldRetryTelegramTransportFallback } =
await import("../../extensions/telegram/src/fetch.js"));
({ makeProxyFetch } = await import("../../extensions/telegram/src/proxy.js"));
});
beforeEach(() => {
undiciMocks.fetch.mockReset();
undiciMocks.agentCtor.mockClear();
undiciMocks.envHttpProxyAgentCtor.mockClear();
@@ -61,7 +49,6 @@ describe("fetchRemoteMedia telegram network policy", () => {
EnvHttpProxyAgent: undiciMocks.envHttpProxyAgentCtor,
ProxyAgent: undiciMocks.proxyAgentCtor,
};
await loadTelegramNetworkModules();
});
function createTelegramFetchFailedError(code: string): Error {
@@ -77,10 +64,6 @@ describe("fetchRemoteMedia telegram network policy", () => {
afterAll(() => {
Reflect.deleteProperty(globalThis as object, TEST_UNDICI_RUNTIME_DEPS_KEY);
for (const id of mockedModuleIds) {
vi.doUnmock(id);
}
vi.resetModules();
});
it("preserves Telegram resolver transport policy for file downloads", async () => {