mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
test: extract sendpayload outbound contract suite
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import type { ReplyPayload } from "openclaw/plugin-sdk/zalo";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
installSendPayloadContractSuite,
|
||||
primeSendMock,
|
||||
} from "../../../src/test-utils/send-payload-contract.js";
|
||||
import { zaloPlugin } from "./channel.js";
|
||||
|
||||
vi.mock("./send.js", () => ({
|
||||
@@ -25,78 +29,16 @@ describe("zaloPlugin outbound sendPayload", () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zl-1" });
|
||||
});
|
||||
|
||||
it("text-only delegates to sendText", async () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zl-t1" });
|
||||
|
||||
const result = await zaloPlugin.outbound!.sendPayload!(baseCtx({ text: "hello" }));
|
||||
|
||||
expect(mockedSend).toHaveBeenCalledWith("123456789", "hello", expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: "zalo", messageId: "zl-t1" });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zl-m1" });
|
||||
|
||||
const result = await zaloPlugin.outbound!.sendPayload!(
|
||||
baseCtx({ text: "cap", mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
|
||||
expect(mockedSend).toHaveBeenCalledWith(
|
||||
"123456789",
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "zalo" });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
mockedSend
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zl-1" })
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zl-2" });
|
||||
|
||||
const result = await zaloPlugin.outbound!.sendPayload!(
|
||||
baseCtx({
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(mockedSend).toHaveBeenCalledTimes(2);
|
||||
expect(mockedSend).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"123456789",
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(mockedSend).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"123456789",
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "zalo", messageId: "zl-2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const result = await zaloPlugin.outbound!.sendPayload!(baseCtx({}));
|
||||
|
||||
expect(mockedSend).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: "zalo", messageId: "" });
|
||||
});
|
||||
|
||||
it("chunking splits long text", async () => {
|
||||
mockedSend
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zl-c1" })
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zl-c2" });
|
||||
|
||||
const longText = "a".repeat(3000);
|
||||
const result = await zaloPlugin.outbound!.sendPayload!(baseCtx({ text: longText }));
|
||||
|
||||
// textChunkLimit is 2000 with chunkTextForOutbound, so it should split
|
||||
expect(mockedSend.mock.calls.length).toBeGreaterThanOrEqual(2);
|
||||
for (const call of mockedSend.mock.calls) {
|
||||
expect((call[1] as string).length).toBeLessThanOrEqual(2000);
|
||||
}
|
||||
expect(result).toMatchObject({ channel: "zalo" });
|
||||
installSendPayloadContractSuite({
|
||||
channel: "zalo",
|
||||
chunking: { mode: "split", longTextLength: 3000, maxChunkLength: 2000 },
|
||||
createHarness: ({ payload, sendResults }) => {
|
||||
primeSendMock(mockedSend, { ok: true, messageId: "zl-1" }, sendResults);
|
||||
return {
|
||||
run: async () => await zaloPlugin.outbound!.sendPayload!(baseCtx(payload)),
|
||||
sendMock: mockedSend,
|
||||
to: "123456789",
|
||||
};
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import type { ReplyPayload } from "openclaw/plugin-sdk/zalouser";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
installSendPayloadContractSuite,
|
||||
primeSendMock,
|
||||
} from "../../../src/test-utils/send-payload-contract.js";
|
||||
import { zalouserPlugin } from "./channel.js";
|
||||
|
||||
vi.mock("./send.js", () => ({
|
||||
@@ -40,15 +44,6 @@ describe("zalouserPlugin outbound sendPayload", () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-1" });
|
||||
});
|
||||
|
||||
it("text-only delegates to sendText", async () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-t1" });
|
||||
|
||||
const result = await zalouserPlugin.outbound!.sendPayload!(baseCtx({ text: "hello" }));
|
||||
|
||||
expect(mockedSend).toHaveBeenCalledWith("987654321", "hello", expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-t1" });
|
||||
});
|
||||
|
||||
it("group target delegates with isGroup=true and stripped threadId", async () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-g1" });
|
||||
|
||||
@@ -65,21 +60,6 @@ describe("zalouserPlugin outbound sendPayload", () => {
|
||||
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-g1" });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-m1" });
|
||||
|
||||
const result = await zalouserPlugin.outbound!.sendPayload!(
|
||||
baseCtx({ text: "cap", mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
|
||||
expect(mockedSend).toHaveBeenCalledWith(
|
||||
"987654321",
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "zalouser" });
|
||||
});
|
||||
|
||||
it("treats bare numeric targets as direct chats for backward compatibility", async () => {
|
||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-d1" });
|
||||
|
||||
@@ -112,55 +92,17 @@ describe("zalouserPlugin outbound sendPayload", () => {
|
||||
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-g-native" });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
mockedSend
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zlu-1" })
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zlu-2" });
|
||||
|
||||
const result = await zalouserPlugin.outbound!.sendPayload!(
|
||||
baseCtx({
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(mockedSend).toHaveBeenCalledTimes(2);
|
||||
expect(mockedSend).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"987654321",
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(mockedSend).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"987654321",
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const result = await zalouserPlugin.outbound!.sendPayload!(baseCtx({}));
|
||||
|
||||
expect(mockedSend).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: "zalouser", messageId: "" });
|
||||
});
|
||||
|
||||
it("chunking splits long text", async () => {
|
||||
mockedSend
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zlu-c1" })
|
||||
.mockResolvedValueOnce({ ok: true, messageId: "zlu-c2" });
|
||||
|
||||
const longText = "a".repeat(3000);
|
||||
const result = await zalouserPlugin.outbound!.sendPayload!(baseCtx({ text: longText }));
|
||||
|
||||
// textChunkLimit is 2000 with chunkTextForOutbound, so it should split
|
||||
expect(mockedSend.mock.calls.length).toBeGreaterThanOrEqual(2);
|
||||
for (const call of mockedSend.mock.calls) {
|
||||
expect((call[1] as string).length).toBeLessThanOrEqual(2000);
|
||||
}
|
||||
expect(result).toMatchObject({ channel: "zalouser" });
|
||||
installSendPayloadContractSuite({
|
||||
channel: "zalouser",
|
||||
chunking: { mode: "split", longTextLength: 3000, maxChunkLength: 2000 },
|
||||
createHarness: ({ payload, sendResults }) => {
|
||||
primeSendMock(mockedSend, { ok: true, messageId: "zlu-1" }, sendResults);
|
||||
return {
|
||||
run: async () => await zalouserPlugin.outbound!.sendPayload!(baseCtx(payload)),
|
||||
sendMock: mockedSend,
|
||||
to: "987654321",
|
||||
};
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,9 +1,17 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { describe, vi } from "vitest";
|
||||
import type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||
import {
|
||||
installSendPayloadContractSuite,
|
||||
primeSendMock,
|
||||
} from "../../../test-utils/send-payload-contract.js";
|
||||
import { createDirectTextMediaOutbound } from "./direct-text-media.js";
|
||||
|
||||
function makeOutbound() {
|
||||
const sendFn = vi.fn().mockResolvedValue({ messageId: "m1" });
|
||||
function createDirectHarness(params: {
|
||||
payload: ReplyPayload;
|
||||
sendResults?: Array<{ messageId: string }>;
|
||||
}) {
|
||||
const sendFn = vi.fn();
|
||||
primeSendMock(sendFn, { messageId: "m1" }, params.sendResults);
|
||||
const outbound = createDirectTextMediaOutbound({
|
||||
channel: "imessage",
|
||||
resolveSender: () => sendFn,
|
||||
@@ -24,94 +32,16 @@ function baseCtx(payload: ReplyPayload) {
|
||||
}
|
||||
|
||||
describe("createDirectTextMediaOutbound sendPayload", () => {
|
||||
it("text-only delegates to sendText", async () => {
|
||||
const { outbound, sendFn } = makeOutbound();
|
||||
const result = await outbound.sendPayload!(baseCtx({ text: "hello" }));
|
||||
|
||||
expect(sendFn).toHaveBeenCalledTimes(1);
|
||||
expect(sendFn).toHaveBeenCalledWith("user1", "hello", expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: "imessage", messageId: "m1" });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
const { outbound, sendFn } = makeOutbound();
|
||||
const result = await outbound.sendPayload!(
|
||||
baseCtx({ text: "cap", mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
|
||||
expect(sendFn).toHaveBeenCalledTimes(1);
|
||||
expect(sendFn).toHaveBeenCalledWith(
|
||||
"user1",
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "imessage", messageId: "m1" });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
const sendFn = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ messageId: "m1" })
|
||||
.mockResolvedValueOnce({ messageId: "m2" });
|
||||
const outbound = createDirectTextMediaOutbound({
|
||||
channel: "imessage",
|
||||
resolveSender: () => sendFn,
|
||||
resolveMaxBytes: () => undefined,
|
||||
buildTextOptions: (opts) => opts as never,
|
||||
buildMediaOptions: (opts) => opts as never,
|
||||
});
|
||||
const result = await outbound.sendPayload!(
|
||||
baseCtx({
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
}),
|
||||
);
|
||||
|
||||
expect(sendFn).toHaveBeenCalledTimes(2);
|
||||
expect(sendFn).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"user1",
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(sendFn).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"user1",
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "imessage", messageId: "m2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const { outbound, sendFn } = makeOutbound();
|
||||
const result = await outbound.sendPayload!(baseCtx({}));
|
||||
|
||||
expect(sendFn).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: "imessage", messageId: "" });
|
||||
});
|
||||
|
||||
it("chunking splits long text", async () => {
|
||||
const sendFn = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ messageId: "c1" })
|
||||
.mockResolvedValueOnce({ messageId: "c2" });
|
||||
const outbound = createDirectTextMediaOutbound({
|
||||
channel: "signal",
|
||||
resolveSender: () => sendFn,
|
||||
resolveMaxBytes: () => undefined,
|
||||
buildTextOptions: (opts) => opts as never,
|
||||
buildMediaOptions: (opts) => opts as never,
|
||||
});
|
||||
// textChunkLimit is 4000; generate text exceeding that
|
||||
const longText = "a".repeat(5000);
|
||||
const result = await outbound.sendPayload!(baseCtx({ text: longText }));
|
||||
|
||||
expect(sendFn.mock.calls.length).toBeGreaterThanOrEqual(2);
|
||||
// Each chunk should be within the limit
|
||||
for (const call of sendFn.mock.calls) {
|
||||
expect((call[1] as string).length).toBeLessThanOrEqual(4000);
|
||||
}
|
||||
expect(result).toMatchObject({ channel: "signal" });
|
||||
installSendPayloadContractSuite({
|
||||
channel: "imessage",
|
||||
chunking: { mode: "split", longTextLength: 5000, maxChunkLength: 4000 },
|
||||
createHarness: ({ payload, sendResults }) => {
|
||||
const { outbound, sendFn } = createDirectHarness({ payload, sendResults });
|
||||
return {
|
||||
run: async () => await outbound.sendPayload!(baseCtx(payload)),
|
||||
sendMock: sendFn,
|
||||
to: "user1",
|
||||
};
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,98 +1,37 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { describe, vi } from "vitest";
|
||||
import type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||
import {
|
||||
installSendPayloadContractSuite,
|
||||
primeSendMock,
|
||||
} from "../../../test-utils/send-payload-contract.js";
|
||||
import { discordOutbound } from "./discord.js";
|
||||
|
||||
function baseCtx(payload: ReplyPayload) {
|
||||
return {
|
||||
function createHarness(params: {
|
||||
payload: ReplyPayload;
|
||||
sendResults?: Array<{ messageId: string }>;
|
||||
}) {
|
||||
const sendDiscord = vi.fn();
|
||||
primeSendMock(sendDiscord, { messageId: "dc-1", channelId: "123456" }, params.sendResults);
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "channel:123456",
|
||||
text: "",
|
||||
payload,
|
||||
payload: params.payload,
|
||||
deps: {
|
||||
sendDiscord: vi.fn().mockResolvedValue({ messageId: "dc-1", channelId: "123456" }),
|
||||
sendDiscord,
|
||||
},
|
||||
};
|
||||
return {
|
||||
run: async () => await discordOutbound.sendPayload!(ctx),
|
||||
sendMock: sendDiscord,
|
||||
to: ctx.to,
|
||||
};
|
||||
}
|
||||
|
||||
describe("discordOutbound sendPayload", () => {
|
||||
it("text-only delegates to sendText", async () => {
|
||||
const ctx = baseCtx({ text: "hello" });
|
||||
const result = await discordOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendDiscord).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendDiscord).toHaveBeenCalledWith(
|
||||
"channel:123456",
|
||||
"hello",
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "discord" });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
const ctx = baseCtx({ text: "cap", mediaUrl: "https://example.com/a.jpg" });
|
||||
const result = await discordOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendDiscord).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendDiscord).toHaveBeenCalledWith(
|
||||
"channel:123456",
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "discord" });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
const sendDiscord = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ messageId: "dc-1", channelId: "123456" })
|
||||
.mockResolvedValueOnce({ messageId: "dc-2", channelId: "123456" });
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "channel:123456",
|
||||
text: "",
|
||||
payload: {
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
} as ReplyPayload,
|
||||
deps: { sendDiscord },
|
||||
};
|
||||
const result = await discordOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(sendDiscord).toHaveBeenCalledTimes(2);
|
||||
expect(sendDiscord).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"channel:123456",
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(sendDiscord).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"channel:123456",
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "discord", messageId: "dc-2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const ctx = baseCtx({});
|
||||
const result = await discordOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendDiscord).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: "discord", messageId: "" });
|
||||
});
|
||||
|
||||
it("text exceeding chunk limit is sent as-is when chunker is null", async () => {
|
||||
// Discord has chunker: null, so long text should be sent as a single message
|
||||
const ctx = baseCtx({ text: "a".repeat(3000) });
|
||||
const result = await discordOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendDiscord).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendDiscord).toHaveBeenCalledWith(
|
||||
"channel:123456",
|
||||
"a".repeat(3000),
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "discord" });
|
||||
installSendPayloadContractSuite({
|
||||
channel: "discord",
|
||||
chunking: { mode: "passthrough", longTextLength: 3000 },
|
||||
createHarness,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,92 +1,41 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { describe, vi } from "vitest";
|
||||
import type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||
import {
|
||||
installSendPayloadContractSuite,
|
||||
primeSendMock,
|
||||
} from "../../../test-utils/send-payload-contract.js";
|
||||
import { slackOutbound } from "./slack.js";
|
||||
|
||||
function baseCtx(payload: ReplyPayload) {
|
||||
return {
|
||||
function createHarness(params: {
|
||||
payload: ReplyPayload;
|
||||
sendResults?: Array<{ messageId: string }>;
|
||||
}) {
|
||||
const sendSlack = vi.fn();
|
||||
primeSendMock(
|
||||
sendSlack,
|
||||
{ messageId: "sl-1", channelId: "C12345", ts: "1234.5678" },
|
||||
params.sendResults,
|
||||
);
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "C12345",
|
||||
text: "",
|
||||
payload,
|
||||
payload: params.payload,
|
||||
deps: {
|
||||
sendSlack: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ messageId: "sl-1", channelId: "C12345", ts: "1234.5678" }),
|
||||
sendSlack,
|
||||
},
|
||||
};
|
||||
return {
|
||||
run: async () => await slackOutbound.sendPayload!(ctx),
|
||||
sendMock: sendSlack,
|
||||
to: ctx.to,
|
||||
};
|
||||
}
|
||||
|
||||
describe("slackOutbound sendPayload", () => {
|
||||
it("text-only delegates to sendText", async () => {
|
||||
const ctx = baseCtx({ text: "hello" });
|
||||
const result = await slackOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendSlack).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendSlack).toHaveBeenCalledWith("C12345", "hello", expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: "slack" });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
const ctx = baseCtx({ text: "cap", mediaUrl: "https://example.com/a.jpg" });
|
||||
const result = await slackOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendSlack).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendSlack).toHaveBeenCalledWith(
|
||||
"C12345",
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "slack" });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
const sendSlack = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ messageId: "sl-1", channelId: "C12345" })
|
||||
.mockResolvedValueOnce({ messageId: "sl-2", channelId: "C12345" });
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "C12345",
|
||||
text: "",
|
||||
payload: {
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
} as ReplyPayload,
|
||||
deps: { sendSlack },
|
||||
};
|
||||
const result = await slackOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(sendSlack).toHaveBeenCalledTimes(2);
|
||||
expect(sendSlack).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"C12345",
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(sendSlack).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"C12345",
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "slack", messageId: "sl-2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const ctx = baseCtx({});
|
||||
const result = await slackOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendSlack).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: "slack", messageId: "" });
|
||||
});
|
||||
|
||||
it("text exceeding chunk limit is sent as-is when chunker is null", async () => {
|
||||
// Slack has chunker: null, so long text should be sent as a single message
|
||||
const ctx = baseCtx({ text: "a".repeat(5000) });
|
||||
const result = await slackOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendSlack).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendSlack).toHaveBeenCalledWith("C12345", "a".repeat(5000), expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: "slack" });
|
||||
installSendPayloadContractSuite({
|
||||
channel: "slack",
|
||||
chunking: { mode: "passthrough", longTextLength: 5000 },
|
||||
createHarness,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,106 +1,37 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { describe, vi } from "vitest";
|
||||
import type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||
import {
|
||||
installSendPayloadContractSuite,
|
||||
primeSendMock,
|
||||
} from "../../../test-utils/send-payload-contract.js";
|
||||
import { whatsappOutbound } from "./whatsapp.js";
|
||||
|
||||
function baseCtx(payload: ReplyPayload) {
|
||||
return {
|
||||
function createHarness(params: {
|
||||
payload: ReplyPayload;
|
||||
sendResults?: Array<{ messageId: string }>;
|
||||
}) {
|
||||
const sendWhatsApp = vi.fn();
|
||||
primeSendMock(sendWhatsApp, { messageId: "wa-1" }, params.sendResults);
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "5511999999999@c.us",
|
||||
text: "",
|
||||
payload,
|
||||
payload: params.payload,
|
||||
deps: {
|
||||
sendWhatsApp: vi.fn().mockResolvedValue({ messageId: "wa-1" }),
|
||||
sendWhatsApp,
|
||||
},
|
||||
};
|
||||
return {
|
||||
run: async () => await whatsappOutbound.sendPayload!(ctx),
|
||||
sendMock: sendWhatsApp,
|
||||
to: ctx.to,
|
||||
};
|
||||
}
|
||||
|
||||
describe("whatsappOutbound sendPayload", () => {
|
||||
it("text-only delegates to sendText", async () => {
|
||||
const ctx = baseCtx({ text: "hello" });
|
||||
const result = await whatsappOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendWhatsApp).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendWhatsApp).toHaveBeenCalledWith(
|
||||
"5511999999999@c.us",
|
||||
"hello",
|
||||
expect.any(Object),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "whatsapp", messageId: "wa-1" });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
const ctx = baseCtx({ text: "cap", mediaUrl: "https://example.com/a.jpg" });
|
||||
const result = await whatsappOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendWhatsApp).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.deps.sendWhatsApp).toHaveBeenCalledWith(
|
||||
"5511999999999@c.us",
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "whatsapp" });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
const sendWhatsApp = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ messageId: "wa-1" })
|
||||
.mockResolvedValueOnce({ messageId: "wa-2" });
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "5511999999999@c.us",
|
||||
text: "",
|
||||
payload: {
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
} as ReplyPayload,
|
||||
deps: { sendWhatsApp },
|
||||
};
|
||||
const result = await whatsappOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(sendWhatsApp).toHaveBeenCalledTimes(2);
|
||||
expect(sendWhatsApp).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
"5511999999999@c.us",
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(sendWhatsApp).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
"5511999999999@c.us",
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: "whatsapp", messageId: "wa-2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const ctx = baseCtx({});
|
||||
const result = await whatsappOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(ctx.deps.sendWhatsApp).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: "whatsapp", messageId: "" });
|
||||
});
|
||||
|
||||
it("chunking splits long text", async () => {
|
||||
const sendWhatsApp = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ messageId: "wa-c1" })
|
||||
.mockResolvedValueOnce({ messageId: "wa-c2" });
|
||||
const longText = "a".repeat(5000);
|
||||
const ctx = {
|
||||
cfg: {},
|
||||
to: "5511999999999@c.us",
|
||||
text: "",
|
||||
payload: { text: longText } as ReplyPayload,
|
||||
deps: { sendWhatsApp },
|
||||
};
|
||||
const result = await whatsappOutbound.sendPayload!(ctx);
|
||||
|
||||
expect(sendWhatsApp.mock.calls.length).toBeGreaterThanOrEqual(2);
|
||||
for (const call of sendWhatsApp.mock.calls) {
|
||||
expect((call[1] as string).length).toBeLessThanOrEqual(4000);
|
||||
}
|
||||
expect(result).toMatchObject({ channel: "whatsapp" });
|
||||
installSendPayloadContractSuite({
|
||||
channel: "whatsapp",
|
||||
chunking: { mode: "split", longTextLength: 5000, maxChunkLength: 4000 },
|
||||
createHarness,
|
||||
});
|
||||
});
|
||||
|
||||
138
src/test-utils/send-payload-contract.ts
Normal file
138
src/test-utils/send-payload-contract.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { expect, it, type Mock } from "vitest";
|
||||
|
||||
type PayloadLike = {
|
||||
mediaUrl?: string;
|
||||
mediaUrls?: string[];
|
||||
text?: string;
|
||||
};
|
||||
|
||||
type SendResultLike = {
|
||||
messageId: string;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
type ChunkingMode =
|
||||
| {
|
||||
longTextLength: number;
|
||||
maxChunkLength: number;
|
||||
mode: "split";
|
||||
}
|
||||
| {
|
||||
longTextLength: number;
|
||||
mode: "passthrough";
|
||||
};
|
||||
|
||||
export function installSendPayloadContractSuite(params: {
|
||||
channel: string;
|
||||
chunking: ChunkingMode;
|
||||
createHarness: (params: { payload: PayloadLike; sendResults?: SendResultLike[] }) => {
|
||||
run: () => Promise<Record<string, unknown>>;
|
||||
sendMock: Mock;
|
||||
to: string;
|
||||
};
|
||||
}) {
|
||||
it("text-only delegates to sendText", async () => {
|
||||
const { run, sendMock, to } = params.createHarness({
|
||||
payload: { text: "hello" },
|
||||
});
|
||||
const result = await run();
|
||||
|
||||
expect(sendMock).toHaveBeenCalledTimes(1);
|
||||
expect(sendMock).toHaveBeenCalledWith(to, "hello", expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: params.channel });
|
||||
});
|
||||
|
||||
it("single media delegates to sendMedia", async () => {
|
||||
const { run, sendMock, to } = params.createHarness({
|
||||
payload: { text: "cap", mediaUrl: "https://example.com/a.jpg" },
|
||||
});
|
||||
const result = await run();
|
||||
|
||||
expect(sendMock).toHaveBeenCalledTimes(1);
|
||||
expect(sendMock).toHaveBeenCalledWith(
|
||||
to,
|
||||
"cap",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/a.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: params.channel });
|
||||
});
|
||||
|
||||
it("multi-media iterates URLs with caption on first", async () => {
|
||||
const { run, sendMock, to } = params.createHarness({
|
||||
payload: {
|
||||
text: "caption",
|
||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
||||
},
|
||||
sendResults: [{ messageId: "m-1" }, { messageId: "m-2" }],
|
||||
});
|
||||
const result = await run();
|
||||
|
||||
expect(sendMock).toHaveBeenCalledTimes(2);
|
||||
expect(sendMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
to,
|
||||
"caption",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/1.jpg" }),
|
||||
);
|
||||
expect(sendMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
to,
|
||||
"",
|
||||
expect.objectContaining({ mediaUrl: "https://example.com/2.jpg" }),
|
||||
);
|
||||
expect(result).toMatchObject({ channel: params.channel, messageId: "m-2" });
|
||||
});
|
||||
|
||||
it("empty payload returns no-op", async () => {
|
||||
const { run, sendMock } = params.createHarness({ payload: {} });
|
||||
const result = await run();
|
||||
|
||||
expect(sendMock).not.toHaveBeenCalled();
|
||||
expect(result).toEqual({ channel: params.channel, messageId: "" });
|
||||
});
|
||||
|
||||
if (params.chunking.mode === "passthrough") {
|
||||
it("text exceeding chunk limit is sent as-is when chunker is null", async () => {
|
||||
const text = "a".repeat(params.chunking.longTextLength);
|
||||
const { run, sendMock, to } = params.createHarness({ payload: { text } });
|
||||
const result = await run();
|
||||
|
||||
expect(sendMock).toHaveBeenCalledTimes(1);
|
||||
expect(sendMock).toHaveBeenCalledWith(to, text, expect.any(Object));
|
||||
expect(result).toMatchObject({ channel: params.channel });
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const chunking = params.chunking;
|
||||
|
||||
it("chunking splits long text", async () => {
|
||||
const text = "a".repeat(chunking.longTextLength);
|
||||
const { run, sendMock } = params.createHarness({
|
||||
payload: { text },
|
||||
sendResults: [{ messageId: "c-1" }, { messageId: "c-2" }],
|
||||
});
|
||||
const result = await run();
|
||||
|
||||
expect(sendMock.mock.calls.length).toBeGreaterThanOrEqual(2);
|
||||
for (const call of sendMock.mock.calls) {
|
||||
expect((call[1] as string).length).toBeLessThanOrEqual(chunking.maxChunkLength);
|
||||
}
|
||||
expect(result).toMatchObject({ channel: params.channel });
|
||||
});
|
||||
}
|
||||
|
||||
export function primeSendMock(
|
||||
sendMock: Mock,
|
||||
fallbackResult: Record<string, unknown>,
|
||||
sendResults: SendResultLike[] = [],
|
||||
) {
|
||||
sendMock.mockReset();
|
||||
if (sendResults.length === 0) {
|
||||
sendMock.mockResolvedValue(fallbackResult);
|
||||
return;
|
||||
}
|
||||
for (const result of sendResults) {
|
||||
sendMock.mockResolvedValueOnce(result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user