-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_comprehensive.py
More file actions
196 lines (171 loc) · 7.27 KB
/
test_comprehensive.py
File metadata and controls
196 lines (171 loc) · 7.27 KB
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
"""
Comprehensive automated test suite for Dark Pattern Detector
Tests backend API, vector DB, and extension logic
"""
import requests
import json
import time
print("="*70)
print("DARK PATTERN DETECTOR - COMPREHENSIVE TEST SUITE")
print("="*70)
test_results = []
def test_case(name, fn):
"""Run a test case and track results"""
print(f"\n[TEST] {name}")
try:
fn()
test_results.append((name, "PASS"))
print(f"[PASS] {name}")
except Exception as e:
test_results.append((name, f"FAIL: {str(e)}"))
print(f"[FAIL] {name}: {str(e)}")
# ============================================================================
# TEST 1: Backend Health Check
# ============================================================================
def test_health_check():
response = requests.get("http://localhost:8000/")
assert response.status_code == 200
data = response.json()
assert data["status"] == "running"
print(f" ✓ Backend is running")
print(f" ✓ Service: {data['service']}")
test_case("Backend Health Check", test_health_check)
# ============================================================================
# TEST 2: Dark Pattern Detection (Urgency + Scarcity)
# ============================================================================
def test_urgency_scarcity():
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": "WARNING! LAST CHANCE! Only 2 rooms left at this price. 127 people are viewing this property. Book now!",
"buttons": ["Book Now", "Maybe Later"]
}
)
assert response.status_code == 200
data = response.json()
assert data["is_dark_pattern"] == True
assert "urgency" in data["pattern_types"] or "scarcity" in data["pattern_types"]
print(f" ✓ Correctly detected dark pattern")
print(f" ✓ Pattern types: {data['pattern_types']}")
print(f" ✓ Reason: {data['reason'][:100]}...")
test_case("Dark Pattern: Urgency + Scarcity", test_urgency_scarcity)
# ============================================================================
# TEST 3: Dark Pattern Detection (Asymmetric Choice)
# ============================================================================
def test_asymmetric_choice():
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": "Accept All Cookies [HUGE BUTTON] to continue. Manage cookie preferences [tiny link in corner]",
"buttons": ["Accept All Cookies", "manage preferences"]
}
)
assert response.status_code == 200
data = response.json()
assert data["is_dark_pattern"] == True
assert "asymmetry" in data["pattern_types"]
print(f" ✓ Correctly detected asymmetric choice")
print(f" ✓ Reason: {data['reason'][:100]}...")
test_case("Dark Pattern: Asymmetric Choice", test_asymmetric_choice)
# ============================================================================
# TEST 4: Dark Pattern Detection (Guilt Language)
# ============================================================================
def test_guilt_language():
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": "We'll miss you :( Are you sure you want to leave? You'll lose access to all exclusive member benefits!",
"buttons": ["Stay Subscribed", "Unsubscribe"]
}
)
assert response.status_code == 200
data = response.json()
assert data["is_dark_pattern"] == True
assert "guilt" in data["pattern_types"]
print(f" ✓ Correctly detected guilt manipulation")
print(f" ✓ Reason: {data['reason'][:100]}...")
test_case("Dark Pattern: Guilt Language", test_guilt_language)
# ============================================================================
# TEST 5: Ethical UI (Should NOT flag)
# ============================================================================
def test_ethical_ui():
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": "Cookie Preferences: Accept All, Reject All, or Customize. You can change these settings anytime in your account.",
"buttons": ["Accept All", "Reject All", "Customize"]
}
)
assert response.status_code == 200
data = response.json()
assert data["is_dark_pattern"] == False
assert len(data["pattern_types"]) == 0
print(f" ✓ Correctly identified as ethical UI")
print(f" ✓ No false positive")
print(f" ✓ Reason: {data['reason'][:100]}...")
test_case("Ethical UI: Balanced Choice", test_ethical_ui)
# ============================================================================
# TEST 6: Ethical UI with Clear Information
# ============================================================================
def test_ethical_informative():
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": "This product has limited stock. We'll notify you when it's back in stock if you'd like.",
"buttons": ["Notify Me", "No Thanks"]
}
)
assert response.status_code == 200
data = response.json()
assert data["is_dark_pattern"] == False
print(f" ✓ Correctly identified informative message as non-manipulative")
print(f" ✓ Reason: {data['reason'][:100]}...")
test_case("Ethical UI: Informative Without Pressure", test_ethical_informative)
# ============================================================================
# TEST 7: Error Handling (Empty Text)
# ============================================================================
def test_error_handling_empty():
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": "",
"buttons": []
}
)
# Should handle gracefully, not crash
assert response.status_code in [200, 422] # Either handles or rejects invalid input
print(f" ✓ Handles empty input gracefully")
test_case("Error Handling: Empty Input", test_error_handling_empty)
# ============================================================================
# TEST 8: Edge Case (Very Long Text)
# ============================================================================
def test_long_text():
long_text = "Accept cookies. " * 100 # Repeat to make it long
response = requests.post(
"http://localhost:8000/analyze",
json={
"text": long_text,
"buttons": ["Accept"]
}
)
# Should either handle or reject based on validation
assert response.status_code in [200, 422]
print(f" ✓ Handles long text without crashing")
test_case("Edge Case: Long Text", test_long_text)
# ============================================================================
# SUMMARY
# ============================================================================
print("\n" + "="*70)
print("TEST SUMMARY")
print("="*70)
passed = sum(1 for _, result in test_results if result == "PASS")
total = len(test_results)
for name, result in test_results:
status = "✓" if result == "PASS" else "✗"
print(f"{status} {name}: {result}")
print(f"\n{passed}/{total} tests passed")
if passed == total:
print("\n🎉 ALL TESTS PASSED! System is fully functional.")
else:
print(f"\n⚠️ {total - passed} test(s) failed. Review above for details.")
print("="*70)