@flowspec-qa/runner-core 0.3.0 → 0.4.1

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/dist/discovery.js CHANGED
@@ -4,6 +4,8 @@ import os from 'os';
4
4
  import path from 'path';
5
5
  import chalk from 'chalk';
6
6
  import { FlowSpecQAApi } from './api-client.js';
7
+ import { normalizeResultType, } from './evidence-types.js';
8
+ import { addRecord, buildRecord, corpusSummary, createCorpus, detectForm, detectLoginForm, detectScreenChange, finalizeCorpus, findBaselineForFingerprint, inferScreenName, isSameScreen, legacyElementsToUIElements, looksLikeButton, rankNavigableButtons, shouldCapture, } from './evidence-corpus.js';
7
9
  const sleep = (ms) => new Promise(r => setTimeout(r, ms));
8
10
  // ─── ADB path resolution (mirrors devices.ts) ───────────────────────────────
9
11
  const ADB_COMMON_PATHS = [
@@ -27,6 +29,61 @@ function resolveAdb() {
27
29
  }
28
30
  throw new Error('adb not found. Install Android SDK platform-tools and add to PATH.');
29
31
  }
32
+ // ─── Maestro path resolution ─────────────────────────────────────────────────
33
+ function resolveMaestro() {
34
+ const home = process.env.HOME ?? '';
35
+ // Absolute paths: check file existence — avoids needing PATH set in child process
36
+ const absoluteCandidates = [
37
+ `${home}/.maestro/bin/maestro`,
38
+ '/usr/local/bin/maestro',
39
+ '/opt/homebrew/bin/maestro',
40
+ ];
41
+ for (const p of absoluteCandidates) {
42
+ if (fs.existsSync(p))
43
+ return p;
44
+ }
45
+ // Fallback: rely on PATH (works when user has ~/.maestro/bin in their shell PATH)
46
+ try {
47
+ execSync('which maestro', { stdio: 'ignore', timeout: 2000 });
48
+ return 'maestro';
49
+ }
50
+ catch { /* not in PATH */ }
51
+ throw new Error('Maestro CLI no encontrado. Instálalo con:\n' +
52
+ ' curl -Ls "https://get.maestro.mobile.dev" | bash\n' +
53
+ 'Luego abre una terminal nueva o ejecuta: export PATH="$PATH:$HOME/.maestro/bin"');
54
+ }
55
+ /** Returns an env object that always includes ~/.maestro/bin in PATH */
56
+ function maestroEnvWithPath() {
57
+ const home = process.env.HOME ?? '';
58
+ const maestroBin = `${home}/.maestro/bin`;
59
+ const currentPath = process.env.PATH ?? '';
60
+ return {
61
+ ...process.env,
62
+ PATH: currentPath.includes(maestroBin) ? currentPath : `${maestroBin}:${currentPath}`,
63
+ };
64
+ }
65
+ const cleanYamlText = (t) => {
66
+ return t.replace(/"/g, '\\"').replace(/\n/g, ' ');
67
+ };
68
+ const MAX_TEXT_SELECTOR_LEN = 40;
69
+ const buildMaestroSelector = (selectorId, isIos = false) => {
70
+ if (selectorId.startsWith('text:')) {
71
+ const raw = cleanYamlText(selectorId.replace('text:', ''));
72
+ // Maestro uses text as a regex — very long strings cause "element not found" failures.
73
+ // Truncate to the first MAX_TEXT_SELECTOR_LEN characters for reliable matching.
74
+ if (raw.length > MAX_TEXT_SELECTOR_LEN) {
75
+ const safe = raw.slice(0, MAX_TEXT_SELECTOR_LEN);
76
+ const escaped = safe.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
77
+ return `textRegex: ".*${escaped}.*"`;
78
+ }
79
+ return `text: "${raw}"`;
80
+ }
81
+ if (selectorId.startsWith('below:')) {
82
+ const textVal = cleanYamlText(selectorId.replace('below:', ''));
83
+ return `below:\n text: "${textVal}"\n index: 0`;
84
+ }
85
+ return `id: "${selectorId}"`;
86
+ };
30
87
  /**
31
88
  * Autonomous Discovery Engine 2.0
32
89
  * Uses Playwright and Maestro to extract real UI intelligence.
@@ -44,13 +101,137 @@ export const discoveryEngine = {
44
101
  * returns it as a base64-encoded PNG string.
45
102
  * Returns null silently if ADB capture fails (non-fatal).
46
103
  */
47
- async _captureScreenshotMobile(adbS, tag) {
104
+ /**
105
+ * Capture a screenshot from the Android device and return it as a
106
+ * base64-encoded PNG string. Returns null (PARTIAL_EVIDENCE) if all
107
+ * methods fail — the screen was still discovered via hierarchy.
108
+ *
109
+ * Fallback chain:
110
+ * 1. adb exec-out screencap -p (direct pipe, no temp file on device)
111
+ * 2. adb shell screencap + pull (classic two-step)
112
+ * 3. null → PARTIAL_EVIDENCE
113
+ */
114
+ /**
115
+ * Verifies that the target app is in the foreground (Android only).
116
+ * Uses `adb shell dumpsys window` to read the current focused window package.
117
+ *
118
+ * If the app is NOT in foreground (e.g. home screen showed up because the app
119
+ * failed to launch or crashed), it retries launching with increasing wait times:
120
+ * Attempt 1: monkey launch + 5 s
121
+ * Attempt 2: force-stop + monkey launch + 8 s
122
+ * Attempt 3: pm clear + monkey launch + 12 s
123
+ *
124
+ * If all retries fail, asks the user interactively via options.onRetryFailed.
125
+ * Returns true if the app is confirmed in foreground, false otherwise.
126
+ */
127
+ async _ensureAppInForeground(adbS, appId, options, maxRetries = 3) {
128
+ const getForegroudPackage = () => {
129
+ try {
130
+ // dumpsys window returns lines like:
131
+ // mCurrentFocus=Window{... cl.consorcio.mobileapp/com.example.MainActivity}
132
+ const out = execSync(`${adbS} shell dumpsys window | grep -E "mCurrentFocus|mFocusedApp"`, {
133
+ encoding: 'utf8',
134
+ stdio: ['ignore', 'pipe', 'pipe'],
135
+ timeout: 5000,
136
+ });
137
+ const match = out.match(/([a-zA-Z][a-zA-Z0-9_.]+)\/[a-zA-Z]/);
138
+ return match ? match[1] : '';
139
+ }
140
+ catch {
141
+ return '';
142
+ }
143
+ };
144
+ const isAppInForeground = () => {
145
+ const pkg = getForegroudPackage();
146
+ return pkg === appId || pkg.startsWith(appId);
147
+ };
148
+ // Fast-path: app already in foreground
149
+ if (isAppInForeground())
150
+ return true;
151
+ const waitTimes = [5000, 8000, 12000];
152
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
153
+ const waitMs = waitTimes[attempt - 1] ?? 12000;
154
+ console.log(chalk.yellow(` ⚠️ La app "${appId}" no está en primer plano (intento ${attempt}/${maxRetries}). Relanzando...`));
155
+ this._progress(options, 'launch', `Relanzando app (intento ${attempt})...`);
156
+ try {
157
+ if (attempt >= 2) {
158
+ // Force stop first on second+ attempts
159
+ try {
160
+ execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
161
+ }
162
+ catch { }
163
+ await sleep(800);
164
+ }
165
+ if (attempt === maxRetries) {
166
+ // Last resort: full state clear
167
+ try {
168
+ execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
169
+ }
170
+ catch { }
171
+ await sleep(1500);
172
+ }
173
+ execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
174
+ }
175
+ catch { }
176
+ await sleep(waitMs);
177
+ if (isAppInForeground()) {
178
+ console.log(chalk.green(` ✅ App "${appId}" confirmada en primer plano tras reintento ${attempt}.`));
179
+ return true;
180
+ }
181
+ }
182
+ // All retries exhausted — ask the user if there is an interactive callback
183
+ console.log(chalk.red(` ❌ La app "${appId}" no pudo ser confirmada en primer plano tras ${maxRetries} intentos.`));
184
+ if (options.onRetryFailed) {
185
+ const proceed = await options.onRetryFailed(`La app "${appId}" no respondió después de ${maxRetries} intentos de relanzamiento. ` +
186
+ `¿Deseas continuar de todas formas (puede generar evidencia incorrecta)?`);
187
+ return proceed ?? false;
188
+ }
189
+ return false;
190
+ },
191
+ async _captureScreenshotMobile(adbS, tag, deviceId, appId) {
192
+ // ── iOS path: use Maestro screenshot flow ──────────────────────────────
193
+ if (adbS === 'maestro') {
194
+ const tempPath = path.join(os.tmpdir(), `fsqa-shot-${Date.now()}-${tag}.png`);
195
+ const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
196
+ const maestro = resolveMaestro();
197
+ try {
198
+ const flow = `appId: ${appId}\n---\n- screenshot: ${tempPath}`;
199
+ const tmp = path.join(os.tmpdir(), `fsqa-shot-flow-${Date.now()}.yaml`);
200
+ fs.writeFileSync(tmp, flow);
201
+ execSync(`"${maestro}" ${deviceFlag}test ${tmp}`, { stdio: ['ignore', 'pipe', 'pipe'], timeout: 15000 });
202
+ try {
203
+ fs.unlinkSync(tmp);
204
+ }
205
+ catch { }
206
+ if (fs.existsSync(tempPath)) {
207
+ const b64 = fs.readFileSync(tempPath, { encoding: 'base64' });
208
+ try {
209
+ fs.unlinkSync(tempPath);
210
+ }
211
+ catch { }
212
+ return `data:image/png;base64,${b64}`;
213
+ }
214
+ }
215
+ catch (err) {
216
+ console.log(chalk.dim(` ⚠️ Captura iOS falló (${tag}): ${err instanceof Error ? err.message.split('\n')[0] : String(err)}`));
217
+ }
218
+ return null;
219
+ }
220
+ // ── Method 1: exec-out direct to buffer (no temp file on device or disk) ──
221
+ try {
222
+ // execSync with no encoding returns a Buffer of raw bytes
223
+ const buffer = execSync(`${adbS} exec-out screencap -p`, { timeout: 8000, maxBuffer: 20 * 1024 * 1024 });
224
+ if (Buffer.isBuffer(buffer) && buffer.length > 1000) {
225
+ return buffer.toString('base64');
226
+ }
227
+ }
228
+ catch { }
229
+ // ── Method 2: shell screencap + pull (classic two-step) ─────────────────
48
230
  try {
49
231
  const remotePath = `/sdcard/fpqa_discovery_${tag}.png`;
50
- const localPath = path.join(os.tmpdir(), `fpqa_discovery_${tag}_${Date.now()}.png`);
232
+ const localPath = path.join(os.tmpdir(), `fpqa_disc_${tag}_${Date.now()}.png`);
51
233
  execSync(`${adbS} shell screencap -p "${remotePath}"`, { stdio: 'ignore', timeout: 6000 });
52
234
  execSync(`${adbS} pull "${remotePath}" "${localPath}"`, { stdio: 'ignore', timeout: 8000 });
53
- // Clean up remote file
54
235
  try {
55
236
  execSync(`${adbS} shell rm "${remotePath}"`, { stdio: 'ignore', timeout: 3000 });
56
237
  }
@@ -60,11 +241,15 @@ export const discoveryEngine = {
60
241
  fs.unlinkSync(localPath);
61
242
  }
62
243
  catch { }
63
- return buffer.toString('base64');
64
- }
65
- catch {
66
- return null;
244
+ if (buffer.length > 1000)
245
+ return buffer.toString('base64');
67
246
  }
247
+ catch { }
248
+ // ── Method 3: PARTIAL_EVIDENCE ───────────────────────────────────────────
249
+ // Screen was discovered via Maestro hierarchy but screenshot unavailable.
250
+ // Marked in the report as needing recapture before STRICT visual cases
251
+ // can be approved.
252
+ return null;
68
253
  },
69
254
  // ── Hierarchy helpers ────────────────────────────────────────────────────
70
255
  /** Capture maestro hierarchy from current screen and return parsed JSON */
@@ -73,14 +258,48 @@ export const discoveryEngine = {
73
258
  // Without it, maestro hierarchy prompts interactively and hangs in non-TTY contexts.
74
259
  // Redirect stderr to /dev/null to suppress verbose INFO/DEBUG logs.
75
260
  const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
76
- const rawOutput = execSync(`maestro ${deviceFlag}hierarchy 2>/dev/null`, {
261
+ const maestro = resolveMaestro();
262
+ const rawOutput = execSync(`"${maestro}" ${deviceFlag}hierarchy 2>/dev/null`, {
77
263
  encoding: 'utf8',
78
264
  maxBuffer: 20 * 1024 * 1024, // 20 MB — hierarchy JSON can be large
79
- env: env ?? process.env,
265
+ env: { ...maestroEnvWithPath(), ...(env ?? {}) },
80
266
  });
81
- const lines = rawOutput.split('\n');
82
- const jsonStart = lines.findIndex(l => l.trim().startsWith('{'));
83
- const jsonStr = jsonStart >= 0 ? lines.slice(jsonStart).join('\n') : rawOutput;
267
+ // Maestro outputs Java warnings + JSON + sometimes trailing log lines.
268
+ // Extract exactly the balanced JSON object by tracking brace depth.
269
+ const start = rawOutput.indexOf('{');
270
+ if (start === -1)
271
+ throw new Error('No JSON found in maestro hierarchy output');
272
+ let depth = 0;
273
+ let inString = false;
274
+ let escape = false;
275
+ let end = -1;
276
+ for (let i = start; i < rawOutput.length; i++) {
277
+ const ch = rawOutput[i];
278
+ if (escape) {
279
+ escape = false;
280
+ continue;
281
+ }
282
+ if (ch === '\\' && inString) {
283
+ escape = true;
284
+ continue;
285
+ }
286
+ if (ch === '"') {
287
+ inString = !inString;
288
+ continue;
289
+ }
290
+ if (inString)
291
+ continue;
292
+ if (ch === '{')
293
+ depth++;
294
+ else if (ch === '}') {
295
+ depth--;
296
+ if (depth === 0) {
297
+ end = i;
298
+ break;
299
+ }
300
+ }
301
+ }
302
+ const jsonStr = end !== -1 ? rawOutput.slice(start, end + 1) : rawOutput.slice(start);
84
303
  return JSON.parse(jsonStr);
85
304
  },
86
305
  /** Execute a Maestro YAML flow (written to a temp file) */
@@ -89,23 +308,99 @@ export const discoveryEngine = {
89
308
  const tmpFile = path.join(os.tmpdir(), `discovery_flow_${Date.now()}.yaml`);
90
309
  fs.writeFileSync(tmpFile, yaml);
91
310
  try {
92
- const result = spawnSync('maestro', ['test', tmpFile], {
311
+ const args = ['test'];
312
+ if (env?.MAESTRO_DEVICE_ID) {
313
+ args.push('--device', env.MAESTRO_DEVICE_ID);
314
+ }
315
+ args.push(tmpFile);
316
+ const result = spawnSync(resolveMaestro(), args, {
93
317
  encoding: 'utf8',
94
318
  timeout: 120000,
95
319
  stdio: ['ignore', 'pipe', 'pipe'],
96
- maxBuffer: 20 * 1024 * 1024, // 20 MB — Maestro outputs verbose logs
97
- env: env ?? process.env,
320
+ maxBuffer: 20 * 1024 * 1024,
321
+ env: { ...maestroEnvWithPath(), ...(env ?? {}) },
98
322
  });
99
323
  fs.unlinkSync(tmpFile);
100
- return result.status === 0;
324
+ const ok = result.status === 0;
325
+ // Filter JVM runtime warnings (sun.misc.Unsafe, jansi, etc.) — not real Maestro errors
326
+ const filterJvmWarnings = (text) => text.split('\n')
327
+ .filter(l => !/^WARNING:/.test(l.trim()) && !/^> Please consider reporting/.test(l.trim()))
328
+ .join('\n')
329
+ .trim();
330
+ if (!ok) {
331
+ console.log(chalk.red(`\n⚠️ Maestro Flow falló (código de salida: ${result.status})`));
332
+ const cleanStdout = filterJvmWarnings(result.stdout ?? '');
333
+ const cleanStderr = filterJvmWarnings(result.stderr ?? '');
334
+ if (cleanStdout) {
335
+ console.log(chalk.yellow(`Maestro stdout:\n${cleanStdout}`));
336
+ }
337
+ if (cleanStderr) {
338
+ console.log(chalk.yellow(`Maestro stderr:\n${cleanStderr}`));
339
+ }
340
+ }
341
+ return {
342
+ ok,
343
+ stdout: result.stdout ?? '',
344
+ stderr: result.stderr ?? '',
345
+ };
101
346
  }
102
- catch {
347
+ catch (e) {
103
348
  try {
104
349
  fs.unlinkSync(tmpFile);
105
350
  }
106
351
  catch { }
107
- return false;
352
+ console.log(chalk.red(`\n⚠️ Error de ejecución en Maestro: ${e?.message ?? e}`));
353
+ return { ok: false, stdout: '', stderr: e?.message ?? 'unknown error' };
354
+ }
355
+ },
356
+ /** Smart navigation back to parent screen */
357
+ async _smartBack(appId, maestroEnv, deviceId, parentUI, launchUI, loginForm, parentName) {
358
+ let returnedToParent = false;
359
+ const BACK_BUTTON_RE = /^(cerrar|volver|atr[aá]s|cancelar|close|back|cancel|btn_back|btn_close|btn_cancel)$/i;
360
+ for (let attempt = 0; attempt < 4; attempt++) {
361
+ let tappedBack = false;
362
+ const currentHier = await this._captureHierarchy(maestroEnv, deviceId).catch(() => null);
363
+ if (currentHier) {
364
+ const currentEls = this.extractMaestroSelectors(currentHier);
365
+ const backBtn = currentEls.find(e => {
366
+ const id = (e.id || '').toLowerCase();
367
+ const text = (e.text || '').toLowerCase().trim();
368
+ return BACK_BUTTON_RE.test(text) || BACK_BUTTON_RE.test(id);
369
+ });
370
+ if (backBtn) {
371
+ console.log(chalk.gray(` 🔙 Tapping back/close element "${backBtn.text || backBtn.id}" to return to parent...`));
372
+ const isIos = !!deviceId && /iPhone|iPad/i.test(deviceId);
373
+ const backSelector = buildMaestroSelector(backBtn.id, isIos);
374
+ this._runMaestroFlow(appId, [
375
+ `- tapOn:\n ${backSelector}`,
376
+ `- waitForAnimationToEnd:\n timeout: 1000`,
377
+ ], maestroEnv);
378
+ tappedBack = true;
379
+ }
380
+ }
381
+ if (!tappedBack) {
382
+ this._runMaestroFlow(appId, [
383
+ `- back`,
384
+ `- waitForAnimationToEnd:\n timeout: 1000`,
385
+ ], maestroEnv);
386
+ }
387
+ await sleep(800);
388
+ const checkHier = await this._captureHierarchy(maestroEnv, deviceId).catch(() => null);
389
+ if (!checkHier)
390
+ continue;
391
+ const checkEls = this.extractMaestroSelectors(checkHier);
392
+ const checkUI = legacyElementsToUIElements(checkEls, '__pending__');
393
+ if (isSameScreen(checkUI, parentUI, 0.7)) {
394
+ returnedToParent = true;
395
+ break;
396
+ }
397
+ if (loginForm && isSameScreen(checkUI, launchUI, 0.7)) {
398
+ console.log(chalk.yellow(` ⚠️ Volvimos a la pantalla inicial mientras navegábamos hacia atrás. BFS detenido.`));
399
+ returnedToParent = false;
400
+ break;
401
+ }
108
402
  }
403
+ return returnedToParent;
109
404
  },
110
405
  // ── Text extraction from accessibility hierarchy ─────────────────────────
111
406
  /**
@@ -169,6 +464,13 @@ export const discoveryEngine = {
169
464
  const planPath = path.join(outputDir, 'discovery_plan.md');
170
465
  fs.writeFileSync(planPath, result.planContent);
171
466
  console.log(chalk.green(`\n✅ Plan de Discovery guardado: ${chalk.bold(planPath)}`));
467
+ // Persist the raw result so `flowspecqa report` can rebuild the HTML later
468
+ try {
469
+ fs.writeFileSync(path.join(outputDir, 'discovery_data.json'), JSON.stringify({ ...result, framework: options.framework, url: options.url }));
470
+ }
471
+ catch (dataErr) {
472
+ console.log(chalk.yellow(`⚠️ discovery_data.json no guardado: ${dataErr.message}`));
473
+ }
172
474
  // Generate HTML evidence report
173
475
  try {
174
476
  const { generateDiscoveryReport } = await import('./discovery-report.js');
@@ -177,52 +479,88 @@ export const discoveryEngine = {
177
479
  framework: options.framework,
178
480
  url: options.url,
179
481
  });
482
+ result.reportPath = reportPath;
180
483
  console.log(chalk.green(`📋 Reporte de evidencias: ${chalk.bold(reportPath)}`));
181
484
  }
182
485
  catch (reportErr) {
183
486
  console.log(chalk.yellow(`⚠️ Reporte no generado: ${reportErr.message}`));
184
487
  }
185
- // Sync with SaaS
488
+ // Sync with SaaS — only with an explicit project and entity: never guess them
489
+ result.synced = false;
490
+ if (!options.projectId || !options.entityId) {
491
+ result.syncError = 'faltan --project y/o --entity';
492
+ console.log(chalk.yellow('⚠️ Sync omitido: indica --project y --entity para guardar el plan en FlowSpecQA.'));
493
+ return result;
494
+ }
186
495
  try {
187
496
  this._progress(options, 'sync', 'Sincronizando con FlowSpecQA SaaS...');
188
497
  console.log(chalk.yellow('📡 Sincronizando con FlowSpecQA SaaS...'));
189
498
  await FlowSpecQAApi.syncDiscoveryPlan({
190
- url: options.url,
191
- plan_content: result.planContent,
192
- raw_scan_data: result.rawScanData,
193
- framework: options.framework,
194
499
  project_id: options.projectId,
195
500
  entity_id: options.entityId,
501
+ url: options.url,
502
+ plan_content: result.planContent,
503
+ framework: options.framework || 'playwright',
504
+ evidence_corpus: result.corpus,
196
505
  });
506
+ result.synced = true;
197
507
  console.log(chalk.green('✅ Sincronizado. El generador de IA ahora tiene contexto UI real.'));
508
+ // Ingest discovery plan into Qdrant — only if this run discovered MORE screens than before
509
+ if (options.projectId && options.entityId) {
510
+ const newScreenCount = result.evidence?.screens?.filter((s) => !s.isErrorState).length ?? 0;
511
+ const minScreensToIngest = 2; // at least Login + one authenticated screen
512
+ if (newScreenCount < minScreensToIngest) {
513
+ console.log(chalk.yellow(`⚠️ Indexado omitido: el plan tiene ${newScreenCount} pantalla(s) navegable(s) y se requieren ${minScreensToIngest}. ` +
514
+ `El plan anterior se conserva. Si la app tiene login, repite con --username y --password; ` +
515
+ `si no, ejecuta: flowspecqa ingest --entity ${options.entityId} --project ${options.projectId}`));
516
+ }
517
+ else {
518
+ try {
519
+ this._progress(options, 'sync', 'Indexando plan en memoria vectorial...');
520
+ const ingestResult = await FlowSpecQAApi.ingestDiscoveryPlan({
521
+ project_id: options.projectId,
522
+ entity_id: options.entityId,
523
+ source: 'discovery-plan',
524
+ });
525
+ console.log(chalk.green(`✅ Plan indexado en Qdrant (~${ingestResult.chunks_ingested} chunks).`));
526
+ }
527
+ catch (ingestErr) {
528
+ const ingestMsg = ingestErr.response?.data?.detail || ingestErr.message || String(ingestErr);
529
+ console.log(chalk.yellow(`⚠️ Indexado Qdrant omitido: ${ingestMsg}`));
530
+ }
531
+ }
532
+ }
198
533
  }
199
534
  catch (error) {
200
535
  const errorMsg = error.response?.data?.detail || error.message || String(error);
201
- console.log(chalk.red(`⚠️ Sync omitido: ${errorMsg}`));
202
- console.log(chalk.gray(` Ejecuta: flowspecqa generate --from ${planPath}\n`));
536
+ result.syncError = errorMsg;
537
+ console.log(chalk.red(`⚠️ Sync fallido: ${errorMsg}`));
538
+ console.log(chalk.gray(` El plan quedó solo en local (${planPath}). Revisa \`flowspecqa doctor\` y repite el discover.\n`));
203
539
  }
204
540
  return result;
205
541
  },
206
- // ── Mobile Discovery ─────────────────────────────────────────────────────
207
- async discoverMobile(options) {
208
- const appId = options.url;
209
- const username = options.username || '';
210
- const password = options.password || '';
542
+ // ── Phase 13: Manual Capture (single screen, no BFS) ──────────────────────
543
+ /**
544
+ * Capture the device's current screen as a single EvidenceRecord and upload
545
+ * it to the SaaS as a manual capture (`capture_mode='manual'`).
546
+ *
547
+ * Used by `flowspecqa capture` to fill specific evidence gaps that the
548
+ * autonomous BFS couldn't reach (e.g. comprobante after a PIN/2FA flow).
549
+ * The caller is responsible for navigating the device to the desired
550
+ * screen before invoking this.
551
+ *
552
+ * Spec: DISCOVERY_SPEC.md § 13.
553
+ */
554
+ async captureSingleManualScreen(opts) {
211
555
  const adb = resolveAdb();
212
- const adbS = options.deviceId ? `"${adb}" -s ${options.deviceId}` : `"${adb}"`;
213
- const maestroEnv = options.deviceId
214
- ? { ...process.env, MAESTRO_DEVICE_ID: options.deviceId }
215
- : process.env;
216
- const startedAt = Date.now();
217
- const allScreens = [];
218
- const navigationSteps = [];
219
- this._progress(options, 'start', `Iniciando Discovery mobile: ${appId}`);
220
- console.log(chalk.gray(`📱 Discovery multi-pantalla${options.deviceId ? ` — dispositivo ${options.deviceId}` : ''}...`));
221
- try {
222
- // ── Wake up screen + reset app state ──────────────────────────────
223
- this._progress(options, 'reset', 'Reseteando estado de la app...');
224
- console.log(chalk.gray(' 🔄 Reseteando estado de la app...'));
225
- // Wake the screen — Maestro hierarchy fails silently when screen is off/locked
556
+ const adbS = opts.deviceId ? `"${adb}" -s ${opts.deviceId}` : `"${adb}"`;
557
+ const env = opts.deviceId ? { ...process.env, MAESTRO_DEVICE_ID: opts.deviceId } : process.env;
558
+ const isIos = !!opts.deviceId && /iPhone|iPad/i.test(opts.deviceId);
559
+ if (!opts.projectId)
560
+ throw new Error('captureManual requiere projectId (usa --project).');
561
+ opts.onProgress?.('capture', 'Capturando estado actual del dispositivo...');
562
+ // 1. Wake the screen + take screenshot from current state (no relaunch)
563
+ if (!isIos) {
226
564
  try {
227
565
  execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
228
566
  }
@@ -231,227 +569,1017 @@ export const discoveryEngine = {
231
569
  execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
232
570
  }
233
571
  catch { }
234
- await sleep(800);
572
+ await sleep(400);
573
+ }
574
+ const screenshot = await this._captureScreenshotMobile(isIos ? 'maestro' : adbS, 'manual', opts.deviceId, opts.appId);
575
+ // 2. Extract hierarchy + elements
576
+ const hierarchy = await this._captureHierarchy(env, opts.deviceId);
577
+ const elements = this.extractMaestroSelectors(hierarchy);
578
+ const texts = this.extractAllTexts(hierarchy);
579
+ const ui = legacyElementsToUIElements(elements, '__pending__');
580
+ // 3. Compute fingerprint (same algo as discoverMobile)
581
+ const fingerprintTexts = ui.map(e => e.visibleText).filter((t) => !!t);
582
+ const { screenFingerprint } = await import('./evidence-corpus.js');
583
+ const fp = screenFingerprint(ui, fingerprintTexts);
584
+ opts.onProgress?.('upload', 'Subiendo captura al SaaS...');
585
+ // 4. Upload to backend (non-fatal if the upload fails — the local
586
+ // capture is still useful for the operator)
587
+ const result = await FlowSpecQAApi.uploadManualCapture({
588
+ entity_id: opts.entityId,
589
+ label: opts.label,
590
+ screen_name: opts.label,
591
+ elements: ui,
592
+ texts,
593
+ screenshot_b64: screenshot ?? undefined,
594
+ screen_fingerprint: fp,
595
+ platform: isIos ? 'ios' : 'android',
596
+ project_id: opts.projectId,
597
+ });
598
+ return {
599
+ record_id: result.record_id,
600
+ session_id: result.session_id,
601
+ evidence_quality: result.evidence_quality,
602
+ elements_count: ui.length,
603
+ has_screenshot: !!screenshot,
604
+ };
605
+ },
606
+ // ── Mobile Discovery ─────────────────────────────────────────────────────
607
+ async discoverMobile(options) {
608
+ const appId = options.url;
609
+ let username = options.username || '';
610
+ let password = options.password || '';
611
+ // ── Platform detection ────────────────────────────────────────────────
612
+ // iOS device IDs are UUIDs (contain multiple '-') or Apple hardware UDIDs
613
+ // (hex string with exactly one '-' like '00008101-001414A93A46001E').
614
+ // Android serials are purely numeric or alphanumeric without '-'.
615
+ const deviceId = options.deviceId;
616
+ const isIos = !!deviceId && (
617
+ // UUID format: 9B9A9E1D-3A91-563C-8A8A-8C196FC121DA (4 dashes)
618
+ (deviceId.match(/-/g) || []).length >= 2 ||
619
+ // Apple hardware UDID: 00008101-001414A93A46001E (1 dash, 24+ hex chars)
620
+ /^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{12,}$/.test(deviceId));
621
+ let adbS = '';
622
+ if (!isIos) {
235
623
  try {
236
- execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
624
+ const adb = resolveAdb();
625
+ adbS = deviceId ? `"${adb}" -s ${deviceId}` : `"${adb}"`;
237
626
  }
238
- catch { }
627
+ catch {
628
+ console.log(chalk.yellow('⚠️ adb no disponible — solo modo iOS/Maestro disponible.'));
629
+ }
630
+ }
631
+ const maestroEnv = deviceId
632
+ ? { ...process.env, MAESTRO_DEVICE_ID: deviceId }
633
+ : process.env;
634
+ const startedAt = Date.now();
635
+ const allScreens = [];
636
+ const navigationSteps = [];
637
+ // ── Evidence Corpus (evidence-first Discovery) ─────────────────────────
638
+ const corpus = createCorpus(appId);
639
+ this._progress(options, 'start', `Iniciando Discovery mobile: ${appId}`);
640
+ console.log(chalk.gray(`📱 Discovery multi-pantalla${options.deviceId ? ` — dispositivo ${options.deviceId}` : ''}...`));
641
+ // ── Phase 11 — Baseline fetch for incremental / recapture modes ────────
642
+ // We fetch the baseline ONCE up-front; the BFS consults it per-screen.
643
+ // Spec: DISCOVERY_SPEC.md § 11.1
644
+ const mode = options.mode ?? 'full';
645
+ let baselines = null;
646
+ let incrementalSkipCount = 0;
647
+ let incrementalCaptureCount = 0;
648
+ if ((mode === 'incremental' || mode === 'recapture') && options.entityId) {
239
649
  try {
240
- execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
650
+ const resp = await FlowSpecQAApi.getDiscoveryBaseline(options.entityId);
651
+ baselines = resp.baselines || [];
652
+ const verifiedCount = baselines.filter(b => b.status === 'verified').length;
653
+ const recaptureCount = baselines.filter(b => b.status === 'needs_recapture' || b.status === 'partial').length;
654
+ console.log(chalk.cyan(` 📚 Baseline cargado para ${options.entityId} — ${baselines.length} pantallas ` +
655
+ `(${verifiedCount} verified, ${recaptureCount} needs_recapture)`));
656
+ if (mode === 'incremental' && verifiedCount === 0) {
657
+ console.log(chalk.yellow(` ⚠️ Sin baselines verificados — modo incremental se comporta como full hasta que valides pantallas en la UI`));
658
+ }
241
659
  }
242
- catch { }
243
- await sleep(1000);
244
- // ── Screen 1: Login ────────────────────────────────────────────────
245
- this._progress(options, 'screen', 'Pantalla 1 — Login');
246
- console.log(chalk.gray(' 📸 Pantalla 1 — Login...'));
247
- execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
248
- // After pm clear + cold launch, app needs more time to render (permissions reset, first run)
249
- await sleep(4500);
250
- const loginScreenshot = await this._captureScreenshotMobile(adbS, 'login');
251
- const loginHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
252
- const loginElements = this.extractMaestroSelectors(loginHierarchy);
253
- const loginTexts = this.extractAllTexts(loginHierarchy);
254
- allScreens.push({
255
- name: 'Login',
256
- isErrorState: false,
257
- screenshot: loginScreenshot,
258
- elements: loginElements,
259
- allTexts: loginTexts,
260
- });
261
- navigationSteps.push({ from: 'Inicio', to: 'Login', action: 'Lanzar app' });
262
- console.log(chalk.green(` ✅ Login: ${loginElements.length} elementos, ${loginScreenshot ? 'screenshot ✓' : 'sin screenshot'}`));
263
- // ── Error state: Login vacío ───────────────────────────────────────
264
- this._progress(options, 'error_state', 'Login vacío — capturando mensajes de error reales...');
265
- console.log(chalk.gray(' 📸 Error state: Login vacío — Submit sin datos...'));
266
- const emptyLoginOk = this._runMaestroFlow(appId, [
267
- `- tapOn:\n id: "btn_login"`,
268
- `- waitForAnimationToEnd:\n timeout: 1500`,
269
- ], maestroEnv);
270
- if (emptyLoginOk) {
271
- await sleep(1500); // wait for error UI to settle before screencap
272
- const loginErrorScreenshot = await this._captureScreenshotMobile(adbS, 'login_error');
273
- const loginErrorHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
274
- const loginErrorAllElems = this.extractMaestroSelectors(loginErrorHierarchy);
275
- const loginErrorTexts = this.extractAllTexts(loginErrorHierarchy);
276
- const knownLoginIds = new Set(loginElements.map(e => e.id));
277
- const loginErrorNew = loginErrorAllElems.filter(e => !knownLoginIds.has(e.id));
278
- // New error texts = texts that weren't on the clean login screen
279
- const loginNewTexts = loginErrorTexts.filter(t => !loginTexts.includes(t));
280
- allScreens.push({
281
- name: 'Login (Error — campos vacíos)',
282
- isErrorState: true,
283
- screenshot: loginErrorScreenshot,
284
- elements: loginErrorNew,
285
- allTexts: loginNewTexts,
286
- });
287
- navigationSteps.push({ from: 'Login', to: 'Login (Error — campos vacíos)', action: 'Submit vacío' });
288
- const errList = loginNewTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
289
- console.log(chalk.green(` ✅ Login error state: ${loginErrorNew.length} elementos nuevos${errList ? ` | Textos: ${errList}` : ''}`));
660
+ catch (e) {
661
+ console.log(chalk.yellow(` ⚠️ No se pudo cargar baseline: ${e?.message || e}. Continuando en modo full.`));
662
+ baselines = null;
663
+ }
664
+ }
665
+ try {
666
+ // ── Phase 12 — Resume Mode short-circuit ──────────────────────────
667
+ // When mode='resume', the human has manually navigated past auth
668
+ // barriers (PIN/2FA/OTP/SSO) and the app is already in a usable
669
+ // state. We skip both the reset (force-stop / pm clear) AND the
670
+ // relaunch (monkey / launchApp clearState) because doing either
671
+ // would invalidate the human's manual setup.
672
+ // Spec: DISCOVERY_SPEC.md § 12.2
673
+ if (mode === 'resume') {
674
+ this._progress(options, 'reset', 'Modo resume — usando estado actual del dispositivo');
675
+ console.log(chalk.cyan(' 🤝 Modo resume: el humano ya pasó las barreras (PIN/2FA/SSO).\n' +
676
+ ' Discovery captura el estado actual y continúa desde ahí.\n'));
677
+ // Wake the screen in case it dimmed during the manual setup
678
+ if (!isIos) {
679
+ try {
680
+ execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
681
+ }
682
+ catch { }
683
+ try {
684
+ execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
685
+ }
686
+ catch { }
687
+ await sleep(500);
688
+ }
689
+ // No app reset, no launchApp — the user did that manually.
290
690
  }
291
691
  else {
292
- console.log(chalk.yellow(' ⚠️ No se pudo provocar error de login vacío'));
692
+ // ── Wake up screen + reset app state ──────────────────────────────
693
+ this._progress(options, 'reset', 'Reseteando estado de la app...');
694
+ console.log(chalk.gray(` 🔄 Reseteando estado de la app (${isIos ? 'iOS' : 'Android'})...`));
695
+ if (isIos) {
696
+ // iOS: state reset is handled via launchApp clearState:true in the launch flow below
697
+ }
698
+ else {
699
+ // Android: wake screen + clear app data via ADB
700
+ try {
701
+ execSync(`${adbS} shell input keyevent KEYCODE_WAKEUP`, { stdio: 'ignore' });
702
+ }
703
+ catch { }
704
+ try {
705
+ execSync(`${adbS} shell wm dismiss-keyguard`, { stdio: 'ignore' });
706
+ }
707
+ catch { }
708
+ await sleep(1000);
709
+ try {
710
+ execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
711
+ }
712
+ catch { }
713
+ await sleep(800); // espera extra para que el proceso muera completamente
714
+ try {
715
+ execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
716
+ }
717
+ catch { }
718
+ await sleep(2000); // tiempo extra post-clear para que Android libere recursos
719
+ }
293
720
  }
294
- // ── Navigate to Screen 2: Accounts ────────────────────────────────
295
- if (username && password) {
296
- this._progress(options, 'login', 'Autenticando para descubrir pantallas protegidas...');
297
- console.log(chalk.gray(' 🔐 Autenticando para explorar pantallas privadas...'));
298
- this._runMaestroFlow(appId, [
299
- `- launchApp:\n clearState: true`,
300
- `- waitForAnimationToEnd`,
301
- `- tapOn:\n id: "et_username"`,
302
- `- eraseText: 50`,
303
- `- inputText: "${username}"`,
304
- `- waitForAnimationToEnd`,
305
- `- hideKeyboard`,
306
- `- tapOn:\n id: "et_password"`,
307
- `- eraseText: 30`,
308
- `- inputText: "${password}"`,
309
- `- waitForAnimationToEnd`,
310
- `- hideKeyboard`,
311
- `- tapOn:\n id: "btn_login"`,
312
- `- waitForAnimationToEnd`,
313
- ], maestroEnv);
314
- await sleep(3000);
315
- let postLoginHierarchy;
721
+ // ─────────────────────────────────────────────────────────────────
722
+ // Phase 0 — GENERIC mobile Discovery (no hardcoded IDs)
723
+ // Spec: smartcase/DISCOVERY_SPEC.md § 9 "Fase 0"
724
+ // ─────────────────────────────────────────────────────────────────
725
+ // ── Step 1: Launch app + capture initial screen ──────────────────
726
+ this._progress(options, 'screen', mode === 'resume'
727
+ ? 'Capturando estado actual del dispositivo (resume mode)...'
728
+ : 'Pantalla inicial — Lanzando app...');
729
+ console.log(chalk.gray(mode === 'resume'
730
+ ? ' 📸 Capturando pantalla actual (sin relanzar la app)...'
731
+ : ' 📸 Capturando pantalla inicial...'));
732
+ if (mode === 'resume') {
733
+ // Resume mode: do NOT relaunch — capture whatever the device shows
734
+ }
735
+ else if (isIos) {
736
+ // iOS: launch via Maestro flow (handles clearState + permissions)
737
+ const maestro = resolveMaestro();
738
+ const deviceFlag = deviceId ? `--device ${deviceId} ` : '';
739
+ const launchFlow = `appId: ${appId}\n---\n- launchApp:\n clearState: true\n- waitForAnimationToEnd:\n timeout: 3000`;
740
+ const tmp = path.join(os.tmpdir(), `fsqa-launch-${Date.now()}.yaml`);
741
+ fs.writeFileSync(tmp, launchFlow);
316
742
  try {
317
- postLoginHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
743
+ execSync(`"${maestro}" ${deviceFlag}test ${tmp}`, { stdio: ['ignore', 'pipe', 'pipe'], timeout: 30000 });
318
744
  }
319
- catch {
320
- postLoginHierarchy = null;
745
+ catch (err) {
746
+ const stderr = err.stderr?.toString() || '';
747
+ throw new Error(`Maestro falló: ${stderr.split('\n').slice(0, 5).join(' | ')}`);
321
748
  }
322
- const postLoginElements = postLoginHierarchy ? this.extractMaestroSelectors(postLoginHierarchy) : [];
323
- const loginSuccess = postLoginElements.some(e => e.id === 'tv_welcome' || e.id === 'tv_total_balance');
324
- if (loginSuccess) {
325
- // ── Screen 2: Accounts ───────────────────────────────────────
326
- this._progress(options, 'screen', 'Pantalla 2 — Cuentas (autenticado)');
327
- console.log(chalk.gray(' 📸 Pantalla 2 — Cuentas...'));
749
+ finally {
750
+ try {
751
+ fs.unlinkSync(tmp);
752
+ }
753
+ catch { }
754
+ }
755
+ }
756
+ else {
757
+ execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
758
+ }
759
+ // Cold launch takes longer on first run; resume just needs a brief settle.
760
+ // Banking apps require extra time: SSL pinning checks, root detection, animated splash screens.
761
+ await sleep(mode === 'resume' ? 1500 : (isIos ? 6000 : 8000));
762
+ // ── Verify the app is actually in foreground before capturing ─────────
763
+ // If the app failed to launch (e.g. pm clear was too slow), the home screen
764
+ // may be visible. _ensureAppInForeground retries up to 3 times automatically.
765
+ if (!isIos && mode !== 'resume') {
766
+ const inFg = await this._ensureAppInForeground(adbS, appId, options);
767
+ if (!inFg) {
768
+ console.log(chalk.yellow(' ⚠️ Continuando Discovery sin confirmar app en primer plano — evidencia puede ser incorrecta.'));
769
+ }
770
+ }
771
+ const launchScreenshot = await this._captureScreenshotMobile(isIos ? 'maestro' : adbS, 'initial', deviceId, appId);
772
+ const launchHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
773
+ const launchElements = this.extractMaestroSelectors(launchHierarchy);
774
+ const launchTexts = this.extractAllTexts(launchHierarchy);
775
+ const launchUI = legacyElementsToUIElements(launchElements, '__pending__');
776
+ // Generic screen name — no app-specific assumption
777
+ const initialScreenName = 'Pantalla Inicial';
778
+ allScreens.push({
779
+ name: initialScreenName,
780
+ isErrorState: false,
781
+ screenshot: launchScreenshot,
782
+ elements: launchElements,
783
+ allTexts: launchTexts,
784
+ });
785
+ navigationSteps.push({ from: 'Inicio', to: initialScreenName, action: 'Lanzar app' });
786
+ console.log(chalk.green(` ✅ ${initialScreenName}: ${launchElements.length} elementos, ${launchScreenshot ? 'screenshot ✓' : 'sin screenshot'}`));
787
+ // ── EvidenceRecord ev_001 — App launch (or resume start) ─────────
788
+ // Phase 12: in resume mode, screen_origin reflects the human-managed
789
+ // setup and the result_description notes that prior steps weren't
790
+ // recorded. The action_type stays 'launch' (it IS the entry point of
791
+ // the corpus session, just not literally an app launch).
792
+ const ev001 = buildRecord({
793
+ sessionId: corpus.sessionId,
794
+ screen_origin: mode === 'resume' ? 'Inicio (manual)' : 'Inicio',
795
+ action_type: 'launch',
796
+ action_element: appId,
797
+ screenshot_before: null,
798
+ elements_before: [],
799
+ screen_destination: initialScreenName,
800
+ result_type: 'new_screen',
801
+ result_description: mode === 'resume'
802
+ ? `Estado inicial provisto por humano (resume mode). Pasos manuales (PIN/2FA/SSO) no se registraron — Discovery captura desde la pantalla actual con ${launchElements.length} elementos.`
803
+ : `App lanzada — ${launchElements.length} elementos en pantalla inicial`,
804
+ screenshot_after: launchScreenshot,
805
+ elements_after: launchUI,
806
+ captured_by: 'autonomous',
807
+ capture_mode: mode === 'resume' ? 'resume' : 'autonomous',
808
+ });
809
+ addRecord(corpus, ev001);
810
+ // ── Step 2: Detect login form on initial screen ──────────────────
811
+ // Generic detection — works with any app that has 1+ EditText + 1+ Button.
812
+ // Phase 12: in resume mode the human is already past the login, so we
813
+ // skip the login form detection entirely (any EditText we see now is
814
+ // a regular form, e.g. transfer amount, not credentials).
815
+ const loginForm = detectLoginForm(launchUI);
816
+ // Tracking state for BFS exploration
817
+ let lastScreenName = initialScreenName;
818
+ let lastUI = launchUI;
819
+ let lastTexts = launchTexts;
820
+ let lastScreenshot = launchScreenshot;
821
+ let loggedIn = !loginForm; // If no login form, assume already in main flow
822
+ let isDummyCredentials = false;
823
+ if (loginForm) {
824
+ if (!username || !password) {
825
+ // If interactive callback is available, prompt the user first
826
+ if (options.onFormDetected) {
827
+ console.log(chalk.cyan('\n 🔐 Formulario de login detectado sin credenciales. Solicitando interacción con el usuario...'));
828
+ const credentialsForm = {
829
+ screenName: initialScreenName,
830
+ fields: [
831
+ { id: loginForm.usernameId, label: loginForm.usernameLabel || 'Usuario / RUT' },
832
+ { id: loginForm.passwordId, label: loginForm.passwordLabel || 'Contraseña / Clave' }
833
+ ],
834
+ submitButtonId: loginForm.submitId,
835
+ submitButtonLabel: loginForm.submitLabel || 'Iniciar Sesión',
836
+ };
837
+ try {
838
+ const userCredentials = await options.onFormDetected(credentialsForm);
839
+ if (userCredentials && userCredentials[loginForm.usernameId] && userCredentials[loginForm.passwordId]) {
840
+ username = userCredentials[loginForm.usernameId];
841
+ password = userCredentials[loginForm.passwordId];
842
+ options.username = username;
843
+ options.password = password;
844
+ isDummyCredentials = false;
845
+ }
846
+ }
847
+ catch (err) {
848
+ console.log(chalk.yellow(` ⚠️ Error solicitando credenciales interactivas: ${err.message}`));
849
+ }
850
+ }
851
+ // Fallback heuristic if credentials are still not provided (e.g. user declined or non-interactive)
852
+ if (!username || !password) {
853
+ isDummyCredentials = true;
854
+ const usernameLabelLower = (loginForm.usernameLabel || '').toLowerCase();
855
+ const passwordLabelLower = (loginForm.passwordLabel || '').toLowerCase();
856
+ // RUT / DNI detection -> clean numeric format (no hyphens/chars if numeric input expected)
857
+ username = /rut|run|dni|cedula|cédula|identi|nro|número|numero/i.test(usernameLabelLower)
858
+ ? '123456789'
859
+ : 'user@example.com';
860
+ // PIN / Code detection -> numeric passcode
861
+ password = /pin|clave|passcode|codigo|código|pass|contrase|secret/i.test(passwordLabelLower)
862
+ ? '123456'
863
+ : 'Password123';
864
+ options.username = username;
865
+ options.password = password;
866
+ console.log(chalk.yellow(` ℹ️ No se proporcionaron credenciales (--username / --password). ` +
867
+ `Usando credenciales ficticias heurísticas ("${username}" / "${password}") ` +
868
+ `para habilitar el botón y explorar los estados de error/validación.`));
869
+ }
870
+ }
871
+ console.log(chalk.cyan(` 🔐 Form de login detectado: ` +
872
+ `usuario="${loginForm.usernameId}"${loginForm.usernameLabel ? ` (${loginForm.usernameLabel})` : ''}, ` +
873
+ `password="${loginForm.passwordId}", ` +
874
+ `submit="${loginForm.submitId}"${loginForm.submitLabel ? ` (${loginForm.submitLabel})` : ''}`));
875
+ // ── Step 2a: Empty submit → validation error state ─────────────
876
+ this._progress(options, 'error_state', 'Submit vacío — capturando validaciones...');
877
+ console.log(chalk.gray(' 📸 Probando submit vacío para capturar errores de validación...'));
878
+ const emptySubmitFlow = `- tapOn:\n ${buildMaestroSelector(loginForm.submitId, isIos)}`;
879
+ const emptySubmit = this._runMaestroFlow(appId, [
880
+ emptySubmitFlow,
881
+ `- waitForAnimationToEnd:\n timeout: 1500`,
882
+ ], maestroEnv).ok;
883
+ if (emptySubmit) {
328
884
  await sleep(1500);
329
- const accountsScreenshot = await this._captureScreenshotMobile(adbS, 'accounts');
330
- const accountsHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
331
- const accountsElements = this.extractMaestroSelectors(accountsHierarchy);
332
- const accountsTexts = this.extractAllTexts(accountsHierarchy);
333
- allScreens.push({
334
- name: 'Cuentas (autenticado)',
335
- isErrorState: false,
336
- screenshot: accountsScreenshot,
337
- elements: accountsElements,
338
- allTexts: accountsTexts,
339
- });
340
- navigationSteps.push({ from: 'Login', to: 'Cuentas', action: 'Login con credenciales válidas' });
341
- console.log(chalk.green(` ✅ Cuentas: ${accountsElements.length} elementos, ${accountsScreenshot ? 'screenshot ✓' : 'sin screenshot'}`));
342
- // ── Screen 3: Transfer ───────────────────────────────────────
343
- this._progress(options, 'screen', 'Pantalla 3 — Transferencia');
344
- console.log(chalk.gray(' 📸 Pantalla 3 — Transferencia...'));
345
- const navSuccess = this._runMaestroFlow(appId, [
346
- `- tapOn:\n id: "btn_transfer"`,
347
- `- waitForAnimationToEnd`,
348
- ], maestroEnv);
349
- if (navSuccess) {
350
- await sleep(1200);
351
- const transferScreenshot = await this._captureScreenshotMobile(adbS, 'transfer');
352
- const transferHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
353
- const transferElements = this.extractMaestroSelectors(transferHierarchy);
354
- const transferTexts = this.extractAllTexts(transferHierarchy);
885
+ const emptyShot = await this._captureScreenshotMobile(adbS, 'empty_submit');
886
+ const emptyHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
887
+ const emptyEls = emptyHier ? this.extractMaestroSelectors(emptyHier) : [];
888
+ const emptyTxts = emptyHier ? this.extractAllTexts(emptyHier) : [];
889
+ const knownIds = new Set(launchElements.map(e => e.id));
890
+ const newEls = emptyEls.filter(e => !knownIds.has(e.id));
891
+ const newTxts = emptyTxts.filter(t => !launchTexts.includes(t));
892
+ if (newEls.length > 0 || newTxts.length > 0) {
893
+ const errName = `${initialScreenName} (Error — campos vacíos)`;
355
894
  allScreens.push({
356
- name: 'Transferencia',
357
- isErrorState: false,
358
- screenshot: transferScreenshot,
359
- elements: transferElements,
360
- allTexts: transferTexts,
895
+ name: errName,
896
+ isErrorState: true,
897
+ screenshot: emptyShot,
898
+ elements: newEls,
899
+ allTexts: newTxts,
361
900
  });
362
- navigationSteps.push({ from: 'Cuentas', to: 'Transferencia', action: 'Tap btn_transfer' });
363
- console.log(chalk.green(` ✅ Transferencia: ${transferElements.length} elementos, ${transferScreenshot ? 'screenshot ✓' : 'sin screenshot'}`));
364
- // ── Error state: Transfer vacío ────────────────────────────
365
- this._progress(options, 'error_state', 'Transfer vacío — capturando mensajes de error...');
366
- console.log(chalk.gray(' 📸 Error state: Transfer vacío — Submit sin monto...'));
367
- const emptyTransferOk = this._runMaestroFlow(appId, [
368
- `- tapOn:\n id: "btn_confirm"`,
369
- `- waitForAnimationToEnd:\n timeout: 1500`,
370
- ], maestroEnv);
371
- if (emptyTransferOk) {
372
- await sleep(1500); // wait for error UI to settle before screencap
373
- const transferErrorScreenshot = await this._captureScreenshotMobile(adbS, 'transfer_error');
374
- const transferErrorHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
375
- const transferErrorAllElems = this.extractMaestroSelectors(transferErrorHierarchy);
376
- const transferErrorTexts = this.extractAllTexts(transferErrorHierarchy);
377
- const knownTransferIds = new Set(transferElements.map(e => e.id));
378
- const transferErrorNew = transferErrorAllElems.filter(e => !knownTransferIds.has(e.id));
379
- const transferNewTexts = transferErrorTexts.filter(t => !transferTexts.includes(t));
380
- allScreens.push({
381
- name: 'Transferencia (Error — monto vacío)',
382
- isErrorState: true,
383
- screenshot: transferErrorScreenshot,
384
- elements: transferErrorNew,
385
- allTexts: transferNewTexts,
386
- });
387
- navigationSteps.push({ from: 'Transferencia', to: 'Transferencia (Error — monto vacío)', action: 'Submit vacío' });
388
- const errList = transferNewTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
389
- console.log(chalk.green(` ✅ Transfer error state: ${transferErrorNew.length} elementos${errList ? ` | Textos: ${errList}` : ''}`));
390
- }
391
- else {
392
- console.log(chalk.yellow(' ⚠️ No se pudo provocar error de transfer vacío'));
393
- }
901
+ navigationSteps.push({ from: initialScreenName, to: errName, action: 'Submit vacío' });
902
+ const errList = newTxts.slice(0, 3).map(t => `"${t}"`).join(', ');
903
+ console.log(chalk.green(` ✅ Validación capturada: ${newEls.length} elementos / ${newTxts.length} textos${errList ? ` | ${errList}` : ''}`));
904
+ const ev002 = buildRecord({
905
+ sessionId: corpus.sessionId,
906
+ screen_origin: initialScreenName,
907
+ action_type: 'form_submit',
908
+ action_element: loginForm.submitId,
909
+ action_label: loginForm.submitLabel,
910
+ screenshot_before: launchScreenshot,
911
+ elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
912
+ input_data: { [loginForm.usernameId]: '', [loginForm.passwordId]: '' },
913
+ screen_destination: errName,
914
+ result_type: 'validation_state',
915
+ result_description: `Validación de campos vacíos — textos: ${errList || 'ninguno detectado'}`,
916
+ screenshot_after: emptyShot,
917
+ elements_after: legacyElementsToUIElements(newEls, '__pending__'),
918
+ captured_by: 'autonomous',
919
+ });
920
+ addRecord(corpus, ev002);
394
921
  }
395
922
  else {
396
- console.log(chalk.yellow(' ⚠️ Pantalla de Transferencia no accesible — omitiendo'));
397
- }
398
- // ── Error state: Credenciales inválidas ───────────────────────
399
- this._progress(options, 'error_state', 'Credenciales inválidas — capturando mensaje real...');
400
- console.log(chalk.gray(' 📸 Error state: Credenciales inválidas...'));
401
- // Force a clean state via ADB before the Maestro flow — avoids capturing
402
- // stale Transfer-screen elements if the previous error-state flow left the
403
- // device on that screen and launchApp:clearState takes time.
404
- try {
405
- execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
406
- execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
923
+ console.log(chalk.yellow(' ⚠️ Submit vacío no produjo cambios visibles'));
407
924
  }
408
- catch { }
409
- await sleep(1500);
925
+ }
926
+ // ── Step 2b: Login with credentials ────────────────────────────
927
+ if (username && password) {
928
+ this._progress(options, 'login', 'Autenticando con IDs detectados...');
929
+ console.log(chalk.gray(` 🔐 Login: usuario="${username}" en ${loginForm.usernameId} → submit ${loginForm.submitId}`));
930
+ const userSelector = buildMaestroSelector(loginForm.usernameId, isIos);
931
+ const passSelector = buildMaestroSelector(loginForm.passwordId, isIos);
932
+ const subSelector = buildMaestroSelector(loginForm.submitId, isIos);
410
933
  this._runMaestroFlow(appId, [
934
+ // Do NOT use clearState: true here — the app is already at the login screen.
935
+ // clearState resets the entire app, takes 8+ seconds for banking apps, and
936
+ // triggers security checks (SSL pinning, root detection) that delay the UI.
937
+ // Instead, just bring the app to foreground (if it went to background) and
938
+ // interact with the fields that are already visible.
411
939
  `- launchApp`,
412
940
  `- waitForAnimationToEnd:\n timeout: 3000`,
413
- `- tapOn:\n id: "et_username"`,
414
- `- inputText: "usuario_invalido_fpqa@test.com"`,
415
- `- waitForAnimationToEnd`,
941
+ // Scroll up to ensure the login form is fully visible from the top
942
+ `- scrollUntilVisible:\n element:\n ${userSelector.replace(/\n\s+/g, '\n ')}\n direction: UP`,
943
+ `- tapOn:\n ${userSelector}`,
944
+ `- eraseText: 50`,
945
+ `- inputText: "${username}"`,
946
+ `- waitForAnimationToEnd:\n timeout: 2000`,
416
947
  `- hideKeyboard`,
417
- `- tapOn:\n id: "et_password"`,
418
- `- inputText: "wrongpassword999"`,
419
- `- waitForAnimationToEnd`,
948
+ // After keyboard dismissal the layout reflows — scroll to show password field
949
+ `- waitForAnimationToEnd:\n timeout: 2000`,
950
+ `- tapOn:\n ${passSelector}`,
951
+ `- eraseText: 50`,
952
+ `- inputText: "${password}"`,
953
+ `- waitForAnimationToEnd:\n timeout: 2000`,
420
954
  `- hideKeyboard`,
421
- `- tapOn:\n id: "btn_login"`,
422
- `- waitForAnimationToEnd:\n timeout: 4000`,
955
+ `- waitForAnimationToEnd:\n timeout: 2000`,
956
+ `- tapOn:\n ${subSelector}`,
957
+ `- waitForAnimationToEnd:\n timeout: 5000`,
423
958
  ], maestroEnv);
424
- await sleep(2500); // wait for the error toast / message to render
425
- let invalidCredsHierarchy = null;
959
+ // ── Verify app is still in foreground after login flow ─────────────
960
+ // launchApp clearState: true can leave the home screen if the app
961
+ // takes too long to initialize (SSL pinning, root detection, etc.)
962
+ if (!isIos) {
963
+ const inFgAfterLogin = await this._ensureAppInForeground(adbS, appId, options);
964
+ if (!inFgAfterLogin) {
965
+ console.log(chalk.yellow(' ⚠️ App no confirmada en primer plano post-login — el análisis de éxito puede ser incorrecto.'));
966
+ }
967
+ }
968
+ await sleep(3000);
969
+ let postLoginHier = null;
426
970
  try {
427
- invalidCredsHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
971
+ postLoginHier = await this._captureHierarchy(maestroEnv, options.deviceId);
428
972
  }
429
973
  catch { }
430
- if (invalidCredsHierarchy) {
431
- const invalidCredsScreenshot = await this._captureScreenshotMobile(adbS, 'invalid_creds');
432
- const invalidCredsAllElems = this.extractMaestroSelectors(invalidCredsHierarchy);
433
- const invalidCredsTexts = this.extractAllTexts(invalidCredsHierarchy);
434
- const invalidCredsNewTexts = invalidCredsTexts.filter(t => !loginTexts.includes(t));
435
- const knownLoginIds = new Set(loginElements.map(e => e.id));
436
- const invalidCredsNewElems = invalidCredsAllElems.filter(e => !knownLoginIds.has(e.id));
437
- if (invalidCredsNewTexts.length > 0) {
438
- allScreens.push({
439
- name: 'Login (Error — credenciales inválidas)',
440
- isErrorState: true,
441
- screenshot: invalidCredsScreenshot,
442
- elements: invalidCredsNewElems,
443
- allTexts: invalidCredsNewTexts,
444
- });
445
- navigationSteps.push({ from: 'Login', to: 'Login (Error — credenciales inválidas)', action: 'Credenciales inválidas' });
446
- const errList = invalidCredsNewTexts.slice(0, 3).map(t => `"${t}"`).join(', ');
447
- console.log(chalk.green(` ✅ Creds inválidas: textos reales encontrados: ${errList}`));
974
+ const postLoginEls = postLoginHier ? this.extractMaestroSelectors(postLoginHier) : [];
975
+ const postLoginUI = legacyElementsToUIElements(postLoginEls, '__pending__');
976
+ const postLoginTxts = postLoginHier ? this.extractAllTexts(postLoginHier) : [];
977
+ // GENERIC success detection: did the screen change?
978
+ const change = detectScreenChange(launchUI, postLoginUI);
979
+ const postLoginShot = await this._captureScreenshotMobile(adbS, 'post_login');
980
+ if (change.changed && isDummyCredentials) {
981
+ // ── Dummy credentials: capture error state, then reset to initial screen ──
982
+ const errScreenName = `${initialScreenName} (Error — credenciales inválidas)`;
983
+ allScreens.push({
984
+ name: errScreenName,
985
+ isErrorState: true,
986
+ screenshot: postLoginShot,
987
+ elements: postLoginEls,
988
+ allTexts: postLoginTxts,
989
+ });
990
+ navigationSteps.push({ from: initialScreenName, to: errScreenName, action: 'Login con credenciales ficticias' });
991
+ console.log(chalk.green(` ✅ Credenciales ficticias: pantalla cambió ` +
992
+ `(${change.newIdsCount} nuevos elementos, similitud ${change.similarity.toFixed(2)}) — ` +
993
+ `capturado como estado de error. Regresando a pantalla inicial para BFS pre-auth.`));
994
+ const ev003 = buildRecord({
995
+ sessionId: corpus.sessionId,
996
+ screen_origin: initialScreenName,
997
+ action_type: 'form_submit',
998
+ action_element: loginForm.submitId,
999
+ action_label: loginForm.submitLabel,
1000
+ screenshot_before: launchScreenshot,
1001
+ elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
1002
+ input_data: { [loginForm.usernameId]: username, [loginForm.passwordId]: '***' },
1003
+ screen_destination: errScreenName,
1004
+ result_type: 'validation_state',
1005
+ result_description: `Credenciales ficticias rechazadas — ${postLoginEls.length} elementos, ${change.newIdsCount} nuevos vs pantalla inicial`,
1006
+ screenshot_after: postLoginShot,
1007
+ elements_after: postLoginUI,
1008
+ captured_by: 'autonomous',
1009
+ });
1010
+ addRecord(corpus, ev003);
1011
+ // Reset app state to return to the initial login screen for BFS
1012
+ this._progress(options, 'reset', 'Regresando a pantalla inicial para exploración BFS...');
1013
+ if (!isIos) {
1014
+ try {
1015
+ execSync(`${adbS} shell am force-stop ${appId}`, { stdio: 'ignore' });
1016
+ }
1017
+ catch { }
1018
+ try {
1019
+ execSync(`${adbS} shell pm clear ${appId}`, { stdio: 'ignore' });
1020
+ }
1021
+ catch { }
1022
+ await sleep(1000);
1023
+ execSync(`${adbS} shell monkey -p ${appId} -c android.intent.category.LAUNCHER 1`, { stdio: 'ignore' });
1024
+ }
1025
+ else {
1026
+ this._runMaestroFlow(appId, [
1027
+ `- launchApp:\n clearState: true`,
1028
+ `- waitForAnimationToEnd:\n timeout: 3000`,
1029
+ ], maestroEnv);
448
1030
  }
1031
+ await sleep(isIos ? 5000 : 4500);
1032
+ // Re-capture initial screen elements for BFS
1033
+ const resetHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
1034
+ if (resetHier) {
1035
+ const resetEls = this.extractMaestroSelectors(resetHier);
1036
+ const resetUI = legacyElementsToUIElements(resetEls, '__pending__');
1037
+ const resetTxts = this.extractAllTexts(resetHier);
1038
+ const resetShot = await this._captureScreenshotMobile(isIos ? 'maestro' : adbS, 'reset_after_dummy');
1039
+ lastScreenName = initialScreenName;
1040
+ lastUI = resetUI;
1041
+ lastTexts = resetTxts;
1042
+ lastScreenshot = resetShot;
1043
+ }
1044
+ // loggedIn stays false → BFS will explore pre-auth buttons
1045
+ }
1046
+ else if (change.changed) {
1047
+ loggedIn = true;
1048
+ const authScreenName = 'Pantalla Autenticada';
1049
+ allScreens.push({
1050
+ name: authScreenName,
1051
+ isErrorState: false,
1052
+ screenshot: postLoginShot,
1053
+ elements: postLoginEls,
1054
+ allTexts: postLoginTxts,
1055
+ });
1056
+ navigationSteps.push({ from: initialScreenName, to: authScreenName, action: 'Login con credenciales válidas' });
1057
+ console.log(chalk.green(` ✅ Login exitoso: pantalla cambió ` +
1058
+ `(${change.newIdsCount} nuevos elementos, similitud ${change.similarity.toFixed(2)}) — ` +
1059
+ `${postLoginEls.length} elementos en pantalla autenticada`));
1060
+ const ev003 = buildRecord({
1061
+ sessionId: corpus.sessionId,
1062
+ screen_origin: initialScreenName,
1063
+ action_type: 'form_submit',
1064
+ action_element: loginForm.submitId,
1065
+ action_label: loginForm.submitLabel,
1066
+ screenshot_before: launchScreenshot,
1067
+ elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
1068
+ input_data: { [loginForm.usernameId]: username, [loginForm.passwordId]: '***' },
1069
+ screen_destination: authScreenName,
1070
+ result_type: 'new_screen',
1071
+ result_description: `Login exitoso — ${postLoginEls.length} elementos visibles, ${change.newIdsCount} nuevos vs pantalla inicial`,
1072
+ screenshot_after: postLoginShot,
1073
+ elements_after: postLoginUI,
1074
+ captured_by: 'autonomous',
1075
+ });
1076
+ addRecord(corpus, ev003);
1077
+ lastScreenName = authScreenName;
1078
+ lastUI = postLoginUI;
1079
+ lastTexts = postLoginTxts;
1080
+ lastScreenshot = postLoginShot;
1081
+ }
1082
+ else {
1083
+ console.log(chalk.yellow(` ⚠️ Login no produjo cambio de pantalla (similitud ${change.similarity.toFixed(2)}). ` +
1084
+ `Posibles causas: credenciales rechazadas, paso adicional (PIN/2FA), o app aún cargando.`));
1085
+ const ev003 = buildRecord({
1086
+ sessionId: corpus.sessionId,
1087
+ screen_origin: initialScreenName,
1088
+ action_type: 'form_submit',
1089
+ action_element: loginForm.submitId,
1090
+ action_label: loginForm.submitLabel,
1091
+ screenshot_before: launchScreenshot,
1092
+ elements_before: legacyElementsToUIElements(launchElements, ev001.evidenceId),
1093
+ input_data: { [loginForm.usernameId]: username, [loginForm.passwordId]: '***' },
1094
+ screen_destination: initialScreenName,
1095
+ result_type: 'partial_flow',
1096
+ result_description: `Login no produjo cambio — similitud ${change.similarity.toFixed(2)} (credenciales rechazadas, 2FA, o flujo adicional)`,
1097
+ screenshot_after: postLoginShot,
1098
+ elements_after: postLoginUI,
1099
+ captured_by: 'autonomous',
1100
+ });
1101
+ addRecord(corpus, ev003);
449
1102
  }
450
1103
  }
1104
+ }
1105
+ else if (mode === 'resume') {
1106
+ console.log(chalk.cyan(' 🤝 Modo resume: detectando posibles formularios en el estado actual...'));
1107
+ }
1108
+ else {
1109
+ console.log(chalk.gray(' ℹ️ No se detectó form de login (la app abre directo en su flujo principal)'));
1110
+ }
1111
+ // ── Step 3: Generic BFS over navigable buttons ───────────────────
1112
+ // For each clickable button on the current screen, tap it and observe
1113
+ // what happens. Works on both pre-auth (login screen) and post-auth
1114
+ // screens — rankNavigableButtons already excludes EditText and other
1115
+ // form fields, so only actual navigation buttons are explored.
1116
+ // 1. Tap → capture new screen state
1117
+ // 2. If we landed back on the login screen → record logout, halt BFS
1118
+ // 3. detectForm on the new screen
1119
+ // 4. If form: empty submit FIRST (capture validation evidence)
1120
+ // 5. Then prompt the tester via onFormDetected (real flow)
1121
+ // 6. Go back to parent screen (multiple `back` if needed)
1122
+ if (lastUI.length > 0) {
1123
+ const visited = new Set();
1124
+ const maxButtons = 8; // safety cap on BFS breadth per session
1125
+ // When not logged in, exclude login form elements from BFS candidates
1126
+ // so we don't accidentally tap the submit button without credentials.
1127
+ if (loginForm && !loggedIn) {
1128
+ visited.add(loginForm.usernameId);
1129
+ visited.add(loginForm.passwordId);
1130
+ visited.add(loginForm.submitId);
1131
+ }
1132
+ const candidates = rankNavigableButtons(lastUI, visited, appId).slice(0, maxButtons);
1133
+ const parentName = lastScreenName;
1134
+ const parentUI = lastUI;
1135
+ const parentShot = lastScreenshot;
1136
+ const parentTexts = lastTexts;
1137
+ // Track visited screens by element-ID fingerprint so we don't re-explore
1138
+ // the same form when two buttons lead to the same destination
1139
+ // (e.g. TRANSFERIR and PAGAR both opening "Nueva Transferencia").
1140
+ // We seed the set with launch + parent screens.
1141
+ const visitedScreens = [
1142
+ { name: initialScreenName, ui: launchUI },
1143
+ { name: parentName, ui: parentUI },
1144
+ ];
1145
+ if (candidates.length === 0) {
1146
+ console.log(chalk.gray(' ℹ️ Sin botones navegables para BFS'));
1147
+ }
451
1148
  else {
452
- console.log(chalk.yellow(' ⚠️ Login falló — solo se capturó pantalla de Login'));
1149
+ console.log(chalk.cyan(`\n 🌐 BFS sobre ${candidates.length} botón(es) en "${parentName}"${!loggedIn ? ' (pre-auth)' : ''}...`));
1150
+ }
1151
+ for (const btn of candidates) {
1152
+ if (visited.has(btn.elementId))
1153
+ continue;
1154
+ visited.add(btn.elementId);
1155
+ const btnLabel = btn.visibleText || btn.elementId;
1156
+ this._progress(options, 'screen', `Explorando "${btnLabel}"`);
1157
+ console.log(chalk.gray(`\n 👆 Tap en "${btnLabel}" (${btn.elementId})...`));
1158
+ const bfsSelector = buildMaestroSelector(btn.elementId, isIos);
1159
+ const tapResult = this._runMaestroFlow(appId, [
1160
+ `- tapOn:\n ${bfsSelector}`,
1161
+ `- waitForAnimationToEnd:\n timeout: 3000`,
1162
+ ], maestroEnv);
1163
+ if (!tapResult.ok) {
1164
+ console.log(chalk.yellow(` ⚠️ Tap en ${btn.elementId} falló — saltando`));
1165
+ continue;
1166
+ }
1167
+ await sleep(1500);
1168
+ const tapHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
1169
+ if (!tapHier) {
1170
+ console.log(chalk.yellow(` ⚠️ Sin jerarquía post-tap — saltando`));
1171
+ continue;
1172
+ }
1173
+ const tapEls = this.extractMaestroSelectors(tapHier);
1174
+ const tapUI = legacyElementsToUIElements(tapEls, '__pending__');
1175
+ const tapTxts = this.extractAllTexts(tapHier);
1176
+ const tapChange = detectScreenChange(parentUI, tapUI);
1177
+ const tapShot = await this._captureScreenshotMobile(adbS, `tap_${btn.elementId}`);
1178
+ if (!tapChange.changed) {
1179
+ console.log(chalk.gray(` ℹ️ Tap no cambió la pantalla (similitud ${tapChange.similarity.toFixed(2)}) — siguiente`));
1180
+ continue;
1181
+ }
1182
+ // ── Logout detection ────────────────────────────────────────
1183
+ // If the new screen looks like the original login screen, the user
1184
+ // logged out. Record the navigation and halt BFS — exploring further
1185
+ // would re-prompt the tester for credentials they already provided.
1186
+ if (loginForm && isSameScreen(tapUI, launchUI, 0.7)) {
1187
+ console.log(chalk.cyan(` 🚪 "${btnLabel}" cerró sesión (regresó a la pantalla inicial). BFS detenido.`));
1188
+ navigationSteps.push({ from: parentName, to: initialScreenName, action: `Tap ${btn.elementId}` });
1189
+ const logoutEv = buildRecord({
1190
+ sessionId: corpus.sessionId,
1191
+ screen_origin: parentName,
1192
+ action_type: 'tap',
1193
+ action_element: btn.elementId,
1194
+ action_label: btn.visibleText,
1195
+ screenshot_before: parentShot,
1196
+ elements_before: parentUI,
1197
+ screen_destination: initialScreenName,
1198
+ result_type: 'session_end',
1199
+ result_description: `Cierre de sesión — la app regresó a la pantalla inicial (similitud ${tapChange.similarity.toFixed(2)})`,
1200
+ screenshot_after: tapShot,
1201
+ elements_after: tapUI,
1202
+ captured_by: 'autonomous',
1203
+ });
1204
+ addRecord(corpus, logoutEv);
1205
+ break; // halt BFS — re-running Discovery will re-login
1206
+ }
1207
+ // ── Duplicate-screen detection ──────────────────────────────
1208
+ // If tapping this button leads to a screen we've already explored
1209
+ // (e.g. PAGAR opens the same form as TRANSFERIR), we still want to
1210
+ // capture independent evidence for THIS flow — the entry button
1211
+ // matters even if the destination UI looks identical:
1212
+ // - Same screen, same data, same result → flows are equivalent
1213
+ // - Same screen, same data, different result → real divergence
1214
+ // (different backend, different validation, mock bug, etc.)
1215
+ // We skip the tester re-prompt (data already provided) but reuse
1216
+ // it to execute the form again and record the result.
1217
+ const duplicateOf = visitedScreens.find(s => isSameScreen(tapUI, s.ui, 0.7));
1218
+ if (duplicateOf) {
1219
+ console.log(chalk.gray(` 🔁 "${btnLabel}" lleva a "${duplicateOf.name}" (ya explorada).`));
1220
+ navigationSteps.push({ from: parentName, to: duplicateOf.name, action: `Tap ${btn.elementId}` });
1221
+ // Record the navigation itself
1222
+ const dupEv = buildRecord({
1223
+ sessionId: corpus.sessionId,
1224
+ screen_origin: parentName,
1225
+ action_type: 'tap',
1226
+ action_element: btn.elementId,
1227
+ action_label: btn.visibleText,
1228
+ screenshot_before: parentShot,
1229
+ elements_before: parentUI,
1230
+ screen_destination: duplicateOf.name,
1231
+ result_type: 'duplicate_route',
1232
+ result_description: `Navegación al mismo destino que otro botón — alias de "${duplicateOf.name}" (similitud ${tapChange.similarity.toFixed(2)})`,
1233
+ screenshot_after: tapShot,
1234
+ elements_after: tapUI,
1235
+ captured_by: 'autonomous',
1236
+ });
1237
+ addRecord(corpus, dupEv);
1238
+ // ── Re-execute the form using previously-captured data ──────
1239
+ // Look up the most recent interactive form_submit on this screen
1240
+ // that has real input data (not just empty validation).
1241
+ const screenForm = detectForm(tapUI, duplicateOf.name);
1242
+ const prevForm = [...corpus.records].reverse().find(r => r.screen_origin === duplicateOf.name &&
1243
+ r.action_type === 'form_submit' &&
1244
+ r.captured_by === 'interactive' &&
1245
+ r.input_data &&
1246
+ Object.values(r.input_data).some(v => v && v !== '***'));
1247
+ if (screenForm && prevForm && prevForm.input_data) {
1248
+ console.log(chalk.cyan(` 🔄 Re-ejecutando el form con los datos previamente provistos ` +
1249
+ `(${Object.keys(prevForm.input_data).length} campos) para capturar ` +
1250
+ `evidencia independiente del flujo "${btnLabel}".`));
1251
+ const fillSteps = [];
1252
+ for (const [fid, val] of Object.entries(prevForm.input_data)) {
1253
+ if (val === '***' || !val)
1254
+ continue;
1255
+ const selector = buildMaestroSelector(fid, isIos);
1256
+ fillSteps.push(`- tapOn:\n ${selector}`, `- eraseText: 50`, `- inputText: "${val}"`, `- hideKeyboard`);
1257
+ }
1258
+ const subSelectorAlias = buildMaestroSelector(screenForm.submitButtonId, isIos);
1259
+ fillSteps.push(`- tapOn:\n ${subSelectorAlias}`, `- waitForAnimationToEnd:\n timeout: 5000`);
1260
+ const formRes = this._runMaestroFlow(appId, fillSteps, maestroEnv);
1261
+ await sleep(2500);
1262
+ const aliasShot = await this._captureScreenshotMobile(adbS, `alias_${btn.elementId}`);
1263
+ const aliasHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
1264
+ const aliasEls = aliasHier ? this.extractMaestroSelectors(aliasHier) : [];
1265
+ const aliasUI = legacyElementsToUIElements(aliasEls, '__pending__');
1266
+ const aliasTxts = aliasHier ? this.extractAllTexts(aliasHier) : [];
1267
+ const successText = aliasTxts.find(t => /comprobante|éxito|exitosa|confirmado|listo|aprobad/i.test(t));
1268
+ const isSuccess = !!successText && formRes.ok;
1269
+ // Compare this run's result to the previous run's result.
1270
+ // Normalize previous result_type so legacy values still match the new enum.
1271
+ const prevNormalized = normalizeResultType(prevForm.result_type);
1272
+ const sameAsOriginal = (isSuccess && prevNormalized === 'new_screen') ||
1273
+ (formRes.ok && prevNormalized === 'new_screen') ||
1274
+ (!formRes.ok && (prevNormalized === 'partial_flow' || prevNormalized === 'error_state'));
1275
+ const flowTag = `[vía ${btnLabel}]`;
1276
+ let aliasDest;
1277
+ let aliasResult;
1278
+ let aliasDesc;
1279
+ if (isSuccess) {
1280
+ aliasDest = `${duplicateOf.name} — Éxito ${flowTag}`;
1281
+ aliasResult = 'new_screen';
1282
+ aliasDesc = `Éxito por flujo "${btnLabel}" — texto: "${successText}". ${sameAsOriginal ? 'Comportamiento idéntico al flujo original.' : 'Difiere del flujo original — revisar.'}`;
1283
+ console.log(chalk.green(` ✅ Flujo "${btnLabel}" exitoso ${sameAsOriginal ? '(igual al original)' : '(¡diferente al original!)'}`));
1284
+ }
1285
+ else if (!formRes.ok) {
1286
+ aliasDest = `${duplicateOf.name} — Submit incompleto ${flowTag}`;
1287
+ aliasResult = 'partial_flow';
1288
+ aliasDesc = `Maestro reportó fallo en flujo "${btnLabel}". Pantalla: ${aliasTxts.slice(0, 3).join(' | ') || '(sin texto)'}. ${sameAsOriginal ? 'Mismo modo de fallo que el flujo original.' : 'Modo de fallo difiere del original.'}`;
1289
+ console.log(chalk.yellow(` ⚠️ Flujo "${btnLabel}" falló ${sameAsOriginal ? '(igual al original)' : '(¡diferente al original!)'}`));
1290
+ }
1291
+ else {
1292
+ aliasDest = `${duplicateOf.name} — Resultado ${flowTag}`;
1293
+ aliasResult = 'new_screen';
1294
+ aliasDesc = `Submit del flujo "${btnLabel}" navegó. ${sameAsOriginal ? 'Comportamiento equivalente al original.' : 'Difiere del original.'}`;
1295
+ console.log(chalk.green(` ✅ Flujo "${btnLabel}" navegó ${sameAsOriginal ? '(igual al original)' : '(¡diferente al original!)'}`));
1296
+ }
1297
+ allScreens.push({
1298
+ name: aliasDest,
1299
+ isErrorState: aliasResult === 'partial_flow',
1300
+ screenshot: aliasShot,
1301
+ elements: aliasEls,
1302
+ allTexts: aliasTxts,
1303
+ });
1304
+ navigationSteps.push({ from: duplicateOf.name, to: aliasDest, action: `Tap ${screenForm.submitButtonId}` });
1305
+ const aliasEv = buildRecord({
1306
+ sessionId: corpus.sessionId,
1307
+ screen_origin: duplicateOf.name,
1308
+ action_type: 'form_submit',
1309
+ action_element: screenForm.submitButtonId,
1310
+ action_label: screenForm.submitButtonLabel,
1311
+ screenshot_before: tapShot,
1312
+ elements_before: tapUI,
1313
+ input_data: prevForm.input_data,
1314
+ screen_destination: aliasDest,
1315
+ result_type: aliasResult,
1316
+ result_description: aliasDesc,
1317
+ screenshot_after: aliasShot,
1318
+ elements_after: aliasUI,
1319
+ captured_by: 'autonomous', // re-run by the engine, not prompted
1320
+ });
1321
+ addRecord(corpus, aliasEv);
1322
+ }
1323
+ else if (screenForm) {
1324
+ console.log(chalk.gray(` ℹ️ Sin datos previos del form para reusar — saltando re-ejecución.`));
1325
+ }
1326
+ // Return to parent for the next BFS button
1327
+ const returnedToParent = await this._smartBack(appId, maestroEnv, options.deviceId, parentUI, launchUI, loginForm, parentName);
1328
+ if (!returnedToParent) {
1329
+ console.log(chalk.yellow(` ⚠️ No se pudo regresar a "${parentName}". BFS detenido.`));
1330
+ break;
1331
+ }
1332
+ continue; // next BFS button
1333
+ }
1334
+ // ── Smart screen name from visible content ──────────────────
1335
+ const screenName = inferScreenName(tapTxts, [...parentTexts, ...launchTexts], // exclude texts seen on parent + launch
1336
+ `Pantalla tras "${btnLabel}"`);
1337
+ // ── Phase 11 — Incremental Discovery: should we capture this screen? ──
1338
+ // Compute the fingerprint of the screen we just landed on, then ask
1339
+ // shouldCapture() whether the baseline already covers it. If yes,
1340
+ // we record a lightweight navigation record and skip the deep
1341
+ // exploration of the form / empty-submit / interactive prompt steps.
1342
+ // Spec: DISCOVERY_SPEC.md § 11.1 + § 11.2
1343
+ let skipDeepExploration = false;
1344
+ if (mode === 'incremental' && baselines) {
1345
+ const tapTextsForFp = tapEls.map(e => e.text).filter(t => !!t && t !== 'N/A');
1346
+ // Simple deterministic fingerprint substitute (full algo lives in
1347
+ // evidence-corpus; here we use the same input to stay consistent)
1348
+ const { screenFingerprint } = await import('./evidence-corpus.js');
1349
+ const currentFp = screenFingerprint(legacyElementsToUIElements(tapEls, '__pending__'), tapTextsForFp);
1350
+ const matchedBaseline = findBaselineForFingerprint(baselines, currentFp);
1351
+ const decision = shouldCapture(currentFp, matchedBaseline);
1352
+ if (!decision.capture) {
1353
+ skipDeepExploration = true;
1354
+ incrementalSkipCount++;
1355
+ console.log(chalk.cyan(` ♻️ "${screenName}" reusa baseline verified (skip deep exploration). ` +
1356
+ `Razón: ${decision.reason}`));
1357
+ navigationSteps.push({ from: parentName, to: screenName, action: `Tap ${btn.elementId} [reused]` });
1358
+ const reuseEv = buildRecord({
1359
+ sessionId: corpus.sessionId,
1360
+ screen_origin: parentName,
1361
+ action_type: 'tap',
1362
+ action_element: btn.elementId,
1363
+ action_label: btn.visibleText,
1364
+ screenshot_before: parentShot,
1365
+ elements_before: parentUI,
1366
+ screen_destination: screenName,
1367
+ result_type: 'new_screen',
1368
+ result_description: `Reused verified baseline (incremental mode). matched_baseline=${decision.matched_baseline_id}`,
1369
+ screenshot_after: tapShot,
1370
+ elements_after: tapUI,
1371
+ captured_by: 'autonomous',
1372
+ });
1373
+ addRecord(corpus, reuseEv);
1374
+ }
1375
+ else {
1376
+ incrementalCaptureCount++;
1377
+ console.log(chalk.gray(` 📸 "${screenName}" requiere captura — ${decision.reason}`));
1378
+ }
1379
+ }
1380
+ if (skipDeepExploration) {
1381
+ // Still go back to parent for the next BFS iteration (skipping
1382
+ // the empty-submit + onFormDetected + recursive logic below)
1383
+ this._runMaestroFlow(appId, [
1384
+ `- back`,
1385
+ `- waitForAnimationToEnd:\n timeout: 1500`,
1386
+ ], maestroEnv);
1387
+ await sleep(1000);
1388
+ continue;
1389
+ }
1390
+ allScreens.push({
1391
+ name: screenName,
1392
+ isErrorState: false,
1393
+ screenshot: tapShot,
1394
+ elements: tapEls,
1395
+ allTexts: tapTxts,
1396
+ });
1397
+ // Mark this new screen as visited for future BFS iterations
1398
+ visitedScreens.push({ name: screenName, ui: tapUI });
1399
+ navigationSteps.push({ from: parentName, to: screenName, action: `Tap ${btn.elementId}` });
1400
+ console.log(chalk.green(` ✅ Nueva pantalla "${screenName}": ${tapEls.length} elementos (${tapChange.newIdsCount} nuevos)`));
1401
+ const tapEv = buildRecord({
1402
+ sessionId: corpus.sessionId,
1403
+ screen_origin: parentName,
1404
+ action_type: 'tap',
1405
+ action_element: btn.elementId,
1406
+ action_label: btn.visibleText,
1407
+ screenshot_before: parentShot,
1408
+ elements_before: parentUI,
1409
+ screen_destination: screenName,
1410
+ result_type: 'new_screen',
1411
+ result_description: `Navegación: ${tapChange.newIdsCount} elementos nuevos, similitud ${tapChange.similarity.toFixed(2)}`,
1412
+ screenshot_after: tapShot,
1413
+ elements_after: tapUI,
1414
+ captured_by: 'autonomous',
1415
+ });
1416
+ addRecord(corpus, tapEv);
1417
+ // ── Step 4: Detect form on the new screen ────────────────────
1418
+ const screenForm = detectForm(tapUI, screenName);
1419
+ // ── Step 4a: Empty submit FIRST → captures validation evidence
1420
+ // Done before the tester prompt so validations come from a clean
1421
+ // form state, not after a successful real submit (which would have
1422
+ // already navigated us elsewhere).
1423
+ if (screenForm) {
1424
+ this._progress(options, 'error_state', `Submit vacío en "${screenName}"`);
1425
+ console.log(chalk.gray(` 📸 Probando validación en "${screenName}" (submit vacío)...`));
1426
+ const emptySelector = buildMaestroSelector(screenForm.submitButtonId, isIos);
1427
+ const emptyFormOk = this._runMaestroFlow(appId, [
1428
+ `- tapOn:\n ${emptySelector}`,
1429
+ `- waitForAnimationToEnd:\n timeout: 1500`,
1430
+ ], maestroEnv).ok;
1431
+ if (emptyFormOk) {
1432
+ await sleep(1500);
1433
+ const errHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
1434
+ if (errHier) {
1435
+ const errEls = this.extractMaestroSelectors(errHier);
1436
+ const errTxts = this.extractAllTexts(errHier);
1437
+ const errShot = await this._captureScreenshotMobile(adbS, `err_${btn.elementId}`);
1438
+ const knownIds = new Set(tapEls.map(e => e.id));
1439
+ const newErrEls = errEls.filter(e => !knownIds.has(e.id));
1440
+ const newErrTxts = errTxts.filter(t => !tapTxts.includes(t));
1441
+ if (newErrEls.length > 0 || newErrTxts.length > 0) {
1442
+ const errName = `${screenName} (Error — campos vacíos)`;
1443
+ allScreens.push({
1444
+ name: errName,
1445
+ isErrorState: true,
1446
+ screenshot: errShot,
1447
+ elements: newErrEls,
1448
+ allTexts: newErrTxts,
1449
+ });
1450
+ navigationSteps.push({ from: screenName, to: errName, action: 'Submit vacío' });
1451
+ const inputDataEmpty = {};
1452
+ for (const f of screenForm.fields)
1453
+ inputDataEmpty[f.id] = '';
1454
+ const errList = newErrTxts.slice(0, 3).map(t => `"${t}"`).join(', ');
1455
+ console.log(chalk.green(` ✅ Validación capturada: ${newErrEls.length} elementos / ${newErrTxts.length} textos${errList ? ` | ${errList}` : ''}`));
1456
+ const errEv = buildRecord({
1457
+ sessionId: corpus.sessionId,
1458
+ screen_origin: screenName,
1459
+ action_type: 'form_submit',
1460
+ action_element: screenForm.submitButtonId,
1461
+ action_label: screenForm.submitButtonLabel,
1462
+ screenshot_before: tapShot,
1463
+ elements_before: tapUI,
1464
+ input_data: inputDataEmpty,
1465
+ screen_destination: errName,
1466
+ result_type: 'validation_state',
1467
+ result_description: `Validación: ${errList || 'sin texto detectado'}`,
1468
+ screenshot_after: errShot,
1469
+ elements_after: legacyElementsToUIElements(newErrEls, '__pending__'),
1470
+ captured_by: 'autonomous',
1471
+ });
1472
+ addRecord(corpus, errEv);
1473
+ }
1474
+ }
1475
+ }
1476
+ }
1477
+ // ── Step 4b: Interactive form prompt (real data flow) ──────
1478
+ // Only run if we still seem to be on the form screen. If empty
1479
+ // submit caused navigation away (rare), we skip the prompt.
1480
+ if (screenForm && options.onFormDetected) {
1481
+ // Re-capture state to ensure we're still on the form screen
1482
+ const stillOnFormHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
1483
+ const stillOnFormEls = stillOnFormHier ? this.extractMaestroSelectors(stillOnFormHier) : [];
1484
+ const stillOnFormUI = legacyElementsToUIElements(stillOnFormEls, '__pending__');
1485
+ const stillThere = isSameScreen(stillOnFormUI, tapUI, 0.6);
1486
+ if (!stillThere) {
1487
+ console.log(chalk.gray(` ℹ️ Form ya no visible (submit vacío navegó a otra pantalla). Saltando prompt interactivo.`));
1488
+ }
1489
+ else {
1490
+ console.log(chalk.cyan(` 📋 Form detectado en "${screenName}": ${screenForm.fields.length} campos`));
1491
+ const fieldData = await options.onFormDetected(screenForm);
1492
+ if (fieldData) {
1493
+ const fillSteps = [];
1494
+ for (const [fid, val] of Object.entries(fieldData)) {
1495
+ const selector = buildMaestroSelector(fid, isIos);
1496
+ fillSteps.push(`- tapOn:\n ${selector}`, `- eraseText: 50`, `- inputText: "${val}"`, `- hideKeyboard`);
1497
+ }
1498
+ const submitSelector = buildMaestroSelector(screenForm.submitButtonId, isIos);
1499
+ fillSteps.push(`- tapOn:\n ${submitSelector}`, `- waitForAnimationToEnd:\n timeout: 5000`);
1500
+ const formRes = this._runMaestroFlow(appId, fillSteps, maestroEnv);
1501
+ await sleep(2500);
1502
+ const formShot = await this._captureScreenshotMobile(adbS, `form_${btn.elementId}`);
1503
+ const formHier = await this._captureHierarchy(maestroEnv, options.deviceId).catch(() => null);
1504
+ const formEls = formHier ? this.extractMaestroSelectors(formHier) : [];
1505
+ const formUI = legacyElementsToUIElements(formEls, '__pending__');
1506
+ const formTxts = formHier ? this.extractAllTexts(formHier) : [];
1507
+ const formChange = detectScreenChange(tapUI, formUI);
1508
+ const successText = formTxts.find(t => /comprobante|éxito|exitosa|confirmado|listo|aprobad/i.test(t));
1509
+ const isSuccess = !!successText && formRes.ok;
1510
+ let destName;
1511
+ let resultType;
1512
+ let resultDescription;
1513
+ if (isSuccess) {
1514
+ destName = inferScreenName(formTxts, [...tapTxts, ...parentTexts], `Resultado exitoso de ${screenName}`);
1515
+ resultType = 'new_screen';
1516
+ resultDescription = `Formulario exitoso — texto detectado: "${successText}" (${formEls.length} elementos)`;
1517
+ console.log(chalk.green(` ✅ Form exitoso: "${destName}"`));
1518
+ }
1519
+ else if (formRes.ok && formChange.changed) {
1520
+ destName = inferScreenName(formTxts, [...tapTxts, ...parentTexts], `Resultado de ${screenName}`);
1521
+ resultType = 'new_screen';
1522
+ resultDescription = `Formulario navegó a otra pantalla — sin texto explícito de éxito (similitud ${formChange.similarity.toFixed(2)})`;
1523
+ console.log(chalk.green(` ✅ Form navegó: "${destName}" (sin marcador de éxito)`));
1524
+ }
1525
+ else if (!formRes.ok) {
1526
+ destName = `${screenName} (Submit incompleto)`;
1527
+ resultType = 'partial_flow';
1528
+ resultDescription = `Maestro reportó fallo en el flow. Posible PIN/2FA, validación intermedia, o paso adicional requerido. Pantalla: ${formTxts.slice(0, 3).join(' | ') || '(sin texto)'}`;
1529
+ console.log(chalk.yellow(` ⚠️ Form submit incompleto — Maestro reportó fallo. Pantalla: ${formTxts.slice(0, 3).join(' | ') || '(desconocida)'}`));
1530
+ }
1531
+ else {
1532
+ destName = `${screenName} (Sin cambio)`;
1533
+ resultType = 'no_observable_change';
1534
+ resultDescription = `Submit no produjo cambio visible (similitud ${formChange.similarity.toFixed(2)})`;
1535
+ console.log(chalk.yellow(` ⚠️ Submit no cambió la pantalla`));
1536
+ }
1537
+ allScreens.push({
1538
+ name: destName,
1539
+ isErrorState: resultType === 'partial_flow' || resultType === 'no_observable_change',
1540
+ screenshot: formShot,
1541
+ elements: formEls,
1542
+ allTexts: formTxts,
1543
+ });
1544
+ navigationSteps.push({ from: screenName, to: destName, action: `Tap ${screenForm.submitButtonId}` });
1545
+ const formEv = buildRecord({
1546
+ sessionId: corpus.sessionId,
1547
+ screen_origin: screenName,
1548
+ action_type: 'form_submit',
1549
+ action_element: screenForm.submitButtonId,
1550
+ action_label: screenForm.submitButtonLabel,
1551
+ screenshot_before: tapShot,
1552
+ elements_before: tapUI,
1553
+ input_data: fieldData,
1554
+ screen_destination: destName,
1555
+ result_type: resultType,
1556
+ result_description: resultDescription,
1557
+ screenshot_after: formShot,
1558
+ elements_after: formUI,
1559
+ captured_by: 'interactive',
1560
+ });
1561
+ addRecord(corpus, formEv);
1562
+ }
1563
+ }
1564
+ }
1565
+ // ── Step 5: Return to parent screen for next BFS iteration ────
1566
+ // Try up to 4 backs until we reach a screen similar to the parent.
1567
+ // Some flows (form fill → comprobante → modal) require multiple backs.
1568
+ const returnedToParent = await this._smartBack(appId, maestroEnv, options.deviceId, parentUI, launchUI, loginForm, parentName);
1569
+ if (!returnedToParent) {
1570
+ console.log(chalk.yellow(` ⚠️ No se pudo regresar a "${parentName}" después de 4 backs. BFS detenido.`));
1571
+ break;
1572
+ }
453
1573
  }
454
1574
  }
1575
+ // ── Finalize evidence corpus ───────────────────────────────────────
1576
+ const finalCorpus = finalizeCorpus(corpus);
1577
+ console.log(chalk.dim(` 📦 ${corpusSummary(finalCorpus)}`));
1578
+ // Phase 11 — incremental summary
1579
+ if (mode === 'incremental' && (incrementalSkipCount + incrementalCaptureCount) > 0) {
1580
+ console.log(chalk.cyan(` 📚 Modo incremental: ${incrementalSkipCount} pantalla(s) reusadas, ` +
1581
+ `${incrementalCaptureCount} capturadas`));
1582
+ }
455
1583
  // ── Build evidence package ─────────────────────────────────────────
456
1584
  const finishedAt = Date.now();
457
1585
  const totalElements = allScreens.reduce((sum, s) => sum + s.elements.length, 0);
@@ -486,6 +1614,7 @@ export const discoveryEngine = {
486
1614
  framework: 'maestro',
487
1615
  },
488
1616
  evidence,
1617
+ corpus: finalCorpus,
489
1618
  };
490
1619
  }
491
1620
  catch (error) {
@@ -701,14 +1830,35 @@ ${elements.slice(0, 20).map((e) => {
701
1830
  * These become the candidates for the interactive exploration loop.
702
1831
  */
703
1832
  detectUnexplored(evidence) {
704
- // Collect element IDs that were explicitly tapped in navigation
1833
+ // Collect element IDs that were explicitly tapped during Discovery.
1834
+ // Matches several action formats produced by the engine:
1835
+ // - "Tap btn_xxx" (BFS Phase 0)
1836
+ // - "Tap en btn_xxx" (legacy)
1837
+ // - "Tap btn_xxx (label)" (variants)
1838
+ // - "Lanzar app" (initial launch — no element)
1839
+ // - "Submit vacío" (form validation — no element)
1840
+ // - "Login con credenciales válidas" (no element)
705
1841
  const exploredIds = new Set();
706
1842
  for (const step of evidence.metrics.navigationSteps) {
707
- const tapMatch = step.action.match(/^Tap\s+(\S+)/i);
1843
+ // Match "Tap <id>" or "Tap en <id>" — first non-stopword token after Tap
1844
+ const tapMatch = step.action.match(/^Tap(?:\s+en)?\s+(\S+)/i);
708
1845
  if (tapMatch)
709
1846
  exploredIds.add(tapMatch[1]);
710
1847
  }
711
- // Elements handled by the hardcoded login / submit flows
1848
+ // Also count elements that participated in form_submit EvidenceRecords
1849
+ // (their submit button + each field id). Those are "interacted with".
1850
+ if (evidence.records) {
1851
+ for (const rec of evidence.records ?? []) {
1852
+ if (rec.action_element)
1853
+ exploredIds.add(rec.action_element);
1854
+ if (rec.input_data) {
1855
+ for (const fid of Object.keys(rec.input_data))
1856
+ exploredIds.add(fid);
1857
+ }
1858
+ }
1859
+ }
1860
+ // Login/submit elements that the generic flow always handles
1861
+ // (kept for legacy compat; modern flow uses detectLoginForm dynamically)
712
1862
  const ALWAYS_HANDLED = new Set([
713
1863
  'btn_login', 'btn_confirm', 'et_username', 'et_password',
714
1864
  'btn_submit', 'btn_register', 'btn_back',
@@ -727,11 +1877,22 @@ ${elements.slice(0, 20).map((e) => {
727
1877
  continue;
728
1878
  if (ALWAYS_HANDLED.has(elem.id))
729
1879
  continue;
730
- // Only Button / ImageButton are worth suggesting for exploration.
731
- // TextViews and FrameLayouts might be clickable but we can't tell from type alone —
732
- // those that matter get picked up via the first tap anyway.
733
- const isClickable = ['Button', 'ImageButton'].includes(elem.type);
734
- if (!isClickable)
1880
+ // Skip pure inputs (EditText, CheckBox, etc.) — those are filled, not navigated
1881
+ if (['EditText', 'CheckBox', 'RadioButton', 'Switch'].includes(elem.type))
1882
+ continue;
1883
+ // Use the same heuristic as the BFS: looksLikeButton recognizes
1884
+ // TextView with id `btn_*` or visible text like "Continuar" as
1885
+ // navigable buttons (Phase 0 — apps with custom UI like Scotiabank).
1886
+ // We need to convert the legacy `{ type, text, id }` shape to the
1887
+ // UIElement shape that looksLikeButton expects.
1888
+ const uiElem = {
1889
+ elementId: elem.id,
1890
+ elementType: elem.type,
1891
+ visibleText: elem.text !== 'N/A' ? elem.text : undefined,
1892
+ isInteractive: false,
1893
+ evidenceRef: '',
1894
+ };
1895
+ if (!looksLikeButton(uiElem))
735
1896
  continue;
736
1897
  seenIds.add(elem.id);
737
1898
  unexplored.push({
@@ -748,7 +1909,7 @@ ${elements.slice(0, 20).map((e) => {
748
1909
  * Convert a navigation step action string into the Maestro YAML steps
749
1910
  * needed to perform that action.
750
1911
  */
751
- _getActionYaml(action, creds) {
1912
+ _getActionYaml(action, creds, isIos = false) {
752
1913
  if (action === 'Lanzar app') {
753
1914
  return [
754
1915
  `- launchApp:\n clearState: true`,
@@ -763,7 +1924,7 @@ ${elements.slice(0, 20).map((e) => {
763
1924
  `- waitForAnimationToEnd`,
764
1925
  `- hideKeyboard`,
765
1926
  `- tapOn:\n id: "et_password"`,
766
- `- eraseText: 30`,
1927
+ `- eraseText: 50`,
767
1928
  `- inputText: "${creds.password}"`,
768
1929
  `- waitForAnimationToEnd`,
769
1930
  `- hideKeyboard`,
@@ -774,7 +1935,7 @@ ${elements.slice(0, 20).map((e) => {
774
1935
  const tapMatch = action.match(/^Tap\s+(\S+)/i);
775
1936
  if (tapMatch) {
776
1937
  return [
777
- `- tapOn:\n id: "${tapMatch[1]}"`,
1938
+ `- tapOn:\n ${buildMaestroSelector(tapMatch[1], isIos)}`,
778
1939
  `- waitForAnimationToEnd`,
779
1940
  ];
780
1941
  }
@@ -785,7 +1946,7 @@ ${elements.slice(0, 20).map((e) => {
785
1946
  * navigates from the app's initial state to `targetScreen`.
786
1947
  * Returns true if navigation succeeded, false otherwise.
787
1948
  */
788
- async _navigateToScreen(targetScreen, evidence, creds, appId, maestroEnv) {
1949
+ async _navigateToScreen(targetScreen, evidence, creds, appId, maestroEnv, isIos = false) {
789
1950
  // Build adjacency list — skip edges that lead to error states
790
1951
  const graph = new Map();
791
1952
  for (const step of evidence.metrics.navigationSteps) {
@@ -808,13 +1969,13 @@ ${elements.slice(0, 20).map((e) => {
808
1969
  if (screen === targetScreen) {
809
1970
  if (actions.length === 0)
810
1971
  return true;
811
- return this._runMaestroFlow(appId, actions, maestroEnv);
1972
+ return this._runMaestroFlow(appId, actions, maestroEnv).ok;
812
1973
  }
813
1974
  if (visited.has(screen))
814
1975
  continue;
815
1976
  visited.add(screen);
816
1977
  for (const edge of (graph.get(screen) || [])) {
817
- const stepYaml = this._getActionYaml(edge.action, creds);
1978
+ const stepYaml = this._getActionYaml(edge.action, creds, isIos);
818
1979
  queue.push({ screen: edge.to, actions: [...actions, ...stepYaml] });
819
1980
  }
820
1981
  }
@@ -830,7 +1991,7 @@ ${elements.slice(0, 20).map((e) => {
830
1991
  *
831
1992
  * Returns the new ScreenEvidence entries that were added.
832
1993
  */
833
- async exploreElement(element, options, evidence) {
1994
+ async exploreElement(element, options, evidence, corpus) {
834
1995
  const appId = options.url;
835
1996
  const adb = resolveAdb();
836
1997
  const adbS = options.deviceId ? `"${adb}" -s ${options.deviceId}` : `"${adb}"`;
@@ -839,26 +2000,30 @@ ${elements.slice(0, 20).map((e) => {
839
2000
  : process.env;
840
2001
  const creds = { username: options.username || '', password: options.password || '' };
841
2002
  const added = [];
2003
+ const isIos = !!options.deviceId && /iPhone|iPad/i.test(options.deviceId);
842
2004
  console.log(chalk.gray(` 🔭 Navegando a "${element.sourceScreen}"...`));
843
2005
  // 1. Navigate to the source screen from app start
844
- const navOk = await this._navigateToScreen(element.sourceScreen, evidence, creds, appId, maestroEnv);
2006
+ const navOk = await this._navigateToScreen(element.sourceScreen, evidence, creds, appId, maestroEnv, isIos);
845
2007
  if (!navOk) {
846
2008
  console.log(chalk.yellow(` ⚠️ No se pudo navegar a "${element.sourceScreen}"`));
847
2009
  return [];
848
2010
  }
849
2011
  await sleep(2000); // wait for screen to settle
850
- // 2. Tap the element
2012
+ // 2. Capture before state for EvidenceRecord
2013
+ const sourceScreenData = evidence.screens.find(s => s.name === element.sourceScreen);
2014
+ const screenshotBefore = await this._captureScreenshotMobile(adbS, `explore_${element.elementId}_before`);
2015
+ // 3. Tap the element
851
2016
  console.log(chalk.gray(` 👆 Tap en ${element.elementId}...`));
852
2017
  const tapOk = this._runMaestroFlow(appId, [
853
- `- tapOn:\n id: "${element.elementId}"`,
2018
+ `- tapOn:\n ${buildMaestroSelector(element.elementId, isIos)}`,
854
2019
  `- waitForAnimationToEnd:\n timeout: 3000`,
855
- ], maestroEnv);
2020
+ ], maestroEnv).ok;
856
2021
  if (!tapOk) {
857
2022
  console.log(chalk.yellow(` ⚠️ Tap en ${element.elementId} falló`));
858
2023
  return [];
859
2024
  }
860
2025
  await sleep(2000); // wait for the new screen to render
861
- // 3. Capture resulting state
2026
+ // 4. Capture resulting state
862
2027
  let hierarchy;
863
2028
  try {
864
2029
  hierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
@@ -871,7 +2036,6 @@ ${elements.slice(0, 20).map((e) => {
871
2036
  const newElements = this.extractMaestroSelectors(hierarchy);
872
2037
  const newTexts = this.extractAllTexts(hierarchy);
873
2038
  // Decide if this looks like a new screen
874
- const sourceScreenData = evidence.screens.find(s => s.name === element.sourceScreen);
875
2039
  const sourceIds = new Set((sourceScreenData?.elements || []).map(e => e.id));
876
2040
  const novelElements = newElements.filter(e => !sourceIds.has(e.id));
877
2041
  if (novelElements.length < 2 && !screenshot) {
@@ -904,21 +2068,123 @@ ${elements.slice(0, 20).map((e) => {
904
2068
  });
905
2069
  evidence.metrics.screensDiscovered++;
906
2070
  evidence.metrics.elementsFound += novelElements.length;
2071
+ // 5. Phase 13 — Real-time Sync (Assisted/Manual mode visibility)
2072
+ // Upload this step to the SaaS immediately so the user sees progress in the Evidence tab.
2073
+ try {
2074
+ const ui = legacyElementsToUIElements(newElements, '__pending__');
2075
+ const { screenFingerprint } = await import('./evidence-corpus.js');
2076
+ const fp = screenFingerprint(ui, newTexts);
2077
+ if (!options.projectId || !options.entityId)
2078
+ throw new Error('sin --project/--entity: paso no subido');
2079
+ await FlowSpecQAApi.uploadManualCapture({
2080
+ project_id: options.projectId,
2081
+ entity_id: options.entityId,
2082
+ label: `Step: Tap ${element.elementId} -> ${screenName}`,
2083
+ screen_name: screenName,
2084
+ elements: ui,
2085
+ texts: newTexts,
2086
+ screenshot_b64: screenshot || undefined,
2087
+ screen_fingerprint: fp,
2088
+ platform: options.deviceId?.includes('-') ? 'ios' : 'android', // Simple heuristic
2089
+ });
2090
+ }
2091
+ catch (err) {
2092
+ console.log(chalk.yellow(` ⚠️ No se pudo sincronizar evidencia con SaaS: ${err}`));
2093
+ }
907
2094
  // Track verified IDs from the new screen
908
2095
  for (const e of newElements) {
909
2096
  if (e.id && !evidence.verifiedIds.includes(e.id))
910
2097
  evidence.verifiedIds.push(e.id);
911
2098
  }
912
2099
  console.log(chalk.green(` ✅ Nueva pantalla: "${screenName}" | ${novelElements.length} elementos nuevos${screenshot ? ' | screenshot ✓' : ''}`));
2100
+ // ── EvidenceRecord — Tap this element ─────────────────────────────────
2101
+ if (corpus) {
2102
+ const exploreRecord = buildRecord({
2103
+ sessionId: corpus.sessionId,
2104
+ screen_origin: element.sourceScreen,
2105
+ action_type: 'tap',
2106
+ action_element: element.elementId,
2107
+ action_label: element.elementText || undefined,
2108
+ screenshot_before: screenshotBefore,
2109
+ elements_before: legacyElementsToUIElements(sourceScreenData?.elements || [], '__pending__'),
2110
+ screen_destination: screenName,
2111
+ result_type: 'new_screen',
2112
+ result_description: `Pantalla nueva: ${novelElements.length} elementos noveles`,
2113
+ screenshot_after: screenshot,
2114
+ elements_after: legacyElementsToUIElements(newElements, '__pending__'),
2115
+ captured_by: 'autonomous',
2116
+ });
2117
+ addRecord(corpus, exploreRecord);
2118
+ // Form detection on the new screen
2119
+ const newUIElements = legacyElementsToUIElements(newElements, exploreRecord.evidenceId);
2120
+ const detectedForm = detectForm(newUIElements, screenName);
2121
+ if (detectedForm && options.onFormDetected) {
2122
+ console.log(chalk.cyan(`\n 📋 Formulario en "${screenName}": ${detectedForm.fields.length} campos`));
2123
+ const fieldData = await options.onFormDetected(detectedForm);
2124
+ if (fieldData) {
2125
+ const fillSteps = [];
2126
+ for (const [fieldId, value] of Object.entries(fieldData)) {
2127
+ fillSteps.push(`- tapOn:\n ${buildMaestroSelector(fieldId, isIos)}`, `- eraseText: 50`, `- inputText: "${value}"`, `- hideKeyboard`);
2128
+ }
2129
+ fillSteps.push(`- tapOn:\n ${buildMaestroSelector(detectedForm.submitButtonId, isIos)}`, `- waitForAnimationToEnd:\n timeout: 5000`);
2130
+ const formOk = this._runMaestroFlow(appId, fillSteps, maestroEnv).ok;
2131
+ if (formOk) {
2132
+ await sleep(2500);
2133
+ const formResultShot = await this._captureScreenshotMobile(adbS, `explore_${element.elementId}_form`);
2134
+ let formHierarchy;
2135
+ try {
2136
+ formHierarchy = await this._captureHierarchy(maestroEnv, options.deviceId);
2137
+ }
2138
+ catch { }
2139
+ const formResultElems = formHierarchy ? this.extractMaestroSelectors(formHierarchy) : [];
2140
+ const formResultTexts = formHierarchy ? this.extractAllTexts(formHierarchy) : [];
2141
+ const isSuccess = formResultTexts.some(t => /comprobante|éxito|exitosa|confirmado|ok/i.test(t));
2142
+ const formDest = isSuccess ? `${screenName} (Éxito)` : `${screenName} (resultado)`;
2143
+ const formResult = 'new_screen'; // success or generic navigation both produce a new screen
2144
+ const formRecord = buildRecord({
2145
+ sessionId: corpus.sessionId,
2146
+ screen_origin: screenName,
2147
+ action_type: 'form_submit',
2148
+ action_element: detectedForm.submitButtonId,
2149
+ action_label: detectedForm.submitButtonLabel,
2150
+ screenshot_before: screenshot,
2151
+ elements_before: newUIElements,
2152
+ input_data: fieldData,
2153
+ screen_destination: formDest,
2154
+ result_type: formResult,
2155
+ result_description: `Formulario interactivo — resultado: ${formResult}`,
2156
+ screenshot_after: formResultShot,
2157
+ elements_after: legacyElementsToUIElements(formResultElems, '__pending__'),
2158
+ captured_by: 'interactive',
2159
+ });
2160
+ addRecord(corpus, formRecord);
2161
+ const formScreen = {
2162
+ name: formDest,
2163
+ isErrorState: !isSuccess,
2164
+ screenshot: formResultShot,
2165
+ elements: formResultElems,
2166
+ allTexts: formResultTexts,
2167
+ };
2168
+ added.push(formScreen);
2169
+ evidence.screens.push(formScreen);
2170
+ evidence.metrics.navigationSteps.push({
2171
+ from: screenName, to: formDest,
2172
+ action: `Form submit interactivo (${detectedForm.submitButtonId})`,
2173
+ });
2174
+ console.log(chalk.green(` ✅ Formulario interactivo: "${formDest}"`));
2175
+ }
2176
+ }
2177
+ }
2178
+ }
913
2179
  // 5. Try to trigger error state on new screen (tap first submit-like button without filling fields)
914
2180
  const submitBtn = newElements.find(e => ['btn_confirm', 'btn_submit', 'btn_pay', 'btn_send', 'btn_continue', 'btn_accept'].includes(e.id) ||
915
2181
  (e.type === 'Button' && e.id !== element.elementId));
916
2182
  if (submitBtn) {
917
2183
  console.log(chalk.gray(` 📸 Intentando error state (${submitBtn.id})...`));
918
2184
  const errorOk = this._runMaestroFlow(appId, [
919
- `- tapOn:\n id: "${submitBtn.id}"`,
2185
+ `- tapOn:\n ${buildMaestroSelector(submitBtn.id, isIos)}`,
920
2186
  `- waitForAnimationToEnd:\n timeout: 1500`,
921
- ], maestroEnv);
2187
+ ], maestroEnv).ok;
922
2188
  if (errorOk) {
923
2189
  await sleep(1500);
924
2190
  try {
@@ -1054,29 +2320,72 @@ ${flows.join('\n\n')}
1054
2320
  'android.widget.Switch',
1055
2321
  'android.widget.ImageButton',
1056
2322
  'android.widget.TextView',
2323
+ 'android.view.ViewGroup', // Some apps use ViewGroups as buttons
1057
2324
  ];
2325
+ // Known system/overlay elements that should never be explored
2326
+ const SYSTEM_BLACKLIST = [
2327
+ 'trigger_container', // Samsung Edge Panels
2328
+ 'navigation_bar_frame', // Android nav bar
2329
+ 'status_bar_container', // Android status bar
2330
+ 'com.android.systemui', // System UI elements
2331
+ ];
2332
+ // Track the last visible text label seen during traversal.
2333
+ // Used to generate `below:` selectors for EditText fields that
2334
+ // lack their own resource-id, text, and hintText (e.g. "RUT", "Clave").
2335
+ let lastTextLabel = '';
2336
+ let editTextCount = 0;
1058
2337
  const traverse = (current) => {
1059
2338
  if (!current || typeof current !== 'object')
1060
2339
  return;
1061
2340
  const attrs = current.attributes || {};
1062
2341
  const resourceId = attrs['resource-id'] || '';
1063
- const text = attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '';
2342
+ const text = (attrs['text'] || attrs['accessibilityText'] || attrs['hintText'] || '').trim();
1064
2343
  const cls = attrs['class'] || '';
1065
2344
  const clickable = attrs['clickable'] === 'true';
1066
- const isAppElement = resourceId && !resourceId.startsWith('android:id/') && !resourceId.startsWith('com.android.');
2345
+ const isEditText = cls.includes('EditText') || cls.includes('Input');
2346
+ if (isEditText) {
2347
+ editTextCount++;
2348
+ }
2349
+ // Non-interactive text elements (labels) update the context tracker
2350
+ const isTextView = cls.includes('TextView');
2351
+ if (isTextView && !clickable && text.length > 1) {
2352
+ lastTextLabel = text.substring(0, 40).trim();
2353
+ }
2354
+ const shortId = resourceId.includes('/') ? resourceId.split('/')[1] : resourceId;
2355
+ const isBlacklisted = SYSTEM_BLACKLIST.some(b => resourceId.includes(b));
2356
+ const isSystemId = resourceId.startsWith('android:id/') || resourceId.startsWith('com.android.');
1067
2357
  const isInteractive = INTERACTIVE_CLASSES.includes(cls) || clickable;
1068
- if (isAppElement && isInteractive) {
1069
- elements.push({
1070
- type: cls.split('.').pop() || cls,
1071
- text: text || 'N/A',
1072
- id: resourceId.includes('/') ? resourceId.split('/')[1] : resourceId,
1073
- });
2358
+ const hasIdentifier = (resourceId && !isSystemId) || text.length > 1 || isEditText;
2359
+ if (!isBlacklisted && isInteractive && hasIdentifier) {
2360
+ let finalId = '';
2361
+ if (resourceId && !isSystemId) {
2362
+ finalId = shortId;
2363
+ }
2364
+ else if (text.length > 1) {
2365
+ finalId = `text:${text.substring(0, 64).trim()}`;
2366
+ }
2367
+ else if (isEditText && lastTextLabel) {
2368
+ // Use the nearest preceding label as a `below:` selector
2369
+ finalId = `below:${lastTextLabel}`;
2370
+ }
2371
+ else if (isEditText) {
2372
+ // Last resort: use index (may not work in all Maestro versions)
2373
+ finalId = `below:Campo ${editTextCount}`;
2374
+ }
2375
+ if (finalId) {
2376
+ elements.push({
2377
+ type: cls.split('.').pop() || cls,
2378
+ text: text || lastTextLabel || 'N/A',
2379
+ id: finalId,
2380
+ clickable,
2381
+ });
2382
+ }
1074
2383
  }
1075
2384
  for (const child of (current.children || []))
1076
2385
  traverse(child);
1077
2386
  };
1078
2387
  traverse(node);
1079
- return elements.slice(0, 30);
2388
+ return elements.slice(0, 40); // Increased limit to capture more elements
1080
2389
  },
1081
2390
  };
1082
2391
  //# sourceMappingURL=discovery.js.map