refactor(ide): Improve IDE detection discovery (#6765)

This commit is contained in:
Shreya Keshive
2025-08-25 11:39:57 -07:00
committed by GitHub
parent 0641b1c095
commit 776627c855
13 changed files with 373 additions and 152 deletions

View File

@@ -4,65 +4,133 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, afterEach, vi } from 'vitest';
import { detectIde, DetectedIde } from './detect-ide.js';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { detectIde, DetectedIde, getIdeInfo } from './detect-ide.js';
describe('detectIde', () => {
const ideProcessInfo = { pid: 123, command: 'some/path/to/code' };
const ideProcessInfoNoCode = { pid: 123, command: 'some/path/to/fork' };
afterEach(() => {
vi.unstubAllEnvs();
});
it.each([
{
env: {},
expected: DetectedIde.VSCode,
},
{
env: { __COG_BASHRC_SOURCED: '1' },
expected: DetectedIde.Devin,
},
{
env: { REPLIT_USER: 'test' },
expected: DetectedIde.Replit,
},
{
env: { CURSOR_TRACE_ID: 'test' },
expected: DetectedIde.Cursor,
},
{
env: { CODESPACES: 'true' },
expected: DetectedIde.Codespaces,
},
{
env: { EDITOR_IN_CLOUD_SHELL: 'true' },
expected: DetectedIde.CloudShell,
},
{
env: { CLOUD_SHELL: 'true' },
expected: DetectedIde.CloudShell,
},
{
env: { TERM_PRODUCT: 'Trae' },
expected: DetectedIde.Trae,
},
{
env: { FIREBASE_DEPLOY_AGENT: 'true' },
expected: DetectedIde.FirebaseStudio,
},
{
env: { MONOSPACE_ENV: 'true' },
expected: DetectedIde.FirebaseStudio,
},
])('detects the IDE for $expected', ({ env, expected }) => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
for (const [key, value] of Object.entries(env)) {
vi.stubEnv(key, value);
}
expect(detectIde()).toBe(expected);
it('should return undefined if TERM_PROGRAM is not vscode', () => {
vi.stubEnv('TERM_PROGRAM', '');
expect(detectIde(ideProcessInfo)).toBeUndefined();
});
it('returns undefined for non-vscode', () => {
vi.stubEnv('TERM_PROGRAM', 'definitely-not-vscode');
expect(detectIde()).toBeUndefined();
it('should detect Devin', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('__COG_BASHRC_SOURCED', '1');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.Devin);
});
it('should detect Replit', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('REPLIT_USER', 'testuser');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.Replit);
});
it('should detect Cursor', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('CURSOR_TRACE_ID', 'some-id');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.Cursor);
});
it('should detect Codespaces', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('CODESPACES', 'true');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.Codespaces);
});
it('should detect Cloud Shell via EDITOR_IN_CLOUD_SHELL', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('EDITOR_IN_CLOUD_SHELL', 'true');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.CloudShell);
});
it('should detect Cloud Shell via CLOUD_SHELL', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('CLOUD_SHELL', 'true');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.CloudShell);
});
it('should detect Trae', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('TERM_PRODUCT', 'Trae');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.Trae);
});
it('should detect Firebase Studio via FIREBASE_DEPLOY_AGENT', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('FIREBASE_DEPLOY_AGENT', 'true');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.FirebaseStudio);
});
it('should detect Firebase Studio via MONOSPACE_ENV', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('MONOSPACE_ENV', 'true');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.FirebaseStudio);
});
it('should detect VSCode when no other IDE is detected and command includes "code"', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.VSCode);
});
it('should detect VSCodeFork when no other IDE is detected and command does not include "code"', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
expect(detectIde(ideProcessInfoNoCode)).toBe(DetectedIde.VSCodeFork);
});
it('should prioritize other IDEs over VSCode detection', () => {
vi.stubEnv('TERM_PROGRAM', 'vscode');
vi.stubEnv('REPLIT_USER', 'testuser');
expect(detectIde(ideProcessInfo)).toBe(DetectedIde.Replit);
});
});
describe('getIdeInfo', () => {
it('should return correct info for Devin', () => {
expect(getIdeInfo(DetectedIde.Devin)).toEqual({ displayName: 'Devin' });
});
it('should return correct info for Replit', () => {
expect(getIdeInfo(DetectedIde.Replit)).toEqual({ displayName: 'Replit' });
});
it('should return correct info for Cursor', () => {
expect(getIdeInfo(DetectedIde.Cursor)).toEqual({ displayName: 'Cursor' });
});
it('should return correct info for CloudShell', () => {
expect(getIdeInfo(DetectedIde.CloudShell)).toEqual({
displayName: 'Cloud Shell',
});
});
it('should return correct info for Codespaces', () => {
expect(getIdeInfo(DetectedIde.Codespaces)).toEqual({
displayName: 'GitHub Codespaces',
});
});
it('should return correct info for FirebaseStudio', () => {
expect(getIdeInfo(DetectedIde.FirebaseStudio)).toEqual({
displayName: 'Firebase Studio',
});
});
it('should return correct info for Trae', () => {
expect(getIdeInfo(DetectedIde.Trae)).toEqual({ displayName: 'Trae' });
});
it('should return correct info for VSCode', () => {
expect(getIdeInfo(DetectedIde.VSCode)).toEqual({ displayName: 'VS Code' });
});
it('should return correct info for VSCodeFork', () => {
expect(getIdeInfo(DetectedIde.VSCodeFork)).toEqual({ displayName: 'IDE' });
});
});