Add MCP Root change notifications (#6502)

This commit is contained in:
Jacob MacDonald
2025-08-18 14:09:02 -07:00
committed by GitHub
parent 465ac9f547
commit 3960ccf781
4 changed files with 200 additions and 7 deletions

View File

@@ -4,7 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
@@ -294,6 +294,88 @@ describe('WorkspaceContext with real filesystem', () => {
});
});
describe('onDirectoriesChanged', () => {
it('should call listener when adding a directory', () => {
const workspaceContext = new WorkspaceContext(cwd);
const listener = vi.fn();
workspaceContext.onDirectoriesChanged(listener);
workspaceContext.addDirectory(otherDir);
expect(listener).toHaveBeenCalledOnce();
});
it('should not call listener when adding a duplicate directory', () => {
const workspaceContext = new WorkspaceContext(cwd);
workspaceContext.addDirectory(otherDir);
const listener = vi.fn();
workspaceContext.onDirectoriesChanged(listener);
workspaceContext.addDirectory(otherDir);
expect(listener).not.toHaveBeenCalled();
});
it('should call listener when setting different directories', () => {
const workspaceContext = new WorkspaceContext(cwd);
const listener = vi.fn();
workspaceContext.onDirectoriesChanged(listener);
workspaceContext.setDirectories([otherDir]);
expect(listener).toHaveBeenCalledOnce();
});
it('should not call listener when setting same directories', () => {
const workspaceContext = new WorkspaceContext(cwd);
const listener = vi.fn();
workspaceContext.onDirectoriesChanged(listener);
workspaceContext.setDirectories([cwd]);
expect(listener).not.toHaveBeenCalled();
});
it('should support multiple listeners', () => {
const workspaceContext = new WorkspaceContext(cwd);
const listener1 = vi.fn();
const listener2 = vi.fn();
workspaceContext.onDirectoriesChanged(listener1);
workspaceContext.onDirectoriesChanged(listener2);
workspaceContext.addDirectory(otherDir);
expect(listener1).toHaveBeenCalledOnce();
expect(listener2).toHaveBeenCalledOnce();
});
it('should allow unsubscribing a listener', () => {
const workspaceContext = new WorkspaceContext(cwd);
const listener = vi.fn();
const unsubscribe = workspaceContext.onDirectoriesChanged(listener);
unsubscribe();
workspaceContext.addDirectory(otherDir);
expect(listener).not.toHaveBeenCalled();
});
it('should not fail if a listener throws an error', () => {
const workspaceContext = new WorkspaceContext(cwd);
const errorListener = () => {
throw new Error('test error');
};
const listener = vi.fn();
workspaceContext.onDirectoriesChanged(errorListener);
workspaceContext.onDirectoriesChanged(listener);
expect(() => {
workspaceContext.addDirectory(otherDir);
}).not.toThrow();
expect(listener).toHaveBeenCalledOnce();
});
});
describe('getDirectories', () => {
it('should return a copy of directories array', () => {
const workspaceContext = new WorkspaceContext(cwd);