-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode-actions.test.ts
More file actions
805 lines (674 loc) · 27.5 KB
/
code-actions.test.ts
File metadata and controls
805 lines (674 loc) · 27.5 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/**
* Code Actions tests.
* Tests for quick fix code action providers.
*/
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as vscode from 'vscode';
import { registerCodeActions, extractSchemaFilename } from '../code-actions';
import { getExtensionContext, createDevProxyInstall, getFixturePath } from './helpers';
import { DiagnosticCodes } from '../constants';
import { getDiagnosticCode, sleep } from '../utils';
suite('Code Actions', () => {
let sandbox: sinon.SinonSandbox;
let mockContext: vscode.ExtensionContext;
setup(async () => {
sandbox = sinon.createSandbox();
mockContext = await getExtensionContext();
});
teardown(() => {
sandbox.restore();
});
suite('registerCodeActions', () => {
test('should not register actions when devProxyInstall is not set', () => {
const emptyContext = {
globalState: {
get: sandbox.stub().returns(undefined),
},
subscriptions: [],
} as unknown as vscode.ExtensionContext;
const registerSpy = sandbox.spy(vscode.languages, 'registerCodeActionsProvider');
registerCodeActions(emptyContext);
assert.ok(registerSpy.notCalled, 'Should not register any providers');
});
test('should register code action providers when devProxyInstall is set', () => {
const devProxyInstall = createDevProxyInstall({ version: '0.24.0' });
const contextWithInstall = {
globalState: {
get: sandbox.stub().returns(devProxyInstall),
},
subscriptions: [],
} as unknown as vscode.ExtensionContext;
const registerSpy = sandbox.spy(vscode.languages, 'registerCodeActionsProvider');
registerCodeActions(contextWithInstall);
// Should register 16 providers (2 per fix type: json + jsonc, 8 fix types)
assert.strictEqual(registerSpy.callCount, 16, 'Should register 16 code action providers');
});
test('should handle beta version correctly', () => {
const devProxyInstall = createDevProxyInstall({
version: '0.25.0-beta.1',
isBeta: true,
});
const contextWithInstall = {
globalState: {
get: sandbox.stub().returns(devProxyInstall),
},
subscriptions: [],
} as unknown as vscode.ExtensionContext;
// Should not throw
registerCodeActions(contextWithInstall);
});
});
suite('Invalid Schema Fix', () => {
test('should provide fix when invalidSchema diagnostic exists', async () => {
// Create a test document
const docContent = `{
"$schema": "https://old-url/schema.json"
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
// Create diagnostic
const range = new vscode.Range(1, 14, 1, 45);
const diagnostic = new vscode.Diagnostic(
range,
'Invalid schema',
vscode.DiagnosticSeverity.Warning
);
diagnostic.code = getDiagnosticCode(DiagnosticCodes.invalidSchema);
// Get code actions
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
doc.uri,
range,
vscode.CodeActionKind.QuickFix.value
);
// Should have at least one fix (may have others from VS Code)
const schemaFix = codeActions?.find(a => a.title === 'Update schema');
// Note: This may not find the fix if diagnostics aren't set up properly in test env
// The main point is the code path is exercised
});
});
suite('Invalid Config Section Schema Fix', () => {
test('should provide fix when invalidConfigSectionSchema diagnostic exists', async () => {
const context = await getExtensionContext();
await context.globalState.update(
'devProxyInstall',
createDevProxyInstall({ version: '0.24.0' })
);
const fileName = 'config-section-schema-mismatch.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const configSchemaDiagnostic = diagnostics.find(d => {
const code =
typeof d.code === 'object' && d.code !== null
? (d.code as { value: string }).value
: d.code;
return code === 'invalidConfigSectionSchema';
});
assert.ok(configSchemaDiagnostic, 'Should have invalidConfigSectionSchema diagnostic');
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
configSchemaDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const schemaFix = codeActions?.find(a => a.title === 'Update config section schema');
assert.ok(schemaFix, 'Should provide config section schema fix');
assert.ok(schemaFix!.edit, 'Fix should have an edit');
// Verify the fix would update to the correct version
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
test('should provide bulk fix when multiple config section schemas are outdated', async () => {
const context = await getExtensionContext();
await context.globalState.update(
'devProxyInstall',
createDevProxyInstall({ version: '0.24.0' })
);
const fileName = 'config-section-schema-multiple-mismatch.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const configSchemaDiagnostic = diagnostics.find(d => {
const code =
typeof d.code === 'object' && d.code !== null
? (d.code as { value: string }).value
: d.code;
return code === 'invalidConfigSectionSchema';
});
assert.ok(configSchemaDiagnostic, 'Should have invalidConfigSectionSchema diagnostic');
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
configSchemaDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const bulkFix = codeActions?.find(a => a.title === 'Update all config section schemas');
assert.ok(bulkFix, 'Should provide bulk fix for multiple schema mismatches');
assert.ok(bulkFix!.isPreferred, 'Bulk fix should be preferred');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
test('should apply config section schema fix correctly', async () => {
const context = await getExtensionContext();
await context.globalState.update(
'devProxyInstall',
createDevProxyInstall({ version: '0.24.0' })
);
const fileName = 'config-section-schema-mismatch.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const configSchemaDiagnostic = diagnostics.find(d => {
const code =
typeof d.code === 'object' && d.code !== null
? (d.code as { value: string }).value
: d.code;
return code === 'invalidConfigSectionSchema';
});
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
configSchemaDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const schemaFix = codeActions?.find(a => a.title === 'Update config section schema');
assert.ok(schemaFix, 'Should have schema fix');
assert.ok(schemaFix!.edit, 'Fix should have an edit');
// Apply the edit
const applied = await vscode.workspace.applyEdit(schemaFix!.edit!);
assert.ok(applied, 'Edit should be applied successfully');
// Verify the schema was updated by checking document text
const updatedText = document.getText();
assert.ok(
updatedText.includes('v0.24.0'),
'Schema should be updated to version 0.24.0'
);
// Revert the changes
await vscode.commands.executeCommand('workbench.action.files.revert');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
});
suite('Deprecated Plugin Path Fix', () => {
test('should return empty array when no deprecatedPluginPath diagnostic', async () => {
const docContent = `{
"plugins": [
{
"name": "MockResponsePlugin",
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
}
]
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
const range = new vscode.Range(4, 20, 4, 60);
// With no matching diagnostic, should get no deprecated path fixes
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
doc.uri,
range,
vscode.CodeActionKind.QuickFix.value
);
const pluginPathFix = codeActions?.find(a => a.title === 'Update plugin path');
assert.strictEqual(pluginPathFix, undefined, 'Should not provide fix without diagnostic');
});
});
suite('Optional Config Fix', () => {
test('should return empty array when no pluginConfigOptional diagnostic', async () => {
const docContent = `{
"plugins": [
{
"name": "CachingGuidancePlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll"
}
]
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
const range = new vscode.Range(2, 0, 2, 10);
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
doc.uri,
range,
vscode.CodeActionKind.QuickFix.value
);
const configFix = codeActions?.find(
a => a.title.includes('Add') && a.title.includes('configuration')
);
assert.strictEqual(configFix, undefined, 'Should not provide fix without diagnostic');
});
test('should add configSection and config when fix is applied', async () => {
const fileName = 'config-plugin-config-optional.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const optionalConfigDiagnostic = diagnostics.find(d =>
d.message.includes('can be configured with a configSection')
);
assert.ok(optionalConfigDiagnostic, 'Should have pluginConfigOptional diagnostic');
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
optionalConfigDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const configFix = codeActions?.find(
a => a.title.includes('Add') && a.title.includes('configuration')
);
assert.ok(configFix, 'Should provide optional config fix');
assert.ok(configFix!.edit, 'Fix should have an edit');
// Apply the edit
const applied = await vscode.workspace.applyEdit(configFix!.edit!);
assert.ok(applied, 'Edit should be applied successfully');
// Verify the configSection was added to the plugin
const updatedText = document.getText();
assert.ok(
updatedText.includes('"configSection": "cachingGuidance"'),
'configSection should be added to plugin'
);
// Verify the config section was added at root level
assert.ok(
updatedText.includes('"cachingGuidance"'),
'Config section should be added at root level'
);
// Revert the changes
await vscode.commands.executeCommand('workbench.action.files.revert');
});
});
suite('Language Model Fix', () => {
test('should return empty array when no missingLanguageModel diagnostic', async () => {
const docContent = `{
"plugins": [
{
"name": "MockResponsePlugin"
}
]
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
const range = new vscode.Range(2, 0, 2, 10);
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
doc.uri,
range,
vscode.CodeActionKind.QuickFix.value
);
const lmFix = codeActions?.find(a => a.title === 'Enable local language model');
assert.strictEqual(lmFix, undefined, 'Should not provide fix without diagnostic');
});
});
suite('Missing Config Fix', () => {
test('should return empty array when no pluginConfigMissing diagnostic', async () => {
const docContent = `{
"plugins": [
{
"name": "LatencyPlugin",
"enabled": true,
"pluginPath": "~appFolder/plugins/DevProxy.Plugins.dll",
"configSection": "latencyPlugin"
}
],
"latencyPlugin": {
"minMs": 200,
"maxMs": 10000
}
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
const range = new vscode.Range(6, 0, 6, 10);
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
doc.uri,
range,
vscode.CodeActionKind.QuickFix.value
);
const configFix = codeActions?.find(
a => a.title.includes('Add') && a.title.includes('config section')
);
assert.strictEqual(configFix, undefined, 'Should not provide fix without diagnostic');
});
test('should add config section when fix is applied', async () => {
const fileName = 'config-plugin-config-missing.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const missingConfigDiagnostic = diagnostics.find(d =>
d.message.includes('config section is missing')
);
assert.ok(missingConfigDiagnostic, 'Should have pluginConfigMissing diagnostic');
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
missingConfigDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const configFix = codeActions?.find(
a => a.title.includes('Add') && a.title.includes('config section')
);
assert.ok(configFix, 'Should provide config section fix');
assert.ok(configFix!.edit, 'Fix should have an edit');
// Apply the edit
const applied = await vscode.workspace.applyEdit(configFix!.edit!);
assert.ok(applied, 'Edit should be applied successfully');
// Verify the config section was added
const updatedText = document.getText();
assert.ok(
updatedText.includes('"genericRandomErrorPlugin"'),
'Config section should be added'
);
assert.ok(
updatedText.includes('"errorsFile"'),
'Config section should contain expected properties'
);
// Revert the changes
await vscode.commands.executeCommand('workbench.action.files.revert');
});
});
suite('Invalid Config Section Fix', () => {
test('should provide remove and link fixes when invalidConfigSection diagnostic exists', async () => {
const context = await getExtensionContext();
await context.globalState.update(
'devProxyInstall',
createDevProxyInstall({ version: '0.24.0' })
);
const fileName = 'config-invalid-config-section.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const invalidConfigDiagnostic = diagnostics.find(d =>
d.message.includes('does not correspond to any plugin')
);
assert.ok(invalidConfigDiagnostic, 'Should have invalidConfigSection diagnostic');
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
invalidConfigDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const removeFix = codeActions?.find(a => a.title.includes('Remove'));
assert.ok(removeFix, 'Should provide remove section fix');
assert.ok(removeFix!.edit, 'Remove fix should have an edit');
const linkFix = codeActions?.find(a => a.title.includes('Link'));
assert.ok(linkFix, 'Should provide link to plugin fix');
assert.ok(linkFix!.command, 'Link fix should have a command');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
test('should remove config section when remove fix is applied', async () => {
const context = await getExtensionContext();
await context.globalState.update(
'devProxyInstall',
createDevProxyInstall({ version: '0.24.0' })
);
const fileName = 'config-invalid-config-section.json';
const filePath = getFixturePath(fileName);
const document = await vscode.workspace.openTextDocument(filePath);
await vscode.window.showTextDocument(document);
await sleep(1000);
const diagnostics = vscode.languages.getDiagnostics(document.uri);
const invalidConfigDiagnostic = diagnostics.find(d =>
d.message.includes('does not correspond to any plugin')
);
assert.ok(invalidConfigDiagnostic, 'Should have invalidConfigSection diagnostic');
const codeActions = await vscode.commands.executeCommand<vscode.CodeAction[]>(
'vscode.executeCodeActionProvider',
document.uri,
invalidConfigDiagnostic!.range,
vscode.CodeActionKind.QuickFix.value
);
const removeFix = codeActions?.find(a => a.title.includes('Remove'));
assert.ok(removeFix, 'Should have remove fix');
// Apply the edit
const applied = await vscode.workspace.applyEdit(removeFix!.edit!);
assert.ok(applied, 'Edit should be applied successfully');
// Verify the config section was removed
const updatedText = document.getText();
assert.ok(
!updatedText.includes('"orphanedConfig"'),
'Config section should be removed'
);
// Revert the changes
await vscode.commands.executeCommand('workbench.action.files.revert');
await vscode.commands.executeCommand('workbench.action.closeActiveEditor');
});
});
});
suite('Invalid Schema Code Action Logic', () => {
test('should create correct replacement URL for stable version', async () => {
// Open a document with an outdated schema
const docContent = `{
"$schema": "https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.20.0/rc.schema.json",
"plugins": []
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
// The fix should replace with the correct schema URL
// This test verifies the document can be created and processed
assert.ok(doc, 'Document should be created');
assert.ok(doc.getText().includes('$schema'), 'Document should contain schema');
});
});
suite('Deprecated Plugin Path Code Action Logic', () => {
test('should use correct replacement path', async () => {
const docContent = `{
"plugins": [
{
"name": "MockResponsePlugin",
"pluginPath": "C:/old/path/DevProxy.Plugins.dll"
}
]
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
// The correct path should be ~appFolder/plugins/DevProxy.Plugins.dll
assert.ok(doc, 'Document should be created');
});
});
suite('Language Model Code Action Logic', () => {
test('should handle document with existing languageModel that has enabled: false', async () => {
const docContent = `{
"plugins": [
{
"name": "OpenAIMockResponsePlugin"
}
],
"languageModel": {
"enabled": false
}
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
assert.ok(doc, 'Document should be created');
assert.ok(doc.getText().includes('"enabled": false'), 'Should have enabled: false');
});
test('should handle document with languageModel missing enabled property', async () => {
const docContent = `{
"plugins": [
{
"name": "OpenAIMockResponsePlugin"
}
],
"languageModel": {
"model": "gpt-4"
}
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
assert.ok(doc, 'Document should be created');
assert.ok(!doc.getText().includes('"enabled"'), 'Should not have enabled property');
});
test('should handle document without languageModel section', async () => {
const docContent = `{
"plugins": [
{
"name": "OpenAIMockResponsePlugin"
}
]
}`;
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
assert.ok(doc, 'Document should be created');
assert.ok(!doc.getText().includes('languageModel'), 'Should not have languageModel');
});
test('should handle malformed JSON gracefully', async () => {
// The fallback text-based insertion should handle this
const docContent = `{
"plugins": [
}`;
// This would fail JSON parsing, testing the fallback path
try {
const doc = await vscode.workspace.openTextDocument({
content: docContent,
language: 'json',
});
assert.ok(doc, 'Document should still be created');
} catch {
// Expected - malformed JSON
}
});
});
suite('Code Action Provider Registration', () => {
let sandbox: sinon.SinonSandbox;
setup(() => {
sandbox = sinon.createSandbox();
});
teardown(() => {
sandbox.restore();
});
test('should register providers for both json and jsonc', () => {
const devProxyInstall = {
version: '0.24.0',
isBeta: false,
isInstalled: true,
isOutdated: false,
isRunning: false,
platform: 'darwin',
};
const contextWithInstall = {
globalState: {
get: sandbox.stub().returns(devProxyInstall),
},
subscriptions: [],
} as unknown as vscode.ExtensionContext;
const registerSpy = sandbox.spy(vscode.languages, 'registerCodeActionsProvider');
registerCodeActions(contextWithInstall);
// Check that both json and jsonc are registered
const jsonCalls = registerSpy.getCalls().filter(call => call.args[0] === 'json');
const jsoncCalls = registerSpy.getCalls().filter(call => call.args[0] === 'jsonc');
assert.strictEqual(jsonCalls.length, 8, 'Should register 8 providers for json');
assert.strictEqual(jsoncCalls.length, 8, 'Should register 8 providers for jsonc');
});
test('should add subscriptions to context', () => {
const devProxyInstall = {
version: '0.24.0',
isBeta: false,
isInstalled: true,
isOutdated: false,
isRunning: false,
platform: 'darwin',
};
const subscriptions: vscode.Disposable[] = [];
const contextWithInstall = {
globalState: {
get: sandbox.stub().returns(devProxyInstall),
},
subscriptions,
} as unknown as vscode.ExtensionContext;
registerCodeActions(contextWithInstall);
assert.strictEqual(subscriptions.length, 16, 'Should add 16 subscriptions');
});
test('should strip beta suffix from version for schema URL', () => {
const devProxyInstall = {
version: '0.25.0-beta.1',
isBeta: true,
isInstalled: true,
isOutdated: false,
isRunning: false,
platform: 'darwin',
};
const contextWithInstall = {
globalState: {
get: sandbox.stub().returns(devProxyInstall),
},
subscriptions: [],
} as unknown as vscode.ExtensionContext;
// Should not throw - verifies beta version handling
registerCodeActions(contextWithInstall);
});
});
suite('extractSchemaFilename', () => {
test('should extract rc.schema.json from config file schema URL', () => {
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.29.0/rc.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'rc.schema.json');
});
test('should extract mockresponseplugin.mocksfile.schema.json from mocks file schema URL', () => {
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.24.0/mockresponseplugin.mocksfile.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'mockresponseplugin.mocksfile.schema.json');
});
test('should extract crudapiplugin.apifile.schema.json from CRUD API file schema URL', () => {
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.1.0/crudapiplugin.apifile.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'crudapiplugin.apifile.schema.json');
});
test('should extract rewriteplugin.rewritesfile.schema.json from rewrites file schema URL', () => {
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v0.25.0/rewriteplugin.rewritesfile.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'rewriteplugin.rewritesfile.schema.json');
});
test('should extract genericrandomerrorplugin.errorsfile.schema.json', () => {
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v2.0.0/genericrandomerrorplugin.errorsfile.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'genericrandomerrorplugin.errorsfile.schema.json');
});
test('should extract ratelimitingplugin.customresponsefile.schema.json', () => {
const url = 'https://raw.githubusercontent.com/dotnet/dev-proxy/main/schemas/v1.0.0/ratelimitingplugin.customresponsefile.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'ratelimitingplugin.customresponsefile.schema.json');
});
test('should return default rc.schema.json for URL without .schema.json', () => {
const url = 'https://example.com/some/path/file.json';
assert.strictEqual(extractSchemaFilename(url), 'rc.schema.json');
});
test('should return default rc.schema.json for empty string', () => {
assert.strictEqual(extractSchemaFilename(''), 'rc.schema.json');
});
test('should return default rc.schema.json for malformed URL', () => {
const url = 'not-a-valid-url';
assert.strictEqual(extractSchemaFilename(url), 'rc.schema.json');
});
test('should handle schema URL from old microsoft org', () => {
const url = 'https://raw.githubusercontent.com/microsoft/dev-proxy/main/schemas/v0.20.0/mockresponseplugin.schema.json';
assert.strictEqual(extractSchemaFilename(url), 'mockresponseplugin.schema.json');
});
test('should be case-insensitive for .schema.json extension', () => {
const url = 'https://example.com/path/MyPlugin.SCHEMA.JSON';
assert.strictEqual(extractSchemaFilename(url), 'MyPlugin.SCHEMA.JSON');
});
});