Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions packages/vscode-ide-companion/src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import {
detectIdeFromEnv,
} from '@google/gemini-cli-core/src/ide/detect-ide.js';

const createDisposable = (name: string) => ({
name,
dispose: vi.fn(),
});

vi.mock('@google/gemini-cli-core/src/ide/detect-ide.js', async () => {
const actual = await vi.importActual(
'@google/gemini-cli-core/src/ide/detect-ide.js',
Expand Down Expand Up @@ -43,16 +48,26 @@ vi.mock('vscode', () => ({
},
workspace: {
workspaceFolders: [],
onDidCloseTextDocument: vi.fn(),
registerTextDocumentContentProvider: vi.fn(),
onDidChangeWorkspaceFolders: vi.fn(),
onDidGrantWorkspaceTrust: vi.fn(),
onDidCloseTextDocument: vi.fn(() =>
createDisposable('onDidCloseTextDocument'),
),
registerTextDocumentContentProvider: vi.fn(() =>
createDisposable('registerTextDocumentContentProvider'),
),
onDidChangeWorkspaceFolders: vi.fn(() =>
createDisposable('onDidChangeWorkspaceFolders'),
),
onDidGrantWorkspaceTrust: vi.fn(() =>
createDisposable('onDidGrantWorkspaceTrust'),
),
getConfiguration: vi.fn(() => ({
get: vi.fn(),
})),
},
commands: {
registerCommand: vi.fn(),
registerCommand: vi.fn((command: string) =>
createDisposable(`registerCommand:${command}`),
),
executeCommand: vi.fn(),
},
Uri: {
Expand Down Expand Up @@ -131,6 +146,27 @@ describe('activate', () => {
expect(vscode.workspace.onDidGrantWorkspaceTrust).toHaveBeenCalled();
});

it('tracks all activation disposables for cleanup', async () => {
await activate(context);

Comment on lines +149 to +151

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The test suite calls activate(context) in multiple tests, which starts an Express server on a random port each time. However, deactivate() is never called in afterEach or anywhere in the tests, causing these servers to remain running in the background. This leads to resource leaks (ports, memory, file descriptors) and potential test flakiness. Please import deactivate from ./extension.js and call it in afterEach to ensure proper cleanup of the IDE server.

expect(context.subscriptions).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'registerCommand:gemini.diff.accept',
}),
expect.objectContaining({
name: 'registerCommand:gemini.diff.cancel',
}),
expect.objectContaining({
name: 'onDidChangeWorkspaceFolders',
}),
expect.objectContaining({
name: 'onDidGrantWorkspaceTrust',
}),
]),
);
});

it('should launch the Gemini CLI when the user clicks the button', async () => {
const showInformationMessageMock = vi
.mocked(vscode.window.showInformationMessage)
Expand Down
8 changes: 4 additions & 4 deletions packages/vscode-ide-companion/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export async function activate(context: vscode.ExtensionContext) {
DIFF_SCHEME,
diffContentProvider,
),
(vscode.commands.registerCommand(
vscode.commands.registerCommand(
'gemini.diff.accept',
(uri?: vscode.Uri) => {
const docUri = uri ?? vscode.window.activeTextEditor?.document.uri;
Expand All @@ -152,7 +152,7 @@ export async function activate(context: vscode.ExtensionContext) {
diffManager.cancelDiff(docUri);
}
},
)),
),
);

ideServer = new IDEServer(log, diffManager);
Expand All @@ -174,14 +174,14 @@ export async function activate(context: vscode.ExtensionContext) {
}

context.subscriptions.push(
(vscode.workspace.onDidChangeWorkspaceFolders(() => {
vscode.workspace.onDidChangeWorkspaceFolders(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ideServer.syncEnvVars();
}),
vscode.workspace.onDidGrantWorkspaceTrust(() => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
ideServer.syncEnvVars();
})),
}),
vscode.commands.registerCommand('gemini-cli.runGeminiCLI', async () => {
const workspaceFolders = vscode.workspace.workspaceFolders;
if (!workspaceFolders || workspaceFolders.length === 0) {
Expand Down