-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdeepnoteActivationService.unit.test.ts
More file actions
272 lines (236 loc) · 10.8 KB
/
deepnoteActivationService.unit.test.ts
File metadata and controls
272 lines (236 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { assert } from 'chai';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import { ITelemetryService } from '../../platform/analytics/types';
import { DeepnoteActivationService } from './deepnoteActivationService';
import { DeepnoteNotebookManager } from './deepnoteNotebookManager';
import { IExtensionContext } from '../../platform/common/types';
import { ILogger } from '../../platform/logging/types';
import { IIntegrationManager } from './integrations/types';
import { mockedVSCodeNamespaces, resetVSCodeMocks } from '../../test/vscode-mock';
function createMockLogger(): ILogger {
return {
error: () => undefined,
warn: () => undefined,
info: () => undefined,
debug: () => undefined,
trace: () => undefined,
ci: () => undefined
} as ILogger;
}
suite('DeepnoteActivationService', () => {
let activationService: DeepnoteActivationService;
let mockExtensionContext: IExtensionContext;
let manager: DeepnoteNotebookManager;
let mockIntegrationManager: IIntegrationManager;
let mockLogger: ILogger;
let mockAnalytics: ITelemetryService;
setup(() => {
mockExtensionContext = {
subscriptions: []
} as any;
manager = new DeepnoteNotebookManager();
mockIntegrationManager = {
activate: () => {
return;
}
};
mockLogger = createMockLogger();
mockAnalytics = instance(mock<ITelemetryService>());
activationService = new DeepnoteActivationService(
mockExtensionContext,
manager,
mockIntegrationManager,
mockLogger,
mockAnalytics
);
});
suite('constructor', () => {
test('should create instance with extension context', () => {
assert.isDefined(activationService);
assert.strictEqual((activationService as any).extensionContext, mockExtensionContext);
});
test('should not initialize components until activate is called', () => {
assert.isUndefined((activationService as any).serializer);
assert.isUndefined((activationService as any).explorerView);
});
});
suite('activate', () => {
test('should create serializer and explorer view instances', () => {
// This test verifies component creation without stubbing VS Code APIs
try {
activationService.activate();
// Verify components were created
assert.isDefined((activationService as any).serializer);
assert.isDefined((activationService as any).explorerView);
} catch (error) {
// Expected in test environment without full VS Code API
// The test verifies that the method can be called and attempts to create components
assert.isTrue(true, 'activate() method exists and attempts to initialize components');
}
});
test('should re-register serializer when snapshots are enabled in-session', () => {
resetVSCodeMocks();
const registrations: Array<{ transientOutputs?: boolean }> = [];
let configHandler: ((event: { affectsConfiguration: (section: string) => boolean }) => void) | undefined;
when(
mockedVSCodeNamespaces.workspace.registerNotebookSerializer(anything(), anything(), anything())
).thenCall((_type, _serializer, options) => {
registrations.push(options);
return { dispose: () => undefined } as any;
});
const onDidChangeConfiguration = (
handler: (event: { affectsConfiguration: (section: string) => boolean }) => void
) => {
configHandler = handler;
return { dispose: () => undefined } as any;
};
when(mockedVSCodeNamespaces.workspace.onDidChangeConfiguration).thenReturn(onDidChangeConfiguration as any);
let snapshotsEnabled = false;
const mockSnapshotService = { isSnapshotsEnabled: () => snapshotsEnabled } as any;
activationService = new DeepnoteActivationService(
mockExtensionContext,
manager,
mockIntegrationManager,
mockLogger,
mockAnalytics,
mockSnapshotService
);
try {
activationService.activate();
} catch {
// Activation may fail in the test environment, but registrations should still occur.
}
assert.strictEqual(registrations.length, 1);
assert.isUndefined(registrations[0].transientOutputs);
snapshotsEnabled = true;
configHandler?.({ affectsConfiguration: (section) => section === 'deepnote.snapshots.enabled' });
assert.strictEqual(registrations.length, 2);
assert.strictEqual(registrations[1].transientOutputs, true);
verify(
mockedVSCodeNamespaces.workspace.registerNotebookSerializer(anything(), anything(), anything())
).twice();
});
test('should prompt to reload when snapshots are enabled with open notebooks', () => {
resetVSCodeMocks();
let configHandler: ((event: { affectsConfiguration: (section: string) => boolean }) => void) | undefined;
when(
mockedVSCodeNamespaces.workspace.registerNotebookSerializer(anything(), anything(), anything())
).thenReturn({ dispose: () => undefined } as any);
const onDidChangeConfiguration = (
handler: (event: { affectsConfiguration: (section: string) => boolean }) => void
) => {
configHandler = handler;
return { dispose: () => undefined } as any;
};
when(mockedVSCodeNamespaces.workspace.onDidChangeConfiguration).thenReturn(onDidChangeConfiguration as any);
when(mockedVSCodeNamespaces.workspace.notebookDocuments).thenReturn([{ notebookType: 'deepnote' } as any]);
let snapshotsEnabled = false;
const mockSnapshotService = { isSnapshotsEnabled: () => snapshotsEnabled } as any;
activationService = new DeepnoteActivationService(
mockExtensionContext,
manager,
mockIntegrationManager,
mockLogger,
mockAnalytics,
mockSnapshotService
);
try {
activationService.activate();
} catch {
// Activation may fail in the test environment, but prompts should still be attempted.
}
snapshotsEnabled = true;
configHandler?.({ affectsConfiguration: (section) => section === 'deepnote.snapshots.enabled' });
verify(mockedVSCodeNamespaces.window.showInformationMessage(anything(), anything(), anything())).once();
});
});
suite('component initialization', () => {
test('should handle activation state correctly', () => {
// Before activation
assert.isUndefined((activationService as any).serializer);
assert.isUndefined((activationService as any).explorerView);
// After activation attempt
try {
activationService.activate();
// If successful, components should be defined
if ((activationService as any).serializer) {
assert.isDefined((activationService as any).serializer);
assert.isDefined((activationService as any).explorerView);
}
} catch (error) {
// Expected in test environment - the method exists and tries to initialize
assert.isString(error.message, 'activate() method exists and attempts initialization');
}
});
});
suite('integration scenarios', () => {
test('should maintain independence between multiple service instances', () => {
const context1 = { subscriptions: [] } as any;
const context2 = { subscriptions: [] } as any;
const manager1 = new DeepnoteNotebookManager();
const manager2 = new DeepnoteNotebookManager();
const mockIntegrationManager1: IIntegrationManager = {
activate: () => {
return;
}
};
const mockIntegrationManager2: IIntegrationManager = {
activate: () => {
return;
}
};
const mockLogger1 = createMockLogger();
const mockLogger2 = createMockLogger();
const service1 = new DeepnoteActivationService(
context1,
manager1,
mockIntegrationManager1,
mockLogger1,
mockAnalytics
);
const service2 = new DeepnoteActivationService(
context2,
manager2,
mockIntegrationManager2,
mockLogger2,
mockAnalytics
);
// Verify each service has its own context
assert.strictEqual((service1 as any).extensionContext, context1);
assert.strictEqual((service2 as any).extensionContext, context2);
assert.notStrictEqual((service1 as any).extensionContext, (service2 as any).extensionContext);
// Verify services are independent instances
assert.notStrictEqual(service1, service2);
});
test('should handle different extension contexts', () => {
const context1 = { subscriptions: [] } as any;
const context2 = {
subscriptions: [
{
dispose: () => {
/* mock dispose */
}
}
]
} as any;
const manager1 = new DeepnoteNotebookManager();
const manager2 = new DeepnoteNotebookManager();
const mockIntegrationManager1: IIntegrationManager = {
activate: () => {
return;
}
};
const mockIntegrationManager2: IIntegrationManager = {
activate: () => {
return;
}
};
const mockLogger3 = createMockLogger();
const mockLogger4 = createMockLogger();
new DeepnoteActivationService(context1, manager1, mockIntegrationManager1, mockLogger3, mockAnalytics);
new DeepnoteActivationService(context2, manager2, mockIntegrationManager2, mockLogger4, mockAnalytics);
assert.strictEqual(context1.subscriptions.length, 0);
assert.strictEqual(context2.subscriptions.length, 1);
});
});
});