|
| 1 | +import * as vscode from 'vscode'; |
| 2 | +import type { dh as DhcType } from '@deephaven/jsapi-types'; |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { DhcService } from './DhcService'; |
| 5 | +import { ConfigService } from './ConfigService'; |
| 6 | +import type { RemoteFileSourceService } from './RemoteFileSourceService'; |
| 7 | +import type { UniqueID } from '../types'; |
| 8 | + |
| 9 | +vi.mock('vscode'); |
| 10 | + |
| 11 | +vi.mock('./ConfigService', () => { |
| 12 | + const mockConfigService = { |
| 13 | + getImportPrefixes: vi.fn(), |
| 14 | + }; |
| 15 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 16 | + return { ConfigService: mockConfigService }; |
| 17 | +}); |
| 18 | + |
| 19 | +/** Build a minimal DhcService instance that has a session already set up. */ |
| 20 | +function createTestDhcService({ |
| 21 | + pythonRemoteFileSourcePlugin, |
| 22 | + setControllerImportPrefixes = vi.fn(), |
| 23 | + setPythonServerExecutionContext = vi.fn().mockResolvedValue(undefined), |
| 24 | + sessionRunCode = vi.fn().mockResolvedValue({ |
| 25 | + error: '', |
| 26 | + changes: { created: [], updated: [], removed: [] }, |
| 27 | + }), |
| 28 | +}: { |
| 29 | + pythonRemoteFileSourcePlugin?: DhcType.Widget | null; |
| 30 | + setControllerImportPrefixes?: ReturnType<typeof vi.fn>; |
| 31 | + setPythonServerExecutionContext?: ReturnType<typeof vi.fn>; |
| 32 | + sessionRunCode?: ReturnType<typeof vi.fn>; |
| 33 | +} = {}): DhcService { |
| 34 | + const mockSession = { |
| 35 | + runCode: sessionRunCode, |
| 36 | + } as unknown as DhcType.IdeSession; |
| 37 | + |
| 38 | + const mockCn = { |
| 39 | + getConsoleTypes: vi.fn().mockResolvedValue(['python']), |
| 40 | + } as unknown as DhcType.IdeConnection; |
| 41 | + |
| 42 | + const mockRemoteFileSourceService = { |
| 43 | + setControllerImportPrefixes, |
| 44 | + setPythonServerExecutionContext, |
| 45 | + } as unknown as RemoteFileSourceService; |
| 46 | + |
| 47 | + const mockDiagnosticsCollection = { |
| 48 | + set: vi.fn(), |
| 49 | + clear: vi.fn(), |
| 50 | + } as unknown as vscode.DiagnosticCollection; |
| 51 | + |
| 52 | + const service = Object.assign(Object.create(DhcService.prototype), { |
| 53 | + session: mockSession, |
| 54 | + cn: mockCn, |
| 55 | + cnId: 'test-cn-id' as UniqueID, |
| 56 | + pythonRemoteFileSourcePlugin: |
| 57 | + pythonRemoteFileSourcePlugin !== undefined |
| 58 | + ? pythonRemoteFileSourcePlugin |
| 59 | + : ({} as DhcType.Widget), |
| 60 | + groovyRemoteFileSourcePluginService: null, |
| 61 | + remoteFileSourceService: mockRemoteFileSourceService, |
| 62 | + diagnosticsCollection: mockDiagnosticsCollection, |
| 63 | + groovyDiagnosticsCollection: mockDiagnosticsCollection, |
| 64 | + outputChannel: { |
| 65 | + appendLine: vi.fn(), |
| 66 | + show: vi.fn(), |
| 67 | + } as unknown as vscode.OutputChannel, |
| 68 | + toaster: { error: vi.fn(), info: vi.fn() }, |
| 69 | + _isRunningCode: false, |
| 70 | + _onDidChangeRunningCodeStatus: { fire: vi.fn() }, |
| 71 | + disposables: { add: vi.fn() }, |
| 72 | + serverUrl: new URL('http://localhost:10000/'), |
| 73 | + }); |
| 74 | + |
| 75 | + return service; |
| 76 | +} |
| 77 | + |
| 78 | +function mockTextDoc(codeText: string): vscode.TextDocument { |
| 79 | + return { |
| 80 | + uri: vscode.Uri.file('/path/to/file.py'), |
| 81 | + getText: vi.fn().mockReturnValue(codeText), |
| 82 | + } as unknown as vscode.TextDocument; |
| 83 | +} |
| 84 | + |
| 85 | +describe('DhcService.runCode – importPrefixes setting', () => { |
| 86 | + const mockSetControllerImportPrefixes = vi.fn(); |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + vi.clearAllMocks(); |
| 90 | + }); |
| 91 | + |
| 92 | + it.each([ |
| 93 | + { |
| 94 | + label: 'uses setting prefixes when configured', |
| 95 | + configPrefixes: ['myPrefix'], |
| 96 | + input: 'x = 1', |
| 97 | + expected: new Set(['myPrefix']), |
| 98 | + }, |
| 99 | + { |
| 100 | + label: 'uses all prefixes when multiple configured', |
| 101 | + configPrefixes: ['prefix1', 'prefix2'], |
| 102 | + input: 'x = 1', |
| 103 | + expected: new Set(['prefix1', 'prefix2']), |
| 104 | + }, |
| 105 | + { |
| 106 | + label: 'falls back to extraction when undefined and code has prefixes', |
| 107 | + configPrefixes: undefined, |
| 108 | + input: 'deephaven_enterprise.controller_import.meta_import("myPrefix")\n', |
| 109 | + expected: new Set(['myPrefix']), |
| 110 | + }, |
| 111 | + { |
| 112 | + label: 'skips call when undefined and no prefixes in snippet', |
| 113 | + configPrefixes: undefined, |
| 114 | + input: 'x = 1', |
| 115 | + expected: null, |
| 116 | + }, |
| 117 | + { |
| 118 | + label: 'calls with empty set for full file runs even when no prefixes found', |
| 119 | + configPrefixes: undefined, |
| 120 | + input: mockTextDoc('x = 1'), |
| 121 | + expected: new Set(), |
| 122 | + }, |
| 123 | + { |
| 124 | + label: 'setting takes precedence over meta_import in code', |
| 125 | + configPrefixes: ['forced'], |
| 126 | + input: 'deephaven_enterprise.controller_import.meta_import("otherPrefix")\n', |
| 127 | + expected: new Set(['forced']), |
| 128 | + }, |
| 129 | + { |
| 130 | + label: 'setting takes precedence over meta_import in text doc', |
| 131 | + configPrefixes: ['forced'], |
| 132 | + input: mockTextDoc( |
| 133 | + 'deephaven_enterprise.controller_import.meta_import("otherPrefix")\n' |
| 134 | + ), |
| 135 | + expected: new Set(['forced']), |
| 136 | + }, |
| 137 | + ])('$label', async ({ configPrefixes, input, expected }) => { |
| 138 | + vi.mocked(ConfigService.getImportPrefixes).mockReturnValue(configPrefixes); |
| 139 | + |
| 140 | + const service = createTestDhcService({ |
| 141 | + setControllerImportPrefixes: mockSetControllerImportPrefixes, |
| 142 | + }); |
| 143 | + |
| 144 | + await service.runCode(input, 'python'); |
| 145 | + |
| 146 | + if (expected == null) { |
| 147 | + expect(mockSetControllerImportPrefixes).not.toHaveBeenCalled(); |
| 148 | + } else { |
| 149 | + expect(mockSetControllerImportPrefixes).toHaveBeenCalledWith(expected); |
| 150 | + } |
| 151 | + }); |
| 152 | +}); |
0 commit comments