|
1 | 1 | // Irc tests cover monitor plugin behavior. |
2 | | -import { describe, expect, it } from "vitest"; |
3 | | -import { resolveIrcInboundTarget } from "./monitor.js"; |
| 2 | +import net from "node:net"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { monitorIrcProvider, resolveIrcInboundTarget } from "./monitor.js"; |
| 5 | +import { clearIrcRuntime, setIrcRuntime } from "./runtime.js"; |
| 6 | +import type { CoreConfig } from "./types.js"; |
| 7 | + |
| 8 | +type DisconnectingIrcServer = { |
| 9 | + port: number; |
| 10 | + lines: string[]; |
| 11 | + connectionCount: number; |
| 12 | + close(): Promise<void>; |
| 13 | +}; |
| 14 | + |
| 15 | +async function waitForIrcCondition( |
| 16 | + predicate: () => boolean, |
| 17 | + message: string, |
| 18 | + timeoutMs = 3000, |
| 19 | +): Promise<void> { |
| 20 | + const deadline = Date.now() + timeoutMs; |
| 21 | + while (!predicate()) { |
| 22 | + if (Date.now() >= deadline) { |
| 23 | + throw new Error(message); |
| 24 | + } |
| 25 | + await new Promise((resolve) => { |
| 26 | + setTimeout(resolve, 10); |
| 27 | + }); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function startDisconnectingIrcServer(): Promise<DisconnectingIrcServer> { |
| 32 | + const lines: string[] = []; |
| 33 | + const sockets = new Set<net.Socket>(); |
| 34 | + let connectionCount = 0; |
| 35 | + |
| 36 | + const server = net.createServer((socket) => { |
| 37 | + const connectionNumber = ++connectionCount; |
| 38 | + sockets.add(socket); |
| 39 | + socket.setEncoding("utf8"); |
| 40 | + let buffer = ""; |
| 41 | + socket.on("data", (chunk: string) => { |
| 42 | + buffer += chunk; |
| 43 | + let idx = buffer.indexOf("\n"); |
| 44 | + while (idx !== -1) { |
| 45 | + const line = buffer.slice(0, idx).replace(/\r$/, ""); |
| 46 | + buffer = buffer.slice(idx + 1); |
| 47 | + idx = buffer.indexOf("\n"); |
| 48 | + lines.push(line); |
| 49 | + if (line.startsWith("USER ")) { |
| 50 | + socket.write(":server 001 bot :welcome\r\n"); |
| 51 | + if (connectionNumber === 1) { |
| 52 | + setTimeout(() => socket.destroy(), 10); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + }); |
| 57 | + socket.on("close", () => { |
| 58 | + sockets.delete(socket); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + await new Promise<void>((resolve) => { |
| 63 | + server.listen(0, "127.0.0.1", resolve); |
| 64 | + }); |
| 65 | + const address = server.address(); |
| 66 | + if (!address || typeof address === "string") { |
| 67 | + throw new Error("expected loopback IRC server to bind a TCP port"); |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + port: address.port, |
| 72 | + lines, |
| 73 | + get connectionCount() { |
| 74 | + return connectionCount; |
| 75 | + }, |
| 76 | + close: async () => { |
| 77 | + for (const socket of sockets) { |
| 78 | + socket.destroy(); |
| 79 | + } |
| 80 | + await new Promise<void>((resolve, reject) => { |
| 81 | + server.close((error) => { |
| 82 | + if (error) { |
| 83 | + reject(error); |
| 84 | + return; |
| 85 | + } |
| 86 | + resolve(); |
| 87 | + }); |
| 88 | + }); |
| 89 | + }, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +function installMonitorRuntime() { |
| 94 | + setIrcRuntime({ |
| 95 | + logging: { |
| 96 | + shouldLogVerbose: vi.fn(() => false), |
| 97 | + getChildLogger: vi.fn(() => ({ |
| 98 | + debug: vi.fn(), |
| 99 | + info: vi.fn(), |
| 100 | + warn: vi.fn(), |
| 101 | + error: vi.fn(), |
| 102 | + })), |
| 103 | + }, |
| 104 | + channel: { |
| 105 | + activity: { |
| 106 | + record: vi.fn(), |
| 107 | + }, |
| 108 | + }, |
| 109 | + } as never); |
| 110 | +} |
| 111 | + |
| 112 | +afterEach(() => { |
| 113 | + clearIrcRuntime(); |
| 114 | +}); |
| 115 | + |
| 116 | +describe("irc monitor reconnect", () => { |
| 117 | + it("reconnects when an established IRC socket closes", async () => { |
| 118 | + installMonitorRuntime(); |
| 119 | + const server = await startDisconnectingIrcServer(); |
| 120 | + const config = { |
| 121 | + channels: { |
| 122 | + irc: { |
| 123 | + host: "127.0.0.1", |
| 124 | + port: server.port, |
| 125 | + tls: false, |
| 126 | + nick: "bot", |
| 127 | + username: "bot", |
| 128 | + realname: "OpenClaw", |
| 129 | + channels: ["#openclaw"], |
| 130 | + }, |
| 131 | + }, |
| 132 | + } as CoreConfig; |
| 133 | + let monitor: { stop: () => void } | undefined; |
| 134 | + |
| 135 | + try { |
| 136 | + monitor = await monitorIrcProvider({ config }); |
| 137 | + await waitForIrcCondition( |
| 138 | + () => |
| 139 | + server.connectionCount >= 2 && |
| 140 | + server.lines.filter((line) => line === "USER bot 0 * :OpenClaw").length >= 2, |
| 141 | + "expected IRC monitor to reconnect after the first socket closed", |
| 142 | + ); |
| 143 | + |
| 144 | + expect(server.connectionCount).toBeGreaterThanOrEqual(2); |
| 145 | + } finally { |
| 146 | + monitor?.stop(); |
| 147 | + await server.close(); |
| 148 | + } |
| 149 | + }); |
| 150 | +}); |
4 | 151 |
|
5 | 152 | describe("irc monitor inbound target", () => { |
6 | 153 | it("keeps channel target for group messages", () => { |
|
0 commit comments