|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2026 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, it, vi } from 'vitest'; |
| 8 | +vi.unmock('fs'); |
| 9 | +vi.unmock('node:fs'); |
| 10 | +import * as esbuild from 'esbuild'; |
| 11 | +import path from 'node:path'; |
| 12 | +import { mkdtempSync, writeFileSync, rmSync } from 'node:fs'; |
| 13 | +import { tmpdir } from 'node:os'; |
| 14 | +import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 15 | + |
| 16 | +const __filename = fileURLToPath(import.meta.url); |
| 17 | +const __dirname = path.dirname(__filename); |
| 18 | +const projectRoot = path.resolve(__dirname, '../../'); |
| 19 | + |
| 20 | +describe('proxy-agent bundle shape', () => { |
| 21 | + it('preserves named constructors after ESM splitting', async () => { |
| 22 | + const tmpDir = mkdtempSync(path.join(tmpdir(), 'gemini-proxy-test-')); |
| 23 | + const entryFile = path.join(tmpDir, 'entry.ts'); |
| 24 | + |
| 25 | + // Create a minimal entry file that dynamically imports the proxy agents |
| 26 | + writeFileSync( |
| 27 | + entryFile, |
| 28 | + ` |
| 29 | + export async function getAgents() { |
| 30 | + const httpsMod = await import('https-proxy-agent'); |
| 31 | + const httpMod = await import('http-proxy-agent'); |
| 32 | + return { |
| 33 | + https: httpsMod, |
| 34 | + http: httpMod, |
| 35 | + }; |
| 36 | + } |
| 37 | + `, |
| 38 | + ); |
| 39 | + |
| 40 | + // Bundle with the exact same splitting config and aliases as cliConfig |
| 41 | + await esbuild.build({ |
| 42 | + entryPoints: { gemini: entryFile }, |
| 43 | + outdir: path.join(tmpDir, 'bundle'), |
| 44 | + bundle: true, |
| 45 | + splitting: true, |
| 46 | + format: 'esm', |
| 47 | + platform: 'node', |
| 48 | + outExtension: { '.js': '.mjs' }, |
| 49 | + alias: { |
| 50 | + 'https-proxy-agent': path.resolve( |
| 51 | + projectRoot, |
| 52 | + 'packages/cli/src/patches/https-proxy-agent.ts', |
| 53 | + ), |
| 54 | + 'http-proxy-agent': path.resolve( |
| 55 | + projectRoot, |
| 56 | + 'packages/cli/src/patches/http-proxy-agent.ts', |
| 57 | + ), |
| 58 | + }, |
| 59 | + }); |
| 60 | + |
| 61 | + // Import the bundled chunk |
| 62 | + const bundledEntryUrl = pathToFileURL( |
| 63 | + path.join(tmpDir, 'bundle/gemini.mjs'), |
| 64 | + ).href; |
| 65 | + const { getAgents } = await import(bundledEntryUrl); |
| 66 | + |
| 67 | + const { https, http } = await getAgents(); |
| 68 | + |
| 69 | + // Verify named exports exist |
| 70 | + expect(typeof https.HttpsProxyAgent).toBe('function'); |
| 71 | + expect(typeof http.HttpProxyAgent).toBe('function'); |
| 72 | + |
| 73 | + // Verify they are constructable |
| 74 | + expect(() => new https.HttpsProxyAgent('http://127.0.0.1:9')).not.toThrow(); |
| 75 | + expect(() => new http.HttpProxyAgent('http://127.0.0.1:9')).not.toThrow(); |
| 76 | + |
| 77 | + // Cleanup |
| 78 | + rmSync(tmpDir, { recursive: true, force: true }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments