mobile-debug-mcp 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/android/interact.js +13 -1
- package/dist/android/observe.js +13 -0
- package/dist/cli/ios/run-ios-smoke.js +2 -2
- package/dist/cli/ios/run-ios-ui-tree-tap.js +2 -2
- package/dist/interact/android.js +91 -0
- package/dist/interact/index.js +37 -0
- package/dist/interact/ios.js +120 -0
- package/dist/interact/shared/fingerprint.js +72 -0
- package/dist/interact/shared/scroll_to_element.js +98 -0
- package/dist/ios/interact.js +52 -1
- package/dist/ios/observe.js +12 -0
- package/dist/manage/android.js +162 -0
- package/dist/manage/index.js +364 -0
- package/dist/manage/ios.js +353 -0
- package/dist/observe/android.js +351 -0
- package/dist/observe/fingerprint.js +1 -0
- package/dist/observe/index.js +85 -0
- package/dist/observe/ios.js +320 -0
- package/dist/observe/test/device/logstream-real.js +34 -0
- package/dist/observe/test/device/run-screen-fingerprint.js +29 -0
- package/dist/observe/test/device/run-scroll-test-android.js +22 -0
- package/dist/observe/test/device/test-ui-tree.js +67 -0
- package/dist/observe/test/device/wait_for_element_real.js +69 -0
- package/dist/observe/test/unit/get_screen_fingerprint.test.js +54 -0
- package/dist/observe/test/unit/logparse.test.js +39 -0
- package/dist/observe/test/unit/logstream.test.js +41 -0
- package/dist/observe/test/unit/scroll_to_element.test.js +113 -0
- package/dist/observe/test/unit/wait_for_element_mock.js +92 -0
- package/dist/server.js +54 -9
- package/dist/shared/fingerprint.js +72 -0
- package/dist/shared/scroll_to_element.js +98 -0
- package/dist/tools/interact.js +19 -22
- package/dist/tools/manage.js +2 -2
- package/dist/tools/observe.js +45 -43
- package/dist/tools/scroll_to_element.js +98 -0
- package/dist/utils/android/utils.js +429 -0
- package/dist/utils/cli/idb/check-idb.js +84 -0
- package/dist/utils/cli/idb/idb-helper.js +91 -0
- package/dist/utils/cli/idb/install-idb.js +82 -0
- package/dist/utils/cli/ios/preflight-ios.js +155 -0
- package/dist/utils/cli/ios/run-ios-smoke.js +28 -0
- package/dist/utils/cli/ios/run-ios-ui-tree-tap.js +29 -0
- package/dist/utils/diagnostics.js +1 -1
- package/dist/utils/ios/utils.js +301 -0
- package/dist/utils/resolve-device.js +2 -2
- package/docs/CHANGELOG.md +11 -0
- package/docs/tools/TOOLS.md +3 -3
- package/docs/tools/interact.md +31 -0
- package/docs/tools/observe.md +24 -0
- package/package.json +1 -1
- package/src/{android/interact.ts → interact/android.ts} +15 -2
- package/src/interact/index.ts +47 -0
- package/src/{ios/interact.ts → interact/ios.ts} +58 -3
- package/src/interact/shared/fingerprint.ts +73 -0
- package/src/interact/shared/scroll_to_element.ts +110 -0
- package/src/{android/manage.ts → manage/android.ts} +2 -2
- package/src/{tools/manage.ts → manage/index.ts} +7 -4
- package/src/{ios/manage.ts → manage/ios.ts} +1 -1
- package/src/{android/observe.ts → observe/android.ts} +14 -26
- package/src/observe/index.ts +92 -0
- package/src/{ios/observe.ts → observe/ios.ts} +17 -35
- package/src/server.ts +57 -10
- package/src/{android → utils/android}/utils.ts +2 -2
- package/src/{cli → utils/cli}/ios/run-ios-smoke.ts +2 -2
- package/src/{cli → utils/cli}/ios/run-ios-ui-tree-tap.ts +3 -3
- package/src/utils/diagnostics.ts +1 -1
- package/src/{ios → utils/ios}/utils.ts +2 -2
- package/src/utils/resolve-device.ts +2 -2
- package/test/{device/interact → interact/device}/smoke-test.ts +3 -4
- package/test/{device/manage → manage/device}/run-install-android.ts +1 -1
- package/test/{device/manage → manage/device}/run-install-ios.ts +1 -1
- package/test/{device/manage → manage/device}/run-install-kmp.ts +1 -1
- package/test/{unit/manage → manage/unit}/build.test.ts +1 -1
- package/test/{unit/manage → manage/unit}/build_and_install.test.ts +1 -1
- package/test/{unit/manage → manage/unit}/detection.test.ts +1 -1
- package/test/{unit/manage → manage/unit}/diagnostics.test.ts +2 -2
- package/test/{unit/manage → manage/unit}/install.test.ts +1 -1
- package/test/{unit/manage → manage/unit}/mcp_disable_autodetect.test.ts +1 -1
- package/test/{device/observe → observe/device}/logstream-real.ts +1 -1
- package/test/observe/device/run-screen-fingerprint.ts +36 -0
- package/test/observe/device/run-scroll-test-android.ts +24 -0
- package/test/{device/observe → observe/device}/test-ui-tree.ts +2 -2
- package/test/{device/observe → observe/device}/wait_for_element_real.ts +2 -2
- package/test/observe/unit/get_screen_fingerprint.test.ts +69 -0
- package/test/{unit/observe → observe/unit}/logparse.test.ts +1 -1
- package/test/{unit/observe → observe/unit}/logstream.test.ts +1 -1
- package/test/observe/unit/scroll_to_element.test.ts +129 -0
- package/test/{unit/observe → observe/unit}/wait_for_element_mock.ts +3 -3
- package/test/unit/index.ts +12 -11
- package/src/tools/interact.ts +0 -45
- package/src/tools/observe.ts +0 -82
- package/test/device/README.md +0 -49
- package/test/device/index.ts +0 -27
- package/test/device/utils/test-dist.ts +0 -41
- package/test/unit/utils/detect-java.test.ts +0 -22
- /package/src/{cli → utils/cli}/idb/check-idb.ts +0 -0
- /package/src/{cli → utils/cli}/idb/idb-helper.ts +0 -0
- /package/src/{cli → utils/cli}/idb/install-idb.ts +0 -0
- /package/src/{cli → utils/cli}/ios/preflight-ios.ts +0 -0
- /package/test/{device/interact → interact/device}/run-real-test.ts +0 -0
- /package/test/{device/manage → manage/device}/install.integration.ts +0 -0
- /package/test/{device/manage → manage/device}/run-build-install-ios.ts +0 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { promises as fs } from "fs";
|
|
2
|
+
import { spawn, spawnSync } from "child_process";
|
|
3
|
+
import { execCommand, execCommandWithDiagnostics, getIOSDeviceMetadata, validateBundleId, getIdbCmd, findAppBundle } from "../utils/ios/utils.js";
|
|
4
|
+
import path from "path";
|
|
5
|
+
export class iOSManage {
|
|
6
|
+
async build(projectPath, optsOrVariant) {
|
|
7
|
+
// Support legacy variant string as second arg
|
|
8
|
+
let opts = {};
|
|
9
|
+
if (typeof optsOrVariant === 'string')
|
|
10
|
+
opts.variant = optsOrVariant;
|
|
11
|
+
else
|
|
12
|
+
opts = optsOrVariant || {};
|
|
13
|
+
try {
|
|
14
|
+
// Look for an Xcode workspace or project at the provided path. If not present, scan subdirectories (limited depth)
|
|
15
|
+
async function findProject(root, maxDepth = 4) {
|
|
16
|
+
try {
|
|
17
|
+
const ents = await fs.readdir(root, { withFileTypes: true }).catch(() => []);
|
|
18
|
+
for (const e of ents) {
|
|
19
|
+
// .xcworkspace and .xcodeproj are directories on disk (bundles), not regular files
|
|
20
|
+
if (e.name.endsWith('.xcworkspace'))
|
|
21
|
+
return { dir: root, workspace: e.name };
|
|
22
|
+
if (e.name.endsWith('.xcodeproj'))
|
|
23
|
+
return { dir: root, proj: e.name };
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
catch { }
|
|
27
|
+
if (maxDepth <= 0)
|
|
28
|
+
return null;
|
|
29
|
+
try {
|
|
30
|
+
const ents = await fs.readdir(root, { withFileTypes: true }).catch(() => []);
|
|
31
|
+
for (const e of ents) {
|
|
32
|
+
if (e.isDirectory()) {
|
|
33
|
+
const candidate = await findProject(path.join(root, e.name), maxDepth - 1);
|
|
34
|
+
if (candidate)
|
|
35
|
+
return candidate;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch { }
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
// Resolve projectPath to an absolute path to avoid cwd-relative resolution issues
|
|
43
|
+
const absProjectPath = path.resolve(projectPath);
|
|
44
|
+
// If caller supplied explicit workspace/project, prefer those and set projectRootDir accordingly
|
|
45
|
+
let projectRootDir = absProjectPath;
|
|
46
|
+
let workspace = opts.workspace;
|
|
47
|
+
let proj = opts.project;
|
|
48
|
+
if (workspace) {
|
|
49
|
+
// normalize workspace path and set root to its parent
|
|
50
|
+
workspace = path.isAbsolute(workspace) ? workspace : path.join(absProjectPath, workspace);
|
|
51
|
+
projectRootDir = path.dirname(workspace);
|
|
52
|
+
workspace = path.basename(workspace);
|
|
53
|
+
}
|
|
54
|
+
else if (proj) {
|
|
55
|
+
proj = path.isAbsolute(proj) ? proj : path.join(absProjectPath, proj);
|
|
56
|
+
projectRootDir = path.dirname(proj);
|
|
57
|
+
proj = path.basename(proj);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const projectInfo = await findProject(absProjectPath, 4);
|
|
61
|
+
if (!projectInfo)
|
|
62
|
+
return { error: 'No Xcode project or workspace found' };
|
|
63
|
+
projectRootDir = projectInfo.dir || absProjectPath;
|
|
64
|
+
workspace = projectInfo.workspace;
|
|
65
|
+
proj = projectInfo.proj;
|
|
66
|
+
}
|
|
67
|
+
// Determine destination: prefer explicit option, then env var, otherwise use booted simulator UDID
|
|
68
|
+
let destinationUDID = opts.destinationUDID || process.env.MCP_XCODE_DESTINATION_UDID || process.env.MCP_XCODE_DESTINATION || '';
|
|
69
|
+
if (!destinationUDID) {
|
|
70
|
+
try {
|
|
71
|
+
const meta = await getIOSDeviceMetadata('booted');
|
|
72
|
+
if (meta && meta.id)
|
|
73
|
+
destinationUDID = meta.id;
|
|
74
|
+
}
|
|
75
|
+
catch { }
|
|
76
|
+
}
|
|
77
|
+
// Determine xcode command early so it can be used when detecting schemes
|
|
78
|
+
const xcodeCmd = opts.xcodeCmd || process.env.XCODEBUILD_PATH || 'xcodebuild';
|
|
79
|
+
// Determine available schemes by querying xcodebuild -list rather than guessing
|
|
80
|
+
async function detectScheme(xcodeCmdInner, workspacePath, projectPathFull, cwd) {
|
|
81
|
+
try {
|
|
82
|
+
const args = workspacePath ? ['-list', '-workspace', workspacePath] : ['-list', '-project', projectPathFull];
|
|
83
|
+
// Run xcodebuild directly to list schemes
|
|
84
|
+
const res = spawnSync(xcodeCmdInner, args, { cwd: cwd || projectRootDir, encoding: 'utf8', timeout: 20000 });
|
|
85
|
+
const out = res.stdout || '';
|
|
86
|
+
const schemesMatch = out.match(/Schemes:\s*\n([\s\S]*?)(?:\n\n|$)/m);
|
|
87
|
+
if (schemesMatch) {
|
|
88
|
+
const block = schemesMatch[1];
|
|
89
|
+
const schemes = block.split(/\n/).map(s => s.trim()).filter(Boolean);
|
|
90
|
+
if (schemes.length)
|
|
91
|
+
return schemes[0];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch { }
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
// Prepare build flags and paths (support incremental builds)
|
|
98
|
+
let buildArgs;
|
|
99
|
+
let chosenScheme = opts.scheme || null;
|
|
100
|
+
// Derived data and result bundle (agent-configurable)
|
|
101
|
+
const derivedDataPath = opts.derivedDataPath || process.env.MCP_DERIVED_DATA || path.join(projectRootDir, 'build', 'DerivedData');
|
|
102
|
+
// Use unique result bundle path by default to avoid collisions
|
|
103
|
+
const resultBundlePath = process.env.MCP_XCODE_RESULTBUNDLE_PATH || path.join(projectRootDir, 'build', 'xcresults', `ResultBundle-${Date.now()}-${Math.random().toString(36).slice(2)}.xcresult`);
|
|
104
|
+
const xcodeJobs = parseInt(process.env.MCP_XCODE_JOBS || '', 10) || 4;
|
|
105
|
+
const forceClean = opts.forceClean || process.env.MCP_FORCE_CLEAN === '1';
|
|
106
|
+
// ensure result dirs exist
|
|
107
|
+
await fs.mkdir(path.dirname(resultBundlePath), { recursive: true }).catch(() => { });
|
|
108
|
+
await fs.mkdir(derivedDataPath, { recursive: true }).catch(() => { });
|
|
109
|
+
// remove any pre-existing result bundle path to avoid xcodebuild complaining
|
|
110
|
+
await fs.rm(resultBundlePath, { recursive: true, force: true }).catch(() => { });
|
|
111
|
+
if (workspace) {
|
|
112
|
+
const workspacePath = path.join(projectRootDir, workspace);
|
|
113
|
+
if (!chosenScheme)
|
|
114
|
+
chosenScheme = await detectScheme(xcodeCmd, workspacePath, undefined, projectRootDir);
|
|
115
|
+
const scheme = chosenScheme || workspace.replace(/\.xcworkspace$/, '');
|
|
116
|
+
buildArgs = ['-workspace', workspacePath, '-scheme', scheme, '-configuration', 'Debug', '-sdk', 'iphonesimulator', 'build'];
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const projectPathFull = path.join(projectRootDir, proj);
|
|
120
|
+
if (!chosenScheme)
|
|
121
|
+
chosenScheme = await detectScheme(xcodeCmd, undefined, projectPathFull, projectRootDir);
|
|
122
|
+
const scheme = chosenScheme || proj.replace(/\.xcodeproj$/, '');
|
|
123
|
+
buildArgs = ['-project', projectPathFull, '-scheme', scheme, '-configuration', 'Debug', '-sdk', 'iphonesimulator', 'build'];
|
|
124
|
+
}
|
|
125
|
+
// Insert clean if explicitly requested via env or opts
|
|
126
|
+
if (forceClean) {
|
|
127
|
+
const idx = buildArgs.indexOf('build');
|
|
128
|
+
if (idx >= 0)
|
|
129
|
+
buildArgs.splice(idx, 0, 'clean');
|
|
130
|
+
}
|
|
131
|
+
// If we have a destination UDID, add an explicit destination to avoid xcodebuild picking an ambiguous target
|
|
132
|
+
if (destinationUDID) {
|
|
133
|
+
buildArgs.push('-destination', `platform=iOS Simulator,id=${destinationUDID}`);
|
|
134
|
+
}
|
|
135
|
+
// Add derived data and result bundle for diagnostics and faster incremental builds
|
|
136
|
+
buildArgs.push('-derivedDataPath', derivedDataPath);
|
|
137
|
+
buildArgs.push('-resultBundlePath', resultBundlePath);
|
|
138
|
+
// parallelisation and jobs
|
|
139
|
+
buildArgs.push('-parallelizeTargets');
|
|
140
|
+
buildArgs.push('-jobs', String(xcodeJobs));
|
|
141
|
+
// Prepare results directory for backwards-compatible logs
|
|
142
|
+
const resultsDir = path.join(projectPath, 'build-results');
|
|
143
|
+
// Remove any stale results to avoid xcodebuild complaining about existing result bundles
|
|
144
|
+
await fs.rm(resultsDir, { recursive: true, force: true }).catch(() => { });
|
|
145
|
+
await fs.mkdir(resultsDir, { recursive: true }).catch(() => { });
|
|
146
|
+
const XCODEBUILD_TIMEOUT = parseInt(process.env.MCP_XCODEBUILD_TIMEOUT || '', 10) || 180000; // default 3 minutes
|
|
147
|
+
const MAX_RETRIES = parseInt(process.env.MCP_XCODEBUILD_RETRIES || '', 10) || 1;
|
|
148
|
+
const tries = MAX_RETRIES + 1;
|
|
149
|
+
let lastStdout = '';
|
|
150
|
+
let lastStderr = '';
|
|
151
|
+
let lastErr = null;
|
|
152
|
+
for (let attempt = 1; attempt <= tries; attempt++) {
|
|
153
|
+
// Run xcodebuild with a watchdog
|
|
154
|
+
const res = await new Promise((resolve) => {
|
|
155
|
+
const proc = spawn(xcodeCmd, buildArgs, { cwd: projectRootDir });
|
|
156
|
+
let stdout = '';
|
|
157
|
+
let stderr = '';
|
|
158
|
+
proc.stdout?.on('data', d => stdout += d.toString());
|
|
159
|
+
proc.stderr?.on('data', d => stderr += d.toString());
|
|
160
|
+
let killed = false;
|
|
161
|
+
const to = setTimeout(() => {
|
|
162
|
+
killed = true;
|
|
163
|
+
try {
|
|
164
|
+
proc.kill('SIGKILL');
|
|
165
|
+
}
|
|
166
|
+
catch { }
|
|
167
|
+
}, XCODEBUILD_TIMEOUT);
|
|
168
|
+
proc.on('close', (code) => {
|
|
169
|
+
clearTimeout(to);
|
|
170
|
+
resolve({ code, stdout, stderr, killedByWatchdog: killed });
|
|
171
|
+
});
|
|
172
|
+
proc.on('error', (err) => {
|
|
173
|
+
clearTimeout(to);
|
|
174
|
+
resolve({ code: null, stdout, stderr: String(err), killedByWatchdog: killed });
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
lastStdout = res.stdout;
|
|
178
|
+
lastStderr = res.stderr;
|
|
179
|
+
if (res.code === 0) {
|
|
180
|
+
// success — clear any previous error and stop retrying
|
|
181
|
+
lastErr = null;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
// record the failure for reporting
|
|
185
|
+
lastErr = new Error(res.stderr || `xcodebuild failed with code ${res.code}`);
|
|
186
|
+
lastErr.code = res.code;
|
|
187
|
+
lastErr.exitCode = res.code;
|
|
188
|
+
lastErr.killedByWatchdog = !!res.killedByWatchdog;
|
|
189
|
+
// write logs for diagnostics (helpful whether killed or not)
|
|
190
|
+
try {
|
|
191
|
+
await fs.writeFile(path.join(resultsDir, `xcodebuild-${attempt}.stdout.log`), res.stdout).catch(() => { });
|
|
192
|
+
await fs.writeFile(path.join(resultsDir, `xcodebuild-${attempt}.stderr.log`), res.stderr).catch(() => { });
|
|
193
|
+
}
|
|
194
|
+
catch { }
|
|
195
|
+
// If killed by watchdog and there are remaining attempts, continue to retry
|
|
196
|
+
if (res.killedByWatchdog && attempt < tries) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
// no more retries or not a watchdog kill — break to report lastErr
|
|
200
|
+
if (attempt >= tries)
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
if (lastErr) {
|
|
204
|
+
// Include diagnostics and result bundle path when available; provide structured info useful for agents
|
|
205
|
+
const invokedCommand = `${xcodeCmd} ${buildArgs.map(a => a.includes(' ') ? `"${a}"` : a).join(' ')}`;
|
|
206
|
+
const envSnapshot = { PATH: process.env.PATH };
|
|
207
|
+
return { error: `xcodebuild failed: ${lastErr.message}. See build-results for logs.`, output: `stdout:\n${lastStdout}\nstderr:\n${lastStderr}`, diagnostics: { exitCode: lastErr.code || null, invokedCommand, cwd: projectRootDir, envSnapshot } };
|
|
208
|
+
}
|
|
209
|
+
// Try to locate built .app. First search project tree, then DerivedData if necessary
|
|
210
|
+
const built = await findAppBundle(projectPath);
|
|
211
|
+
if (built)
|
|
212
|
+
return { artifactPath: built };
|
|
213
|
+
// Fallback: search DerivedData for matching product
|
|
214
|
+
const dd = path.join(process.env.HOME || '', 'Library', 'Developer', 'Xcode', 'DerivedData');
|
|
215
|
+
try {
|
|
216
|
+
const entries = await fs.readdir(dd).catch(() => []);
|
|
217
|
+
for (const e of entries) {
|
|
218
|
+
const candidate = path.join(dd, e);
|
|
219
|
+
const found = await findAppBundle(candidate).catch(() => undefined);
|
|
220
|
+
if (found)
|
|
221
|
+
return { artifactPath: found };
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
catch { }
|
|
225
|
+
return { error: 'Could not find .app after build' };
|
|
226
|
+
}
|
|
227
|
+
catch (e) {
|
|
228
|
+
return { error: e instanceof Error ? e.message : String(e) };
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async installApp(appPath, deviceId = "booted") {
|
|
232
|
+
const device = await getIOSDeviceMetadata(deviceId);
|
|
233
|
+
try {
|
|
234
|
+
let toInstall = appPath;
|
|
235
|
+
const stat = await fs.stat(appPath).catch(() => null);
|
|
236
|
+
if (stat && stat.isDirectory()) {
|
|
237
|
+
if (appPath.endsWith('.app')) {
|
|
238
|
+
toInstall = appPath;
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
const found = await findAppBundle(appPath);
|
|
242
|
+
if (found) {
|
|
243
|
+
toInstall = found;
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
// Reuse the existing build() implementation to avoid duplicating the xcodebuild logic
|
|
247
|
+
const buildRes = await this.build(appPath);
|
|
248
|
+
if (buildRes.error)
|
|
249
|
+
throw new Error(buildRes.error);
|
|
250
|
+
toInstall = buildRes.artifactPath;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
const res = await execCommand(['simctl', 'install', deviceId, toInstall], deviceId);
|
|
256
|
+
return { device, installed: true, output: res.output };
|
|
257
|
+
}
|
|
258
|
+
catch (e) {
|
|
259
|
+
// Gather diagnostics for simctl failure
|
|
260
|
+
const diag = execCommandWithDiagnostics(['simctl', 'install', deviceId, toInstall], deviceId);
|
|
261
|
+
try {
|
|
262
|
+
const child = spawn(getIdbCmd(), ['--version']);
|
|
263
|
+
const idbExists = await new Promise((resolve) => {
|
|
264
|
+
child.on('error', () => resolve(false));
|
|
265
|
+
child.on('close', (code) => resolve(code === 0));
|
|
266
|
+
});
|
|
267
|
+
if (idbExists) {
|
|
268
|
+
// attempt idb install via spawn but include diagnostics
|
|
269
|
+
await new Promise((resolve, reject) => {
|
|
270
|
+
const proc = spawn(getIdbCmd(), ['install', toInstall, '--udid', device.id]);
|
|
271
|
+
let stderr = '';
|
|
272
|
+
proc.stderr.on('data', d => stderr += d.toString());
|
|
273
|
+
proc.on('close', code => {
|
|
274
|
+
if (code === 0)
|
|
275
|
+
resolve();
|
|
276
|
+
else
|
|
277
|
+
reject(new Error(stderr || `idb install failed with code ${code}`));
|
|
278
|
+
});
|
|
279
|
+
proc.on('error', err => reject(err));
|
|
280
|
+
});
|
|
281
|
+
return { device, installed: true };
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
catch { }
|
|
285
|
+
return { device, installed: false, error: e instanceof Error ? e.message : String(e), diagnostics: diag };
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
catch (e) {
|
|
289
|
+
return { device, installed: false, error: e instanceof Error ? e.message : String(e) };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
async startApp(bundleId, deviceId = "booted") {
|
|
293
|
+
validateBundleId(bundleId);
|
|
294
|
+
try {
|
|
295
|
+
const result = await execCommand(['simctl', 'launch', deviceId, bundleId], deviceId);
|
|
296
|
+
const device = await getIOSDeviceMetadata(deviceId);
|
|
297
|
+
return { device, appStarted: !!result.output, launchTimeMs: 1000 };
|
|
298
|
+
}
|
|
299
|
+
catch (e) {
|
|
300
|
+
const diag = execCommandWithDiagnostics(['simctl', 'launch', deviceId, bundleId], deviceId);
|
|
301
|
+
const device = await getIOSDeviceMetadata(deviceId);
|
|
302
|
+
return { device, appStarted: false, launchTimeMs: 0, error: e instanceof Error ? e.message : String(e), diagnostics: diag };
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
async terminateApp(bundleId, deviceId = "booted") {
|
|
306
|
+
validateBundleId(bundleId);
|
|
307
|
+
try {
|
|
308
|
+
await execCommand(['simctl', 'terminate', deviceId, bundleId], deviceId);
|
|
309
|
+
const device = await getIOSDeviceMetadata(deviceId);
|
|
310
|
+
return { device, appTerminated: true };
|
|
311
|
+
}
|
|
312
|
+
catch (e) {
|
|
313
|
+
const diag = execCommandWithDiagnostics(['simctl', 'terminate', deviceId, bundleId], deviceId);
|
|
314
|
+
const device = await getIOSDeviceMetadata(deviceId);
|
|
315
|
+
return { device, appTerminated: false, error: e instanceof Error ? e.message : String(e), diagnostics: diag };
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
async restartApp(bundleId, deviceId = "booted") {
|
|
319
|
+
await this.terminateApp(bundleId, deviceId);
|
|
320
|
+
const startResult = await this.startApp(bundleId, deviceId);
|
|
321
|
+
return { device: startResult.device, appRestarted: startResult.appStarted, launchTimeMs: startResult.launchTimeMs };
|
|
322
|
+
}
|
|
323
|
+
async resetAppData(bundleId, deviceId = "booted") {
|
|
324
|
+
validateBundleId(bundleId);
|
|
325
|
+
await this.terminateApp(bundleId, deviceId);
|
|
326
|
+
const device = await getIOSDeviceMetadata(deviceId);
|
|
327
|
+
try {
|
|
328
|
+
const containerResult = await execCommand(['simctl', 'get_app_container', deviceId, bundleId, 'data'], deviceId);
|
|
329
|
+
const dataPath = containerResult.output.trim();
|
|
330
|
+
if (!dataPath)
|
|
331
|
+
throw new Error(`Could not find data container for ${bundleId}`);
|
|
332
|
+
try {
|
|
333
|
+
const libraryPath = `${dataPath}/Library`;
|
|
334
|
+
const documentsPath = `${dataPath}/Documents`;
|
|
335
|
+
const tmpPath = `${dataPath}/tmp`;
|
|
336
|
+
await fs.rm(libraryPath, { recursive: true, force: true }).catch(() => { });
|
|
337
|
+
await fs.rm(documentsPath, { recursive: true, force: true }).catch(() => { });
|
|
338
|
+
await fs.rm(tmpPath, { recursive: true, force: true }).catch(() => { });
|
|
339
|
+
await fs.mkdir(libraryPath, { recursive: true }).catch(() => { });
|
|
340
|
+
await fs.mkdir(documentsPath, { recursive: true }).catch(() => { });
|
|
341
|
+
await fs.mkdir(tmpPath, { recursive: true }).catch(() => { });
|
|
342
|
+
return { device, dataCleared: true };
|
|
343
|
+
}
|
|
344
|
+
catch (e) {
|
|
345
|
+
throw new Error(`Failed to clear data for ${bundleId}: ${e instanceof Error ? e.message : String(e)}`);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
catch (e) {
|
|
349
|
+
const diag = execCommandWithDiagnostics(['simctl', 'get_app_container', deviceId, bundleId, 'data'], deviceId);
|
|
350
|
+
return { device, dataCleared: false, error: e instanceof Error ? e.message : String(e), diagnostics: diag };
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|