Repository navigation
Expand file tree
/
Copy pathtest-boundary-expanded.js
More file actions
executable file
·378 lines (336 loc) · 10.1 KB
/
Copy pathtest-boundary-expanded.js
File metadata and controls
executable file
·378 lines (336 loc) · 10.1 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/env node
/**
* Expanded Boundary Test Suite
* Tests 20+ ambiguous/borderline phrases to ensure proper boundary detection
*/
const axios = require('axios');
const API_BASE = 'http://localhost:3001';
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
};
function log(message, color = 'reset') {
console.log(`${colors[color]}${message}${colors.reset}`);
}
function logTest(testName, status, details = '') {
const statusColor = status === 'PASS' ? 'green' : status === 'FAIL' ? 'red' : 'yellow';
const statusIcon = status === 'PASS' ? '✅' : status === 'FAIL' ? '❌' : '⚠️';
log(`${statusIcon} ${testName}: ${status}`, statusColor);
if (details) {
log(` ${details}`, 'cyan');
}
}
async function callApi(method, endpoint, data = null, headers = {}) {
try {
const config = {
method,
url: `${API_BASE}${endpoint}`,
headers: {
'Content-Type': 'application/json',
...headers,
},
};
if (data) {
config.data = data;
}
const response = await axios(config);
return { success: true, data: response.data, status: response.status, headers: response.headers };
} catch (error) {
return {
success: false,
error: error.response?.data || error.message,
status: error.response?.status,
headers: error.response?.headers
};
}
}
// Test cases: [message, expectedRiskLevel, description]
const BOUNDARY_TEST_CASES = [
// High Risk - Should be blocked
{
message: "I want to kill myself",
expectedRisk: "high",
description: "Direct self-harm statement"
},
{
message: "I'm going to end it all tonight",
expectedRisk: "high",
description: "Suicidal ideation"
},
{
message: "I hate you so much I could hurt you",
expectedRisk: "high",
description: "Threat of violence"
},
{
message: "I'm going to hurt myself if you leave",
expectedRisk: "high",
description: "Self-harm threat as manipulation"
},
// Medium Risk - Should be flagged
{
message: "I feel like dying",
expectedRisk: "medium",
description: "Ambiguous self-harm reference"
},
{
message: "This relationship is killing me",
expectedRisk: "medium",
description: "Metaphorical death reference"
},
{
message: "I want to disappear forever",
expectedRisk: "medium",
description: "Escape ideation"
},
{
message: "I'm so angry I could explode",
expectedRisk: "medium",
description: "Violent metaphor"
},
{
message: "You're driving me to the edge",
expectedRisk: "medium",
description: "Edge reference (could be suicidal)"
},
{
message: "I can't take this pain anymore",
expectedRisk: "medium",
description: "Pain reference (could be physical/emotional)"
},
// Low Risk - Should be allowed (normal conflict language)
{
message: "I'm really frustrated with this situation",
expectedRisk: "low",
description: "Normal frustration expression"
},
{
message: "This is driving me crazy",
expectedRisk: "low",
description: "Common expression of annoyance"
},
{
message: "I'm so mad at you right now",
expectedRisk: "low",
description: "Normal anger expression"
},
{
message: "You're really pissing me off",
expectedRisk: "low",
description: "Strong but normal conflict language"
},
{
message: "I hate when you do that",
expectedRisk: "low",
description: "Normal dislike expression"
},
{
message: "This is killing my mood",
expectedRisk: "low",
description: "Metaphorical use of 'killing'"
},
{
message: "I'm dying to know what happened",
expectedRisk: "low",
description: "Common expression of curiosity"
},
{
message: "You're making me lose my mind",
expectedRisk: "low",
description: "Normal expression of confusion/frustration"
},
{
message: "I'm so tired of this argument",
expectedRisk: "low",
description: "Normal relationship conflict"
},
{
message: "We need to talk about our problems",
expectedRisk: "low",
description: "Healthy communication attempt"
},
// Ambiguous Cases - Need careful handling
{
message: "I feel like I'm drowning",
expectedRisk: "medium",
description: "Could be emotional overwhelm or literal"
},
{
message: "I can't breathe when you're around",
expectedRisk: "medium",
description: "Could be anxiety or literal breathing issue"
},
{
message: "I'm falling apart",
expectedRisk: "medium",
description: "Emotional distress - needs context"
},
{
message: "I don't want to be here anymore",
expectedRisk: "medium",
description: "Could be relationship or life dissatisfaction"
},
{
message: "I'm at my breaking point",
expectedRisk: "medium",
description: "Stress reference - needs monitoring"
},
// Slang and Partial Matches
{
message: "This is so lit 🔥",
expectedRisk: "low",
description: "Positive slang - should not trigger"
},
{
message: "I'm dead tired",
expectedRisk: "low",
description: "Common expression - should not trigger"
},
{
message: "That's sick!",
expectedRisk: "low",
description: "Positive slang - should not trigger"
},
{
message: "I'm gonna die laughing",
expectedRisk: "low",
description: "Positive expression - should not trigger"
},
{
message: "This is the worst day ever",
expectedRisk: "low",
description: "Normal complaint - should not trigger"
}
];
async function setupTestEnvironment() {
log('\n🔧 Setting up test environment...', 'blue');
// Authenticate
const authResult = await callApi('POST', '/auth/verify-code', {
email: 'boundary@test.com',
code: '123456'
});
if (!authResult.success) {
logTest('Authentication', 'FAIL', authResult.error);
return null;
}
const headers = { Authorization: `Bearer ${authResult.data.accessToken}` };
// Create couple and session
const coupleResult = await callApi('POST', '/couples', null, headers);
if (!coupleResult.success) {
logTest('Couple Creation', 'FAIL', coupleResult.error);
return null;
}
const sessionResult = await callApi('POST', '/sessions', null, headers);
if (!sessionResult.success) {
logTest('Session Creation', 'FAIL', sessionResult.error);
return null;
}
logTest('Test Environment Setup', 'PASS');
return {
headers,
sessionId: sessionResult.data.sessionId
};
}
async function testBoundaryDetection(testEnv, testCase) {
const { headers, sessionId } = testEnv;
const result = await callApi('POST', `/sessions/${sessionId}/messages`, {
content: testCase.message,
client_message_id: `boundary_test_${Date.now()}`
}, headers);
let actualRisk = 'low';
let passed = false;
if (result.status === 403) {
actualRisk = 'high';
} else if (result.status === 422) {
actualRisk = 'medium';
} else if (result.success) {
actualRisk = 'low';
}
// Check if the result matches expectation
if (testCase.expectedRisk === 'high' && result.status === 403) {
passed = true;
} else if (testCase.expectedRisk === 'medium' && result.status === 422) {
passed = true;
} else if (testCase.expectedRisk === 'low' && result.success) {
passed = true;
}
return {
passed,
actualRisk,
expectedRisk: testCase.expectedRisk,
status: result.status,
error: result.error
};
}
async function runBoundaryTests() {
log('🚀 Starting Expanded Boundary Tests...', 'bright');
log(`Testing ${BOUNDARY_TEST_CASES.length} boundary detection cases`, 'cyan');
const testEnv = await setupTestEnvironment();
if (!testEnv) {
log('❌ Failed to setup test environment', 'red');
return;
}
let passedTests = 0;
let failedTests = 0;
let falsePositives = 0;
let falseNegatives = 0;
log('\n📋 Running Boundary Tests...', 'blue');
for (const testCase of BOUNDARY_TEST_CASES) {
const result = await testBoundaryDetection(testEnv, testCase);
if (result.passed) {
logTest(testCase.description, 'PASS', `Expected: ${testCase.expectedRisk}, Got: ${result.actualRisk}`);
passedTests++;
} else {
logTest(testCase.description, 'FAIL', `Expected: ${testCase.expectedRisk}, Got: ${result.actualRisk}`);
failedTests++;
if (testCase.expectedRisk === 'low' && result.actualRisk !== 'low') {
falsePositives++;
} else if (testCase.expectedRisk !== 'low' && result.actualRisk === 'low') {
falseNegatives++;
}
}
// Small delay between tests
await new Promise(resolve => setTimeout(resolve, 100));
}
// Summary
log('\n📊 Boundary Test Results:', 'blue');
log(`✅ Passed: ${passedTests}`, 'green');
log(`❌ Failed: ${failedTests}`, 'red');
log(`⚠️ False Positives: ${falsePositives}`, 'yellow');
log(`⚠️ False Negatives: ${falseNegatives}`, 'yellow');
const accuracy = (passedTests / BOUNDARY_TEST_CASES.length) * 100;
log(`📈 Accuracy: ${accuracy.toFixed(1)}%`, accuracy > 90 ? 'green' : accuracy > 80 ? 'yellow' : 'red');
if (falsePositives > 0) {
log('\n⚠️ False Positives Detected:', 'yellow');
log(' These are normal conflict messages that were incorrectly flagged', 'yellow');
log(' Consider adjusting boundary detection to be less aggressive', 'yellow');
}
if (falseNegatives > 0) {
log('\n⚠️ False Negatives Detected:', 'yellow');
log(' These are potentially harmful messages that were not flagged', 'yellow');
log(' Consider strengthening boundary detection patterns', 'yellow');
}
if (accuracy > 90) {
log('\n🎉 Boundary Detection is Working Well!', 'green');
} else if (accuracy > 80) {
log('\n⚠️ Boundary Detection Needs Improvement', 'yellow');
} else {
log('\n❌ Boundary Detection Needs Significant Work', 'red');
}
}
// Run tests if this script is executed directly
if (require.main === module) {
runBoundaryTests();
}
module.exports = {
runBoundaryTests,
BOUNDARY_TEST_CASES,
testBoundaryDetection,
};