File size: 21,304 Bytes
52efc7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  describe,
  it,
  expect,
  vi,
  beforeEach,
  afterEach,
  type Mocked,
} from 'vitest';
import { IdeClient, IDEConnectionStatus } from './ide-client.js';
import type * as fs from 'node:fs';
import { getIdeProcessInfo } from './process-utils.js';
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { detectIde, IDE_DEFINITIONS } from './detect-ide.js';
import * as os from 'node:os';

import {
  getConnectionConfigFromFile,
  getStdioConfigFromEnv,
  getPortFromEnv,
  validateWorkspacePath,
  getIdeServerHost,
} from './ide-connection-utils.js';

vi.mock('node:fs', async (importOriginal) => {
  const actual = await importOriginal<typeof fs>();
  return {
    ...(actual as object),
    promises: {
      ...actual.promises,
      readFile: vi.fn(),
      readdir: vi.fn(),
    },
    realpathSync: (p: string) => p,
    existsSync: vi.fn(() => false),
  };
});
vi.mock('./process-utils.js');
vi.mock('@modelcontextprotocol/sdk/client/index.js');
vi.mock('@modelcontextprotocol/sdk/client/streamableHttp.js');
vi.mock('@modelcontextprotocol/sdk/client/stdio.js');
vi.mock('./detect-ide.js');
vi.mock('node:os');
vi.mock('./ide-connection-utils.js');

