|
| 1 | +import { |
| 2 | + decodePngDimensionsFromBase64, |
| 3 | + parseUiRepairCandidate, |
| 4 | + validateUiRepairCandidate, |
| 5 | +} from './agent.desktop-repair'; |
| 6 | + |
| 7 | +describe('agent.desktop-repair', () => { |
| 8 | + describe('parseUiRepairCandidate', () => { |
| 9 | + it('parses valid JSON with rationale_code', () => { |
| 10 | + const res = parseUiRepairCandidate( |
| 11 | + JSON.stringify({ |
| 12 | + x: 10, |
| 13 | + y: 20, |
| 14 | + confidence: 0.9, |
| 15 | + rationale_code: 'CLOSE_X', |
| 16 | + }), |
| 17 | + ); |
| 18 | + expect(res.ok).toBe(true); |
| 19 | + if (res.ok) { |
| 20 | + expect(res.candidate.rationaleCode).toBe('CLOSE_X'); |
| 21 | + expect(res.candidate.x).toBe(10); |
| 22 | + expect(res.candidate.y).toBe(20); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + it('rejects missing JSON', () => { |
| 27 | + const res = parseUiRepairCandidate('nope'); |
| 28 | + expect(res.ok).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + it('rejects invalid rationale_code', () => { |
| 32 | + const res = parseUiRepairCandidate( |
| 33 | + JSON.stringify({ |
| 34 | + x: 10, |
| 35 | + y: 20, |
| 36 | + confidence: 0.9, |
| 37 | + rationale_code: 'DELETE_ALL', |
| 38 | + }), |
| 39 | + ); |
| 40 | + expect(res.ok).toBe(false); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('decodePngDimensionsFromBase64', () => { |
| 45 | + it('extracts width/height from a tiny PNG', () => { |
| 46 | + // 1x1 transparent PNG |
| 47 | + const base64 = |
| 48 | + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PF2K1QAAAABJRU5ErkJggg=='; |
| 49 | + const dims = decodePngDimensionsFromBase64(base64); |
| 50 | + expect(dims).toEqual({ width: 1, height: 1 }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('validateUiRepairCandidate', () => { |
| 55 | + it('accepts CLOSE_X in top-right region across multiple viewport sizes', () => { |
| 56 | + const viewports = [ |
| 57 | + { width: 100, height: 100 }, |
| 58 | + { width: 800, height: 600 }, |
| 59 | + { width: 1366, height: 768 }, |
| 60 | + { width: 1920, height: 1080 }, |
| 61 | + ]; |
| 62 | + |
| 63 | + for (const vp of viewports) { |
| 64 | + const res = validateUiRepairCandidate({ |
| 65 | + candidate: { |
| 66 | + x: Math.floor(vp.width * 0.9), |
| 67 | + y: Math.floor(vp.height * 0.1), |
| 68 | + confidence: 0.9, |
| 69 | + rationaleCode: 'CLOSE_X', |
| 70 | + }, |
| 71 | + dimensions: vp, |
| 72 | + minConfidence: 0.7, |
| 73 | + }); |
| 74 | + expect(res.ok).toBe(true); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + it('rejects CLOSE_X outside top-right region', () => { |
| 79 | + const res = validateUiRepairCandidate({ |
| 80 | + candidate: { x: 10, y: 90, confidence: 0.9, rationaleCode: 'CLOSE_X' }, |
| 81 | + dimensions: { width: 100, height: 100 }, |
| 82 | + minConfidence: 0.7, |
| 83 | + }); |
| 84 | + expect(res.ok).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + it('accepts DISMISS_BUTTON only in conservative right-side region', () => { |
| 88 | + const viewports = [ |
| 89 | + { width: 320, height: 240 }, |
| 90 | + { width: 1024, height: 768 }, |
| 91 | + { width: 1920, height: 1080 }, |
| 92 | + ]; |
| 93 | + |
| 94 | + for (const vp of viewports) { |
| 95 | + const res = validateUiRepairCandidate({ |
| 96 | + candidate: { |
| 97 | + x: Math.floor(vp.width * 0.85), |
| 98 | + y: Math.floor(vp.height * 0.5), |
| 99 | + confidence: 0.95, |
| 100 | + rationaleCode: 'DISMISS_BUTTON', |
| 101 | + }, |
| 102 | + dimensions: vp, |
| 103 | + minConfidence: 0.7, |
| 104 | + }); |
| 105 | + expect(res.ok).toBe(true); |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it('rejects low confidence', () => { |
| 110 | + const res = validateUiRepairCandidate({ |
| 111 | + candidate: { x: 90, y: 10, confidence: 0.2, rationaleCode: 'CLOSE_X' }, |
| 112 | + dimensions: { width: 100, height: 100 }, |
| 113 | + minConfidence: 0.7, |
| 114 | + }); |
| 115 | + expect(res.ok).toBe(false); |
| 116 | + }); |
| 117 | + }); |
| 118 | +}); |
0 commit comments