summaryrefslogtreecommitdiff
path: root/packages/core/src/ide/ide-installer.test.ts
blob: 1afd7a36b5ef863b258c2165efe044878c15a7eb (plain)
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
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { vi, describe, it, expect, beforeEach, afterEach } from 'vitest';
import { getIdeInstaller, IdeInstaller } from './ide-installer.js';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import { DetectedIde } from './detect-ide.js';

vi.mock('child_process');
vi.mock('fs');
vi.mock('os');

describe('ide-installer', () => {
  describe('getIdeInstaller', () => {
    it('should return a VsCodeInstaller for "vscode"', () => {
      const installer = getIdeInstaller(DetectedIde.VSCode);
      expect(installer).not.toBeNull();
      // A more specific check might be needed if we export the class
      expect(installer).toBeInstanceOf(Object);
    });

    it('should return an OpenVSXInstaller for "vscodium"', () => {
      const installer = getIdeInstaller(DetectedIde.VSCodium);
      expect(installer).not.toBeNull();
      expect(installer).toBeInstanceOf(Object);
    });

    it('should return a DefaultIDEInstaller for an unknown IDE', () => {
      const installer = getIdeInstaller('unknown' as DetectedIde);
      // Assuming DefaultIDEInstaller is the fallback
      expect(installer).not.toBeNull();
      expect(installer).toBeInstanceOf(Object);
    });
  });

  describe('VsCodeInstaller', () => {
    let installer: IdeInstaller;

    beforeEach(() => {
      // We get a new installer for each test to reset the find command logic
      installer = getIdeInstaller(DetectedIde.VSCode)!;
      vi.spyOn(child_process, 'execSync').mockImplementation(() => '');
      vi.spyOn(fs, 'existsSync').mockReturnValue(false);
      vi.spyOn(os, 'homedir').mockReturnValue('/home/user');
    });

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

    describe('install', () => {
      it('should return a failure message if VS Code is not installed', async () => {
        vi.spyOn(child_process, 'execSync').mockImplementation(() => {
          throw new Error('Command not found');
        });
        vi.spyOn(fs, 'existsSync').mockReturnValue(false);
        // Re-create the installer so it re-runs findVsCodeCommand
        installer = getIdeInstaller(DetectedIde.VSCode)!;
        const result = await installer.install();
        expect(result.success).toBe(false);
        expect(result.message).toContain('VS Code CLI not found');
      });
    });
  });

  describe('OpenVSXInstaller', () => {
    let installer: IdeInstaller;

    beforeEach(() => {
      installer = getIdeInstaller(DetectedIde.VSCodium)!;
    });

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

    describe('install', () => {
      it('should call execSync with the correct command and return success', async () => {
        const execSyncSpy = vi
          .spyOn(child_process, 'execSync')
          .mockImplementation(() => '');
        const result = await installer.install();
        expect(execSyncSpy).toHaveBeenCalledWith(
          'npx ovsx get google.gemini-cli-vscode-ide-companion',
          { stdio: 'pipe' },
        );
        expect(result.success).toBe(true);
        expect(result.message).toContain(
          'VS Code companion extension was installed successfully from OpenVSX',
        );
      });

      it('should return a failure message on failed installation', async () => {
        vi.spyOn(child_process, 'execSync').mockImplementation(() => {
          throw new Error('Command failed');
        });
        const result = await installer.install();
        expect(result.success).toBe(false);
        expect(result.message).toContain(
          'Failed to install VS Code companion extension from OpenVSX',
        );
      });
    });
  });
});