-
-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathproject-config.test.ts
More file actions
478 lines (409 loc) · 16.3 KB
/
project-config.test.ts
File metadata and controls
478 lines (409 loc) · 16.3 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
import { describe, expect, it } from 'vitest';
import path from 'node:path';
import { homedir } from 'node:os';
import { parse as parseYaml } from 'yaml';
import { createMockFileSystemExecutor } from '../../test-utils/mock-executors.ts';
import {
loadProjectConfig,
persistActiveSessionDefaultsProfileToProjectConfig,
persistProjectConfigPatch,
persistSessionDefaultsToProjectConfig,
} from '../project-config.ts';
const cwd = '/repo';
const configPath = path.join(cwd, '.xcodebuildmcp', 'config.yaml');
const configDir = path.join(cwd, '.xcodebuildmcp');
type MockWrite = { path: string; content: string };
type MockFsFixture = {
fs: ReturnType<typeof createMockFileSystemExecutor>;
writes: MockWrite[];
mkdirs: string[];
};
function createFsFixture(options?: { exists?: boolean; readFile?: string }): MockFsFixture {
const writes: MockWrite[] = [];
const mkdirs: string[] = [];
const exists = options?.exists ?? false;
const readFileContent = options?.readFile;
const fs = createMockFileSystemExecutor({
existsSync: (targetPath) => (targetPath === configPath ? exists : false),
readFile: async (targetPath) => {
if (targetPath !== configPath) {
throw new Error(`Unexpected readFile path: ${targetPath}`);
}
if (readFileContent == null) {
throw new Error('readFile called but no readFile content was provided');
}
return readFileContent;
},
writeFile: async (targetPath, content) => {
writes.push({ path: targetPath, content });
},
mkdir: async (targetPath) => {
mkdirs.push(targetPath);
},
});
return { fs, writes, mkdirs };
}
describe('project-config', () => {
describe('loadProjectConfig', () => {
it('should return found=false when config does not exist', async () => {
const { fs } = createFsFixture({ exists: false });
const result = await loadProjectConfig({ fs, cwd });
expect(result).toEqual({ found: false });
});
it('should normalize mutual exclusivity and resolve relative paths', async () => {
const yaml = [
'schemaVersion: 1',
'enabledWorkflows: simulator,device',
'customWorkflows:',
' My-Workflow:',
' - build_run_sim',
' - SCREENSHOT',
'debug: true',
'axePath: "./bin/axe"',
'sessionDefaults:',
' projectPath: "./App.xcodeproj"',
' workspacePath: "./App.xcworkspace"',
' simulatorName: "iPhone 17"',
' simulatorId: "SIM-1"',
' derivedDataPath: "./.derivedData"',
'',
].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
if (!result.found) throw new Error('expected config to be found');
const defaults = result.config.sessionDefaults ?? {};
expect(result.config.enabledWorkflows).toEqual(['simulator', 'device']);
expect(result.config.customWorkflows).toEqual({
'my-workflow': ['build_run_sim', 'screenshot'],
});
expect(result.config.debug).toBe(true);
expect(result.config.axePath).toBe(path.join(cwd, 'bin', 'axe'));
expect(defaults.workspacePath).toBe(path.join(cwd, 'App.xcworkspace'));
expect(defaults.projectPath).toBeUndefined();
expect(defaults.simulatorId).toBe('SIM-1');
expect(defaults.simulatorName).toBe('iPhone 17');
expect(defaults.derivedDataPath).toBe(path.join(cwd, '.derivedData'));
expect(result.notices.length).toBeGreaterThan(0);
});
it('should normalize debuggerBackend and resolve template paths', async () => {
const yaml = [
'schemaVersion: 1',
'debuggerBackend: lldb',
'iosTemplatePath: "./templates/ios"',
'macosTemplatePath: "/opt/templates/macos"',
'',
].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
if (!result.found) throw new Error('expected config to be found');
expect(result.config.debuggerBackend).toBe('lldb-cli');
expect(result.config.iosTemplatePath).toBe(path.join(cwd, 'templates', 'ios'));
expect(result.config.macosTemplatePath).toBe('/opt/templates/macos');
});
it('normalizes custom workflow entries while loading config', async () => {
const yaml = [
'schemaVersion: 1',
'customWorkflows:',
' valid-workflow:',
' - build_run_sim',
' invalid-workflow: build_run_sim',
' "":',
' - screenshot',
'',
].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
if (!result.found) throw new Error('expected config to be found');
expect(result.config.customWorkflows).toEqual({
'invalid-workflow': ['build_run_sim'],
'valid-workflow': ['build_run_sim'],
});
});
it('should resolve file URLs in session defaults and top-level paths', async () => {
const yaml = [
'schemaVersion: 1',
'axePath: "file:///repo/bin/axe"',
'sessionDefaults:',
' workspacePath: "file:///repo/App.xcworkspace"',
' derivedDataPath: "file:///repo/.derivedData"',
'',
].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
if (!result.found) throw new Error('expected config to be found');
expect(result.config.axePath).toBe('/repo/bin/axe');
const defaults = result.config.sessionDefaults ?? {};
expect(defaults.workspacePath).toBe('/repo/App.xcworkspace');
expect(defaults.derivedDataPath).toBe('/repo/.derivedData');
});
it('normalizes namespaced session defaults profiles and active profile', async () => {
const yaml = [
'schemaVersion: 1',
'activeSessionDefaultsProfile: "ios"',
'sessionDefaultsProfiles:',
' ios:',
' projectPath: "./App.xcodeproj"',
' workspacePath: "./App.xcworkspace"',
' simulatorName: "iPhone 17"',
' watch:',
' workspacePath: "./Watch.xcworkspace"',
'',
].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
if (!result.found) throw new Error('expected config to be found');
expect(result.config.activeSessionDefaultsProfile).toBe('ios');
expect(result.config.sessionDefaultsProfiles?.ios?.workspacePath).toBe(
path.join(cwd, 'App.xcworkspace'),
);
expect(result.config.sessionDefaultsProfiles?.ios?.projectPath).toBeUndefined();
expect(result.config.sessionDefaultsProfiles?.watch?.workspacePath).toBe(
path.join(cwd, 'Watch.xcworkspace'),
);
});
it('should expand tilde in derivedDataPath and other path fields', async () => {
const yaml = [
'schemaVersion: 1',
'sessionDefaults:',
' workspacePath: "./App.xcworkspace"',
' derivedDataPath: "~/.derivedData/myproject"',
'',
].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
if (!result.found) throw new Error('expected config to be found');
const defaults = result.config.sessionDefaults ?? {};
expect(defaults.derivedDataPath).toBe(path.join(homedir(), '.derivedData', 'myproject'));
expect(defaults.workspacePath).toBe(path.join(cwd, 'App.xcworkspace'));
});
it('should return an error result when schemaVersion is unsupported', async () => {
const yaml = ['schemaVersion: 2', 'sessionDefaults:', ' scheme: "App"', ''].join('\n');
const { fs } = createFsFixture({ exists: true, readFile: yaml });
const result = await loadProjectConfig({ fs, cwd });
expect(result.found).toBe(false);
expect('error' in result).toBe(true);
if ('error' in result) {
expect(result.error).toBeInstanceOf(Error);
}
});
it('should return an error result when YAML does not parse to an object', async () => {
const { fs } = createFsFixture({ exists: true, readFile: '- item' });
const result = await loadProjectConfig({ fs, cwd });
expect(result.found).toBe(false);
expect('error' in result).toBe(true);
if ('error' in result) {
expect(result.error.message).toBe('Project config must be an object');
}
});
});
describe('persistSessionDefaultsToProjectConfig', () => {
it('should merge patches, delete exclusive keys, and preserve unknown sections', async () => {
const yaml = [
'schemaVersion: 1',
'debug: true',
'enabledWorkflows:',
' - simulator',
'sessionDefaults:',
' scheme: "Old"',
' simulatorName: "OldSim"',
'server:',
' enabledWorkflows:',
' - simulator',
'',
].join('\n');
const { fs, writes, mkdirs } = createFsFixture({ exists: true, readFile: yaml });
await persistSessionDefaultsToProjectConfig({
fs,
cwd,
patch: { scheme: 'New', simulatorId: 'SIM-1' },
deleteKeys: ['simulatorName'],
});
expect(mkdirs).toContain(configDir);
expect(writes.length).toBe(1);
expect(writes[0].path).toBe(configPath);
const parsed = parseYaml(writes[0].content) as {
schemaVersion: number;
debug?: boolean;
enabledWorkflows?: string[];
sessionDefaults?: Record<string, unknown>;
server?: { enabledWorkflows?: string[] };
};
expect(parsed.schemaVersion).toBe(1);
expect(parsed.debug).toBe(true);
expect(parsed.enabledWorkflows).toEqual(['simulator']);
expect(parsed.sessionDefaults?.scheme).toBe('New');
expect(parsed.sessionDefaults?.simulatorId).toBe('SIM-1');
expect(parsed.sessionDefaults?.simulatorName).toBeUndefined();
expect(parsed.server?.enabledWorkflows).toEqual(['simulator']);
});
it('should overwrite invalid existing config with a minimal valid config', async () => {
const { fs, writes } = createFsFixture({ exists: true, readFile: '- not-an-object' });
await persistSessionDefaultsToProjectConfig({
fs,
cwd,
patch: { scheme: 'App' },
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
schemaVersion: number;
sessionDefaults?: Record<string, unknown>;
};
expect(parsed.schemaVersion).toBe(1);
expect(parsed.sessionDefaults?.scheme).toBe('App');
});
it('persists session defaults to a named profile', async () => {
const yaml = [
'schemaVersion: 1',
'sessionDefaultsProfiles:',
' ios:',
' scheme: "Old"',
'',
].join('\n');
const { fs, writes } = createFsFixture({ exists: true, readFile: yaml });
await persistSessionDefaultsToProjectConfig({
fs,
cwd,
profile: 'ios',
patch: { scheme: 'NewIOS', simulatorId: 'SIM-1' },
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
sessionDefaultsProfiles?: Record<string, Record<string, unknown>>;
};
expect(parsed.sessionDefaultsProfiles?.ios?.scheme).toBe('NewIOS');
expect(parsed.sessionDefaultsProfiles?.ios?.simulatorId).toBe('SIM-1');
});
it('trims named profile before persisting session defaults', async () => {
const { fs, writes } = createFsFixture({ exists: false });
await persistSessionDefaultsToProjectConfig({
fs,
cwd,
profile: ' ios ',
patch: { scheme: 'NewIOS' },
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
sessionDefaultsProfiles?: Record<string, Record<string, unknown>>;
};
expect(parsed.sessionDefaultsProfiles?.ios?.scheme).toBe('NewIOS');
expect(parsed.sessionDefaultsProfiles?.[' ios ']).toBeUndefined();
});
it('throws when named profile is empty after trimming', async () => {
const { fs } = createFsFixture({ exists: false });
await expect(
persistSessionDefaultsToProjectConfig({
fs,
cwd,
profile: ' ',
patch: { scheme: 'NewIOS' },
}),
).rejects.toThrow('Profile name cannot be empty.');
});
});
describe('persistProjectConfigPatch', () => {
it('writes top-level setup fields and session defaults', async () => {
const { fs, writes } = createFsFixture({ exists: false });
await persistProjectConfigPatch({
fs,
cwd,
patch: {
enabledWorkflows: ['simulator', 'ui-automation'],
debug: true,
sentryDisabled: true,
sessionDefaults: {
workspacePath: './MyApp.xcworkspace',
scheme: 'MyApp',
simulatorId: 'SIM-1',
},
},
deleteSessionDefaultKeys: ['projectPath'],
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
enabledWorkflows?: string[];
debug?: boolean;
sentryDisabled?: boolean;
sessionDefaults?: Record<string, unknown>;
};
expect(parsed.enabledWorkflows).toEqual(['simulator', 'ui-automation']);
expect(parsed.debug).toBe(true);
expect(parsed.sentryDisabled).toBe(true);
expect(parsed.sessionDefaults?.workspacePath).toBe('./MyApp.xcworkspace');
expect(parsed.sessionDefaults?.projectPath).toBeUndefined();
});
it('preserves unknown sections while patching setup fields', async () => {
const yaml = [
'schemaVersion: 1',
'server:',
' enabledWorkflows:',
' - simulator',
'sessionDefaults:',
' projectPath: "./App.xcodeproj"',
'',
].join('\n');
const { fs, writes } = createFsFixture({ exists: true, readFile: yaml });
await persistProjectConfigPatch({
fs,
cwd,
patch: {
debug: false,
enabledWorkflows: ['simulator'],
sessionDefaults: {
workspacePath: './App.xcworkspace',
},
},
deleteSessionDefaultKeys: ['projectPath'],
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
server?: { enabledWorkflows?: string[] };
sessionDefaults?: Record<string, unknown>;
};
expect(parsed.server?.enabledWorkflows).toEqual(['simulator']);
expect(parsed.sessionDefaults?.workspacePath).toBe('./App.xcworkspace');
expect(parsed.sessionDefaults?.projectPath).toBeUndefined();
});
});
describe('persistActiveSessionDefaultsProfileToProjectConfig', () => {
it('persists active profile name', async () => {
const { fs, writes } = createFsFixture({ exists: true, readFile: 'schemaVersion: 1\n' });
await persistActiveSessionDefaultsProfileToProjectConfig({
fs,
cwd,
profile: 'ios',
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
activeSessionDefaultsProfile?: string;
};
expect(parsed.activeSessionDefaultsProfile).toBe('ios');
});
it('trims active profile name before persisting', async () => {
const { fs, writes } = createFsFixture({ exists: true, readFile: 'schemaVersion: 1\n' });
await persistActiveSessionDefaultsProfileToProjectConfig({
fs,
cwd,
profile: ' ios ',
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
activeSessionDefaultsProfile?: string;
};
expect(parsed.activeSessionDefaultsProfile).toBe('ios');
});
it('removes active profile when switching to global', async () => {
const yaml = ['schemaVersion: 1', 'activeSessionDefaultsProfile: "watch"', ''].join('\n');
const { fs, writes } = createFsFixture({ exists: true, readFile: yaml });
await persistActiveSessionDefaultsProfileToProjectConfig({
fs,
cwd,
profile: null,
});
expect(writes.length).toBe(1);
const parsed = parseYaml(writes[0].content) as {
activeSessionDefaultsProfile?: string;
};
expect(parsed.activeSessionDefaultsProfile).toBeUndefined();
});
});
});