|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createCheckPythonEnvTool } from './checkPythonEnvironment'; |
| 3 | +import type { PipServerController } from '../../types'; |
| 4 | +import { McpToolResponse } from '../utils/mcpUtils'; |
| 5 | + |
| 6 | +vi.mock('vscode'); |
| 7 | + |
| 8 | +const MOCK_EXECUTION_TIME_MS = 100; |
| 9 | + |
| 10 | +const EXPECTED_AVAILABLE = { |
| 11 | + success: true, |
| 12 | + message: 'Python environment is available', |
| 13 | + details: { |
| 14 | + isAvailable: true, |
| 15 | + interpreterPath: '/path/to/python', |
| 16 | + }, |
| 17 | + executionTimeMs: MOCK_EXECUTION_TIME_MS, |
| 18 | +} as const; |
| 19 | + |
| 20 | +const EXPECTED_NOT_AVAILABLE = { |
| 21 | + success: true, |
| 22 | + message: 'Python environment is not available', |
| 23 | + details: { |
| 24 | + isAvailable: false, |
| 25 | + }, |
| 26 | + executionTimeMs: MOCK_EXECUTION_TIME_MS, |
| 27 | +} as const; |
| 28 | + |
| 29 | +const EXPECTED_CONTROLLER_NOT_AVAILABLE = { |
| 30 | + success: false, |
| 31 | + message: 'PipServerController not available', |
| 32 | + executionTimeMs: MOCK_EXECUTION_TIME_MS, |
| 33 | +} as const; |
| 34 | + |
| 35 | +const EXPECTED_CHECK_ERROR = { |
| 36 | + success: false, |
| 37 | + message: 'Failed to check Python environment: Check failed', |
| 38 | + executionTimeMs: MOCK_EXECUTION_TIME_MS, |
| 39 | +} as const; |
| 40 | + |
| 41 | +describe('checkPythonEnvironment', () => { |
| 42 | + const mockPipServerController = { |
| 43 | + checkPipInstall: vi.fn(), |
| 44 | + } as unknown as PipServerController; |
| 45 | + |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks(); |
| 48 | + |
| 49 | + vi.spyOn(McpToolResponse.prototype, 'getElapsedTimeMs').mockReturnValue( |
| 50 | + MOCK_EXECUTION_TIME_MS |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should return correct tool spec', () => { |
| 55 | + const tool = createCheckPythonEnvTool({ |
| 56 | + pipServerController: mockPipServerController, |
| 57 | + }); |
| 58 | + |
| 59 | + expect(tool.name).toBe('checkPythonEnvironment'); |
| 60 | + expect(tool.spec.title).toBe('Check Python Environment'); |
| 61 | + expect(tool.spec.description).toBe( |
| 62 | + 'Check if the Python environment supports starting a Deephaven pip server.' |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should return error when pipServerController is null', async () => { |
| 67 | + const tool = createCheckPythonEnvTool({ pipServerController: null }); |
| 68 | + const result = await tool.handler({}); |
| 69 | + |
| 70 | + expect(result.structuredContent).toEqual(EXPECTED_CONTROLLER_NOT_AVAILABLE); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return success when Python environment is available', async () => { |
| 74 | + vi.mocked(mockPipServerController.checkPipInstall).mockResolvedValue({ |
| 75 | + isAvailable: true, |
| 76 | + interpreterPath: '/path/to/python', |
| 77 | + }); |
| 78 | + |
| 79 | + const tool = createCheckPythonEnvTool({ |
| 80 | + pipServerController: mockPipServerController, |
| 81 | + }); |
| 82 | + const result = await tool.handler({}); |
| 83 | + |
| 84 | + expect(mockPipServerController.checkPipInstall).toHaveBeenCalledOnce(); |
| 85 | + expect(result.structuredContent).toEqual(EXPECTED_AVAILABLE); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return success when Python environment is not available', async () => { |
| 89 | + vi.mocked(mockPipServerController.checkPipInstall).mockResolvedValue({ |
| 90 | + isAvailable: false, |
| 91 | + }); |
| 92 | + |
| 93 | + const tool = createCheckPythonEnvTool({ |
| 94 | + pipServerController: mockPipServerController, |
| 95 | + }); |
| 96 | + const result = await tool.handler({}); |
| 97 | + |
| 98 | + expect(mockPipServerController.checkPipInstall).toHaveBeenCalledOnce(); |
| 99 | + expect(result.structuredContent).toEqual(EXPECTED_NOT_AVAILABLE); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should handle errors from checkPipInstall', async () => { |
| 103 | + const error = new Error('Check failed'); |
| 104 | + vi.mocked(mockPipServerController.checkPipInstall).mockRejectedValue(error); |
| 105 | + |
| 106 | + const tool = createCheckPythonEnvTool({ |
| 107 | + pipServerController: mockPipServerController, |
| 108 | + }); |
| 109 | + const result = await tool.handler({}); |
| 110 | + |
| 111 | + expect(result.structuredContent).toEqual(EXPECTED_CHECK_ERROR); |
| 112 | + }); |
| 113 | +}); |
0 commit comments