Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
80 changes: 80 additions & 0 deletions packages/vscode-ide-companion/src/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,86 @@ describe('activate', () => {
expect(vscode.workspace.onDidGrantWorkspaceTrust).toHaveBeenCalled();
});

describe('disposable tracking', () => {
// Each registration returns a Disposable tagged with what produced it, so
// the assertions below can tell *which* Disposables reached
// context.subscriptions rather than merely that the registration ran.
//
// Asserting the call happened is not enough: a comma expression
// `(regA(), regB())` still evaluates both operands, so both mocks are
// called while only regB's Disposable becomes the argument to push().
// These tests read context.subscriptions for that reason.
const tagged = (tag: string) => ({ tag, dispose: vi.fn() });

beforeEach(() => {
vi.mocked(context.globalState.get).mockReturnValue(true);
vi.mocked(vscode.workspace.onDidCloseTextDocument).mockImplementation(
() => tagged('onDidCloseTextDocument') as never,
);
vi.mocked(
vscode.workspace.registerTextDocumentContentProvider,
).mockImplementation(
() => tagged('registerTextDocumentContentProvider') as never,
);
vi.mocked(
vscode.workspace.onDidChangeWorkspaceFolders,
).mockImplementation(
() => tagged('onDidChangeWorkspaceFolders') as never,
);
vi.mocked(vscode.workspace.onDidGrantWorkspaceTrust).mockImplementation(
() => tagged('onDidGrantWorkspaceTrust') as never,
);
vi.mocked(vscode.commands.registerCommand).mockImplementation(
(command: string) => tagged(`command:${command}`) as never,
);
});

const subscribedTags = () =>
context.subscriptions.map((d) => (d as unknown as { tag: string }).tag);

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 current implementation of subscribedTags directly accesses the tag property on all items in context.subscriptions. However, ideServer.start(context) also pushes standard, untagged Disposable objects (or undefined if the underlying event emitter is mocked to return undefined) to context.subscriptions.

Currently, these tests only pass because ideServer.start throws an unhandled error during test execution (due to vscode.workspace.onDidOpenTextDocument being unmocked), which prevents those untagged disposables from being pushed. If ideServer.start is ever successfully executed or mocked in the future, subscribedTags will throw a TypeError (when trying to read .tag of undefined) or include undefined values, causing the assertions to fail.

Using optional chaining and filtering out non-string tags makes the test suite robust against these untagged disposables, ensuring we handle the undefined cases safely.

    const subscribedTags = () =>
      context.subscriptions
        .map((d) => (d as any)?.tag)
        .filter((tag): tag is string => typeof tag === 'string');
References
  1. When consuming an object, if a property is optional in its type definition (interface), callers must handle the undefined case (e.g., by providing a default with ??). Do not rely on the implementation details of the function that creates the object to always provide a value, as this can change. Code against the interface contract.


it('tracks every registration made during activation', async () => {
await activate(context);

expect(subscribedTags()).toEqual(
expect.arrayContaining([
'onDidCloseTextDocument',
'registerTextDocumentContentProvider',
'command:gemini.diff.accept',
'command:gemini.diff.cancel',
'onDidChangeWorkspaceFolders',
'onDidGrantWorkspaceTrust',
'command:gemini-cli.runGeminiCLI',
'command:gemini-cli.showNotices',
]),
);
});

it('tracks the gemini.diff.accept command so re-activation does not collide', async () => {
// Left untracked, the command stays registered after deactivation and a
// re-activate in the same extension host throws
// "command 'gemini.diff.accept' already exists".
await activate(context);

expect(subscribedTags()).toContain('command:gemini.diff.accept');
});

it('tracks the onDidChangeWorkspaceFolders listener so it stops on deactivation', async () => {
// Left untracked, the listener outlives the IDEServer it calls and keeps
// firing syncEnvVars() against a stopped server.
await activate(context);

expect(subscribedTags()).toContain('onDidChangeWorkspaceFolders');
});

it('pushes one Disposable per registration', async () => {
await activate(context);

const tags = subscribedTags();
expect(tags).toHaveLength(new Set(tags).size);
expect(tags).toHaveLength(8);
});
});

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