test(discord): isolate component runtime fixtures

This commit is contained in:
Peter Steinberger
2026-07-17 15:09:48 +01:00
parent 600ff3acb4
commit 4ec2f9db25
3 changed files with 49 additions and 45 deletions
@@ -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<typeof import("openclaw/plugin-sdk/channel-inbound")>();
const runMockedTurn = async (plan: Parameters<typeof actual.dispatchChannelInboundTurn>[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<typeof import("openclaw/plugin-sdk/channel-inbound")>(
"openclaw/plugin-sdk/channel-inbound",
);
type RunParams = Parameters<typeof actual.runChannelInboundEvent>[0];
return {
...actual,
runChannelInboundEvent: async (params: Parameters<typeof actual.runChannelInboundEvent>[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<typeof actual.dispatchChannelInboundTurn>[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;
@@ -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 () => {
@@ -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<PluginRuntime> =
replyOptions?: unknown;
replyResolver?: unknown;
}) => Promise<unknown>;
const pipeline = params.replyPipeline
? createChannelReplyPipeline({
...(params.replyPipeline as Omit<
Parameters<typeof createChannelReplyPipeline>[0],
"cfg" | "agentId" | "channel" | "accountId"
>),
cfg: params.cfg as Parameters<typeof createChannelReplyPipeline>[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<PluginRuntime> =
ctx: ctxPayload,
cfg: params.cfg,
dispatcherOptions: {
...dispatcherPipeline,
...(params.dispatcherOptions as Record<string, unknown> | undefined),
deliver: async (payload, info) => {
await delivery.deliver(payload, info);
},
onError: delivery.onError,
},
replyOptions: params.replyOptions,
replyOptions: {
...(onModelSelected ? { onModelSelected } : {}),
...(params.replyOptions as Record<string, unknown> | undefined),
},
replyResolver: params.replyResolver,
});
return {