Skip to content

Commit 36440de

Browse files
authored
fix(zalo): deliver text when optional media is blank (#114450)
* fix(zalo): preserve text sends with blank media * test(zalo): compare serialized Bot API payload safely * test(zalo): prove message delivery over real HTTP
1 parent 2bd4858 commit 36440de

3 files changed

Lines changed: 118 additions & 2 deletions

File tree

‎extensions/zalo/src/actions.test.ts‎

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Zalo tests cover actions plugin behavior.
2-
import { describe, expect, it } from "vitest";
2+
import http from "node:http";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
34
import { zaloMessageActions } from "./actions.js";
45
import type { OpenClawConfig } from "./runtime-api.js";
56

@@ -33,3 +34,93 @@ describe("zaloMessageActions.describeMessageTool", () => {
3334
expect(zaloMessageActions.supportsAction?.({ action: "react" })).toBe(false);
3435
});
3536
});
37+
38+
describe("zaloMessageActions.handleAction", () => {
39+
afterEach(() => {
40+
vi.restoreAllMocks();
41+
vi.unstubAllEnvs();
42+
});
43+
44+
it("sends whitespace-only media as text over the real Bot API HTTP transport", async () => {
45+
const handleAction = zaloMessageActions.handleAction;
46+
if (!handleAction) {
47+
throw new Error("Expected Zalo message action handler");
48+
}
49+
50+
const requests: Array<{
51+
method: string | undefined;
52+
path: string | undefined;
53+
contentType: string | undefined;
54+
body: string;
55+
}> = [];
56+
const server = http.createServer((request, response) => {
57+
let body = "";
58+
request.setEncoding("utf8");
59+
request.on("data", (chunk: string) => {
60+
body += chunk;
61+
});
62+
request.on("end", () => {
63+
requests.push({
64+
method: request.method,
65+
path: request.url,
66+
contentType: request.headers["content-type"],
67+
body,
68+
});
69+
response.writeHead(200, { "content-type": "application/json" });
70+
response.end(
71+
JSON.stringify({
72+
ok: true,
73+
result: { message_id: "z-msg-with-blank-media" },
74+
}),
75+
);
76+
});
77+
});
78+
await new Promise<void>((resolve) => {
79+
server.listen(0, "127.0.0.1", resolve);
80+
});
81+
const { port } = server.address() as { port: number };
82+
vi.stubEnv("ZALO_API_URL", `http://127.0.0.1:${port}/zalo/`);
83+
84+
try {
85+
const result = await handleAction({
86+
channel: "zalo",
87+
action: "send",
88+
params: {
89+
to: "dm-chat-blank-media",
90+
message: "hello there",
91+
media: " ",
92+
},
93+
cfg: {
94+
channels: {
95+
zalo: {
96+
enabled: true,
97+
botToken: "test-zalo-token",
98+
},
99+
},
100+
},
101+
accountId: "default",
102+
});
103+
104+
expect(result.details).toEqual({
105+
ok: true,
106+
to: "dm-chat-blank-media",
107+
messageId: "z-msg-with-blank-media",
108+
});
109+
expect(requests).toEqual([
110+
{
111+
method: "POST",
112+
path: "/zalo/bottest-zalo-token/sendMessage",
113+
contentType: "application/json",
114+
body: JSON.stringify({
115+
chat_id: "dm-chat-blank-media",
116+
text: "hello there",
117+
}),
118+
},
119+
]);
120+
} finally {
121+
await new Promise<void>((resolve) => {
122+
server.close(() => resolve());
123+
});
124+
}
125+
});
126+
});

‎extensions/zalo/src/send.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,31 @@ describe("zalo send", () => {
106106
expect(successful.receipt.parts[0]?.kind).toBe("media");
107107
});
108108

109+
it("sends text through the message API when the media URL is whitespace", async () => {
110+
sendMessageMock.mockResolvedValueOnce({
111+
ok: true,
112+
result: { message_id: "z-msg-with-blank-media" },
113+
});
114+
115+
const result = await sendMessageZalo("dm-chat-blank-media", "hello there", {
116+
token: "zalo-token",
117+
mediaUrl: " ",
118+
});
119+
120+
expect(sendMessageMock).toHaveBeenCalledWith(
121+
"zalo-token",
122+
{
123+
chat_id: "dm-chat-blank-media",
124+
text: "hello there",
125+
},
126+
undefined,
127+
);
128+
expect(sendPhotoMock).not.toHaveBeenCalled();
129+
expect(requireSuccessfulSend(result, "z-msg-with-blank-media").receipt.parts[0]?.kind).toBe(
130+
"text",
131+
);
132+
});
133+
109134
it("fails fast for missing token or blank photo URLs", async () => {
110135
const missingToken = await sendMessageZalo("dm-chat-3", "hello", {});
111136
expectFailedSend(missingToken, "No Zalo bot token configured");

‎extensions/zalo/src/send.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export async function sendMessageZalo(
155155
}
156156
const { context } = resolved;
157157

158-
if (options.mediaUrl) {
158+
if (options.mediaUrl && (options.mediaUrl.trim() || !text)) {
159159
return sendPhotoZalo(context.chatId, options.mediaUrl, {
160160
...options,
161161
token: context.token,

0 commit comments

Comments
 (0)