|
| 1 | +// Doctor web fetch proxy tests cover explicit opt-in diagnostics without exposing proxy values. |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
| 3 | +import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 4 | +import { noteWebFetchProxyDiagnostic } from "./doctor-web-fetch-proxy.js"; |
| 5 | + |
| 6 | +function serviceWithEnv(environment?: Record<string, string>) { |
| 7 | + return { |
| 8 | + readCommand: vi.fn(async () => |
| 9 | + environment ? { programArguments: ["openclaw", "gateway"], environment } : null, |
| 10 | + ), |
| 11 | + }; |
| 12 | +} |
| 13 | + |
| 14 | +async function collectDiagnostic( |
| 15 | + params: Omit<Parameters<typeof noteWebFetchProxyDiagnostic>[0], "noteFn">, |
| 16 | +): Promise<string | null> { |
| 17 | + let diagnostic: string | null = null; |
| 18 | + await noteWebFetchProxyDiagnostic({ |
| 19 | + ...params, |
| 20 | + noteFn: (message) => { |
| 21 | + if (typeof message !== "string") { |
| 22 | + throw new TypeError("expected doctor proxy diagnostic to be a string"); |
| 23 | + } |
| 24 | + diagnostic = message; |
| 25 | + }, |
| 26 | + }); |
| 27 | + return diagnostic; |
| 28 | +} |
| 29 | + |
| 30 | +describe("web_fetch proxy doctor diagnostic", () => { |
| 31 | + it("reports direct routing for an installed Gateway proxy without exposing its value", async () => { |
| 32 | + const proxyUrl = "http://private-proxy.example:8080/proxy-value-marker"; |
| 33 | + const diagnostic = await collectDiagnostic({ |
| 34 | + cfg: {}, |
| 35 | + env: {}, |
| 36 | + service: serviceWithEnv({ HTTPS_PROXY: proxyUrl }), |
| 37 | + probeDirectConnectivity: vi.fn(async () => "unreachable" as const), |
| 38 | + }); |
| 39 | + |
| 40 | + expect(diagnostic).toContain( |
| 41 | + "HTTP(S) proxy environment detected in the installed Gateway service: HTTPS_PROXY", |
| 42 | + ); |
| 43 | + expect(diagnostic).toContain("web_fetch still uses direct connections"); |
| 44 | + expect(diagnostic).toContain("tools.web.fetch.useTrustedEnvProxy is not enabled"); |
| 45 | + expect(diagnostic).toContain("Direct TLS connectivity to docs.openclaw.ai:443 failed"); |
| 46 | + expect(diagnostic).toContain("openclaw config set tools.web.fetch.useTrustedEnvProxy true"); |
| 47 | + expect(diagnostic).not.toContain(proxyUrl); |
| 48 | + expect(diagnostic).not.toContain("proxy-value-marker"); |
| 49 | + }); |
| 50 | + |
| 51 | + it("reports a reachable direct path from the doctor process", async () => { |
| 52 | + const diagnostic = await collectDiagnostic({ |
| 53 | + cfg: {}, |
| 54 | + env: { http_proxy: "http://proxy.example:8080" }, |
| 55 | + service: serviceWithEnv(), |
| 56 | + probeDirectConnectivity: vi.fn(async () => "reachable" as const), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(diagnostic).toContain("proxy environment detected in the doctor process: http_proxy"); |
| 60 | + expect(diagnostic).toContain("Direct TLS connectivity to docs.openclaw.ai:443 succeeded"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("reports both process and installed service proxy sources", async () => { |
| 64 | + const diagnostic = await collectDiagnostic({ |
| 65 | + cfg: {}, |
| 66 | + env: { HTTP_PROXY: "http://shell-proxy.example:8080" }, |
| 67 | + service: serviceWithEnv({ HTTPS_PROXY: "http://service-proxy.example:8080" }), |
| 68 | + probeDirectConnectivity: vi.fn(async () => "reachable" as const), |
| 69 | + }); |
| 70 | + |
| 71 | + expect(diagnostic).toContain("doctor process: HTTP_PROXY"); |
| 72 | + expect(diagnostic).toContain("installed Gateway service: HTTPS_PROXY"); |
| 73 | + }); |
| 74 | + |
| 75 | + it("does nothing when no HTTP(S) proxy is effective", async () => { |
| 76 | + const probe = vi.fn(async () => "reachable" as const); |
| 77 | + |
| 78 | + await expect( |
| 79 | + collectDiagnostic({ |
| 80 | + cfg: {}, |
| 81 | + env: { ALL_PROXY: "socks5://proxy.example:1080" }, |
| 82 | + service: serviceWithEnv(), |
| 83 | + probeDirectConnectivity: probe, |
| 84 | + }), |
| 85 | + ).resolves.toBeNull(); |
| 86 | + expect(probe).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it.each([ |
| 90 | + { |
| 91 | + name: "trusted proxy opt-in is enabled", |
| 92 | + cfg: { tools: { web: { fetch: { useTrustedEnvProxy: true } } } }, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "web_fetch is disabled", |
| 96 | + cfg: { tools: { web: { fetch: { enabled: false } } } }, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Gateway mode is remote", |
| 100 | + cfg: { gateway: { mode: "remote" } }, |
| 101 | + }, |
| 102 | + ])("does nothing when $name", async ({ cfg }) => { |
| 103 | + const service = serviceWithEnv({ HTTPS_PROXY: "http://proxy.example:8080" }); |
| 104 | + const probe = vi.fn(async () => "unreachable" as const); |
| 105 | + |
| 106 | + await expect( |
| 107 | + collectDiagnostic({ |
| 108 | + cfg: cfg as OpenClawConfig, |
| 109 | + env: {}, |
| 110 | + service, |
| 111 | + probeDirectConnectivity: probe, |
| 112 | + }), |
| 113 | + ).resolves.toBeNull(); |
| 114 | + expect(service.readCommand).not.toHaveBeenCalled(); |
| 115 | + expect(probe).not.toHaveBeenCalled(); |
| 116 | + }); |
| 117 | + |
| 118 | + it("emits one titled note", async () => { |
| 119 | + const noteFn = vi.fn(); |
| 120 | + |
| 121 | + await noteWebFetchProxyDiagnostic({ |
| 122 | + cfg: {}, |
| 123 | + env: { HTTPS_PROXY: "http://proxy.example:8080" }, |
| 124 | + service: serviceWithEnv(), |
| 125 | + probeDirectConnectivity: vi.fn(async () => "reachable" as const), |
| 126 | + noteFn, |
| 127 | + }); |
| 128 | + |
| 129 | + expect(noteFn).toHaveBeenCalledTimes(1); |
| 130 | + expect(noteFn).toHaveBeenCalledWith(expect.stringContaining("web_fetch"), "Web fetch proxy"); |
| 131 | + }); |
| 132 | +}); |
0 commit comments