mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-28 05:16:23 -06:00
test(discord): isolate component runtime fixtures
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user