|
1 | 1 | // 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"; |
3 | 4 | import { zaloMessageActions } from "./actions.js"; |
4 | 5 | import type { OpenClawConfig } from "./runtime-api.js"; |
5 | 6 |
|
@@ -33,3 +34,93 @@ describe("zaloMessageActions.describeMessageTool", () => { |
33 | 34 | expect(zaloMessageActions.supportsAction?.({ action: "react" })).toBe(false); |
34 | 35 | }); |
35 | 36 | }); |
| 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 | +}); |
0 commit comments