-
-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathbuild_device.test.ts
More file actions
277 lines (243 loc) · 8.4 KB
/
build_device.test.ts
File metadata and controls
277 lines (243 loc) · 8.4 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
import { describe, it, expect, beforeEach } from 'vitest';
import { DERIVED_DATA_DIR } from '../../../../utils/log-paths.ts';
import * as z from 'zod';
import { createMockExecutor } from '../../../../test-utils/mock-executors.ts';
import { expectPendingBuildResponse, runToolLogic } from '../../../../test-utils/test-helpers.ts';
import { schema, handler, buildDeviceLogic } from '../build_device.ts';
import { sessionStore } from '../../../../utils/session-store.ts';
function createSpyExecutor(): {
commandCalls: Array<{ args: string[]; logPrefix?: string }>;
executor: ReturnType<typeof createMockExecutor>;
} {
const commandCalls: Array<{ args: string[]; logPrefix?: string }> = [];
const executor = createMockExecutor({
success: true,
output: 'Build succeeded',
onExecute: (command, logPrefix) => {
commandCalls.push({ args: command, logPrefix });
},
});
return { commandCalls, executor };
}
describe('build_device plugin', () => {
beforeEach(() => {
sessionStore.clear();
});
describe('Export Field Validation (Literal)', () => {
it('should have handler function', () => {
expect(typeof handler).toBe('function');
});
it('should expose only optional build-tuning fields in public schema', () => {
const schemaObj = z.strictObject(schema);
expect(schemaObj.safeParse({}).success).toBe(true);
expect(schemaObj.safeParse({ extraArgs: [] }).success).toBe(true);
expect(schemaObj.safeParse({ derivedDataPath: '/path/to/derived-data' }).success).toBe(false);
expect(schemaObj.safeParse({ preferXcodebuild: true }).success).toBe(false);
expect(schemaObj.safeParse({ projectPath: '/path/to/MyProject.xcodeproj' }).success).toBe(
false,
);
const schemaKeys = Object.keys(schema).sort();
expect(schemaKeys).toEqual(['extraArgs']);
});
});
describe('XOR Validation', () => {
it('should error when neither projectPath nor workspacePath provided', async () => {
const result = await handler({
scheme: 'MyScheme',
});
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('Missing required session defaults');
expect(result.content[0].text).toContain('Provide a project or workspace');
});
it('should error when both projectPath and workspacePath provided', async () => {
const result = await handler({
projectPath: '/path/to/MyProject.xcodeproj',
workspacePath: '/path/to/MyProject.xcworkspace',
scheme: 'MyScheme',
});
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('Parameter validation failed');
expect(result.content[0].text).toContain('Mutually exclusive parameters provided');
});
});
describe('Parameter Validation (via Handler)', () => {
it('should return Zod validation error for missing scheme', async () => {
const result = await handler({
projectPath: '/path/to/MyProject.xcodeproj',
});
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('Missing required session defaults');
expect(result.content[0].text).toContain('scheme is required');
});
it('should return Zod validation error for invalid parameter types', async () => {
const result = await handler({
projectPath: 123, // Should be string
scheme: 'MyScheme',
});
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('Parameter validation failed');
expect(result.content[0].text).toContain('projectPath');
});
});
describe('Handler Behavior (Complete Literal Returns)', () => {
it('should pass validation and execute successfully with valid project parameters', async () => {
const mockExecutor = createMockExecutor({
success: true,
output: 'Build succeeded',
});
const { result } = await runToolLogic(() =>
buildDeviceLogic(
{
projectPath: '/path/to/MyProject.xcodeproj',
scheme: 'MyScheme',
},
mockExecutor,
),
);
expect(result.isError()).toBeFalsy();
expectPendingBuildResponse(result, 'get_device_app_path');
});
it('should pass validation and execute successfully with valid workspace parameters', async () => {
const mockExecutor = createMockExecutor({
success: true,
output: 'Build succeeded',
});
const { result } = await runToolLogic(() =>
buildDeviceLogic(
{
workspacePath: '/path/to/MyProject.xcworkspace',
scheme: 'MyScheme',
},
mockExecutor,
),
);
expect(result.isError()).toBeFalsy();
expectPendingBuildResponse(result, 'get_device_app_path');
});
it('should verify workspace command generation with mock executor', async () => {
const spy = createSpyExecutor();
await runToolLogic(() =>
buildDeviceLogic(
{
workspacePath: '/path/to/MyProject.xcworkspace',
scheme: 'MyScheme',
},
spy.executor,
),
);
expect(spy.commandCalls).toHaveLength(1);
expect(spy.commandCalls[0].args).toEqual([
'xcodebuild',
'-workspace',
'/path/to/MyProject.xcworkspace',
'-scheme',
'MyScheme',
'-configuration',
'Debug',
'-skipMacroValidation',
'-destination',
'generic/platform=iOS',
'-derivedDataPath',
DERIVED_DATA_DIR,
'build',
]);
expect(spy.commandCalls[0].logPrefix).toBe('iOS Device Build');
});
it('should verify command generation with mock executor', async () => {
const spy = createSpyExecutor();
await runToolLogic(() =>
buildDeviceLogic(
{
projectPath: '/path/to/MyProject.xcodeproj',
scheme: 'MyScheme',
},
spy.executor,
),
);
expect(spy.commandCalls).toHaveLength(1);
expect(spy.commandCalls[0].args).toEqual([
'xcodebuild',
'-project',
'/path/to/MyProject.xcodeproj',
'-scheme',
'MyScheme',
'-configuration',
'Debug',
'-skipMacroValidation',
'-destination',
'generic/platform=iOS',
'-derivedDataPath',
DERIVED_DATA_DIR,
'build',
]);
expect(spy.commandCalls[0].logPrefix).toBe('iOS Device Build');
});
it('should return exact successful build response', async () => {
const mockExecutor = createMockExecutor({
success: true,
output: 'Build succeeded',
});
const { result } = await runToolLogic(() =>
buildDeviceLogic(
{
projectPath: '/path/to/MyProject.xcodeproj',
scheme: 'MyScheme',
},
mockExecutor,
),
);
expect(result.isError()).toBeFalsy();
expectPendingBuildResponse(result, 'get_device_app_path');
});
it('should return exact build failure response', async () => {
const mockExecutor = createMockExecutor({
success: false,
error: 'Compilation error',
});
const { result } = await runToolLogic(() =>
buildDeviceLogic(
{
projectPath: '/path/to/MyProject.xcodeproj',
scheme: 'MyScheme',
},
mockExecutor,
),
);
expect(result.isError()).toBe(true);
expectPendingBuildResponse(result);
});
it('should include optional parameters in command', async () => {
const spy = createSpyExecutor();
await runToolLogic(() =>
buildDeviceLogic(
{
projectPath: '/path/to/MyProject.xcodeproj',
scheme: 'MyScheme',
configuration: 'Release',
derivedDataPath: '/tmp/derived-data',
extraArgs: ['--verbose'],
},
spy.executor,
),
);
expect(spy.commandCalls).toHaveLength(1);
expect(spy.commandCalls[0].args).toEqual([
'xcodebuild',
'-project',
'/path/to/MyProject.xcodeproj',
'-scheme',
'MyScheme',
'-configuration',
'Release',
'-skipMacroValidation',
'-destination',
'generic/platform=iOS',
'-derivedDataPath',
'/tmp/derived-data',
'--verbose',
'build',
]);
expect(spy.commandCalls[0].logPrefix).toBe('iOS Device Build');
});
});
});