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 type { ReplyPayload } from "openclaw/plugin-sdk/zalo";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import {
|
||||||
|
installSendPayloadContractSuite,
|
||||||
|
primeSendMock,
|
||||||
|
} from "../../../src/test-utils/send-payload-contract.js";
|
||||||
import { zaloPlugin } from "./channel.js";
|
import { zaloPlugin } from "./channel.js";
|
||||||
|
|
||||||
vi.mock("./send.js", () => ({
|
vi.mock("./send.js", () => ({
|
||||||
@@ -25,78 +29,16 @@ describe("zaloPlugin outbound sendPayload", () => {
|
|||||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zl-1" });
|
mockedSend.mockResolvedValue({ ok: true, messageId: "zl-1" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("text-only delegates to sendText", async () => {
|
installSendPayloadContractSuite({
|
||||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zl-t1" });
|
channel: "zalo",
|
||||||
|
chunking: { mode: "split", longTextLength: 3000, maxChunkLength: 2000 },
|
||||||
const result = await zaloPlugin.outbound!.sendPayload!(baseCtx({ text: "hello" }));
|
createHarness: ({ payload, sendResults }) => {
|
||||||
|
primeSendMock(mockedSend, { ok: true, messageId: "zl-1" }, sendResults);
|
||||||
expect(mockedSend).toHaveBeenCalledWith("123456789", "hello", expect.any(Object));
|
return {
|
||||||
expect(result).toMatchObject({ channel: "zalo", messageId: "zl-t1" });
|
run: async () => await zaloPlugin.outbound!.sendPayload!(baseCtx(payload)),
|
||||||
});
|
sendMock: mockedSend,
|
||||||
|
to: "123456789",
|
||||||
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" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import type { ReplyPayload } from "openclaw/plugin-sdk/zalouser";
|
import type { ReplyPayload } from "openclaw/plugin-sdk/zalouser";
|
||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import {
|
||||||
|
installSendPayloadContractSuite,
|
||||||
|
primeSendMock,
|
||||||
|
} from "../../../src/test-utils/send-payload-contract.js";
|
||||||
import { zalouserPlugin } from "./channel.js";
|
import { zalouserPlugin } from "./channel.js";
|
||||||
|
|
||||||
vi.mock("./send.js", () => ({
|
vi.mock("./send.js", () => ({
|
||||||
@@ -40,15 +44,6 @@ describe("zalouserPlugin outbound sendPayload", () => {
|
|||||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-1" });
|
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 () => {
|
it("group target delegates with isGroup=true and stripped threadId", async () => {
|
||||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-g1" });
|
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-g1" });
|
||||||
|
|
||||||
@@ -65,21 +60,6 @@ describe("zalouserPlugin outbound sendPayload", () => {
|
|||||||
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-g1" });
|
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 () => {
|
it("treats bare numeric targets as direct chats for backward compatibility", async () => {
|
||||||
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-d1" });
|
mockedSend.mockResolvedValue({ ok: true, messageId: "zlu-d1" });
|
||||||
|
|
||||||
@@ -112,55 +92,17 @@ describe("zalouserPlugin outbound sendPayload", () => {
|
|||||||
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-g-native" });
|
expect(result).toMatchObject({ channel: "zalouser", messageId: "zlu-g-native" });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("multi-media iterates URLs with caption on first", async () => {
|
installSendPayloadContractSuite({
|
||||||
mockedSend
|
channel: "zalouser",
|
||||||
.mockResolvedValueOnce({ ok: true, messageId: "zlu-1" })
|
chunking: { mode: "split", longTextLength: 3000, maxChunkLength: 2000 },
|
||||||
.mockResolvedValueOnce({ ok: true, messageId: "zlu-2" });
|
createHarness: ({ payload, sendResults }) => {
|
||||||
|
primeSendMock(mockedSend, { ok: true, messageId: "zlu-1" }, sendResults);
|
||||||
const result = await zalouserPlugin.outbound!.sendPayload!(
|
return {
|
||||||
baseCtx({
|
run: async () => await zalouserPlugin.outbound!.sendPayload!(baseCtx(payload)),
|
||||||
text: "caption",
|
sendMock: mockedSend,
|
||||||
mediaUrls: ["https://example.com/1.jpg", "https://example.com/2.jpg"],
|
to: "987654321",
|
||||||
}),
|
};
|
||||||
);
|
},
|
||||||
|
|
||||||
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" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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 type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||||
|
import {
|
||||||
|
installSendPayloadContractSuite,
|
||||||
|
primeSendMock,
|
||||||
|
} from "../../../test-utils/send-payload-contract.js";
|
||||||
import { createDirectTextMediaOutbound } from "./direct-text-media.js";
|
import { createDirectTextMediaOutbound } from "./direct-text-media.js";
|
||||||
|
|
||||||
function makeOutbound() {
|
function createDirectHarness(params: {
|
||||||
const sendFn = vi.fn().mockResolvedValue({ messageId: "m1" });
|
payload: ReplyPayload;
|
||||||
|
sendResults?: Array<{ messageId: string }>;
|
||||||
|
}) {
|
||||||
|
const sendFn = vi.fn();
|
||||||
|
primeSendMock(sendFn, { messageId: "m1" }, params.sendResults);
|
||||||
const outbound = createDirectTextMediaOutbound({
|
const outbound = createDirectTextMediaOutbound({
|
||||||
channel: "imessage",
|
channel: "imessage",
|
||||||
resolveSender: () => sendFn,
|
resolveSender: () => sendFn,
|
||||||
@@ -24,94 +32,16 @@ function baseCtx(payload: ReplyPayload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe("createDirectTextMediaOutbound sendPayload", () => {
|
describe("createDirectTextMediaOutbound sendPayload", () => {
|
||||||
it("text-only delegates to sendText", async () => {
|
installSendPayloadContractSuite({
|
||||||
const { outbound, sendFn } = makeOutbound();
|
channel: "imessage",
|
||||||
const result = await outbound.sendPayload!(baseCtx({ text: "hello" }));
|
chunking: { mode: "split", longTextLength: 5000, maxChunkLength: 4000 },
|
||||||
|
createHarness: ({ payload, sendResults }) => {
|
||||||
expect(sendFn).toHaveBeenCalledTimes(1);
|
const { outbound, sendFn } = createDirectHarness({ payload, sendResults });
|
||||||
expect(sendFn).toHaveBeenCalledWith("user1", "hello", expect.any(Object));
|
return {
|
||||||
expect(result).toMatchObject({ channel: "imessage", messageId: "m1" });
|
run: async () => await outbound.sendPayload!(baseCtx(payload)),
|
||||||
});
|
sendMock: sendFn,
|
||||||
|
to: "user1",
|
||||||
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" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||||
|
import {
|
||||||
|
installSendPayloadContractSuite,
|
||||||
|
primeSendMock,
|
||||||
|
} from "../../../test-utils/send-payload-contract.js";
|
||||||
import { discordOutbound } from "./discord.js";
|
import { discordOutbound } from "./discord.js";
|
||||||
|
|
||||||
function baseCtx(payload: ReplyPayload) {
|
function createHarness(params: {
|
||||||
return {
|
payload: ReplyPayload;
|
||||||
|
sendResults?: Array<{ messageId: string }>;
|
||||||
|
}) {
|
||||||
|
const sendDiscord = vi.fn();
|
||||||
|
primeSendMock(sendDiscord, { messageId: "dc-1", channelId: "123456" }, params.sendResults);
|
||||||
|
const ctx = {
|
||||||
cfg: {},
|
cfg: {},
|
||||||
to: "channel:123456",
|
to: "channel:123456",
|
||||||
text: "",
|
text: "",
|
||||||
payload,
|
payload: params.payload,
|
||||||
deps: {
|
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", () => {
|
describe("discordOutbound sendPayload", () => {
|
||||||
it("text-only delegates to sendText", async () => {
|
installSendPayloadContractSuite({
|
||||||
const ctx = baseCtx({ text: "hello" });
|
channel: "discord",
|
||||||
const result = await discordOutbound.sendPayload!(ctx);
|
chunking: { mode: "passthrough", longTextLength: 3000 },
|
||||||
|
createHarness,
|
||||||
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" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||||
|
import {
|
||||||
|
installSendPayloadContractSuite,
|
||||||
|
primeSendMock,
|
||||||
|
} from "../../../test-utils/send-payload-contract.js";
|
||||||
import { slackOutbound } from "./slack.js";
|
import { slackOutbound } from "./slack.js";
|
||||||
|
|
||||||
function baseCtx(payload: ReplyPayload) {
|
function createHarness(params: {
|
||||||
return {
|
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: {},
|
cfg: {},
|
||||||
to: "C12345",
|
to: "C12345",
|
||||||
text: "",
|
text: "",
|
||||||
payload,
|
payload: params.payload,
|
||||||
deps: {
|
deps: {
|
||||||
sendSlack: vi
|
sendSlack,
|
||||||
.fn()
|
|
||||||
.mockResolvedValue({ messageId: "sl-1", channelId: "C12345", ts: "1234.5678" }),
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
return {
|
||||||
|
run: async () => await slackOutbound.sendPayload!(ctx),
|
||||||
|
sendMock: sendSlack,
|
||||||
|
to: ctx.to,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("slackOutbound sendPayload", () => {
|
describe("slackOutbound sendPayload", () => {
|
||||||
it("text-only delegates to sendText", async () => {
|
installSendPayloadContractSuite({
|
||||||
const ctx = baseCtx({ text: "hello" });
|
channel: "slack",
|
||||||
const result = await slackOutbound.sendPayload!(ctx);
|
chunking: { mode: "passthrough", longTextLength: 5000 },
|
||||||
|
createHarness,
|
||||||
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" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 type { ReplyPayload } from "../../../auto-reply/types.js";
|
||||||
|
import {
|
||||||
|
installSendPayloadContractSuite,
|
||||||
|
primeSendMock,
|
||||||
|
} from "../../../test-utils/send-payload-contract.js";
|
||||||
import { whatsappOutbound } from "./whatsapp.js";
|
import { whatsappOutbound } from "./whatsapp.js";
|
||||||
|
|
||||||
function baseCtx(payload: ReplyPayload) {
|
function createHarness(params: {
|
||||||
return {
|
payload: ReplyPayload;
|
||||||
|
sendResults?: Array<{ messageId: string }>;
|
||||||
|
}) {
|
||||||
|
const sendWhatsApp = vi.fn();
|
||||||
|
primeSendMock(sendWhatsApp, { messageId: "wa-1" }, params.sendResults);
|
||||||
|
const ctx = {
|
||||||
cfg: {},
|
cfg: {},
|
||||||
to: "5511999999999@c.us",
|
to: "5511999999999@c.us",
|
||||||
text: "",
|
text: "",
|
||||||
payload,
|
payload: params.payload,
|
||||||
deps: {
|
deps: {
|
||||||
sendWhatsApp: vi.fn().mockResolvedValue({ messageId: "wa-1" }),
|
sendWhatsApp,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
return {
|
||||||
|
run: async () => await whatsappOutbound.sendPayload!(ctx),
|
||||||
|
sendMock: sendWhatsApp,
|
||||||
|
to: ctx.to,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("whatsappOutbound sendPayload", () => {
|
describe("whatsappOutbound sendPayload", () => {
|
||||||
it("text-only delegates to sendText", async () => {
|
installSendPayloadContractSuite({
|
||||||
const ctx = baseCtx({ text: "hello" });
|
channel: "whatsapp",
|
||||||
const result = await whatsappOutbound.sendPayload!(ctx);
|
chunking: { mode: "split", longTextLength: 5000, maxChunkLength: 4000 },
|
||||||
|
createHarness,
|
||||||
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" });
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
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