diff --git a/extensions/discord/src/test-support/component-runtime.ts b/extensions/discord/src/test-support/component-runtime.ts index ad58c06341e4..6503ce0e72d8 100644 --- a/extensions/discord/src/test-support/component-runtime.ts +++ b/extensions/discord/src/test-support/component-runtime.ts @@ -1,4 +1,5 @@ // Discord plugin module implements component runtime behavior. +import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers"; import { parsePluginBindingApprovalCustomId, resolvePinnedMainDmOwnerFromAllowlist, @@ -54,55 +55,31 @@ const resolvePluginConversationBindingApprovalMock: AsyncUnknownMock = const buildPluginBindingResolvedTextMock: UnknownMock = runtimeMocks.buildPluginBindingResolvedTextMock; -vi.mock("openclaw/plugin-sdk/channel-inbound", async (importOriginal) => { - const actual = await importOriginal(); - const runMockedTurn = async (plan: Parameters[0]) => { - const { cfg, route, delivery, ...prepared } = plan; - return await actual.runPreparedInboundReply({ - ...prepared, - routeSessionKey: route.sessionKey, - storePath: String(resolveStorePathMock(cfg.session?.store, { agentId: route.agentId })), - recordInboundSession: async (...args: unknown[]) => { - await recordInboundSessionMock(...args); - }, - runDispatch: async () => - await dispatchReplyMock({ - ctx: plan.ctxPayload, - cfg, - dispatcherOptions: { - ...plan.dispatcherOptions, - deliver: delivery.deliver, - onError: delivery.onError, - responsePrefixContextProvider: vi.fn(() => ({})), - }, - toolsAllow: plan.toolsAllow, - replyOptions: { - ...plan.replyOptions, - onModelSelected: vi.fn(), - }, - replyResolver: plan.replyResolver, - }), - }); - }; +vi.mock("openclaw/plugin-sdk/channel-inbound", async () => { + const actual = await vi.importActual( + "openclaw/plugin-sdk/channel-inbound", + ); + type RunParams = Parameters[0]; return { ...actual, - runChannelInboundEvent: async (params: Parameters[0]) => { - const input = await params.adapter.ingest(params.raw); - if (!input) { - return { admission: { kind: "drop" as const, reason: "ingest-null" }, dispatched: false }; - } - const resolved = await params.adapter.resolveTurn( - input, - { kind: "interaction", canStartAgentTurn: true }, - {}, - ); - return await runMockedTurn( - resolved as Parameters[0], - ); + runChannelInboundEvent: (params: RunParams) => { + const runtime = createPluginRuntimeMock({ + channel: { + session: { + resolveStorePath: (...args) => resolveStorePathMock(...args) as string, + recordInboundSession: async (...args) => { + await recordInboundSessionMock(...args); + }, + }, + reply: { + dispatchReplyWithBufferedBlockDispatcher: (...args) => dispatchReplyMock(...args), + }, + }, + }); + return runtime.channel.inbound.run(params); }, }; }); - async function readChannelIngressStoreAllowFromForDmPolicy(params: { provider: string; accountId: string; diff --git a/src/plugin-sdk/test-helpers/plugin-runtime-mock.test.ts b/src/plugin-sdk/test-helpers/plugin-runtime-mock.test.ts index fb8db05d5ea6..c412ee92133a 100644 --- a/src/plugin-sdk/test-helpers/plugin-runtime-mock.test.ts +++ b/src/plugin-sdk/test-helpers/plugin-runtime-mock.test.ts @@ -167,6 +167,7 @@ describe("createPluginRuntimeMock", () => { CommandAuthorized: false, SessionKey: "agent:main:test:direct:u1", }, + replyPipeline: {}, delivery: { deliver: vi.fn(async () => undefined) }, }); @@ -178,6 +179,14 @@ describe("createPluginRuntimeMock", () => { }), ); expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledOnce(); + expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledWith( + expect.objectContaining({ + dispatcherOptions: expect.objectContaining({ + responsePrefixContextProvider: expect.any(Function), + }), + replyOptions: expect.objectContaining({ onModelSelected: expect.any(Function) }), + }), + ); }); it("assembles routed prepared turns before dispatch", async () => { diff --git a/src/plugin-sdk/test-helpers/plugin-runtime-mock.ts b/src/plugin-sdk/test-helpers/plugin-runtime-mock.ts index 679d1c90e18d..aa77e65f6ff7 100644 --- a/src/plugin-sdk/test-helpers/plugin-runtime-mock.ts +++ b/src/plugin-sdk/test-helpers/plugin-runtime-mock.ts @@ -10,6 +10,7 @@ import { removeAckReactionHandleAfterReply, shouldAckReaction, } from "../../channels/ack-reactions.js"; +import { createChannelReplyPipeline } from "../../channels/message/reply-pipeline.js"; import { resolveSessionEntryResetFreshness } from "../../config/sessions/entry-freshness.js"; import { createChannelRuntimeContextRegistry } from "../../plugins/runtime/channel-runtime-contexts.js"; import type { PluginRuntime } from "../../plugins/runtime/types.js"; @@ -183,6 +184,19 @@ export function createPluginRuntimeMock(overrides: DeepPartial = replyOptions?: unknown; replyResolver?: unknown; }) => Promise; + const pipeline = params.replyPipeline + ? createChannelReplyPipeline({ + ...(params.replyPipeline as Omit< + Parameters[0], + "cfg" | "agentId" | "channel" | "accountId" + >), + cfg: params.cfg as Parameters[0]["cfg"], + agentId: params.agentId as string, + channel: params.channel as string, + accountId: params.accountId as string | undefined, + }) + : undefined; + const { onModelSelected, ...dispatcherPipeline } = pipeline ?? {}; await recordInboundSession({ storePath, sessionKey, @@ -198,13 +212,17 @@ export function createPluginRuntimeMock(overrides: DeepPartial = ctx: ctxPayload, cfg: params.cfg, dispatcherOptions: { + ...dispatcherPipeline, ...(params.dispatcherOptions as Record | undefined), deliver: async (payload, info) => { await delivery.deliver(payload, info); }, onError: delivery.onError, }, - replyOptions: params.replyOptions, + replyOptions: { + ...(onModelSelected ? { onModelSelected } : {}), + ...(params.replyOptions as Record | undefined), + }, replyResolver: params.replyResolver, }); return {