-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathboot_sim.test.ts
More file actions
185 lines (161 loc) · 5.72 KB
/
boot_sim.test.ts
File metadata and controls
185 lines (161 loc) · 5.72 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
/**
* Tests for boot_sim plugin (session-aware version)
* Follows CLAUDE.md guidance: dependency injection, no vi-mocks, literal validation.
*/
import { describe, it, expect, beforeEach } from 'vitest';
import * as z from 'zod';
import {
createMockCommandResponse,
createMockExecutor,
} from '../../../../test-utils/mock-executors.ts';
import { sessionStore } from '../../../../utils/session-store.ts';
import { schema, handler, boot_simLogic } from '../boot_sim.ts';
describe('boot_sim tool', () => {
beforeEach(() => {
sessionStore.clear();
});
describe('Export Field Validation (Literal)', () => {
it('should expose empty public schema', () => {
const schemaObj = z.object(schema);
expect(schemaObj.safeParse({}).success).toBe(true);
expect(Object.keys(schema)).toHaveLength(0);
const withSimId = schemaObj.safeParse({ simulatorId: 'abc' });
expect(withSimId.success).toBe(true);
expect('simulatorId' in (withSimId.data as Record<string, unknown>)).toBe(false);
});
});
describe('Handler Requirements', () => {
it('should require simulatorId when not provided', async () => {
const result = await handler({});
expect(result.isError).toBe(true);
const message = result.content[0].text;
expect(message).toContain('Missing required session defaults');
expect(message).toContain('Provide simulatorId or simulatorName');
expect(message).toContain('session-set-defaults');
});
});
describe('Logic Behavior (Literal Results)', () => {
it('should handle successful boot', async () => {
const mockExecutor = createMockExecutor({
success: true,
output: 'Simulator booted successfully',
});
const result = await boot_simLogic({ simulatorId: 'test-uuid-123' }, mockExecutor);
expect(result).toEqual({
content: [
{
type: 'text',
text: 'Simulator booted successfully.',
},
],
nextStepParams: {
open_sim: {},
install_app_sim: { simulatorId: 'test-uuid-123', appPath: 'PATH_TO_YOUR_APP' },
launch_app_sim: { simulatorId: 'test-uuid-123', bundleId: 'YOUR_APP_BUNDLE_ID' },
},
});
});
it('should handle command failure', async () => {
const mockExecutor = createMockExecutor({
success: false,
error: 'Simulator not found',
});
const result = await boot_simLogic({ simulatorId: 'invalid-uuid' }, mockExecutor);
expect(result).toEqual({
content: [
{
type: 'text',
text: 'Boot simulator operation failed: Simulator not found',
},
],
});
});
it('should handle already-booted simulator and still enable accessibility', async () => {
const executorCalls: string[][] = [];
const mockExecutor = async (
command: string[],
description?: string,
allowStderr?: boolean,
opts?: { cwd?: string },
detached?: boolean,
) => {
executorCalls.push(command);
void description;
void allowStderr;
void opts;
void detached;
if (command.includes('boot')) {
return createMockCommandResponse({
success: false,
error: 'Unable to boot device in current state: Booted',
});
}
return createMockCommandResponse({ success: true, output: '0' });
};
const result = await boot_simLogic({ simulatorId: 'test-uuid-123' }, mockExecutor);
expect(result.content[0].text).toBe('Simulator is already booted.');
// Should have called accessibility write after detecting already-booted
expect(executorCalls.some((cmd) => cmd.join(' ').includes('defaults write'))).toBe(true);
});
it('should handle exception with Error object', async () => {
const mockExecutor = async () => {
throw new Error('Connection failed');
};
const result = await boot_simLogic({ simulatorId: 'test-uuid-123' }, mockExecutor);
expect(result).toEqual({
content: [
{
type: 'text',
text: 'Boot simulator operation failed: Connection failed',
},
],
});
});
it('should handle exception with string error', async () => {
const mockExecutor = async () => {
throw 'String error';
};
const result = await boot_simLogic({ simulatorId: 'test-uuid-123' }, mockExecutor);
expect(result).toEqual({
content: [
{
type: 'text',
text: 'Boot simulator operation failed: String error',
},
],
});
});
it('should verify command generation with mock executor', async () => {
const calls: Array<{
command: string[];
description?: string;
allowStderr?: boolean;
opts?: { cwd?: string };
}> = [];
const mockExecutor = async (
command: string[],
description?: string,
allowStderr?: boolean,
opts?: { cwd?: string },
detached?: boolean,
) => {
calls.push({ command, description, allowStderr, opts });
void detached;
return createMockCommandResponse({
success: true,
output: 'Simulator booted successfully',
error: undefined,
});
};
await boot_simLogic({ simulatorId: 'test-uuid-123' }, mockExecutor);
// First call is the boot command; subsequent calls are from ensureSimulatorAccessibility
expect(calls.length).toBeGreaterThanOrEqual(1);
expect(calls[0]).toEqual({
command: ['xcrun', 'simctl', 'boot', 'test-uuid-123'],
description: 'Boot Simulator',
allowStderr: false,
opts: undefined,
});
});
});
});