-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-simulator-modes.sh
More file actions
executable file
·276 lines (233 loc) · 8.49 KB
/
test-simulator-modes.sh
File metadata and controls
executable file
·276 lines (233 loc) · 8.49 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
#!/usr/bin/env bash
# Test simulator pure mode vs normal mode behavior
# Tests the logic for reusing vs creating fresh simulators
set -euo pipefail
# Setup
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$SCRIPT_DIR/../test-framework.sh"
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
cd "$REPO_ROOT/examples/ios"
if [ ! -d ".devbox/virtenv/ios" ]; then
echo "ERROR: iOS virtenv not found. Run 'devbox shell' first."
exit 1
fi
IOS_SCRIPTS_DIR=".devbox/virtenv/ios/scripts"
export IOS_SCRIPTS_DIR
. "$IOS_SCRIPTS_DIR/lib/lib.sh"
. "$IOS_SCRIPTS_DIR/platform/core.sh"
. "$IOS_SCRIPTS_DIR/domain/device_manager.sh"
. "$IOS_SCRIPTS_DIR/domain/simulator.sh"
echo "========================================"
echo "Simulator Mode Behavior Tests"
echo "========================================"
echo ""
echo "This test demonstrates the difference between:"
echo " 1. Normal mode: Reuses existing simulator if device matches"
echo " 2. Pure mode: Always creates fresh test-specific simulator"
echo ""
# Check current simulator state
echo "Current State:"
booted_count=$(xcrun simctl list devices | grep -c "Booted" || echo "0")
total_count=$(xcrun simctl list devices | grep -E "iPhone|iPad" | grep -c -E "Booted|Shutdown" || echo "0")
echo " Total simulators: $total_count"
echo " Booted simulators: $booted_count"
if [ "$booted_count" -gt 0 ]; then
echo " Booted simulator details:"
xcrun simctl list devices | grep "Booted" | while read -r line; do
device_name=$(echo "$line" | sed -E 's/^[[:space:]]*([^(]+).*/\1/' | xargs)
udid=$(echo "$line" | grep -oE '[0-9A-F-]{36}')
echo " - $device_name ($udid)"
done
fi
echo ""
# Test 1: Normal mode behavior
log_test "Normal Mode (Reuse existing)"
echo ""
echo "Scenario: Running 'ios.sh simulator start max' (normal mode)"
echo ""
echo "Expected behavior:"
echo " - If simulator with matching device exists and is booted: Reuse it"
echo " - If matching simulator exists but is shutdown: Boot it"
echo " - If no matching simulator: Create and boot new one"
echo " - Simulator persists after script exits"
echo ""
# Check if simulators exist
if [ "$total_count" -gt 0 ]; then
assert_success "true" "Simulators exist - normal mode would check for match"
echo ""
echo "Test reuse detection:"
if [ "$booted_count" -gt 0 ]; then
first_line=$(xcrun simctl list devices | grep "Booted" | head -1)
device_name=$(echo "$first_line" | sed -E 's/^[[:space:]]*([^(]+).*/\1/' | xargs)
udid=$(echo "$first_line" | grep -oE '[0-9A-F-]{36}')
echo " Booted: $device_name"
echo " UDID: $udid"
if xcrun simctl list devices | grep "$udid" | grep -q "Booted"; then
assert_success "true" "Simulator detection correctly identifies booted: $udid"
else
assert_failure "false" "Detection failed"
fi
else
echo " No booted simulators - normal mode would boot existing or create new"
fi
else
echo "No simulators found - normal mode would create new one"
echo " (Run 'devbox run start:sim' to test reuse behavior)"
fi
echo ""
# Test 2: Pure mode behavior
log_test "Pure Mode (Fresh instance)"
echo ""
echo "Scenario: Running 'ios.sh simulator start --pure max' or DEVBOX_PURE_SHELL=1"
echo ""
echo "Expected behavior:"
echo " - Always creates fresh simulator with ' Test' suffix"
echo " - Ignores any existing simulators"
echo " - Creates clean state for deterministic testing"
echo " - Should be deleted after test completes (in e2e tests)"
echo ""
echo "Pure mode flag:"
echo " export IOS_SIMULATOR_PURE=1"
echo " or"
echo " export DEVBOX_PURE_SHELL=1"
echo " This triggers creation of test-specific simulator"
echo ""
# Check for existing test simulators
test_sims=$(xcrun simctl list devices | grep " Test" | grep -c -E "iPhone|iPad" || echo "0")
echo "Test simulators currently present: $test_sims"
if [ "$test_sims" -gt 0 ]; then
echo "Test simulator details:"
xcrun simctl list devices | grep " Test" | grep -E "iPhone|iPad" | while read -r line; do
device_name=$(echo "$line" | sed -E 's/^[[:space:]]*([^(]+).*/\1/' | xargs)
state=$(echo "$line" | sed -E 's/.*(Booted|Shutdown).*/\1/')
echo " - $device_name ($state)"
done
fi
echo ""
# Test 3: Device matching logic
log_test "Device Matching Logic"
echo ""
echo "How simulators are matched to device definitions:"
echo ""
echo "1. Query available simulators:"
echo " xcrun simctl list devices"
echo ""
echo "2. Match by device name (case-insensitive, normalized):"
echo " Device def: 'iPhone 17' -> Look for simulator: 'iPhone 17'"
echo ""
echo "3. Check simulator state:"
echo " if state == 'Booted'; then"
echo " # Reuse this simulator"
echo " elif state == 'Shutdown'; then"
echo " # Boot this simulator"
echo " fi"
echo ""
if [ "$total_count" -gt 0 ]; then
echo "Current device mapping:"
xcrun simctl list devices | grep -E "iPhone|iPad" | grep -E "Booted|Shutdown" | head -5 | while read -r line; do
device_name=$(echo "$line" | sed -E 's/^[[:space:]]*([^(]+).*/\1/' | xargs)
state=$(echo "$line" | sed -E 's/.*(Booted|Shutdown).*/\1/')
udid=$(echo "$line" | grep -oE '[0-9A-F-]{36}')
echo " $device_name -> $udid ($state)"
done
else
echo " (No simulators to demonstrate)"
fi
echo ""
# Test 4: UDID tracking
log_test "UDID Tracking"
echo ""
echo "UDID (Unique Device Identifier) is used because:"
echo " - Unique per simulator instance"
echo " - Required for all simctl commands: xcrun simctl boot <UDID>"
echo " - Stable for the lifetime of the simulator"
echo " - 36-character UUID format (e.g., 12345678-1234-1234-1234-123456789012)"
echo ""
echo "How UDIDs are used:"
echo " - Boot: xcrun simctl boot <UDID>"
echo " - Install app: xcrun simctl install <UDID> app.app"
echo " - Launch app: xcrun simctl launch <UDID> com.bundle.id"
echo " - Check status: xcrun simctl bootstatus <UDID>"
echo ""
if [ "$booted_count" -gt 0 ]; then
first_line=$(xcrun simctl list devices | grep "Booted" | head -1)
udid=$(echo "$first_line" | grep -oE '[0-9A-F-]{36}')
device_name=$(echo "$first_line" | sed -E 's/^[[:space:]]*([^(]+).*/\1/' | xargs)
echo " Example UDID: $udid"
echo " Device: $device_name"
if xcrun simctl bootstatus "$udid" >/dev/null 2>&1; then
echo " Simulator is responsive"
fi
else
echo " No booted simulators to demonstrate"
fi
echo ""
# Test 5: Cleanup behavior
log_test "Cleanup Behavior"
echo ""
echo "Normal mode cleanup:"
echo " - App remains installed and running"
echo " - Simulator kept running for dev convenience"
echo " - Can immediately test/debug the app"
echo ""
echo "Pure mode cleanup (DEVBOX_PURE_SHELL=1):"
echo " - Test simulator is shutdown"
echo " - Test simulator is deleted: xcrun simctl delete <UDID>"
echo " - Next run starts completely fresh"
echo " - No leftover test state"
echo ""
# Test 6: Runtime handling
log_test "Runtime Resolution"
echo ""
echo "iOS runtimes (OS versions) are resolved from device definitions:"
echo ""
echo "Device definition (devbox.d/ios/devices/max.json):"
echo " {\"name\": \"iPhone 17\", \"runtime\": \"26.2\"}"
echo ""
echo "Resolution process:"
echo " 1. Query available runtimes: xcrun simctl list runtimes -j"
echo " 2. Match by iOS version: 26.2 -> iOS-26-2 runtime identifier"
echo " 3. If not available and IOS_DOWNLOAD_RUNTIME=1:"
echo " xcodebuild -downloadPlatform iOS -buildVersion <version>"
echo ""
available_runtimes=$(xcrun simctl list runtimes -j | jq -r '.runtimes[] | select(.platform == "iOS") | .version' | head -3)
if [ -n "$available_runtimes" ]; then
echo "Available iOS runtimes:"
echo "$available_runtimes" | while read -r version; do
echo " - iOS $version"
done
else
echo " No iOS runtimes found"
fi
echo ""
# Summary
echo "========================================"
echo "SUMMARY"
echo "========================================"
echo ""
echo "Key Differences:"
echo ""
echo "Normal Mode:"
echo " + Fast (reuses existing simulator)"
echo " + Good for development/iteration"
echo " + Simulator persists between runs"
echo " + Can inspect/debug app after test"
echo " - May have state from previous runs"
echo ""
echo "Pure Mode:"
echo " + Deterministic (clean state every time)"
echo " + Good for CI/CD pipelines"
echo " + Isolated test runs"
echo " + Test simulators clearly identified (with ' Test' suffix)"
echo " - Slower (creates fresh simulator)"
echo ""
echo "Usage:"
echo " # Normal mode (developer workflow)"
echo " devbox run test:e2e"
echo ""
echo " # Pure mode (CI/CD workflow)"
echo " devbox run --pure test:e2e"
echo " # or"
echo " DEVBOX_PURE_SHELL=1 devbox run test:e2e"
echo ""
echo "All behavior tests passed!"