From b1b162fcdbf47ddccd71e09b8b17dad40dcf58d1 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 24 Mar 2026 09:24:29 +0000 Subject: [PATCH] test: harden threaded channel follow-ups --- .../prepare.thread-session-key.test.ts | 28 +++++++++++-- extensions/telegram/src/send.test-harness.ts | 42 +++++++++++++++++++ ...unauthorized-senders-not-allowfrom.test.ts | 2 +- src/infra/file-lock.ts | 1 + src/plugin-sdk/file-lock.ts | 12 ++++++ src/test-utils/session-state-cleanup.ts | 2 + test/setup.ts | 11 ++--- 7 files changed, 87 insertions(+), 11 deletions(-) diff --git a/extensions/slack/src/monitor/message-handler/prepare.thread-session-key.test.ts b/extensions/slack/src/monitor/message-handler/prepare.thread-session-key.test.ts index ea3a1935766..19ef468770c 100644 --- a/extensions/slack/src/monitor/message-handler/prepare.thread-session-key.test.ts +++ b/extensions/slack/src/monitor/message-handler/prepare.thread-session-key.test.ts @@ -1,9 +1,26 @@ import type { App } from "@slack/bolt"; -import { describe, expect, it } from "vitest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../../../../../src/config/config.js"; import type { SlackMessageEvent } from "../../types.js"; -import { prepareSlackMessage } from "./prepare.js"; -import { createInboundSlackTestContext, createSlackTestAccount } from "./prepare.test-helpers.js"; + +type PrepareSlackMessage = typeof import("./prepare.js").prepareSlackMessage; +type CreateInboundSlackTestContext = + typeof import("./prepare.test-helpers.js").createInboundSlackTestContext; +type CreateSlackTestAccount = typeof import("./prepare.test-helpers.js").createSlackTestAccount; + +let prepareSlackMessage: PrepareSlackMessage; +let createInboundSlackTestContext: CreateInboundSlackTestContext; +let createSlackTestAccount: CreateSlackTestAccount; + +async function loadSlackPrepareModules() { + const [{ prepareSlackMessage: loadedPrepareSlackMessage }, helpers] = await Promise.all([ + import("./prepare.js"), + import("./prepare.test-helpers.js"), + ]); + prepareSlackMessage = loadedPrepareSlackMessage; + createInboundSlackTestContext = helpers.createInboundSlackTestContext; + createSlackTestAccount = helpers.createSlackTestAccount; +} function buildCtx(overrides?: { replyToMode?: "all" | "first" | "off" }) { const replyToMode = overrides?.replyToMode ?? "all"; @@ -31,6 +48,11 @@ function buildChannelMessage(overrides?: Partial): SlackMessa } describe("thread-level session keys", () => { + beforeEach(async () => { + vi.resetModules(); + await loadSlackPrepareModules(); + }); + it("keeps top-level channel turns in one session when replyToMode=off", async () => { const ctx = buildCtx({ replyToMode: "off" }); ctx.resolveUserName = async () => ({ name: "Alice" }); diff --git a/extensions/telegram/src/send.test-harness.ts b/extensions/telegram/src/send.test-harness.ts index d1c0c7c3ba6..5fc58ed3ae3 100644 --- a/extensions/telegram/src/send.test-harness.ts +++ b/extensions/telegram/src/send.test-harness.ts @@ -36,6 +36,35 @@ const { maybePersistResolvedTelegramTarget } = vi.hoisted(() => ({ maybePersistResolvedTelegramTarget: vi.fn(async () => {}), })); +const { + undiciFetch, + undiciSetGlobalDispatcher, + undiciAgentCtor, + undiciEnvHttpProxyAgentCtor, + undiciProxyAgentCtor, +} = vi.hoisted(() => ({ + undiciFetch: vi.fn(), + undiciSetGlobalDispatcher: vi.fn(), + undiciAgentCtor: vi.fn(function MockAgent( + this: { options?: Record }, + options?: Record, + ) { + this.options = options; + }), + undiciEnvHttpProxyAgentCtor: vi.fn(function MockEnvHttpProxyAgent( + this: { options?: Record }, + options?: Record, + ) { + this.options = options; + }), + undiciProxyAgentCtor: vi.fn(function MockProxyAgent( + this: { options?: Record | string }, + options?: Record | string, + ) { + this.options = options; + }), +})); + type TelegramSendTestMocks = { botApi: Record; botCtorSpy: MockFn; @@ -79,6 +108,14 @@ vi.mock("grammy", () => ({ InputFile: class {}, })); +vi.mock("undici", () => ({ + Agent: undiciAgentCtor, + EnvHttpProxyAgent: undiciEnvHttpProxyAgentCtor, + ProxyAgent: undiciProxyAgentCtor, + fetch: undiciFetch, + setGlobalDispatcher: undiciSetGlobalDispatcher, +})); + vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => { const actual = await importOriginal(); return { @@ -101,6 +138,11 @@ export function installTelegramSendTestHooks() { loadWebMedia.mockReset(); maybePersistResolvedTelegramTarget.mockReset(); maybePersistResolvedTelegramTarget.mockResolvedValue(undefined); + undiciFetch.mockReset(); + undiciSetGlobalDispatcher.mockReset(); + undiciAgentCtor.mockClear(); + undiciEnvHttpProxyAgentCtor.mockClear(); + undiciProxyAgentCtor.mockClear(); botCtorSpy.mockReset(); for (const fn of Object.values(botApi)) { fn.mockReset(); diff --git a/extensions/whatsapp/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test.ts b/extensions/whatsapp/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test.ts index b995b5543d5..482c9c4c86b 100644 --- a/extensions/whatsapp/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test.ts +++ b/extensions/whatsapp/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test.ts @@ -1,6 +1,5 @@ import "./monitor-inbox.test-harness.js"; import { describe, expect, it, vi } from "vitest"; -import { monitorWebInbox } from "./inbound.js"; import { DEFAULT_ACCOUNT_ID, expectPairingPromptSent, @@ -59,6 +58,7 @@ async function startWebInboxMonitor(params: { config?: Record; sendReadReceipts?: boolean; }) { + const { monitorWebInbox } = await import("./inbound.js"); if (params.config) { mockLoadConfig.mockReturnValue(params.config); } diff --git a/src/infra/file-lock.ts b/src/infra/file-lock.ts index 3860d9ae4c3..1e544006e72 100644 --- a/src/infra/file-lock.ts +++ b/src/infra/file-lock.ts @@ -1,6 +1,7 @@ export type { FileLockHandle, FileLockOptions } from "../plugin-sdk/file-lock.js"; export { acquireFileLock, + drainFileLockStateForTest, resetFileLockStateForTest, withFileLock, } from "../plugin-sdk/file-lock.js"; diff --git a/src/plugin-sdk/file-lock.ts b/src/plugin-sdk/file-lock.ts index df907da67cc..cd91e06ea93 100644 --- a/src/plugin-sdk/file-lock.ts +++ b/src/plugin-sdk/file-lock.ts @@ -40,6 +40,14 @@ function releaseAllLocksSync(): void { } } +async function drainAllLocks(): Promise { + for (const [normalizedFile, held] of Array.from(HELD_LOCKS.entries())) { + HELD_LOCKS.delete(normalizedFile); + await held.handle.close().catch(() => undefined); + await fs.rm(held.lockPath, { force: true }).catch(() => undefined); + } +} + function rmLockPathSync(lockPath: string): void { try { fsSync.rmSync(lockPath, { force: true }); @@ -133,6 +141,10 @@ export function resetFileLockStateForTest(): void { releaseAllLocksSync(); } +export async function drainFileLockStateForTest(): Promise { + await drainAllLocks(); +} + /** Acquire a re-entrant process-local file lock backed by a `.lock` sidecar file. */ export async function acquireFileLock( filePath: string, diff --git a/src/test-utils/session-state-cleanup.ts b/src/test-utils/session-state-cleanup.ts index 0d2fabb3bf0..4ea68b6bdd6 100644 --- a/src/test-utils/session-state-cleanup.ts +++ b/src/test-utils/session-state-cleanup.ts @@ -1,7 +1,9 @@ import { drainSessionWriteLockStateForTest } from "../agents/session-write-lock.js"; import { clearSessionStoreCacheForTest } from "../config/sessions/store.js"; +import { drainFileLockStateForTest } from "../infra/file-lock.js"; export async function cleanupSessionStateForTest(): Promise { clearSessionStoreCacheForTest(); + await drainFileLockStateForTest(); await drainSessionWriteLockStateForTest(); } diff --git a/test/setup.ts b/test/setup.ts index f01e0a4db9b..1dd7f5901ae 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -56,11 +56,10 @@ import type { ChannelPlugin, } from "../src/channels/plugins/types.js"; import type { OpenClawConfig } from "../src/config/config.js"; -import { clearSessionStoreCacheForTest } from "../src/config/sessions/store.js"; -import { resetFileLockStateForTest } from "../src/infra/file-lock.js"; import type { OutboundSendDeps } from "../src/infra/outbound/deliver.js"; import { installProcessWarningFilter } from "../src/infra/warning-filter.js"; import type { PluginRegistry } from "../src/plugins/registry.js"; +import { cleanupSessionStateForTest } from "../src/test-utils/session-state-cleanup.js"; import { withIsolatedTestHome } from "./test-env.js"; // Set HOME/state isolation before importing any runtime OpenClaw modules. @@ -354,10 +353,9 @@ beforeAll(() => { installDefaultPluginRegistry(); }); -afterEach(() => { - clearSessionStoreCacheForTest(); +afterEach(async () => { + await cleanupSessionStateForTest(); resetContextWindowCacheForTest(); - resetFileLockStateForTest(); resetModelsJsonReadyCacheForTest(); resetSessionWriteLockStateForTest(); if (globalRegistryState.registry !== DEFAULT_PLUGIN_REGISTRY) { @@ -368,8 +366,7 @@ afterEach(() => { }); afterAll(async () => { - clearSessionStoreCacheForTest(); + await cleanupSessionStateForTest(); await drainSessionWriteLockStateForTest(); - resetFileLockStateForTest(); testEnv.cleanup(); });