deflake 1.2.38 → 1.2.40

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.
Files changed (2) hide show
  1. package/cli.js +55 -14
  2. package/package.json +1 -1
package/cli.js CHANGED
@@ -54,29 +54,35 @@ async function main() {
54
54
 
55
55
  // Save backups of all source files before any modifications
56
56
  const backups = new Map(); // path -> original content
57
- let prevFailCount = Infinity;
57
+ let prevFailNames = new Set(); // Track specific failures by name
58
58
 
59
59
  for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
60
60
  console.log(`\n${C.BRIGHT}${C.CYAN}🔄 Fix attempt ${attempt}/${MAX_ATTEMPTS}${C.RESET}`);
61
61
 
62
62
  const artifacts = detectFailures();
63
- const currentFailCount = artifacts.length;
63
+ const currentNames = new Set(artifacts.map(a => a.name));
64
64
 
65
- if (currentFailCount === 0) {
65
+ if (artifacts.length === 0) {
66
66
  console.log(`${C.GREEN}🎉 All tests passing! No failures detected.${C.RESET}`);
67
67
  break;
68
68
  }
69
69
 
70
- // If failures INCREASED since last attempt, rollback all changes
71
- if (attempt > 1 && currentFailCount > prevFailCount) {
72
- console.log(`${C.RED}âš ī¸ Failures increased (${prevFailCount} → ${currentFailCount}). Rolling back all changes...${C.RESET}`);
73
- for (const [filePath, originalContent] of backups) {
74
- fs.writeFileSync(filePath, originalContent);
75
- console.log(` ${C.YELLOW}â†Šī¸ Restored: ${path.basename(filePath)}${C.RESET}`);
70
+ // Check for NEW failures (tests that weren't failing before)
71
+ if (attempt > 1) {
72
+ const newFailures = [...currentNames].filter(n => !prevFailNames.has(n));
73
+ if (newFailures.length > 0) {
74
+ console.log(`${C.RED}âš ī¸ New failures detected after fix attempt! Rolling back all changes...${C.RESET}`);
75
+ for (const name of newFailures) {
76
+ console.log(` ${C.RED}🆕 ${name}${C.RESET}`);
77
+ }
78
+ for (const [filePath, originalContent] of backups) {
79
+ fs.writeFileSync(filePath, originalContent);
80
+ console.log(` ${C.YELLOW}â†Šī¸ Restored: ${path.basename(filePath)}${C.RESET}`);
81
+ }
82
+ break;
76
83
  }
77
- break;
78
84
  }
79
- prevFailCount = currentFailCount;
85
+ prevFailNames = currentNames;
80
86
 
81
87
  const applied = await analyzeAndFix(artifacts, client, argv, output, backups);
82
88
 
@@ -333,9 +339,44 @@ async function applyFix(res, loc, backups = new Map()) {
333
339
  lines.splice(p.line, 0, cleanLine);
334
340
  console.log(` ${C.GREEN}+ Line ${p.line + 1}: ${cleanLine.trim()}${C.RESET}`);
335
341
  } else {
336
- lines[idx] = cleanLine;
337
- console.log(` ${C.RED}- Line ${p.line}: ${originalLine.trim()}${C.RESET}`);
338
- console.log(` ${C.GREEN}+ Line ${p.line}: ${cleanLine.trim()}${C.RESET}`);
342
+ // REPLACE action
343
+ // SMART: Handle multi-line expression replacement
344
+ // If the original line starts a multi-line expression (has unclosed parens)
345
+ // and the replacement is a complete statement, remove continuation lines
346
+ const origOpen = (originalLine.match(/\(/g) || []).length;
347
+ const origClose = (originalLine.match(/\)/g) || []).length;
348
+ const replOpen = (cleanLine.match(/\(/g) || []).length;
349
+ const replClose = (cleanLine.match(/\)/g) || []).length;
350
+
351
+ if (origOpen > origClose && replOpen <= replClose) {
352
+ // Original has unclosed parens, replacement is complete
353
+ // Find the closing of the multi-line expression
354
+ let depth = origOpen - origClose;
355
+ let endIdx = idx;
356
+ for (let j = idx + 1; j < lines.length && depth > 0; j++) {
357
+ const lo = (lines[j].match(/\(/g) || []).length;
358
+ const lc = (lines[j].match(/\)/g) || []).length;
359
+ depth += lo - lc;
360
+ endIdx = j;
361
+ }
362
+ if (endIdx > idx) {
363
+ const removedLines = endIdx - idx;
364
+ console.log(` ${C.GRAY}🔧 Expanding REPLACE to cover ${removedLines + 1}-line expression (lines ${p.line}-${endIdx + 1})${C.RESET}`);
365
+ for (let j = idx; j <= endIdx; j++) {
366
+ console.log(` ${C.RED}- Line ${j + 1}: ${lines[j].trim()}${C.RESET}`);
367
+ }
368
+ lines.splice(idx, endIdx - idx + 1, cleanLine);
369
+ console.log(` ${C.GREEN}+ Line ${p.line}: ${cleanLine.trim()}${C.RESET}`);
370
+ } else {
371
+ lines[idx] = cleanLine;
372
+ console.log(` ${C.RED}- Line ${p.line}: ${originalLine.trim()}${C.RESET}`);
373
+ console.log(` ${C.GREEN}+ Line ${p.line}: ${cleanLine.trim()}${C.RESET}`);
374
+ }
375
+ } else {
376
+ lines[idx] = cleanLine;
377
+ console.log(` ${C.RED}- Line ${p.line}: ${originalLine.trim()}${C.RESET}`);
378
+ console.log(` ${C.GREEN}+ Line ${p.line}: ${cleanLine.trim()}${C.RESET}`);
379
+ }
339
380
  }
340
381
  content = lines.join('\n');
341
382
  appliedCount++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "deflake",
3
- "version": "1.2.38",
3
+ "version": "1.2.40",
4
4
  "description": "AI-powered self-healing tool for Playwright, Cypress, and WebdriverIO tests.",
5
5
  "main": "client.js",
6
6
  "bin": {