|
| 1 | +// GH #119: cascading-selector hardening. When auto-repair patches |
| 2 | +// selector A→A' and the retry then fails on a DIFFERENT selector B, |
| 3 | +// AutoRepairOutcome.nextFailedSelector captures B so MTTR can |
| 4 | +// distinguish "patch didn't work" from "patch worked, next selector |
| 5 | +// broke." |
| 6 | +import { test, mock } from 'node:test'; |
| 7 | +import assert from 'node:assert/strict'; |
| 8 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'; |
| 9 | +import { join } from 'node:path'; |
| 10 | +import { tmpdir } from 'node:os'; |
| 11 | + |
| 12 | +const RUN_ACTION_PATH = '../../dist/tools/run-action.js'; |
| 13 | + |
| 14 | +function makeProject() { |
| 15 | + const root = mkdtempSync(join(tmpdir(), 'gh119-')); |
| 16 | + mkdirSync(join(root, '.rn-agent', 'actions'), { recursive: true }); |
| 17 | + writeFileSync(join(root, '.rn-agent', 'actions', 'sample.yaml'), [ |
| 18 | + 'appId: com.test.app', |
| 19 | + '---', |
| 20 | + '# id: sample', |
| 21 | + '# intent: a sample', |
| 22 | + '# status: experimental', |
| 23 | + '# mutates: false', |
| 24 | + '- launchApp', |
| 25 | + '- tapOn:', |
| 26 | + ' id: "btn-A"', |
| 27 | + ].join('\n')); |
| 28 | + return root; |
| 29 | +} |
| 30 | + |
| 31 | +test('AutoRepairOutcome.nextFailedSelector populated when retry fails on a different selector', async () => { |
| 32 | + const { createRunActionHandler } = await import(RUN_ACTION_PATH); |
| 33 | + const root = makeProject(); |
| 34 | + |
| 35 | + // First call: maestro fails with SELECTOR_NOT_FOUND on selector "btn-A". |
| 36 | + // Second call (post-repair retry): maestro fails on a DIFFERENT selector |
| 37 | + // "btn-B" → cascading failure. |
| 38 | + let mCount = 0; |
| 39 | + const fakeMaestroRun = mock.fn(async () => { |
| 40 | + mCount++; |
| 41 | + if (mCount === 1) { |
| 42 | + return { |
| 43 | + content: [{ type: 'text', text: JSON.stringify({ |
| 44 | + ok: true, |
| 45 | + data: { passed: false, output: '== Element with id "btn-A" not found ==' }, |
| 46 | + }) }], |
| 47 | + }; |
| 48 | + } |
| 49 | + // Retry: also fails, but on a different selector |
| 50 | + return { |
| 51 | + content: [{ type: 'text', text: JSON.stringify({ |
| 52 | + ok: true, |
| 53 | + data: { passed: false, output: '== Element with id "btn-B" not found ==' }, |
| 54 | + }) }], |
| 55 | + }; |
| 56 | + }); |
| 57 | + |
| 58 | + const fakeRepairAction = mock.fn(async () => ({ |
| 59 | + content: [{ type: 'text', text: JSON.stringify({ |
| 60 | + ok: true, |
| 61 | + data: { patched: true, oldSelector: 'btn-A', newSelector: 'btn-A-new', score: 0.9 }, |
| 62 | + meta: { repairTimestamp: new Date().toISOString() }, |
| 63 | + }) }], |
| 64 | + })); |
| 65 | + |
| 66 | + const handler = createRunActionHandler({ maestroRun: fakeMaestroRun, repairAction: fakeRepairAction }); |
| 67 | + const result = await handler({ |
| 68 | + actionId: 'sample', |
| 69 | + projectRoot: root, |
| 70 | + platform: 'ios', |
| 71 | + autoRepair: true, |
| 72 | + }); |
| 73 | + |
| 74 | + // Result should be a failResult with the autoRepair meta |
| 75 | + assert.equal(result.isError, true); |
| 76 | + const env = JSON.parse(result.content[0].text); |
| 77 | + // The autoRepair payload is in meta (failResult shape) |
| 78 | + const autoRepair = env.meta?.autoRepair ?? env.data?.autoRepair; |
| 79 | + assert.ok(autoRepair, `expected autoRepair in envelope; got ${result.content[0].text.slice(0, 300)}`); |
| 80 | + assert.equal(autoRepair.outcome, 'failed'); |
| 81 | + // The fix's contract: when retry fails on a different selector, capture it |
| 82 | + assert.equal(autoRepair.nextFailedSelector, 'btn-B', |
| 83 | + `nextFailedSelector should be the retry's failed selector; got ${autoRepair.nextFailedSelector}`); |
| 84 | + // The original diff stays unchanged |
| 85 | + assert.equal(autoRepair.diff?.selector?.from, 'btn-A'); |
| 86 | + assert.equal(autoRepair.diff?.selector?.to, 'btn-A-new'); |
| 87 | + |
| 88 | + rmSync(root, { recursive: true, force: true }); |
| 89 | +}); |
| 90 | + |
| 91 | +test('AutoRepairOutcome.nextFailedSelector NOT populated when retry fails on the SAME selector (patch did not work)', async () => { |
| 92 | + const { createRunActionHandler } = await import(RUN_ACTION_PATH); |
| 93 | + const root = makeProject(); |
| 94 | + |
| 95 | + let mCount = 0; |
| 96 | + const fakeMaestroRun = mock.fn(async () => { |
| 97 | + mCount++; |
| 98 | + if (mCount === 1) { |
| 99 | + return { |
| 100 | + content: [{ type: 'text', text: JSON.stringify({ |
| 101 | + ok: true, |
| 102 | + data: { passed: false, output: '== Element with id "btn-A" not found ==' }, |
| 103 | + }) }], |
| 104 | + }; |
| 105 | + } |
| 106 | + // Retry fails on the SAME (newly patched) selector — patch didn't work |
| 107 | + return { |
| 108 | + content: [{ type: 'text', text: JSON.stringify({ |
| 109 | + ok: true, |
| 110 | + data: { passed: false, output: '== Element with id "btn-A-new" not found ==' }, |
| 111 | + }) }], |
| 112 | + }; |
| 113 | + }); |
| 114 | + |
| 115 | + const fakeRepairAction = mock.fn(async () => ({ |
| 116 | + content: [{ type: 'text', text: JSON.stringify({ |
| 117 | + ok: true, |
| 118 | + data: { patched: true, oldSelector: 'btn-A', newSelector: 'btn-A-new' }, |
| 119 | + meta: { repairTimestamp: new Date().toISOString() }, |
| 120 | + }) }], |
| 121 | + })); |
| 122 | + |
| 123 | + const handler = createRunActionHandler({ maestroRun: fakeMaestroRun, repairAction: fakeRepairAction }); |
| 124 | + const result = await handler({ |
| 125 | + actionId: 'sample', |
| 126 | + projectRoot: root, |
| 127 | + platform: 'ios', |
| 128 | + autoRepair: true, |
| 129 | + }); |
| 130 | + |
| 131 | + const env = JSON.parse(result.content[0].text); |
| 132 | + const autoRepair = env.meta?.autoRepair ?? env.data?.autoRepair; |
| 133 | + assert.ok(autoRepair); |
| 134 | + assert.equal(autoRepair.outcome, 'failed'); |
| 135 | + // No cascading selector — should be absent |
| 136 | + assert.equal(autoRepair.nextFailedSelector, undefined, |
| 137 | + `nextFailedSelector should NOT be present when same selector failed; got ${autoRepair.nextFailedSelector}`); |
| 138 | + |
| 139 | + rmSync(root, { recursive: true, force: true }); |
| 140 | +}); |
| 141 | + |
| 142 | +test('AutoRepairOutcome.nextFailedSelector NOT populated when retry passed (happy repair path)', async () => { |
| 143 | + const { createRunActionHandler } = await import(RUN_ACTION_PATH); |
| 144 | + const root = makeProject(); |
| 145 | + |
| 146 | + let mCount = 0; |
| 147 | + const fakeMaestroRun = mock.fn(async () => { |
| 148 | + mCount++; |
| 149 | + if (mCount === 1) { |
| 150 | + return { |
| 151 | + content: [{ type: 'text', text: JSON.stringify({ |
| 152 | + ok: true, |
| 153 | + data: { passed: false, output: '== Element with id "btn-A" not found ==' }, |
| 154 | + }) }], |
| 155 | + }; |
| 156 | + } |
| 157 | + return { |
| 158 | + content: [{ type: 'text', text: JSON.stringify({ ok: true, data: { passed: true, output: 'pass' } }) }], |
| 159 | + }; |
| 160 | + }); |
| 161 | + |
| 162 | + const fakeRepairAction = mock.fn(async () => ({ |
| 163 | + content: [{ type: 'text', text: JSON.stringify({ |
| 164 | + ok: true, |
| 165 | + data: { patched: true, oldSelector: 'btn-A', newSelector: 'btn-A-new' }, |
| 166 | + meta: { repairTimestamp: new Date().toISOString() }, |
| 167 | + }) }], |
| 168 | + })); |
| 169 | + |
| 170 | + const handler = createRunActionHandler({ maestroRun: fakeMaestroRun, repairAction: fakeRepairAction }); |
| 171 | + const result = await handler({ |
| 172 | + actionId: 'sample', |
| 173 | + projectRoot: root, |
| 174 | + platform: 'ios', |
| 175 | + autoRepair: true, |
| 176 | + }); |
| 177 | + |
| 178 | + assert.equal(result.isError, undefined); |
| 179 | + const env = JSON.parse(result.content[0].text); |
| 180 | + const autoRepair = env.data.autoRepair; |
| 181 | + assert.equal(autoRepair.outcome, 'passed'); |
| 182 | + assert.equal(autoRepair.nextFailedSelector, undefined); |
| 183 | + |
| 184 | + rmSync(root, { recursive: true, force: true }); |
| 185 | +}); |
0 commit comments