import type { ConfigService } from "@nestjs/config"; import { describe, expect, it, vi } from "vitest"; import { EmexService } from "./emex.service"; // ConfigService.get(key, default) returns the raw ENV STRING (NestJS does not // coerce generics at runtime). These tests guard the regression where a // string port range made the proxy-port arithmetic do string concat // (45 + "10001" = "4510001"), producing an out-of-range port that crashed the // whole app with undici "Invalid URL" at construction time. function makeConfig(overrides: Record): ConfigService { return { get: vi.fn((key: string, def?: unknown) => Object.hasOwn(overrides, key) ? overrides[key] : def, ), } as unknown as ConfigService; } const browser = {} as never; const redis = { set: vi.fn() } as never; const posthog = {} as never; const proxyTelemetry = { record: vi.fn() } as never; const proxyHealth = { isFloxyDown: vi.fn(() => false), reportFloxyFailure: vi.fn(), reportFloxySuccess: vi.fn(), } as never; describe("EmexService proxy-port coercion (regression)", () => { it("does not throw 'Invalid URL' when ports arrive as strings over a real range", () => { const config = makeConfig({ EMEX_USE_PROXY: "true", EMEX_PROXY_HOST: "74.81.81.81", EMEX_PROXY_PORT_START: "10001", EMEX_PROXY_PORT_END: "10099", }); // Pre-fix: threw "Invalid URL" here (port "4510001" > 65535). expect( () => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth), ).not.toThrow(); }); it("constructs with a single-port range (string env)", () => { const config = makeConfig({ EMEX_USE_PROXY: "true", EMEX_PROXY_PORT_START: "823", EMEX_PROXY_PORT_END: "823", }); expect( () => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth), ).not.toThrow(); }); it("falls back to a valid default when the port env is garbage", () => { const config = makeConfig({ EMEX_USE_PROXY: "true", EMEX_PROXY_PORT_START: "not-a-number", EMEX_PROXY_PORT_END: "999999", }); expect( () => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth), ).not.toThrow(); }); it("constructs cleanly with the proxy disabled", () => { const config = makeConfig({ EMEX_USE_PROXY: "false" }); expect( () => new EmexService(config, browser, redis, posthog, proxyTelemetry, proxyHealth), ).not.toThrow(); }); });