-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathframework.test.ts
More file actions
291 lines (257 loc) · 10 KB
/
Copy pathframework.test.ts
File metadata and controls
291 lines (257 loc) · 10 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
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { detectFramework } from '@lib/detection/framework';
import { Integration } from '@lib/constants';
import { ANDROID_AGENT_CONFIG } from '../../../frameworks/android/android-wizard-agent';
import { KMP_AGENT_CONFIG } from '../../../frameworks/kmp/kmp-wizard-agent';
import { JAVA_AGENT_CONFIG } from '../../../frameworks/java/java-wizard-agent';
/** A throwaway project dir seeded with the given files. */
function makeProject(files: Record<string, string>): string {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wizard-detect-'));
for (const [rel, content] of Object.entries(files)) {
const abs = path.join(dir, rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, content);
}
return dir;
}
const tmpDirs: string[] = [];
function project(files: Record<string, string>): { installDir: string } {
const dir = makeProject(files);
tmpDirs.push(dir);
return { installDir: dir };
}
afterAll(() => {
for (const dir of tmpDirs) {
fs.rmSync(dir, { recursive: true, force: true });
}
});
describe('Integration enum order (drives first-match detection)', () => {
const order = Object.values(Integration);
const fallbacks = [
Integration.python,
Integration.ruby,
Integration.javascript_web,
Integration.javascriptNode,
];
test('every language fallback comes after every framework', () => {
const firstFallback = Math.min(...fallbacks.map((f) => order.indexOf(f)));
const lastFramework = Math.max(
...order
.filter((i) => !fallbacks.includes(i))
.map((i) => order.indexOf(i)),
);
expect(firstFallback).toBeGreaterThan(lastFramework);
});
test('generic Node is the last resort of the entire detection', () => {
// javascriptNode matches any package.json; anything after it is unreachable.
expect(order[order.length - 1]).toBe(Integration.javascriptNode);
});
test('java is ordered after kmp, swift, and android (they claim gradle projects first)', () => {
for (const specific of [
Integration.kmp,
Integration.swift,
Integration.android,
]) {
expect(order.indexOf(Integration.java)).toBeGreaterThan(
order.indexOf(specific),
);
}
});
test('kmp is ordered before android and swift (more specific detection wins)', () => {
// A KMP project also looks like an Android/Swift project, so KMP must be
// checked first for first-match detection to resolve it correctly.
expect(order.indexOf(Integration.kmp)).toBeLessThan(
order.indexOf(Integration.android),
);
expect(order.indexOf(Integration.kmp)).toBeLessThan(
order.indexOf(Integration.swift),
);
});
});
describe('detectFramework (end-to-end over real project dirs)', () => {
test('a framework beats the fallbacks even though package.json matches Node', async () => {
const opts = project({
'package.json': JSON.stringify({
dependencies: { next: '^16', react: '^19' },
}),
'package-lock.json': '{}',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.nextjs,
);
});
test('a Vite React app resolves to javascript_web, not javascript_node', async () => {
const opts = project({
'package.json': JSON.stringify({
dependencies: { react: '^19', 'react-dom': '^19' },
devDependencies: { vite: '^6' },
}),
'package-lock.json': '{}',
'vite.config.ts': 'export default {}',
'index.html': '<html></html>',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.javascript_web,
);
});
test('a plain browser app (lockfile + index.html, no bundler) resolves to javascript_web', async () => {
const opts = project({
'package.json': JSON.stringify({ dependencies: {} }),
'package-lock.json': '{}',
'index.html': '<html></html>',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.javascript_web,
);
});
test('a server-side Node project still falls through to javascript_node', async () => {
const opts = project({
'package.json': JSON.stringify({ dependencies: { express: '^4' } }),
'package-lock.json': '{}',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.javascriptNode,
);
});
test('a Kotlin Multiplatform project resolves to kmp', async () => {
const opts = project({
'settings.gradle.kts': 'include(":shared")',
'shared/build.gradle.kts': `plugins { kotlin("multiplatform") }\nkotlin {\n sourceSets {\n commonMain.dependencies {}\n }\n}\n`,
'shared/src/commonMain/kotlin/App.kt': 'class App',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.kmp,
);
});
test('a plain Android project still resolves to android (kmp ordered ahead does not hijack it)', async () => {
const opts = project({
'build.gradle': `plugins { id 'com.android.application' }`,
'app/src/main/AndroidManifest.xml': '<manifest/>',
'app/src/main/kotlin/MainActivity.kt': 'class MainActivity',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.android,
);
});
test('a Maven project resolves to java', async () => {
const opts = project({
'pom.xml':
'<project><groupId>com.example</groupId><artifactId>app</artifactId></project>',
'src/main/java/App.java': 'class App {}',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.java,
);
});
test('a plain Gradle JVM project resolves to java, not android', async () => {
const opts = project({
'build.gradle': `plugins { id 'java' id 'org.springframework.boot' }`,
'src/main/java/App.java': 'class App {}',
});
await expect(detectFramework(opts.installDir)).resolves.toBe(
Integration.java,
);
});
test('a Flutter project is not claimed by anything', async () => {
const opts = project({
'pubspec.yaml': 'name: my_flutter_app\nenvironment:\n sdk: ^3.0.0\n',
'android/build.gradle': `plugins { id 'com.android.application' }`,
'android/app/src/main/AndroidManifest.xml': '<manifest/>',
'android/app/src/main/kotlin/MainActivity.kt': 'class MainActivity',
});
await expect(detectFramework(opts.installDir)).resolves.toBeUndefined();
});
});
describe('android detect', () => {
const detect = ANDROID_AGENT_CONFIG.detection.detect;
const androidGradle = `plugins { id 'com.android.application' }`;
test('claims a native Android project', async () => {
const opts = project({ 'build.gradle': androidGradle });
await expect(detect(opts)).resolves.toBe(true);
});
test('does not claim a Flutter project despite its android/ subtree', async () => {
const opts = project({
'pubspec.yaml': 'name: my_flutter_app\nenvironment:\n sdk: ^3.0.0\n',
'android/build.gradle': androidGradle,
'android/app/src/main/AndroidManifest.xml': '<manifest/>',
'android/app/src/main/kotlin/MainActivity.kt': 'class MainActivity',
});
await expect(detect(opts)).resolves.toBe(false);
});
});
describe('java detect', () => {
const detect = JAVA_AGENT_CONFIG.detection.detect;
test('claims a Maven project', async () => {
const opts = project({ 'pom.xml': '<project></project>' });
await expect(detect(opts)).resolves.toBe(true);
});
test('claims a plain Gradle JVM project', async () => {
const opts = project({
'build.gradle.kts': `plugins { java }\ndependencies {}`,
});
await expect(detect(opts)).resolves.toBe(true);
});
test('does not claim an Android gradle project', async () => {
const opts = project({
'build.gradle': `plugins { id 'com.android.application' }`,
});
await expect(detect(opts)).resolves.toBe(false);
});
test('does not claim a gradle project with an AndroidManifest.xml subtree', async () => {
const opts = project({
'build.gradle': `plugins { id 'java' }`,
'app/src/main/AndroidManifest.xml': '<manifest/>',
});
await expect(detect(opts)).resolves.toBe(false);
});
test('does not claim a KMP project', async () => {
const opts = project({
'build.gradle.kts': `plugins { kotlin("multiplatform") }`,
});
await expect(detect(opts)).resolves.toBe(false);
});
test('does not claim a Flutter project', async () => {
const opts = project({
'pubspec.yaml': 'name: my_flutter_app\n',
'android/build.gradle': `plugins { id 'com.android.application' }`,
'settings.gradle': 'include ":app"',
});
await expect(detect(opts)).resolves.toBe(false);
});
test('does not claim a project with no build files', async () => {
const opts = project({ 'src/main/java/App.java': 'class App {}' });
await expect(detect(opts)).resolves.toBe(false);
});
});
describe('kmp detect', () => {
const detect = KMP_AGENT_CONFIG.detection.detect;
test('claims a project applying the Kotlin Multiplatform plugin', async () => {
const opts = project({
'shared/build.gradle.kts': `plugins { kotlin("multiplatform") }`,
});
await expect(detect(opts)).resolves.toBe(true);
});
test('claims a project with a commonMain source set', async () => {
const opts = project({
'shared/src/commonMain/kotlin/App.kt': 'class App',
});
await expect(detect(opts)).resolves.toBe(true);
});
test('does not claim a plain Android project', async () => {
const opts = project({
'build.gradle': `plugins { id 'com.android.application' }`,
'app/src/main/kotlin/MainActivity.kt': 'class MainActivity',
});
await expect(detect(opts)).resolves.toBe(false);
});
test('does not claim a Flutter project', async () => {
const opts = project({
'pubspec.yaml': 'name: my_flutter_app\nenvironment:\n sdk: ^3.0.0\n',
'android/build.gradle': `plugins { id 'com.android.application' }`,
'android/app/src/main/kotlin/MainActivity.kt': 'class MainActivity',
});
await expect(detect(opts)).resolves.toBe(false);
});
});