Files
openclaw/src/plugin-sdk/inbound-reply-dispatch.test.ts
2026-04-29 23:16:16 +01:00

68 lines
2.3 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import type { DispatchReplyWithBufferedBlockDispatcher } from "../auto-reply/reply/provider-dispatcher.types.js";
import type { FinalizedMsgContext } from "../auto-reply/templating.js";
import type { RecordInboundSession } from "../channels/session.types.js";
import type { OpenClawConfig } from "../config/types.openclaw.js";
import { recordInboundSessionAndDispatchReply } from "./inbound-reply-dispatch.js";
describe("recordInboundSessionAndDispatchReply", () => {
it("delegates record and dispatch through the channel turn kernel once", async () => {
const recordInboundSession = vi.fn(async () => undefined) as unknown as RecordInboundSession;
const deliver = vi.fn(async () => undefined);
const dispatchReplyWithBufferedBlockDispatcher = vi.fn(async (params) => {
await params.dispatcherOptions.deliver(
{
text: "hello",
mediaUrls: ["https://example.com/a.png"],
},
{ kind: "final" },
);
return {
queuedFinal: true,
counts: { tool: 0, block: 0, final: 1 },
};
}) as DispatchReplyWithBufferedBlockDispatcher;
const ctxPayload = {
Body: "body",
RawBody: "body",
CommandBody: "body",
From: "sender",
To: "target",
SessionKey: "agent:main:test:peer",
Provider: "test",
Surface: "test",
} as FinalizedMsgContext;
await recordInboundSessionAndDispatchReply({
cfg: {} as OpenClawConfig,
channel: "test",
accountId: "default",
agentId: "main",
routeSessionKey: "agent:main:test:peer",
storePath: "/tmp/sessions.json",
ctxPayload,
recordInboundSession,
dispatchReplyWithBufferedBlockDispatcher,
deliver,
onRecordError: vi.fn(),
onDispatchError: vi.fn(),
});
expect(recordInboundSession).toHaveBeenCalledTimes(1);
expect(recordInboundSession).toHaveBeenCalledWith(
expect.objectContaining({
sessionKey: "agent:main:test:peer",
ctx: ctxPayload,
}),
);
expect(dispatchReplyWithBufferedBlockDispatcher).toHaveBeenCalledTimes(1);
expect(deliver).toHaveBeenCalledWith({
text: "hello",
mediaUrls: ["https://example.com/a.png"],
mediaUrl: undefined,
sensitiveMedia: undefined,
replyToId: undefined,
});
});
});