|
| 1 | +// Zalo tests cover the configured gateway lifecycle through the real HTTP client. |
| 2 | +import { createServer, type Server } from "node:http"; |
| 3 | +import { |
| 4 | + createEmptyPluginRegistry, |
| 5 | + createStartAccountContext, |
| 6 | + setActivePluginRegistry, |
| 7 | +} from "openclaw/plugin-sdk/channel-test-helpers"; |
| 8 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 9 | +import { resolveZaloAccount } from "./accounts.js"; |
| 10 | +import { zaloPlugin } from "./channel.js"; |
| 11 | +import { setZaloRuntime, type OpenClawConfig, type PluginRuntime } from "./runtime-api.js"; |
| 12 | + |
| 13 | +const originalZaloApiUrl = process.env.ZALO_API_URL; |
| 14 | + |
| 15 | +async function listen(server: Server): Promise<string> { |
| 16 | + await new Promise<void>((resolve, reject) => { |
| 17 | + server.once("error", reject); |
| 18 | + server.listen(0, "127.0.0.1", () => { |
| 19 | + server.off("error", reject); |
| 20 | + resolve(); |
| 21 | + }); |
| 22 | + }); |
| 23 | + const address = server.address(); |
| 24 | + if (!address || typeof address === "string") { |
| 25 | + throw new Error("expected loopback Zalo API address"); |
| 26 | + } |
| 27 | + return `http://127.0.0.1:${String(address.port)}`; |
| 28 | +} |
| 29 | + |
| 30 | +async function close(server: Server): Promise<void> { |
| 31 | + await new Promise<void>((resolve, reject) => { |
| 32 | + server.close((error) => (error ? reject(error) : resolve())); |
| 33 | + }); |
| 34 | +} |
| 35 | + |
| 36 | +function requireStartAccount() { |
| 37 | + const startAccount = zaloPlugin.gateway?.startAccount; |
| 38 | + if (!startAccount) { |
| 39 | + throw new Error("expected Zalo gateway startAccount"); |
| 40 | + } |
| 41 | + return startAccount; |
| 42 | +} |
| 43 | + |
| 44 | +describe("configured Zalo gateway with a pre-aborted lifecycle", () => { |
| 45 | + let server: Server; |
| 46 | + let apiMethods: string[]; |
| 47 | + |
| 48 | + beforeEach(async () => { |
| 49 | + apiMethods = []; |
| 50 | + server = createServer((request, response) => { |
| 51 | + const method = request.url?.match(/\/bot[^/]+\/([^/?]+)/u)?.[1] ?? "unknown"; |
| 52 | + apiMethods.push(method); |
| 53 | + response.writeHead(200, { "content-type": "application/json" }); |
| 54 | + response.end( |
| 55 | + JSON.stringify({ |
| 56 | + ok: true, |
| 57 | + result: |
| 58 | + method === "getMe" |
| 59 | + ? { |
| 60 | + id: "loopback-bot", |
| 61 | + account_name: "Loopback proof bot", |
| 62 | + account_type: "BOT", |
| 63 | + can_join_groups: false, |
| 64 | + } |
| 65 | + : { url: "" }, |
| 66 | + }), |
| 67 | + ); |
| 68 | + }); |
| 69 | + process.env.ZALO_API_URL = await listen(server); |
| 70 | + setZaloRuntime({} as PluginRuntime); |
| 71 | + setActivePluginRegistry(createEmptyPluginRegistry()); |
| 72 | + }); |
| 73 | + |
| 74 | + afterEach(async () => { |
| 75 | + if (originalZaloApiUrl === undefined) { |
| 76 | + delete process.env.ZALO_API_URL; |
| 77 | + } else { |
| 78 | + process.env.ZALO_API_URL = originalZaloApiUrl; |
| 79 | + } |
| 80 | + setActivePluginRegistry(createEmptyPluginRegistry()); |
| 81 | + await close(server); |
| 82 | + }); |
| 83 | + |
| 84 | + it.each([ |
| 85 | + { mode: "polling", channel: { botToken: "loopback-proof-token" } }, |
| 86 | + { |
| 87 | + mode: "webhook", |
| 88 | + channel: { |
| 89 | + botToken: "loopback-proof-token", |
| 90 | + webhookUrl: "https://example.invalid/hooks/zalo", |
| 91 | + webhookSecret: "loopback-proof-secret", |
| 92 | + }, |
| 93 | + }, |
| 94 | + ] as const)("performs only the account probe in $mode mode", async ({ channel }) => { |
| 95 | + const cfg = { channels: { zalo: channel } } as OpenClawConfig; |
| 96 | + const account = resolveZaloAccount({ cfg }); |
| 97 | + const abort = new AbortController(); |
| 98 | + abort.abort(); |
| 99 | + const registry = createEmptyPluginRegistry(); |
| 100 | + setActivePluginRegistry(registry); |
| 101 | + |
| 102 | + await requireStartAccount()( |
| 103 | + createStartAccountContext({ |
| 104 | + account, |
| 105 | + cfg, |
| 106 | + abortSignal: abort.signal, |
| 107 | + }), |
| 108 | + ); |
| 109 | + |
| 110 | + expect(apiMethods).toEqual(["getMe"]); |
| 111 | + expect(registry.httpRoutes).toHaveLength(0); |
| 112 | + }); |
| 113 | +}); |
0 commit comments