Skip to content

Commit 94de89b

Browse files
committed
test(zalo): prove message delivery over real HTTP
1 parent 5835974 commit 94de89b

1 file changed

Lines changed: 75 additions & 33 deletions

File tree

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Zalo tests cover actions plugin behavior.
2+
import http from "node:http";
23
import { afterEach, describe, expect, it, vi } from "vitest";
34
import { zaloMessageActions } from "./actions.js";
45
import type { OpenClawConfig } from "./runtime-api.js";
@@ -37,48 +38,89 @@ describe("zaloMessageActions.describeMessageTool", () => {
3738
describe("zaloMessageActions.handleAction", () => {
3839
afterEach(() => {
3940
vi.restoreAllMocks();
41+
vi.unstubAllEnvs();
4042
});
4143

42-
it("sends text to the Bot API when the message tool supplies whitespace-only media", async () => {
43-
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
44-
Response.json({
45-
ok: true,
46-
result: { message_id: "z-msg-with-blank-media" },
47-
}),
48-
);
44+
it("sends whitespace-only media as text over the real Bot API HTTP transport", async () => {
4945
const handleAction = zaloMessageActions.handleAction;
5046
if (!handleAction) {
5147
throw new Error("Expected Zalo message action handler");
5248
}
5349

54-
await handleAction({
55-
channel: "zalo",
56-
action: "send",
57-
params: {
58-
to: "dm-chat-blank-media",
59-
message: "hello there",
60-
media: " ",
61-
},
62-
cfg: {
63-
channels: {
64-
zalo: {
65-
enabled: true,
66-
botToken: "test-zalo-token",
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+
},
6799
},
68100
},
69-
},
70-
accountId: "default",
71-
});
101+
accountId: "default",
102+
});
72103

73-
expect(fetchMock).toHaveBeenCalledOnce();
74-
const [url, request] = fetchMock.mock.calls[0] ?? [];
75-
expect(url).toBe("https://bot-api.zaloplatforms.com/bottest-zalo-token/sendMessage");
76-
expect(request?.method).toBe("POST");
77-
expect(request?.body).toBe(
78-
JSON.stringify({
79-
chat_id: "dm-chat-blank-media",
80-
text: "hello there",
81-
}),
82-
);
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+
}
83125
});
84126
});

0 commit comments

Comments
 (0)