Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/test/prompts/devContainerConfigGenerator.stest.ts
13388 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import * as assert from 'assert';
6
import * as fs from 'fs';
7
import * as path from 'path';
8
import { DevContainerConfigGenerator } from '../../src/extension/prompt/node/devContainerConfigGenerator';
9
import { DevContainerConfigIndex, DevContainerConfigTemplate } from '../../src/platform/devcontainer/common/devContainerConfigurationService';
10
import { CancellationToken } from '../../src/util/vs/base/common/cancellation';
11
import { IInstantiationService } from '../../src/util/vs/platform/instantiation/common/instantiation';
12
import { ssuite, stest } from '../base/stest';
13
14
15
let index: Promise<DevContainerConfigIndex> | undefined;
16
async function loadIndex() {
17
return index || (index = (async () => {
18
const indexPath = path.join(__dirname, '../test/prompts/fixtures/devcontainer/devContainerIndex.json'); // Cached copy of https://containers.dev/static/devcontainer-index.json
19
const index = JSON.parse(await fs.promises.readFile(indexPath, 'utf8'));
20
const templates = index.collections
21
.filter((c: any) => c.sourceInformation.repository === 'https://github.com/devcontainers/templates')
22
.map((c: any) => c.templates)
23
.flat()
24
.map(({ id, name, description }: any) => ({ id, name, description } as DevContainerConfigTemplate));
25
const features = index.collections
26
.filter((c: any) => c.sourceInformation.repository === 'https://github.com/devcontainers/features')
27
.map((c: any) => c.features)
28
.flat()
29
.map(({ id, name, description }: any) => ({ id, name, description } as DevContainerConfigTemplate));
30
return {
31
templates,
32
features,
33
};
34
})());
35
}
36
37
ssuite({ title: 'Dev Container Configuration', location: 'external' }, () => {
38
const dataPath = path.join(__dirname, '../test/prompts/fixtures/devcontainer/devContainerConfigTestData.json');
39
const data = JSON.parse(fs.readFileSync(dataPath, 'utf8')).slice(0, 11);
40
for (let i = 0; i < data.length; i++) {
41
const d = data[i];
42
stest({ description: `Suggests a devcontainer.json template (sample ${i})` }, async (testingServiceCollection) => {
43
const accessor = testingServiceCollection.createTestingAccessor();
44
const instantiationService = accessor.get(IInstantiationService);
45
const generator = instantiationService.createInstance(DevContainerConfigGenerator);
46
const result = await generator.generate(await loadIndex(), d.files, CancellationToken.None);
47
assert.strictEqual(result.type, 'success');
48
assert.strictEqual(result.template, d.template);
49
});
50
51
stest({ description: `Suggests devcontainer.json features (sample ${i})` }, async (testingServiceCollection) => {
52
const accessor = testingServiceCollection.createTestingAccessor();
53
const instantiationService = accessor.get(IInstantiationService);
54
const generator = instantiationService.createInstance(DevContainerConfigGenerator);
55
const result = await generator.generate(await loadIndex(), d.files, CancellationToken.None);
56
assert.strictEqual(result.type, 'success');
57
assert.ok(result.features.find(f => d.features.includes(f)));
58
});
59
}
60
61
// // npm run simulate -- --grep=devcontainer.json --n=1
62
// stest({ description: `Suggests a devcontainer.json template` }, async (testingServiceCollection) => {
63
// const dataPath = path.join(__dirname, '../test/prompts/fixtures/devcontainer/devContainerConfigTestData.json');
64
// const data = JSON.parse(await fs.promises.readFile(dataPath, 'utf8')).slice(0, 11);
65
// const results = [];
66
// for (let i = 0; i < data.length; i++) {
67
// const generator = new DevContainerConfigGenerator(accessor);
68
// const result = await generator.generate(await loadIndex(), data[i].files, CancellationToken.None);
69
// assert.strictEqual(result.type, 'success');
70
// results.push({
71
// ...data[i],
72
// suggestedTemplate: result.template,
73
// suggestedFeatures: result.features,
74
// });
75
// }
76
// await fs.promises.writeFile(path.join(__dirname, '../test/prompts/devContainerConfigTestResults.json'), JSON.stringify(results, null, 4));
77
// });
78
});
79
80