-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
151 lines (133 loc) · 4.02 KB
/
jest.setup.js
File metadata and controls
151 lines (133 loc) · 4.02 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
// Jest setup file for global test configuration
// Set test timeout
jest.setTimeout(30000);
// Mock console methods to reduce noise in tests
global.console = {
...console,
// Uncomment to suppress console.log in tests
// log: jest.fn(),
// debug: jest.fn(),
// info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
};
// Mock environment variables
process.env.NODE_ENV = 'test';
process.env.LOG_LEVEL = 'error';
process.env.PORT = '3001';
// Global test utilities
global.testUtils = {
// Generate random session ID
generateSessionId: () => `session_${Math.random().toString(36).substr(2, 9)}`,
// Generate random message ID
generateMessageId: () => `msg_${Math.random().toString(36).substr(2, 9)}`,
// Generate random user ID
generateUserId: () => `user_${Math.random().toString(36).substr(2, 9)}`,
// Wait for async operations
wait: (ms) => new Promise(resolve => setTimeout(resolve, ms)),
// Mock authentication token
getAuthToken: (userId = 'userA') => `Bearer token_${userId}`,
// Mock session data
createMockSession: (mode = 'couple') => ({
sessionId: global.testUtils.generateSessionId(),
mode,
turnState: mode === 'couple' ? 'awaitingA' : 'ai_reflect',
safetyLevel: 'low',
boundaryFlag: false
}),
// Mock message data
createMockMessage: (sender = 'userA', content = 'Test message') => ({
id: global.testUtils.generateMessageId(),
sender,
content,
createdAt: new Date().toISOString(),
safetyTags: [],
safetyLevel: 'low',
flagged: false
})
};
// Mock external dependencies
jest.mock('@sync/ai/src/orchestrator', () => ({
aiOrchestrator: {
generateResponse: jest.fn().mockResolvedValue({
content: 'Mock AI response',
metadata: {
prompt_version: 'couple_v2.0',
validation_status: 'valid',
latency_ms: 100,
tokens_used: 50,
safety_tags: []
}
})
}
}));
// Mock long-polling manager
jest.mock('../../services/api/src/lib/longpoll', () => ({
longPollManager: {
subscribe: jest.fn(),
addMessage: jest.fn(),
removeClient: jest.fn(),
clearSessionMessages: jest.fn()
}
}));
// Mock safety and privacy utilities
jest.mock('../../services/api/src/lib/safety-privacy', () => ({
classifyContent: jest.fn().mockReturnValue({
level: 'low',
categories: [],
confidence: 0.5,
action: 'allow'
}),
recordAuditLog: jest.fn(),
getPrivacySettings: jest.fn().mockReturnValue({
dataRetention: 30,
encryptionLevel: 'enhanced',
auditLogging: true,
gdprCompliance: true,
dataAnonymization: true
}),
updatePrivacySettings: jest.fn(),
requestDataExport: jest.fn().mockReturnValue({
exportId: 'export_123',
downloadUrl: '/privacy/download/export_123',
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString()
}),
deleteUserData: jest.fn().mockReturnValue({
status: 'success',
message: 'User data deletion initiated.'
}),
getBoundaryResources: jest.fn().mockReturnValue({
region: 'EU',
safetyLevel: 'enhanced',
resources: [
{ name: 'Crisis Helpline EU', phone: '+800-123-4567', available: '24/7' },
{ name: 'Couples Therapy EU', phone: '+800-987-6543', available: 'Mon-Fri 9-17' },
{ name: 'Emergency Services', phone: '112', available: '24/7' }
]
}),
getCryptoHealth: jest.fn().mockReturnValue({
kms: 'ok',
dek_age_days: 7,
encryption_level: 'enhanced'
})
}));
// Setup and teardown for each test
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
// Reset environment
process.env.NODE_ENV = 'test';
process.env.LOG_LEVEL = 'error';
});
afterEach(() => {
// Clean up after each test
jest.restoreAllMocks();
});
// Global error handler for unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
// Global error handler for uncaught exceptions
process.on('uncaughtException', (error) => {
console.error('Uncaught Exception:', error);
});