File size: 12,611 Bytes
6f38c76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
"""
Test Verification Script
Verifies that all 13 user-annotated differences are detected
"""

import os
import sys
from typing import Dict, List, Any

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from state_schema import VisualDifference


class TestVerification:
    """Verify detection of user-annotated differences"""
    
    # User's 13 manually annotated differences
    USER_ANNOTATED_DIFFERENCES = [
        {
            "id": 1,
            "name": "Header height difference",
            "category": "Layout & Structure",
            "severity": "High",
            "description": "Header height differs between design and development"
        },
        {
            "id": 2,
            "name": "Container width differs",
            "category": "Layout & Structure",
            "severity": "High",
            "description": "Main container width is different"
        },
        {
            "id": 3,
            "name": "Checkout placement difference",
            "category": "Components & Elements",
            "severity": "High",
            "description": "Checkout heading is positioned differently"
        },
        {
            "id": 4,
            "name": "Font family, size, letter spacing differs",
            "category": "Typography",
            "severity": "High",
            "description": "Typography properties changed"
        },
        {
            "id": 5,
            "name": "Login link missing",
            "category": "Components & Elements",
            "severity": "High",
            "description": "Login link component is missing in development"
        },
        {
            "id": 6,
            "name": "Payment component not visible",
            "category": "Components & Elements",
            "severity": "High",
            "description": "Payment component is hidden or not rendered"
        },
        {
            "id": 7,
            "name": "Button size, height, color, no elevation/shadow",
            "category": "Buttons & Interactive",
            "severity": "High",
            "description": "Button styling differs"
        },
        {
            "id": 8,
            "name": "Payment methods design missing",
            "category": "Components & Elements",
            "severity": "High",
            "description": "Payment methods section is missing"
        },
        {
            "id": 9,
            "name": "Contact info & step number missing, font bold",
            "category": "Typography",
            "severity": "High",
            "description": "Contact info styling and visibility changed"
        },
        {
            "id": 10,
            "name": "Icons missing",
            "category": "Components & Elements",
            "severity": "High",
            "description": "Various icons are not displayed"
        },
        {
            "id": 11,
            "name": "Padding (left, right) differs",
            "category": "Spacing & Sizing",
            "severity": "Medium",
            "description": "Horizontal padding is different"
        },
        {
            "id": 12,
            "name": "Image size different",
            "category": "Components & Elements",
            "severity": "Medium",
            "description": "Product images have different dimensions"
        },
        {
            "id": 13,
            "name": "Spacing between components differs",
            "category": "Spacing & Sizing",
            "severity": "Medium",
            "description": "Gap between components is different"
        }
    ]
    
    def __init__(self):
        """Initialize verifier"""
        self.detected_differences: List[VisualDifference] = []
        self.verification_results: Dict[str, Any] = {}
    
    def verify_detection(self, detected_differences: List[Any]) -> Dict[str, Any]:
        """
        Verify that detected differences match user-annotated ones
        
        Args:
            detected_differences: List of detected differences
        
        Returns:
            Verification report
        """
        self.detected_differences = detected_differences
        
        # Convert to list of dicts for easier comparison
        detected_list = []
        for diff in detected_differences:
            if hasattr(diff, '__dict__'):
                detected_list.append(diff.__dict__)
            else:
                detected_list.append(diff)
        
        # Check each user-annotated difference
        results = {
            "total_annotated": len(self.USER_ANNOTATED_DIFFERENCES),
            "total_detected": len(detected_list),
            "matches": [],
            "missing": [],
            "extra": []
        }
        
        # Find matches
        for annotated in self.USER_ANNOTATED_DIFFERENCES:
            found = False
            for detected in detected_list:
                if self._is_match(annotated, detected):
                    results["matches"].append({
                        "annotated_id": annotated["id"],
                        "annotated_name": annotated["name"],
                        "detected_name": detected.get("name", ""),
                        "category": annotated["category"],
                        "severity": annotated["severity"]
                    })
                    found = True
                    break
            
            if not found:
                results["missing"].append({
                    "id": annotated["id"],
                    "name": annotated["name"],
                    "category": annotated["category"],
                    "severity": annotated["severity"]
                })
        
        # Find extra detections
        for detected in detected_list:
            found = False
            for annotated in self.USER_ANNOTATED_DIFFERENCES:
                if self._is_match(annotated, detected):
                    found = True
                    break
            
            if not found:
                results["extra"].append({
                    "name": detected.get("name", ""),
                    "category": detected.get("category", ""),
                    "severity": detected.get("severity", "")
                })
        
        # Calculate detection rate
        results["detection_rate"] = (len(results["matches"]) / len(self.USER_ANNOTATED_DIFFERENCES)) * 100
        
        self.verification_results = results
        return results
    
    def _is_match(self, annotated: Dict, detected: Dict) -> bool:
        """
        Check if annotated and detected differences match
        
        Args:
            annotated: User-annotated difference
            detected: Detected difference
        
        Returns:
            True if they match
        """
        # Match by name similarity
        annotated_name = annotated["name"].lower()
        detected_name = detected.get("name", "").lower() if isinstance(detected, dict) else ""
        
        # Check for keyword matches
        keywords = {
            "header": ["header", "height"],
            "container": ["container", "width"],
            "checkout": ["checkout", "placement"],
            "font": ["font", "typography"],
            "login": ["login", "link"],
            "payment": ["payment"],
            "button": ["button", "size", "color", "shadow", "elevation"],
            "contact": ["contact", "info"],
            "icon": ["icon"],
            "padding": ["padding"],
            "image": ["image", "size"],
            "spacing": ["spacing", "gap", "component"]
        }
        
        for key, terms in keywords.items():
            if any(term in annotated_name for term in terms):
                if any(term in detected_name for term in terms):
                    return True
        
        # Direct name match
        if annotated_name == detected_name:
            return True
        
        # Category and severity match
        if (annotated.get("category") == detected.get("category") and
            annotated.get("severity") == detected.get("severity")):
            # Check if description contains similar keywords
            annotated_desc = annotated.get("description", "").lower()
            detected_desc = detected.get("description", "").lower() if isinstance(detected, dict) else ""
            
            if any(word in detected_desc for word in annotated_desc.split()):
                return True
        
        return False
    
    def print_verification_report(self):
        """Print verification report"""
        if not self.verification_results:
            print("No verification results available")
            return
        
        results = self.verification_results
        
        print("\n" + "="*70)
        print("๐Ÿ” VERIFICATION REPORT: USER-ANNOTATED DIFFERENCES")
        print("="*70)
        
        print(f"\n๐Ÿ“Š Summary:")
        print(f"  Total Annotated Differences: {results['total_annotated']}")
        print(f"  Total Detected Differences: {results['total_detected']}")
        print(f"  Matched: {len(results['matches'])}")
        print(f"  Missing: {len(results['missing'])}")
        print(f"  Extra: {len(results['extra'])}")
        print(f"  Detection Rate: {results['detection_rate']:.1f}%")
        
        if results['matches']:
            print(f"\nโœ… Matched Differences ({len(results['matches'])}):")
            for match in results['matches']:
                print(f"  #{match['annotated_id']}: {match['annotated_name']}")
                print(f"     Category: {match['category']}")
                print(f"     Severity: {match['severity']}")
        
        if results['missing']:
            print(f"\nโŒ Missing Differences ({len(results['missing'])}):")
            for missing in results['missing']:
                print(f"  #{missing['id']}: {missing['name']}")
                print(f"     Category: {missing['category']}")
                print(f"     Severity: {missing['severity']}")
        
        if results['extra']:
            print(f"\nโš ๏ธ  Extra Detections ({len(results['extra'])}):")
            for extra in results['extra']:
                print(f"  โ€ข {extra['name']}")
                print(f"    Category: {extra['category']}")
        
        print("\n" + "="*70)
    
    def get_verification_markdown(self) -> str:
        """Get verification report as markdown"""
        if not self.verification_results:
            return "No verification results available"
        
        results = self.verification_results
        lines = []
        
        lines.append("# ๐Ÿ” Verification Report: User-Annotated Differences\n")
        
        lines.append("## Summary\n")
        lines.append(f"- **Total Annotated**: {results['total_annotated']}")
        lines.append(f"- **Total Detected**: {results['total_detected']}")
        lines.append(f"- **Matched**: {len(results['matches'])}")
        lines.append(f"- **Missing**: {len(results['missing'])}")
        lines.append(f"- **Extra**: {len(results['extra'])}")
        lines.append(f"- **Detection Rate**: {results['detection_rate']:.1f}%\n")
        
        if results['matches']:
            lines.append(f"## โœ… Matched Differences ({len(results['matches'])})\n")
            for match in results['matches']:
                lines.append(f"### #{match['annotated_id']}: {match['annotated_name']}")
                lines.append(f"- **Category**: {match['category']}")
                lines.append(f"- **Severity**: {match['severity']}\n")
        
        if results['missing']:
            lines.append(f"## โŒ Missing Differences ({len(results['missing'])})\n")
            for missing in results['missing']:
                lines.append(f"### #{missing['id']}: {missing['name']}")
                lines.append(f"- **Category**: {missing['category']}")
                lines.append(f"- **Severity**: {missing['severity']}\n")
        
        if results['extra']:
            lines.append(f"## โš ๏ธ Extra Detections ({len(results['extra'])})\n")
            for extra in results['extra']:
                lines.append(f"- **{extra['name']}** ({extra['category']})\n")
        
        return "\n".join(lines)


def verify_system(detected_differences: List[Any]) -> Dict[str, Any]:
    """
    Convenience function to verify system detection
    
    Args:
        detected_differences: List of detected differences
    
    Returns:
        Verification results
    """
    verifier = TestVerification()
    results = verifier.verify_detection(detected_differences)
    verifier.print_verification_report()
    return results