|
| 1 | +import { assert } from 'chai'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | +import { reset, when } from 'ts-mockito'; |
| 4 | +import { Uri } from 'vscode'; |
| 5 | + |
| 6 | +import { IProcessService, IProcessServiceFactory } from '../../platform/common/process/types.node'; |
| 7 | +import { PythonEnvironment } from '../../platform/pythonEnvironments/info'; |
| 8 | +import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../test/vscode-mock'; |
| 9 | +import { DeepnoteAgentSkillsManager } from './deepnoteAgentSkillsManager.node'; |
| 10 | + |
| 11 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 12 | +const getPrivateMethod = (obj: any, methodName: string) => { |
| 13 | + return obj[methodName].bind(obj); |
| 14 | +}; |
| 15 | + |
| 16 | +suite('DeepnoteAgentSkillsManager', () => { |
| 17 | + let manager: DeepnoteAgentSkillsManager; |
| 18 | + let execStub: sinon.SinonStub; |
| 19 | + |
| 20 | + const workspaceFolder = { uri: Uri.file('/workspace/my-project') }; |
| 21 | + |
| 22 | + const testInterpreter: PythonEnvironment = { |
| 23 | + id: 'test-python-id', |
| 24 | + uri: Uri.file('/home/user/.venvs/test-venv/bin/python') |
| 25 | + } as PythonEnvironment; |
| 26 | + |
| 27 | + function configureVSCodeMocks(appName: string, workspaceFolders?: any[]) { |
| 28 | + resetVSCodeMocks(); |
| 29 | + reset(mockedVSCodeNamespaces.env); |
| 30 | + reset(mockedVSCodeNamespaces.workspace); |
| 31 | + |
| 32 | + when(mockedVSCodeNamespaces.env.appName).thenReturn(appName); |
| 33 | + when(mockedVSCodeNamespaces.workspace.workspaceFolders).thenReturn(workspaceFolders as any); |
| 34 | + } |
| 35 | + |
| 36 | + setup(() => { |
| 37 | + configureVSCodeMocks('Cursor', [workspaceFolder]); |
| 38 | + |
| 39 | + execStub = sinon.stub().resolves({ stdout: '', stderr: '' }); |
| 40 | + |
| 41 | + const stubProcessService = { exec: execStub } as unknown as IProcessService; |
| 42 | + const stubFactory = { |
| 43 | + create: sinon.stub().resolves(stubProcessService) |
| 44 | + } as unknown as IProcessServiceFactory; |
| 45 | + |
| 46 | + manager = new DeepnoteAgentSkillsManager(stubFactory); |
| 47 | + }); |
| 48 | + |
| 49 | + suite('updateSkillsInBackground', () => { |
| 50 | + test('should run pip upgrade then install-skills', async () => { |
| 51 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 52 | + manager, |
| 53 | + 'updateSkillsInBackground' |
| 54 | + ); |
| 55 | + |
| 56 | + await updateSkills(testInterpreter); |
| 57 | + |
| 58 | + assert.strictEqual(execStub.callCount, 2); |
| 59 | + |
| 60 | + const [executable, args] = execStub.firstCall.args; |
| 61 | + |
| 62 | + assert.strictEqual(executable, testInterpreter.uri.fsPath); |
| 63 | + assert.deepStrictEqual(args, ['-m', 'pip', 'install', '--upgrade', 'deepnote-cli']); |
| 64 | + }); |
| 65 | + |
| 66 | + test('should call install-skills with correct agent and cwd', async () => { |
| 67 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 68 | + manager, |
| 69 | + 'updateSkillsInBackground' |
| 70 | + ); |
| 71 | + |
| 72 | + await updateSkills(testInterpreter); |
| 73 | + |
| 74 | + const [executable, args, options] = execStub.secondCall.args; |
| 75 | + const expectedBin = Uri.joinPath(testInterpreter.uri, '..', 'deepnote').fsPath; |
| 76 | + |
| 77 | + assert.strictEqual(executable, expectedBin); |
| 78 | + assert.deepStrictEqual(args, ['install-skills', '--agent', 'cursor']); |
| 79 | + assert.strictEqual(options.cwd, workspaceFolder.uri.fsPath); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + suite('session-scoped deduplication', () => { |
| 84 | + test('should mark environment as processed after first call', () => { |
| 85 | + manager.ensureSkillsUpdated('env-1', testInterpreter); |
| 86 | + |
| 87 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 88 | + const processed = (manager as any).processedEnvironments as Set<string>; |
| 89 | + |
| 90 | + assert.isTrue(processed.has('env-1')); |
| 91 | + }); |
| 92 | + |
| 93 | + test('should track different environments separately', () => { |
| 94 | + manager.ensureSkillsUpdated('env-1', testInterpreter); |
| 95 | + manager.ensureSkillsUpdated('env-2', testInterpreter); |
| 96 | + |
| 97 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 98 | + const processed = (manager as any).processedEnvironments as Set<string>; |
| 99 | + |
| 100 | + assert.isTrue(processed.has('env-1')); |
| 101 | + assert.isTrue(processed.has('env-2')); |
| 102 | + assert.strictEqual(processed.size, 2); |
| 103 | + }); |
| 104 | + |
| 105 | + test('should not add duplicate entries for the same environment', () => { |
| 106 | + manager.ensureSkillsUpdated('env-1', testInterpreter); |
| 107 | + manager.ensureSkillsUpdated('env-1', testInterpreter); |
| 108 | + manager.ensureSkillsUpdated('env-1', testInterpreter); |
| 109 | + |
| 110 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 111 | + const processed = (manager as any).processedEnvironments as Set<string>; |
| 112 | + |
| 113 | + assert.strictEqual(processed.size, 1); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + suite('editor detection', () => { |
| 118 | + test('should detect Cursor', async () => { |
| 119 | + configureVSCodeMocks('Cursor', [workspaceFolder]); |
| 120 | + manager = new DeepnoteAgentSkillsManager( |
| 121 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 122 | + (manager as any).processServiceFactory |
| 123 | + ); |
| 124 | + |
| 125 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 126 | + manager, |
| 127 | + 'updateSkillsInBackground' |
| 128 | + ); |
| 129 | + |
| 130 | + await updateSkills(testInterpreter); |
| 131 | + |
| 132 | + assert.deepStrictEqual(execStub.secondCall.args[1], ['install-skills', '--agent', 'cursor']); |
| 133 | + }); |
| 134 | + |
| 135 | + test('should detect Windsurf', async () => { |
| 136 | + configureVSCodeMocks('Windsurf', [workspaceFolder]); |
| 137 | + manager = new DeepnoteAgentSkillsManager( |
| 138 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 139 | + (manager as any).processServiceFactory |
| 140 | + ); |
| 141 | + |
| 142 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 143 | + manager, |
| 144 | + 'updateSkillsInBackground' |
| 145 | + ); |
| 146 | + |
| 147 | + await updateSkills(testInterpreter); |
| 148 | + |
| 149 | + assert.deepStrictEqual(execStub.secondCall.args[1], ['install-skills', '--agent', 'windsurf']); |
| 150 | + }); |
| 151 | + |
| 152 | + test('should default to github copilot for VS Code', async () => { |
| 153 | + configureVSCodeMocks('Visual Studio Code', [workspaceFolder]); |
| 154 | + manager = new DeepnoteAgentSkillsManager( |
| 155 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 156 | + (manager as any).processServiceFactory |
| 157 | + ); |
| 158 | + |
| 159 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 160 | + manager, |
| 161 | + 'updateSkillsInBackground' |
| 162 | + ); |
| 163 | + |
| 164 | + await updateSkills(testInterpreter); |
| 165 | + |
| 166 | + assert.deepStrictEqual(execStub.secondCall.args[1], ['install-skills', '--agent', 'github copilot']); |
| 167 | + }); |
| 168 | + |
| 169 | + test('should default to github copilot for unknown editors', async () => { |
| 170 | + configureVSCodeMocks('SomeUnknownEditor', [workspaceFolder]); |
| 171 | + manager = new DeepnoteAgentSkillsManager( |
| 172 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 173 | + (manager as any).processServiceFactory |
| 174 | + ); |
| 175 | + |
| 176 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 177 | + manager, |
| 178 | + 'updateSkillsInBackground' |
| 179 | + ); |
| 180 | + |
| 181 | + await updateSkills(testInterpreter); |
| 182 | + |
| 183 | + assert.deepStrictEqual(execStub.secondCall.args[1], ['install-skills', '--agent', 'github copilot']); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + suite('edge cases', () => { |
| 188 | + test('should skip when no workspace folder is open', async () => { |
| 189 | + configureVSCodeMocks('Cursor', undefined); |
| 190 | + manager = new DeepnoteAgentSkillsManager( |
| 191 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 192 | + (manager as any).processServiceFactory |
| 193 | + ); |
| 194 | + |
| 195 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 196 | + manager, |
| 197 | + 'updateSkillsInBackground' |
| 198 | + ); |
| 199 | + |
| 200 | + await updateSkills(testInterpreter); |
| 201 | + |
| 202 | + assert.strictEqual(execStub.callCount, 0); |
| 203 | + }); |
| 204 | + |
| 205 | + test('should skip when workspace folders array is empty', async () => { |
| 206 | + configureVSCodeMocks('Cursor', []); |
| 207 | + manager = new DeepnoteAgentSkillsManager( |
| 208 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 209 | + (manager as any).processServiceFactory |
| 210 | + ); |
| 211 | + |
| 212 | + const updateSkills: (interpreter: PythonEnvironment) => Promise<void> = getPrivateMethod( |
| 213 | + manager, |
| 214 | + 'updateSkillsInBackground' |
| 215 | + ); |
| 216 | + |
| 217 | + await updateSkills(testInterpreter); |
| 218 | + |
| 219 | + assert.strictEqual(execStub.callCount, 0); |
| 220 | + }); |
| 221 | + |
| 222 | + test('should swallow errors in ensureSkillsUpdated', () => { |
| 223 | + execStub.rejects(new Error('pip failure')); |
| 224 | + |
| 225 | + // ensureSkillsUpdated is fire-and-forget -- it must not throw |
| 226 | + manager.ensureSkillsUpdated('env-error', testInterpreter); |
| 227 | + |
| 228 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 229 | + const processed = (manager as any).processedEnvironments as Set<string>; |
| 230 | + |
| 231 | + assert.isTrue(processed.has('env-error')); |
| 232 | + }); |
| 233 | + }); |
| 234 | +}); |
0 commit comments