|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import type { FeishuConfig, ResolvedFeishuAccount } from "./types.js"; |
| 3 | + |
| 4 | +const wsClientCtorMock = vi.hoisted(() => |
| 5 | + vi.fn(function wsClientCtor() { |
| 6 | + return { connected: true }; |
| 7 | + }), |
| 8 | +); |
| 9 | +const httpsProxyAgentCtorMock = vi.hoisted(() => |
| 10 | + vi.fn(function httpsProxyAgentCtor(proxyUrl: string) { |
| 11 | + return { proxyUrl }; |
| 12 | + }), |
| 13 | +); |
| 14 | + |
| 15 | +vi.mock("@larksuiteoapi/node-sdk", () => ({ |
| 16 | + AppType: { SelfBuild: "self" }, |
| 17 | + Domain: { Feishu: "https://open.feishu.cn", Lark: "https://open.larksuite.com" }, |
| 18 | + LoggerLevel: { info: "info" }, |
| 19 | + Client: vi.fn(), |
| 20 | + WSClient: wsClientCtorMock, |
| 21 | + EventDispatcher: vi.fn(), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("https-proxy-agent", () => ({ |
| 25 | + HttpsProxyAgent: httpsProxyAgentCtorMock, |
| 26 | +})); |
| 27 | + |
| 28 | +import { createFeishuWSClient } from "./client.js"; |
| 29 | + |
| 30 | +const proxyEnvKeys = ["https_proxy", "HTTPS_PROXY", "http_proxy", "HTTP_PROXY"] as const; |
| 31 | +type ProxyEnvKey = (typeof proxyEnvKeys)[number]; |
| 32 | + |
| 33 | +let priorProxyEnv: Partial<Record<ProxyEnvKey, string | undefined>> = {}; |
| 34 | + |
| 35 | +const baseAccount: ResolvedFeishuAccount = { |
| 36 | + accountId: "main", |
| 37 | + enabled: true, |
| 38 | + configured: true, |
| 39 | + appId: "app_123", |
| 40 | + appSecret: "secret_123", |
| 41 | + domain: "feishu", |
| 42 | + config: {} as FeishuConfig, |
| 43 | +}; |
| 44 | + |
| 45 | +function firstWsClientOptions(): { agent?: unknown } { |
| 46 | + const calls = wsClientCtorMock.mock.calls as unknown as Array<[options: { agent?: unknown }]>; |
| 47 | + return calls[0]?.[0] ?? {}; |
| 48 | +} |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + priorProxyEnv = {}; |
| 52 | + for (const key of proxyEnvKeys) { |
| 53 | + priorProxyEnv[key] = process.env[key]; |
| 54 | + delete process.env[key]; |
| 55 | + } |
| 56 | + vi.clearAllMocks(); |
| 57 | +}); |
| 58 | + |
| 59 | +afterEach(() => { |
| 60 | + for (const key of proxyEnvKeys) { |
| 61 | + const value = priorProxyEnv[key]; |
| 62 | + if (value === undefined) { |
| 63 | + delete process.env[key]; |
| 64 | + } else { |
| 65 | + process.env[key] = value; |
| 66 | + } |
| 67 | + } |
| 68 | +}); |
| 69 | + |
| 70 | +describe("createFeishuWSClient proxy handling", () => { |
| 71 | + it("does not set a ws proxy agent when proxy env is absent", () => { |
| 72 | + createFeishuWSClient(baseAccount); |
| 73 | + |
| 74 | + expect(httpsProxyAgentCtorMock).not.toHaveBeenCalled(); |
| 75 | + const options = firstWsClientOptions(); |
| 76 | + expect(options?.agent).toBeUndefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("uses proxy env precedence: https_proxy first, then HTTPS_PROXY, then http_proxy/HTTP_PROXY", () => { |
| 80 | + process.env.https_proxy = "http://lower-https:8001"; |
| 81 | + process.env.HTTPS_PROXY = "http://upper-https:8002"; |
| 82 | + process.env.http_proxy = "http://lower-http:8003"; |
| 83 | + process.env.HTTP_PROXY = "http://upper-http:8004"; |
| 84 | + |
| 85 | + createFeishuWSClient(baseAccount); |
| 86 | + |
| 87 | + expect(httpsProxyAgentCtorMock).toHaveBeenCalledTimes(1); |
| 88 | + expect(httpsProxyAgentCtorMock).toHaveBeenCalledWith("http://lower-https:8001"); |
| 89 | + const options = firstWsClientOptions(); |
| 90 | + expect(options.agent).toEqual({ proxyUrl: "http://lower-https:8001" }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("passes HTTP_PROXY to ws client when https vars are unset", () => { |
| 94 | + process.env.HTTP_PROXY = "http://upper-http:8999"; |
| 95 | + |
| 96 | + createFeishuWSClient(baseAccount); |
| 97 | + |
| 98 | + expect(httpsProxyAgentCtorMock).toHaveBeenCalledTimes(1); |
| 99 | + expect(httpsProxyAgentCtorMock).toHaveBeenCalledWith("http://upper-http:8999"); |
| 100 | + const options = firstWsClientOptions(); |
| 101 | + expect(options.agent).toEqual({ proxyUrl: "http://upper-http:8999" }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments