Skip to content

Commit 70729ed

Browse files
committed
test(core): convert integration tests to unit test cases and integrate into existing files
1 parent 12b1da6 commit 70729ed

5 files changed

Lines changed: 99 additions & 107 deletions

File tree

packages/core/src/ide/ide-connection-utils.test.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ vi.mock('node:fs', async (importOriginal) => {
3737
};
3838
});
3939
vi.mock('node:os');
40+
vi.mock('undici', () => ({
41+
EnvHttpProxyAgent: vi.fn(),
42+
fetch: vi.fn(),
43+
setGlobalDispatcher: vi.fn(),
44+
Agent: vi.fn(),
45+
}));
4046

4147
describe('ide-connection-utils', () => {
4248
beforeEach(() => {
@@ -698,12 +704,34 @@ describe('ide-connection-utils', () => {
698704
});
699705

700706
describe('createProxyAwareFetch', () => {
701-
it('should return a proxy-aware fetcher function', async () => {
702-
const { createProxyAwareFetch } = await import(
703-
'./ide-connection-utils.js'
704-
);
705-
const fetcher = await createProxyAwareFetch('127.0.0.1');
707+
it('should return a proxy-aware fetcher function that respects NO_PROXY and includes ideServerHost', async () => {
708+
const { createProxyAwareFetch } =
709+
await import('./ide-connection-utils.js');
710+
const { EnvHttpProxyAgent } = await import('undici');
711+
const ideServerHost = '127.0.0.1';
712+
const existingNoProxy = 'google.com,example.com';
713+
vi.stubEnv('NO_PROXY', existingNoProxy);
714+
715+
const fetcher = await createProxyAwareFetch(ideServerHost);
706716
expect(typeof fetcher).toBe('function');
717+
718+
expect(EnvHttpProxyAgent).toHaveBeenCalledWith({
719+
noProxy: `${existingNoProxy},${ideServerHost}`,
720+
});
721+
});
722+
723+
it('should handle missing NO_PROXY when creating proxy-aware fetcher', async () => {
724+
const { createProxyAwareFetch } =
725+
await import('./ide-connection-utils.js');
726+
const { EnvHttpProxyAgent } = await import('undici');
727+
const ideServerHost = 'host.docker.internal';
728+
vi.stubEnv('NO_PROXY', '');
729+
730+
await createProxyAwareFetch(ideServerHost);
731+
732+
expect(EnvHttpProxyAgent).toHaveBeenCalledWith({
733+
noProxy: ideServerHost,
734+
});
707735
});
708736
});
709737
});

packages/core/src/tools/mcp-client.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ let MOCK_CONTEXT: McpContext = MOCK_CONTEXT_DEFAULT;
7474
vi.mock('@modelcontextprotocol/sdk/client/stdio.js');
7575
vi.mock('@modelcontextprotocol/sdk/client/index.js');
7676
vi.mock('@google/genai');
77+
vi.mock('undici', () => ({
78+
EnvHttpProxyAgent: vi.fn(),
79+
fetch: vi.fn(),
80+
setGlobalDispatcher: vi.fn(),
81+
Agent: vi.fn(),
82+
}));
7783
vi.mock('../mcp/oauth-provider.js');
7884
vi.mock('../mcp/oauth-token-storage.js');
7985
vi.mock('../mcp/oauth-utils.js');
@@ -1779,6 +1785,29 @@ describe('mcp-client', () => {
17791785
});
17801786

17811787
describe('createTransport', () => {
1788+
it('should create an HTTP transport that respects NO_PROXY', async () => {
1789+
const { createTransport } = await import('./mcp-client.js');
1790+
const { EnvHttpProxyAgent } = await import('undici');
1791+
const noProxyValue = 'localhost,127.0.0.1';
1792+
vi.stubEnv('NO_PROXY', noProxyValue);
1793+
1794+
await createTransport(
1795+
'test-server',
1796+
{
1797+
url: 'http://test-server',
1798+
type: 'http',
1799+
},
1800+
false,
1801+
MOCK_CONTEXT,
1802+
);
1803+
1804+
expect(EnvHttpProxyAgent).toHaveBeenCalledWith(
1805+
expect.objectContaining({
1806+
noProxy: noProxyValue,
1807+
}),
1808+
);
1809+
});
1810+
17821811
describe('should connect via httpUrl', () => {
17831812
it('uses MCP SDK authProvider token() path for oauth-enabled servers', async () => {
17841813
const mockGetValidTokenWithMetadata = vi.fn().mockResolvedValue({

packages/core/src/utils/fetch.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,33 @@ describe('fetch utils', () => {
271271
}),
272272
);
273273
});
274+
275+
it('should handle empty NO_PROXY', () => {
276+
const proxyUrl = 'http://proxy.example.com';
277+
vi.stubEnv('NO_PROXY', '');
278+
279+
setGlobalProxy(proxyUrl);
280+
281+
expect(EnvHttpProxyAgent).toHaveBeenCalledWith(
282+
expect.objectContaining({
283+
noProxy: '',
284+
}),
285+
);
286+
});
287+
288+
it('should handle multi-entry NO_PROXY with trimming', () => {
289+
const proxyUrl = 'http://proxy.example.com';
290+
const noProxyValue = ' google.com, 127.0.0.1 , localhost ';
291+
vi.stubEnv('NO_PROXY', noProxyValue);
292+
293+
setGlobalProxy(proxyUrl);
294+
295+
expect(EnvHttpProxyAgent).toHaveBeenCalledWith(
296+
expect.objectContaining({
297+
noProxy: 'google.com, 127.0.0.1 , localhost',
298+
}),
299+
);
300+
});
274301
});
275302

276303
describe('createSafeProxyAgent', () => {

packages/core/src/utils/fetch.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ export async function isPrivateIpAsync(url: string): Promise<boolean> {
173173
*/
174174
export function createSafeProxyAgent(proxyUrl: string): EnvHttpProxyAgent {
175175
const trimmedProxy = proxyUrl.trim();
176-
const noProxy = (process.env['NO_PROXY'] || process.env['no_proxy'])?.trim();
176+
const noProxy = (
177+
process.env['NO_PROXY'] ??
178+
process.env['no_proxy'] ??
179+
''
180+
)?.trim();
177181
return new EnvHttpProxyAgent({
178182
httpProxy: trimmedProxy,
179183
httpsProxy: trimmedProxy,
@@ -228,7 +232,11 @@ export async function fetchWithTimeout(
228232
export function setGlobalProxy(proxy: string) {
229233
const trimmedProxy = proxy.trim();
230234
currentProxy = trimmedProxy;
231-
const noProxy = (process.env['NO_PROXY'] || process.env['no_proxy'])?.trim();
235+
const noProxy = (
236+
process.env['NO_PROXY'] ??
237+
process.env['no_proxy'] ??
238+
''
239+
)?.trim();
232240
setGlobalDispatcher(
233241
new EnvHttpProxyAgent({
234242
httpProxy: trimmedProxy,

packages/core/src/utils/proxy_bypass.test.ts

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)