|
| 1 | +import { once } from "node:events"; |
| 2 | +import type { AddressInfo } from "node:net"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import { WebSocketServer } from "ws"; |
| 5 | +import { |
| 6 | + buildMinimalGatewayHelloOkPayload, |
| 7 | + closeMinimalGatewayServer, |
| 8 | + parseMinimalGatewayRequestFrame, |
| 9 | + sendMinimalGatewayConnectChallenge, |
| 10 | + sendMinimalGatewayResponse, |
| 11 | +} from "../gateway/minimal-gateway.test-helpers.js"; |
| 12 | +import { defaultRuntime, ExitError } from "../runtime.js"; |
| 13 | +import { runRegisteredCli } from "../test-utils/command-runner.js"; |
| 14 | +import { withOpenClawTestState } from "../test-utils/openclaw-test-state.js"; |
| 15 | +import { registerLogsCli } from "./logs-cli.js"; |
| 16 | + |
| 17 | +afterEach(() => vi.restoreAllMocks()); |
| 18 | + |
| 19 | +async function withLogsGateway( |
| 20 | + options: { source?: "config" | "environment"; denied?: boolean }, |
| 21 | + run: (fixture: { |
| 22 | + port: string; |
| 23 | + requests: string[]; |
| 24 | + stdout: string[]; |
| 25 | + stderr: string[]; |
| 26 | + }) => Promise<void>, |
| 27 | +) { |
| 28 | + await withOpenClawTestState( |
| 29 | + { |
| 30 | + label: "logs-port", |
| 31 | + env: { |
| 32 | + OPENCLAW_GATEWAY_URL: |
| 33 | + options.source === "environment" ? "ws://remote.example:19001" : undefined, |
| 34 | + OPENCLAW_ALLOW_INSECURE_PRIVATE_WS: undefined, |
| 35 | + }, |
| 36 | + }, |
| 37 | + async (state) => { |
| 38 | + await state.writeConfig({ |
| 39 | + gateway: { |
| 40 | + mode: options.source === "config" ? "remote" : "local", |
| 41 | + auth: { mode: "none" }, |
| 42 | + ...(options.source === "config" ? { remote: { url: "ws://remote.example:19001" } } : {}), |
| 43 | + }, |
| 44 | + }); |
| 45 | + const server = new WebSocketServer({ host: "127.0.0.1", port: 0 }); |
| 46 | + const requests: string[] = []; |
| 47 | + server.on("connection", (socket) => { |
| 48 | + sendMinimalGatewayConnectChallenge(socket); |
| 49 | + socket.on("message", (data) => { |
| 50 | + const frame = parseMinimalGatewayRequestFrame(data); |
| 51 | + if (!frame.id || !frame.method) { |
| 52 | + return; |
| 53 | + } |
| 54 | + requests.push(frame.method); |
| 55 | + if (frame.method === "connect") { |
| 56 | + sendMinimalGatewayResponse( |
| 57 | + socket, |
| 58 | + frame.id, |
| 59 | + buildMinimalGatewayHelloOkPayload({ methods: ["logs.tail"] }), |
| 60 | + ); |
| 61 | + } else if (options.denied) { |
| 62 | + socket.send( |
| 63 | + JSON.stringify({ |
| 64 | + type: "res", |
| 65 | + id: frame.id, |
| 66 | + ok: false, |
| 67 | + error: { code: "INVALID_REQUEST", message: "logs unavailable for this client" }, |
| 68 | + }), |
| 69 | + ); |
| 70 | + } else { |
| 71 | + sendMinimalGatewayResponse(socket, frame.id, { |
| 72 | + file: "selected-gateway.log", |
| 73 | + cursor: 1, |
| 74 | + lines: ["selected local log"], |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + }); |
| 79 | + await once(server, "listening"); |
| 80 | + const port = String((server.address() as AddressInfo).port); |
| 81 | + const stdout: string[] = []; |
| 82 | + const stderr: string[] = []; |
| 83 | + vi.spyOn(process.stdout, "write").mockImplementation((chunk) => { |
| 84 | + stdout.push(String(chunk)); |
| 85 | + return true; |
| 86 | + }); |
| 87 | + vi.spyOn(process.stderr, "write").mockImplementation((chunk) => { |
| 88 | + stderr.push(String(chunk)); |
| 89 | + return true; |
| 90 | + }); |
| 91 | + vi.spyOn(defaultRuntime, "exit").mockImplementation((code) => { |
| 92 | + throw new ExitError(code); |
| 93 | + }); |
| 94 | + try { |
| 95 | + await run({ port, requests, stdout, stderr }); |
| 96 | + } finally { |
| 97 | + await closeMinimalGatewayServer(server); |
| 98 | + } |
| 99 | + }, |
| 100 | + ); |
| 101 | +} |
| 102 | + |
| 103 | +async function runLogs(argv: string[]) { |
| 104 | + await runRegisteredCli({ register: registerLogsCli, argv: ["logs", ...argv] }); |
| 105 | +} |
| 106 | + |
| 107 | +describe("logs local port selection", () => { |
| 108 | + it.each(["config", "environment"] as const)( |
| 109 | + "tails the selected local port instead of validating the %s default URL", |
| 110 | + async (source) => { |
| 111 | + await withLogsGateway({ source }, async ({ port, requests, stdout }) => { |
| 112 | + await runLogs(["--port", port, "--json", "--timeout", "1500"]); |
| 113 | + expect(requests).toEqual(["connect", "logs.tail"]); |
| 114 | + expect(stdout.join("")).toContain("selected local log"); |
| 115 | + }); |
| 116 | + }, |
| 117 | + ); |
| 118 | + |
| 119 | + it.each(["text", "json"])( |
| 120 | + "reports the selected port when the RPC fails in %s mode", |
| 121 | + async (mode) => { |
| 122 | + await withLogsGateway({ denied: true }, async ({ port, requests, stderr }) => { |
| 123 | + const args = [ |
| 124 | + "--port", |
| 125 | + port, |
| 126 | + "--timeout", |
| 127 | + "1500", |
| 128 | + ...(mode === "json" ? ["--json"] : ["--plain"]), |
| 129 | + ]; |
| 130 | + await expect(runLogs(args)).rejects.toBeInstanceOf(ExitError); |
| 131 | + expect(requests).toEqual(["connect", "logs.tail"]); |
| 132 | + const output = stderr.join(""); |
| 133 | + if (mode === "json") { |
| 134 | + expect(JSON.parse(output)).toMatchObject({ |
| 135 | + type: "error", |
| 136 | + details: { url: `ws://127.0.0.1:${port}` }, |
| 137 | + }); |
| 138 | + } else { |
| 139 | + expect(output).toContain(`Gateway target: ws://127.0.0.1:${port}`); |
| 140 | + } |
| 141 | + }); |
| 142 | + }, |
| 143 | + ); |
| 144 | + |
| 145 | + it("honors an explicit URL even with an unusable default URL", async () => { |
| 146 | + await withLogsGateway({ source: "config" }, async ({ port, requests, stdout }) => { |
| 147 | + await runLogs([ |
| 148 | + "--url", |
| 149 | + `ws://127.0.0.1:${port}`, |
| 150 | + "--token", |
| 151 | + "fixture-token", |
| 152 | + "--json", |
| 153 | + "--timeout", |
| 154 | + "1500", |
| 155 | + ]); |
| 156 | + expect(requests).toEqual(["connect", "logs.tail"]); |
| 157 | + expect(stdout.join("")).toContain("selected local log"); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + it.each(["config", "environment"] as const)( |
| 162 | + "still rejects an unsafe %s target without an override", |
| 163 | + async (source) => { |
| 164 | + await withLogsGateway({ source }, async ({ requests }) => { |
| 165 | + await expect(runLogs(["--json"])).rejects.toThrow( |
| 166 | + "uses plaintext ws:// to a non-loopback address", |
| 167 | + ); |
| 168 | + expect(requests).toEqual([]); |
| 169 | + }); |
| 170 | + }, |
| 171 | + ); |
| 172 | + |
| 173 | + it.each([ |
| 174 | + { args: ["--port", "65536"], message: "--port must be an integer between 1 and 65535." }, |
| 175 | + { |
| 176 | + args: ["--port", "19083", "--url", "ws://127.0.0.1:19083"], |
| 177 | + message: "Use either --url or --port, not both.", |
| 178 | + }, |
| 179 | + ])("keeps target validation for $args", async ({ args, message }) => { |
| 180 | + await withLogsGateway({}, async ({ requests }) => { |
| 181 | + await expect(runLogs(args)).rejects.toThrow(message); |
| 182 | + expect(requests).toEqual([]); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}); |
0 commit comments