mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 03:10:21 +00:00
test: harden threaded channel follow-ups
This commit is contained in:
@@ -1,9 +1,26 @@
|
|||||||
import type { App } from "@slack/bolt";
|
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 { OpenClawConfig } from "../../../../../src/config/config.js";
|
||||||
import type { SlackMessageEvent } from "../../types.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" }) {
|
function buildCtx(overrides?: { replyToMode?: "all" | "first" | "off" }) {
|
||||||
const replyToMode = overrides?.replyToMode ?? "all";
|
const replyToMode = overrides?.replyToMode ?? "all";
|
||||||
@@ -31,6 +48,11 @@ function buildChannelMessage(overrides?: Partial<SlackMessageEvent>): SlackMessa
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("thread-level session keys", () => {
|
describe("thread-level session keys", () => {
|
||||||
|
beforeEach(async () => {
|
||||||
|
vi.resetModules();
|
||||||
|
await loadSlackPrepareModules();
|
||||||
|
});
|
||||||
|
|
||||||
it("keeps top-level channel turns in one session when replyToMode=off", async () => {
|
it("keeps top-level channel turns in one session when replyToMode=off", async () => {
|
||||||
const ctx = buildCtx({ replyToMode: "off" });
|
const ctx = buildCtx({ replyToMode: "off" });
|
||||||
ctx.resolveUserName = async () => ({ name: "Alice" });
|
ctx.resolveUserName = async () => ({ name: "Alice" });
|
||||||
|
|||||||
@@ -36,6 +36,35 @@ const { maybePersistResolvedTelegramTarget } = vi.hoisted(() => ({
|
|||||||
maybePersistResolvedTelegramTarget: vi.fn(async () => {}),
|
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 = {
|
type TelegramSendTestMocks = {
|
||||||
botApi: Record<string, MockFn>;
|
botApi: Record<string, MockFn>;
|
||||||
botCtorSpy: MockFn;
|
botCtorSpy: MockFn;
|
||||||
@@ -79,6 +108,14 @@ vi.mock("grammy", () => ({
|
|||||||
InputFile: class {},
|
InputFile: class {},
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
vi.mock("undici", () => ({
|
||||||
|
Agent: undiciAgentCtor,
|
||||||
|
EnvHttpProxyAgent: undiciEnvHttpProxyAgentCtor,
|
||||||
|
ProxyAgent: undiciProxyAgentCtor,
|
||||||
|
fetch: undiciFetch,
|
||||||
|
setGlobalDispatcher: undiciSetGlobalDispatcher,
|
||||||
|
}));
|
||||||
|
|
||||||
vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => {
|
vi.mock("openclaw/plugin-sdk/config-runtime", async (importOriginal) => {
|
||||||
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/config-runtime")>();
|
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/config-runtime")>();
|
||||||
return {
|
return {
|
||||||
@@ -101,6 +138,11 @@ export function installTelegramSendTestHooks() {
|
|||||||
loadWebMedia.mockReset();
|
loadWebMedia.mockReset();
|
||||||
maybePersistResolvedTelegramTarget.mockReset();
|
maybePersistResolvedTelegramTarget.mockReset();
|
||||||
maybePersistResolvedTelegramTarget.mockResolvedValue(undefined);
|
maybePersistResolvedTelegramTarget.mockResolvedValue(undefined);
|
||||||
|
undiciFetch.mockReset();
|
||||||
|
undiciSetGlobalDispatcher.mockReset();
|
||||||
|
undiciAgentCtor.mockClear();
|
||||||
|
undiciEnvHttpProxyAgentCtor.mockClear();
|
||||||
|
undiciProxyAgentCtor.mockClear();
|
||||||
botCtorSpy.mockReset();
|
botCtorSpy.mockReset();
|
||||||
for (const fn of Object.values(botApi)) {
|
for (const fn of Object.values(botApi)) {
|
||||||
fn.mockReset();
|
fn.mockReset();
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
import "./monitor-inbox.test-harness.js";
|
import "./monitor-inbox.test-harness.js";
|
||||||
import { describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { monitorWebInbox } from "./inbound.js";
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_ACCOUNT_ID,
|
DEFAULT_ACCOUNT_ID,
|
||||||
expectPairingPromptSent,
|
expectPairingPromptSent,
|
||||||
@@ -59,6 +58,7 @@ async function startWebInboxMonitor(params: {
|
|||||||
config?: Record<string, unknown>;
|
config?: Record<string, unknown>;
|
||||||
sendReadReceipts?: boolean;
|
sendReadReceipts?: boolean;
|
||||||
}) {
|
}) {
|
||||||
|
const { monitorWebInbox } = await import("./inbound.js");
|
||||||
if (params.config) {
|
if (params.config) {
|
||||||
mockLoadConfig.mockReturnValue(params.config);
|
mockLoadConfig.mockReturnValue(params.config);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export type { FileLockHandle, FileLockOptions } from "../plugin-sdk/file-lock.js";
|
export type { FileLockHandle, FileLockOptions } from "../plugin-sdk/file-lock.js";
|
||||||
export {
|
export {
|
||||||
acquireFileLock,
|
acquireFileLock,
|
||||||
|
drainFileLockStateForTest,
|
||||||
resetFileLockStateForTest,
|
resetFileLockStateForTest,
|
||||||
withFileLock,
|
withFileLock,
|
||||||
} from "../plugin-sdk/file-lock.js";
|
} from "../plugin-sdk/file-lock.js";
|
||||||
|
|||||||
@@ -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 {
|
function rmLockPathSync(lockPath: string): void {
|
||||||
try {
|
try {
|
||||||
fsSync.rmSync(lockPath, { force: true });
|
fsSync.rmSync(lockPath, { force: true });
|
||||||
@@ -133,6 +141,10 @@ export function resetFileLockStateForTest(): void {
|
|||||||
releaseAllLocksSync();
|
releaseAllLocksSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function drainFileLockStateForTest(): Promise<void> {
|
||||||
|
await drainAllLocks();
|
||||||
|
}
|
||||||
|
|
||||||
/** Acquire a re-entrant process-local file lock backed by a `.lock` sidecar file. */
|
/** Acquire a re-entrant process-local file lock backed by a `.lock` sidecar file. */
|
||||||
export async function acquireFileLock(
|
export async function acquireFileLock(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { drainSessionWriteLockStateForTest } from "../agents/session-write-lock.js";
|
import { drainSessionWriteLockStateForTest } from "../agents/session-write-lock.js";
|
||||||
import { clearSessionStoreCacheForTest } from "../config/sessions/store.js";
|
import { clearSessionStoreCacheForTest } from "../config/sessions/store.js";
|
||||||
|
import { drainFileLockStateForTest } from "../infra/file-lock.js";
|
||||||
|
|
||||||
export async function cleanupSessionStateForTest(): Promise<void> {
|
export async function cleanupSessionStateForTest(): Promise<void> {
|
||||||
clearSessionStoreCacheForTest();
|
clearSessionStoreCacheForTest();
|
||||||
|
await drainFileLockStateForTest();
|
||||||
await drainSessionWriteLockStateForTest();
|
await drainSessionWriteLockStateForTest();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,11 +56,10 @@ import type {
|
|||||||
ChannelPlugin,
|
ChannelPlugin,
|
||||||
} from "../src/channels/plugins/types.js";
|
} from "../src/channels/plugins/types.js";
|
||||||
import type { OpenClawConfig } from "../src/config/config.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 type { OutboundSendDeps } from "../src/infra/outbound/deliver.js";
|
||||||
import { installProcessWarningFilter } from "../src/infra/warning-filter.js";
|
import { installProcessWarningFilter } from "../src/infra/warning-filter.js";
|
||||||
import type { PluginRegistry } from "../src/plugins/registry.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";
|
import { withIsolatedTestHome } from "./test-env.js";
|
||||||
|
|
||||||
// Set HOME/state isolation before importing any runtime OpenClaw modules.
|
// Set HOME/state isolation before importing any runtime OpenClaw modules.
|
||||||
@@ -354,10 +353,9 @@ beforeAll(() => {
|
|||||||
installDefaultPluginRegistry();
|
installDefaultPluginRegistry();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(async () => {
|
||||||
clearSessionStoreCacheForTest();
|
await cleanupSessionStateForTest();
|
||||||
resetContextWindowCacheForTest();
|
resetContextWindowCacheForTest();
|
||||||
resetFileLockStateForTest();
|
|
||||||
resetModelsJsonReadyCacheForTest();
|
resetModelsJsonReadyCacheForTest();
|
||||||
resetSessionWriteLockStateForTest();
|
resetSessionWriteLockStateForTest();
|
||||||
if (globalRegistryState.registry !== DEFAULT_PLUGIN_REGISTRY) {
|
if (globalRegistryState.registry !== DEFAULT_PLUGIN_REGISTRY) {
|
||||||
@@ -368,8 +366,7 @@ afterEach(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
clearSessionStoreCacheForTest();
|
await cleanupSessionStateForTest();
|
||||||
await drainSessionWriteLockStateForTest();
|
await drainSessionWriteLockStateForTest();
|
||||||
resetFileLockStateForTest();
|
|
||||||
testEnv.cleanup();
|
testEnv.cleanup();
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user