summaryrefslogtreecommitdiff
path: root/packages/cli/src/ui/commands/mcpCommand.test.ts
diff options
context:
space:
mode:
authorBrian Ray <[email protected]>2025-07-22 10:05:36 -0400
committerGitHub <[email protected]>2025-07-22 14:05:36 +0000
commit4d653c833ac1371939876e03407471248263393e (patch)
treed43a6e590162f7b830a3ab5290615c22009a5052 /packages/cli/src/ui/commands/mcpCommand.test.ts
parent258c8489092c0970db0f693ddf0956e17051316c (diff)
MCP OAuth Part 3 - CLI/UI/Documentation (#4319)
Co-authored-by: Greg Shikhman <[email protected]>
Diffstat (limited to 'packages/cli/src/ui/commands/mcpCommand.test.ts')
-rw-r--r--packages/cli/src/ui/commands/mcpCommand.test.ts166
1 files changed, 166 insertions, 0 deletions
diff --git a/packages/cli/src/ui/commands/mcpCommand.test.ts b/packages/cli/src/ui/commands/mcpCommand.test.ts
index f23cf3ab..e52cb9df 100644
--- a/packages/cli/src/ui/commands/mcpCommand.test.ts
+++ b/packages/cli/src/ui/commands/mcpCommand.test.ts
@@ -30,6 +30,13 @@ vi.mock('@google/gemini-cli-core', async (importOriginal) => {
...actual,
getMCPServerStatus: vi.fn(),
getMCPDiscoveryState: vi.fn(),
+ MCPOAuthProvider: {
+ authenticate: vi.fn(),
+ },
+ MCPOAuthTokenStorage: {
+ getToken: vi.fn(),
+ isTokenExpired: vi.fn(),
+ },
};
});
@@ -810,4 +817,163 @@ describe('mcpCommand', () => {
}
});
});
+
+ describe('auth subcommand', () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it('should list OAuth-enabled servers when no server name is provided', async () => {
+ const context = createMockCommandContext({
+ services: {
+ config: {
+ getMcpServers: vi.fn().mockReturnValue({
+ 'oauth-server': { oauth: { enabled: true } },
+ 'regular-server': {},
+ 'another-oauth': { oauth: { enabled: true } },
+ }),
+ },
+ },
+ });
+
+ const authCommand = mcpCommand.subCommands?.find(
+ (cmd) => cmd.name === 'auth',
+ );
+ expect(authCommand).toBeDefined();
+
+ const result = await authCommand!.action!(context, '');
+ expect(isMessageAction(result)).toBe(true);
+ if (isMessageAction(result)) {
+ expect(result.messageType).toBe('info');
+ expect(result.content).toContain('oauth-server');
+ expect(result.content).toContain('another-oauth');
+ expect(result.content).not.toContain('regular-server');
+ expect(result.content).toContain('/mcp auth <server-name>');
+ }
+ });
+
+ it('should show message when no OAuth servers are configured', async () => {
+ const context = createMockCommandContext({
+ services: {
+ config: {
+ getMcpServers: vi.fn().mockReturnValue({
+ 'regular-server': {},
+ }),
+ },
+ },
+ });
+
+ const authCommand = mcpCommand.subCommands?.find(
+ (cmd) => cmd.name === 'auth',
+ );
+ const result = await authCommand!.action!(context, '');
+
+ expect(isMessageAction(result)).toBe(true);
+ if (isMessageAction(result)) {
+ expect(result.messageType).toBe('info');
+ expect(result.content).toBe(
+ 'No MCP servers configured with OAuth authentication.',
+ );
+ }
+ });
+
+ it('should authenticate with a specific server', async () => {
+ const mockToolRegistry = {
+ discoverToolsForServer: vi.fn(),
+ };
+ const mockGeminiClient = {
+ setTools: vi.fn(),
+ };
+
+ const context = createMockCommandContext({
+ services: {
+ config: {
+ getMcpServers: vi.fn().mockReturnValue({
+ 'test-server': {
+ url: 'http://localhost:3000',
+ oauth: { enabled: true },
+ },
+ }),
+ getToolRegistry: vi.fn().mockResolvedValue(mockToolRegistry),
+ getGeminiClient: vi.fn().mockReturnValue(mockGeminiClient),
+ },
+ },
+ });
+
+ const { MCPOAuthProvider } = await import('@google/gemini-cli-core');
+
+ const authCommand = mcpCommand.subCommands?.find(
+ (cmd) => cmd.name === 'auth',
+ );
+ const result = await authCommand!.action!(context, 'test-server');
+
+ expect(MCPOAuthProvider.authenticate).toHaveBeenCalledWith(
+ 'test-server',
+ { enabled: true },
+ 'http://localhost:3000',
+ );
+ expect(mockToolRegistry.discoverToolsForServer).toHaveBeenCalledWith(
+ 'test-server',
+ );
+ expect(mockGeminiClient.setTools).toHaveBeenCalled();
+
+ expect(isMessageAction(result)).toBe(true);
+ if (isMessageAction(result)) {
+ expect(result.messageType).toBe('info');
+ expect(result.content).toContain('Successfully authenticated');
+ }
+ });
+
+ it('should handle authentication errors', async () => {
+ const context = createMockCommandContext({
+ services: {
+ config: {
+ getMcpServers: vi.fn().mockReturnValue({
+ 'test-server': { oauth: { enabled: true } },
+ }),
+ },
+ },
+ });
+
+ const { MCPOAuthProvider } = await import('@google/gemini-cli-core');
+ (
+ MCPOAuthProvider.authenticate as ReturnType<typeof vi.fn>
+ ).mockRejectedValue(new Error('Auth failed'));
+
+ const authCommand = mcpCommand.subCommands?.find(
+ (cmd) => cmd.name === 'auth',
+ );
+ const result = await authCommand!.action!(context, 'test-server');
+
+ expect(isMessageAction(result)).toBe(true);
+ if (isMessageAction(result)) {
+ expect(result.messageType).toBe('error');
+ expect(result.content).toContain('Failed to authenticate');
+ expect(result.content).toContain('Auth failed');
+ }
+ });
+
+ it('should handle non-existent server', async () => {
+ const context = createMockCommandContext({
+ services: {
+ config: {
+ getMcpServers: vi.fn().mockReturnValue({
+ 'existing-server': {},
+ }),
+ },
+ },
+ });
+
+ const authCommand = mcpCommand.subCommands?.find(
+ (cmd) => cmd.name === 'auth',
+ );
+ const result = await authCommand!.action!(context, 'non-existent');
+
+ expect(isMessageAction(result)).toBe(true);
+ if (isMessageAction(result)) {
+ expect(result.messageType).toBe('error');
+ expect(result.content).toContain("MCP server 'non-existent' not found");
+ }
+ });
+ });
});