describe('IdeClient', () => {
  let mockClient: Mocked<Client>;
  let mockHttpTransport: Mocked<StreamableHTTPClientTransport>;
  let mockStdioTransport: Mocked<StdioClientTransport>;

  beforeEach(async () => {
    // Reset singleton instance for test isolation
    (IdeClient as unknown as { instance: IdeClient | undefined }).instance =
      undefined;

    // Mock environment variables
    process.env['GEMINI_CLI_IDE_WORKSPACE_PATH'] = '/test/workspace';
    delete process.env['GEMINI_CLI_IDE_SERVER_PORT'];
    delete process.env['GEMINI_CLI_IDE_SERVER_STDIO_COMMAND'];
    delete process.env['GEMINI_CLI_IDE_SERVER_STDIO_ARGS'];
    delete process.env['GEMINI_CLI_IDE_AUTH_TOKEN'];

    // Mock dependencies
    vi.spyOn(process, 'cwd').mockReturnValue('/test/workspace/sub-dir');
    vi.mocked(detectIde).mockReturnValue(IDE_DEFINITIONS.vscode);
    vi.mocked(getIdeProcessInfo).mockResolvedValue({
      pid: 12345,
      command: 'test-ide',
    });
    vi.mocked(os.tmpdir).mockReturnValue('/tmp');
    vi.mocked(getIdeServerHost).mockReturnValue('127.0.0.1');

    // Mock MCP client and transports
    mockClient = {
      connect: vi.fn().mockResolvedValue(undefined),
      close: vi.fn(),
      setNotificationHandler: vi.fn(),
      callTool: vi.fn(),
      request: vi.fn(),
    } as unknown as Mocked<Client>;
    mockHttpTransport = {
      close: vi.fn(),
    } as unknown as Mocked<StreamableHTTPClientTransport>;
    mockStdioTransport = {
      close: vi.fn(),
    } as unknown as Mocked<StdioClientTransport>;

    vi.mocked(Client).mockReturnValue(mockClient);
    vi.mocked(StreamableHTTPClientTransport).mockReturnValue(mockHttpTransport);
    vi.mocked(StdioClientTransport).mockReturnValue(mockStdioTransport);

    await IdeClient.getInstance();
  });

  afterEach(() => {
    vi.restoreAllMocks();
  });

  describe('connect', () => {
    it('should connect using HTTP when port is provided in config file', async () => {
      const config = { port: '8080' };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(getConnectionConfigFromFile).toHaveBeenCalledWith(12345);
      expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
        new URL('http://127.0.0.1:8080/mcp'),
        expect.any(Object),
      );
      expect(mockClient.connect).toHaveBeenCalledWith(mockHttpTransport);
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should connect using stdio when stdio config is provided in file', async () => {
      // Update the mock to use the new utility
      const config = { stdio: { command: 'test-cmd', args: ['--foo'] } };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StdioClientTransport).toHaveBeenCalledWith({
        command: 'test-cmd',
        args: ['--foo'],
      });
      expect(mockClient.connect).toHaveBeenCalledWith(mockStdioTransport);
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should prioritize port over stdio when both are in config file', async () => {
      const config = {
        port: '8080',
        stdio: { command: 'test-cmd', args: ['--foo'] },
      };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StreamableHTTPClientTransport).toHaveBeenCalled();
      expect(StdioClientTransport).not.toHaveBeenCalled();
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should connect using HTTP when port is provided in environment variables', async () => {
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(undefined);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      vi.mocked(getPortFromEnv).mockReturnValue('9090');

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
        new URL('http://127.0.0.1:9090/mcp'),
        expect.any(Object),
      );
      expect(mockClient.connect).toHaveBeenCalledWith(mockHttpTransport);
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should connect using stdio when stdio config is in environment variables', async () => {
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(undefined);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      vi.mocked(getStdioConfigFromEnv).mockReturnValue({
        command: 'env-cmd',
        args: ['--bar'],
      });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StdioClientTransport).toHaveBeenCalledWith({
        command: 'env-cmd',
        args: ['--bar'],
      });
      expect(mockClient.connect).toHaveBeenCalledWith(mockStdioTransport);
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should prioritize file config over environment variables', async () => {
      const config = { port: '8080' };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      vi.mocked(getPortFromEnv).mockReturnValue('9090');

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
        new URL('http://127.0.0.1:8080/mcp'),
        expect.any(Object),
      );
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should be disconnected if no config is found', async () => {
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(undefined);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StreamableHTTPClientTransport).not.toHaveBeenCalled();
      expect(StdioClientTransport).not.toHaveBeenCalled();
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Disconnected,
      );
      expect(ideClient.getConnectionStatus().details).toContain(
        'Failed to connect',
      );
    });
  });

  describe('isDiffingEnabled', () => {
    it('should return false if not connected', async () => {
      const ideClient = await IdeClient.getInstance();
      expect(ideClient.isDiffingEnabled()).toBe(false);
    });

    it('should return false if tool discovery fails', async () => {
      const config = { port: '8080' };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      mockClient.request.mockRejectedValue(new Error('Method not found'));

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
      expect(ideClient.isDiffingEnabled()).toBe(false);
    });

    it('should return false if diffing tools are not available', async () => {
      const config = { port: '8080' };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      mockClient.request.mockResolvedValue({
        tools: [{ name: 'someOtherTool' }],
      });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
      expect(ideClient.isDiffingEnabled()).toBe(false);
    });

    it('should return false if only openDiff tool is available', async () => {
      const config = { port: '8080' };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      mockClient.request.mockResolvedValue({
        tools: [{ name: 'openDiff' }],
      });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
      expect(ideClient.isDiffingEnabled()).toBe(false);
    });

    it('should return true if connected and diffing tools are available', async () => {
      const config = { port: '8080' };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      mockClient.request.mockResolvedValue({
        tools: [{ name: 'openDiff' }, { name: 'closeDiff' }],
      });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
      expect(ideClient.isDiffingEnabled()).toBe(true);
    });
  });

  describe('resolveDiffFromCli', () => {
    beforeEach(async () => {
      // Ensure client is "connected" for these tests
      const ideClient = await IdeClient.getInstance();
      // We need to set the client property on the instance for openDiff to work
      (ideClient as unknown as { client: Client }).client = mockClient;
      mockClient.request.mockResolvedValue({
        isError: false,
        content: [],
      });
    });

    it("should resolve an open diff as 'accepted' and return the final content", async () => {
      const ideClient = await IdeClient.getInstance();
      const closeDiffSpy = vi
        .spyOn(
          ideClient as unknown as {
            closeDiff: () => Promise<string | undefined>;
          },
          'closeDiff',
        )
        .mockResolvedValue('final content from ide');

      const diffPromise = ideClient.openDiff('/test.txt', 'new content');

      // Yield to the event loop to allow the openDiff promise executor to run
      await new Promise((resolve) => setImmediate(resolve));

      await ideClient.resolveDiffFromCli('/test.txt', 'accepted');

      const result = await diffPromise;

      expect(result).toEqual({
        status: 'accepted',
        content: 'final content from ide',
      });
      expect(closeDiffSpy).toHaveBeenCalledWith('/test.txt', {
        suppressNotification: true,
      });
      expect(
        (
          ideClient as unknown as { diffResponses: Map<string, unknown> }
        ).diffResponses.has('/test.txt'),
      ).toBe(false);
    });

    it("should resolve an open diff as 'rejected'", async () => {
      const ideClient = await IdeClient.getInstance();
      const closeDiffSpy = vi
        .spyOn(
          ideClient as unknown as {
            closeDiff: () => Promise<string | undefined>;
          },
          'closeDiff',
        )
        .mockResolvedValue(undefined);

      const diffPromise = ideClient.openDiff('/test.txt', 'new content');

      // Yield to the event loop to allow the openDiff promise executor to run
      await new Promise((resolve) => setImmediate(resolve));

      await ideClient.resolveDiffFromCli('/test.txt', 'rejected');

      const result = await diffPromise;

      expect(result).toEqual({
        status: 'rejected',
        content: undefined,
      });
      expect(closeDiffSpy).toHaveBeenCalledWith('/test.txt', {
        suppressNotification: true,
      });
      expect(
        (
          ideClient as unknown as { diffResponses: Map<string, unknown> }
        ).diffResponses.has('/test.txt'),
      ).toBe(false);
    });

    it('should do nothing if no diff is open for the given file path', async () => {
      const ideClient = await IdeClient.getInstance();
      const closeDiffSpy = vi
        .spyOn(
          ideClient as unknown as {
            closeDiff: () => Promise<string | undefined>;
          },
          'closeDiff',
        )
        .mockResolvedValue(undefined);

      // No call to openDiff, so no resolver will exist.
      await ideClient.resolveDiffFromCli('/non-existent.txt', 'accepted');

      expect(closeDiffSpy).toHaveBeenCalledWith('/non-existent.txt', {
        suppressNotification: true,
      });
      // No crash should occur, and nothing should be in the map.
      expect(
        (
          ideClient as unknown as { diffResponses: Map<string, unknown> }
        ).diffResponses.has('/non-existent.txt'),
      ).toBe(false);
    });
  });

  describe('closeDiff', () => {
    beforeEach(async () => {
      const ideClient = await IdeClient.getInstance();
      (ideClient as unknown as { client: Client }).client = mockClient;
    });

    it('should return undefined if client is not connected', async () => {
      const ideClient = await IdeClient.getInstance();
      (ideClient as unknown as { client: Client | undefined }).client =
        undefined;

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });

    it('should call client.request with correct arguments', async () => {
      const ideClient = await IdeClient.getInstance();
      // Return a valid, empty response as the return value is not under test here.
      mockClient.request.mockResolvedValue({ isError: false, content: [] });

      await (
        ideClient as unknown as {
          closeDiff: (
            f: string,
            o?: { suppressNotification?: boolean },
          ) => Promise<void>;
        }
      ).closeDiff('/test.txt', { suppressNotification: true });

      expect(mockClient.request).toHaveBeenCalledWith(
        expect.objectContaining({
          params: {
            name: 'closeDiff',
            arguments: {
              filePath: '/test.txt',
              suppressNotification: true,
            },
          },
        }),
        expect.any(Object), // Schema
        expect.any(Object), // Options
      );
    });

    it('should return content from a valid JSON response', async () => {
      const ideClient = await IdeClient.getInstance();
      const response = {
        isError: false,
        content: [
          { type: 'text', text: JSON.stringify({ content: 'file content' }) },
        ],
      };
      mockClient.request.mockResolvedValue(response);

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<string> }
      ).closeDiff('/test.txt');
      expect(result).toBe('file content');
    });

    it('should return undefined for a valid JSON response with null content', async () => {
      const ideClient = await IdeClient.getInstance();
      const response = {
        isError: false,
        content: [{ type: 'text', text: JSON.stringify({ content: null }) }],
      };
      mockClient.request.mockResolvedValue(response);

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });

    it('should return undefined if response is not valid JSON', async () => {
      const ideClient = await IdeClient.getInstance();
      const response = {
        isError: false,
        content: [{ type: 'text', text: 'not json' }],
      };
      mockClient.request.mockResolvedValue(response);

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });

    it('should return undefined if request result has isError: true', async () => {
      const ideClient = await IdeClient.getInstance();
      const response = {
        isError: true,
        content: [{ type: 'text', text: 'An error occurred' }],
      };
      mockClient.request.mockResolvedValue(response);

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });

    it('should return undefined if client.request throws', async () => {
      const ideClient = await IdeClient.getInstance();
      mockClient.request.mockRejectedValue(new Error('Request failed'));

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });

    it('should return undefined if response has no text part', async () => {
      const ideClient = await IdeClient.getInstance();
      const response = {
        isError: false,
        content: [{ type: 'other' }],
      };
      mockClient.request.mockResolvedValue(response);

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });

    it('should return undefined if response is falsy', async () => {
      const ideClient = await IdeClient.getInstance();
      // Mocking with `null as any` to test the falsy path, as the mock
      // function is strictly typed.
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      mockClient.request.mockResolvedValue(null as any);

      const result = await (
        ideClient as unknown as { closeDiff: (f: string) => Promise<void> }
      ).closeDiff('/test.txt');
      expect(result).toBeUndefined();
    });
  });

  describe('authentication', () => {
    it('should connect with an auth token if provided in the discovery file', async () => {
      const authToken = 'test-auth-token';
      const config = { port: '8080', authToken };
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(config);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
        new URL('http://127.0.0.1:8080/mcp'),
        expect.objectContaining({
          requestInit: {
            headers: {
              Authorization: `Bearer ${authToken}`,
            },
          },
        }),
      );
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });

    it('should connect with an auth token from environment variable if config file is missing', async () => {
      vi.mocked(getConnectionConfigFromFile).mockResolvedValue(undefined);
      vi.mocked(validateWorkspacePath).mockReturnValue({ isValid: true });
      vi.mocked(getPortFromEnv).mockReturnValue('9090');
      process.env['GEMINI_CLI_IDE_AUTH_TOKEN'] = 'env-auth-token';

      const ideClient = await IdeClient.getInstance();
      await ideClient.connect();

      expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
        new URL('http://127.0.0.1:9090/mcp'),
        expect.objectContaining({
          requestInit: {
            headers: {
              Authorization: 'Bearer env-auth-token',
            },
          },
        }),
      );
      expect(ideClient.getConnectionStatus().status).toBe(
        IDEConnectionStatus.Connected,
      );
    });
  });
});