test: harden threaded channel follow-ups

This commit is contained in:
Peter Steinberger
2026-03-24 09:24:29 +00:00
parent 43131dcc08
commit b1b162fcdb
7 changed files with 87 additions and 11 deletions

View File

@@ -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<SlackMessageEvent>): 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" });

View File

@@ -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<string, unknown> },
options?: Record<string, unknown>,
) {
this.options = options;
}),
undiciEnvHttpProxyAgentCtor: vi.fn(function MockEnvHttpProxyAgent(
this: { options?: Record<string, unknown> },
options?: Record<string, unknown>,
) {
this.options = options;
}),
undiciProxyAgentCtor: vi.fn(function MockProxyAgent(
this: { options?: Record<string, unknown> | string },
options?: Record<string, unknown> | string,
) {
this.options = options;
}),
}));
type TelegramSendTestMocks = {
botApi: Record<string, MockFn>;
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<typeof import("openclaw/plugin-sdk/config-runtime")>();
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();

View File

@@ -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<string, unknown>;
sendReadReceipts?: boolean;
}) {
const { monitorWebInbox } = await import("./inbound.js");
if (params.config) {
mockLoadConfig.mockReturnValue(params.config);
}

View File

@@ -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";

View File

@@ -40,6 +40,14 @@ function releaseAllLocksSync(): void {
}
}
async function drainAllLocks(): Promise<void> {
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<void> {
await drainAllLocks();
}
/** Acquire a re-entrant process-local file lock backed by a `.lock` sidecar file. */
export async function acquireFileLock(
filePath: string,

View File

@@ -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<void> {
clearSessionStoreCacheForTest();
await drainFileLockStateForTest();
await drainSessionWriteLockStateForTest();
}

View File

@@ -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();
});