diff --git a/extensions/msteams/src/monitor-handler.adaptive-card.test.ts b/extensions/msteams/src/monitor-handler.adaptive-card.test.ts index 1fa056f6cd6..09b2bb328f6 100644 --- a/extensions/msteams/src/monitor-handler.adaptive-card.test.ts +++ b/extensions/msteams/src/monitor-handler.adaptive-card.test.ts @@ -1,12 +1,12 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; -import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "../runtime-api.js"; +import type { OpenClawConfig, RuntimeEnv } from "../runtime-api.js"; import type { MSTeamsConversationStore } from "./conversation-store.js"; import { type MSTeamsActivityHandler, type MSTeamsMessageHandlerDeps, registerMSTeamsHandlers, } from "./monitor-handler.js"; -import { setMSTeamsRuntime } from "./runtime.js"; +import { installMSTeamsTestRuntime } from "./monitor-handler.test-helpers.js"; import type { MSTeamsTurnContext } from "./sdk-types.js"; const runtimeApiMockState = vi.hoisted(() => ({ @@ -35,43 +35,7 @@ vi.mock("./reply-dispatcher.js", () => ({ })); function createDeps(): MSTeamsMessageHandlerDeps { - setMSTeamsRuntime({ - logging: { shouldLogVerbose: () => false }, - system: { enqueueSystemEvent: vi.fn() }, - channel: { - debounce: { - resolveInboundDebounceMs: () => 0, - createInboundDebouncer: (params: { - onFlush: (entries: T[]) => Promise; - }): { enqueue: (entry: T) => Promise } => ({ - enqueue: async (entry: T) => { - await params.onFlush([entry]); - }, - }), - }, - pairing: { - readAllowFromStore: vi.fn(async () => []), - upsertPairingRequest: vi.fn(async () => null), - }, - text: { - hasControlCommand: () => false, - }, - routing: { - resolveAgentRoute: ({ peer }: { peer: { kind: string; id: string } }) => ({ - sessionKey: `msteams:${peer.kind}:${peer.id}`, - agentId: "default", - accountId: "default", - }), - }, - reply: { - formatAgentEnvelope: ({ body }: { body: string }) => body, - finalizeInboundContext: >(ctx: T) => ctx, - }, - session: { - recordInboundSession: vi.fn(async () => undefined), - }, - }, - } as unknown as PluginRuntime); + installMSTeamsTestRuntime(); return { cfg: {} as OpenClawConfig, diff --git a/extensions/msteams/src/monitor-handler.sso.test.ts b/extensions/msteams/src/monitor-handler.sso.test.ts index a61551a78de..21514155993 100644 --- a/extensions/msteams/src/monitor-handler.sso.test.ts +++ b/extensions/msteams/src/monitor-handler.sso.test.ts @@ -1,5 +1,5 @@ import { beforeAll, describe, expect, it, vi } from "vitest"; -import type { OpenClawConfig, PluginRuntime } from "../runtime-api.js"; +import type { OpenClawConfig } from "../runtime-api.js"; import { type MSTeamsActivityHandler, type MSTeamsMessageHandlerDeps, @@ -8,8 +8,8 @@ import { import { createActivityHandler as baseCreateActivityHandler, createMSTeamsMessageHandlerDeps, + installMSTeamsTestRuntime, } from "./monitor-handler.test-helpers.js"; -import { setMSTeamsRuntime } from "./runtime.js"; import type { MSTeamsTurnContext } from "./sdk-types.js"; import { createMSTeamsSsoTokenStoreMemory } from "./sso-token-store.js"; import { @@ -20,46 +20,6 @@ import { parseSigninVerifyStateValue, } from "./sso.js"; -function installTestRuntime(): void { - setMSTeamsRuntime({ - logging: { shouldLogVerbose: () => false }, - system: { enqueueSystemEvent: vi.fn() }, - channel: { - debounce: { - resolveInboundDebounceMs: () => 0, - createInboundDebouncer: (params: { - onFlush: (entries: T[]) => Promise; - }): { enqueue: (entry: T) => Promise } => ({ - enqueue: async (entry: T) => { - await params.onFlush([entry]); - }, - }), - }, - pairing: { - readAllowFromStore: vi.fn(async () => []), - upsertPairingRequest: vi.fn(async () => null), - }, - text: { - hasControlCommand: () => false, - }, - routing: { - resolveAgentRoute: ({ peer }: { peer: { kind: string; id: string } }) => ({ - sessionKey: `msteams:${peer.kind}:${peer.id}`, - agentId: "default", - accountId: "default", - }), - }, - reply: { - formatAgentEnvelope: ({ body }: { body: string }) => body, - finalizeInboundContext: >(ctx: T) => ctx, - }, - session: { - recordInboundSession: vi.fn(async () => undefined), - }, - }, - } as unknown as PluginRuntime); -} - function createActivityHandler() { const run = vi.fn(async () => undefined); const handler = baseCreateActivityHandler(run); @@ -409,7 +369,7 @@ describe("handleSigninVerifyStateInvoke", () => { describe("msteams signin invoke handler registration", () => { beforeAll(() => { - installTestRuntime(); + installMSTeamsTestRuntime(); }); const blockedSigninScenarios = createBlockedSigninScenarios(); diff --git a/extensions/msteams/src/monitor-handler.test-helpers.ts b/extensions/msteams/src/monitor-handler.test-helpers.ts index 53cf4ca00a9..df488cac8fc 100644 --- a/extensions/msteams/src/monitor-handler.test-helpers.ts +++ b/extensions/msteams/src/monitor-handler.test-helpers.ts @@ -1,9 +1,68 @@ import { vi } from "vitest"; -import type { OpenClawConfig, RuntimeEnv } from "../runtime-api.js"; +import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "../runtime-api.js"; import type { MSTeamsConversationStore } from "./conversation-store.js"; import type { MSTeamsAdapter } from "./messenger.js"; import type { MSTeamsActivityHandler, MSTeamsMessageHandlerDeps } from "./monitor-handler.js"; import type { MSTeamsPollStore } from "./polls.js"; +import { setMSTeamsRuntime } from "./runtime.js"; + +type RuntimeRoutePeer = { peer: { kind: string; id: string } }; + +type MSTeamsTestRuntimeOptions = { + enqueueSystemEvent?: ReturnType; + readAllowFromStore?: ReturnType; + upsertPairingRequest?: ReturnType; + recordInboundSession?: ReturnType; + resolveAgentRoute?: (params: RuntimeRoutePeer) => unknown; + resolveTextChunkLimit?: () => number; + resolveStorePath?: () => string; +}; + +export function installMSTeamsTestRuntime(options: MSTeamsTestRuntimeOptions = {}): void { + setMSTeamsRuntime({ + logging: { shouldLogVerbose: () => false }, + system: { enqueueSystemEvent: options.enqueueSystemEvent ?? vi.fn() }, + channel: { + debounce: { + resolveInboundDebounceMs: () => 0, + createInboundDebouncer: (params: { + onFlush: (entries: T[]) => Promise; + }): { enqueue: (entry: T) => Promise } => ({ + enqueue: async (entry: T) => { + await params.onFlush([entry]); + }, + }), + }, + pairing: { + readAllowFromStore: options.readAllowFromStore ?? vi.fn(async () => []), + upsertPairingRequest: options.upsertPairingRequest ?? vi.fn(async () => null), + }, + text: { + hasControlCommand: () => false, + ...(options.resolveTextChunkLimit + ? { resolveTextChunkLimit: options.resolveTextChunkLimit } + : {}), + }, + routing: { + resolveAgentRoute: + options.resolveAgentRoute ?? + (({ peer }: RuntimeRoutePeer) => ({ + sessionKey: `msteams:${peer.kind}:${peer.id}`, + agentId: "default", + accountId: "default", + })), + }, + reply: { + formatAgentEnvelope: ({ body }: { body: string }) => body, + finalizeInboundContext: >(ctx: T) => ctx, + }, + session: { + recordInboundSession: options.recordInboundSession ?? vi.fn(async () => undefined), + ...(options.resolveStorePath ? { resolveStorePath: options.resolveStorePath } : {}), + }, + }, + } as unknown as PluginRuntime); +} export function createActivityHandler( run = vi.fn(async () => undefined), diff --git a/extensions/msteams/src/monitor-handler/message-handler.test-support.ts b/extensions/msteams/src/monitor-handler/message-handler.test-support.ts index ea255ce74e1..6dec9cb7414 100644 --- a/extensions/msteams/src/monitor-handler/message-handler.test-support.ts +++ b/extensions/msteams/src/monitor-handler/message-handler.test-support.ts @@ -1,7 +1,7 @@ import { vi } from "vitest"; -import type { OpenClawConfig, PluginRuntime, RuntimeEnv } from "../../runtime-api.js"; +import type { OpenClawConfig, RuntimeEnv } from "../../runtime-api.js"; import type { MSTeamsMessageHandlerDeps } from "../monitor-handler.js"; -import { setMSTeamsRuntime } from "../runtime.js"; +import { installMSTeamsTestRuntime } from "../monitor-handler.test-helpers.js"; export const channelConversationId = "19:general@thread.tacv2"; @@ -10,7 +10,7 @@ type MessageHandlerDepsOptions = { readAllowFromStore?: ReturnType; upsertPairingRequest?: ReturnType; recordInboundSession?: ReturnType; - resolveAgentRoute?: ReturnType; + resolveAgentRoute?: (params: { peer: { kind: string; id: string } }) => unknown; }; export function createMessageHandlerDeps( @@ -33,39 +33,15 @@ export function createMessageHandlerDeps( matchedBy: "default" as const, })); - setMSTeamsRuntime({ - logging: { shouldLogVerbose: () => false }, - system: { enqueueSystemEvent }, - channel: { - debounce: { - resolveInboundDebounceMs: () => 0, - createInboundDebouncer: (params: { - onFlush: (entries: T[]) => Promise; - }): { enqueue: (entry: T) => Promise } => ({ - enqueue: async (entry: T) => { - await params.onFlush([entry]); - }, - }), - }, - pairing: { - readAllowFromStore, - upsertPairingRequest, - }, - text: { - hasControlCommand: () => false, - resolveTextChunkLimit: () => 4000, - }, - routing: { resolveAgentRoute }, - reply: { - formatAgentEnvelope: ({ body }: { body: string }) => body, - finalizeInboundContext: >(ctx: T) => ctx, - }, - session: { - recordInboundSession, - resolveStorePath: () => "/tmp/test-store", - }, - }, - } as unknown as PluginRuntime); + installMSTeamsTestRuntime({ + enqueueSystemEvent, + readAllowFromStore, + upsertPairingRequest, + recordInboundSession, + resolveAgentRoute, + resolveTextChunkLimit: () => 4000, + resolveStorePath: () => "/tmp/test-store", + }); const conversationStore = { get: vi.fn(async () => null),