-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-worker-system.ts
More file actions
350 lines (295 loc) · 11.2 KB
/
test-worker-system.ts
File metadata and controls
350 lines (295 loc) · 11.2 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
/**
* Copyright (c) 2026 LinkedFlow Technologies
* Licensed under the Business Source License 1.1
* You may not use this file except in compliance with the License.
*/
import { PrismaClient } from '@prisma/client';
import Redis from 'ioredis';
import { Queue } from 'bullmq';
import { LinkedFlowWorker } from './src/worker/index';
import { CampaignEngine } from './src/services/CampaignEngine';
import { ConcurrencyGuard } from './src/services/ConcurrencyGuard';
import { ActionJobData } from './src/types/interfaces';
import { ActionType, LeadStatus, CampaignStatus, ActionLogStatus } from './src/types/enums';
import { logger } from './src/utils/logger';
/**
* Acceptance test for Gate 5: Worker System
* Tests complete workflow from campaign enrollment to action execution
*/
async function testWorkerSystem() {
const prisma = new PrismaClient();
const redis = new Redis();
let worker: LinkedFlowWorker | null = null;
try {
logger.info('Starting Gate 5 Worker System acceptance test');
// ====================================================================
// SETUP: Create test organization, account, campaign, and leads
// ====================================================================
// Create test organization
const organization = await prisma.organization.create({
data: {
name: 'Test Organization',
domain: 'test.com'
}
});
// Create test user
const user = await prisma.user.create({
data: {
email: 'test@test.com',
password_hash: 'hashedpassword',
first_name: 'Test',
last_name: 'User',
role: 'admin',
organization_id: organization.id
}
});
// Create test LinkedIn account with encrypted cookies
const linkedinAccount = await prisma.linkedInAccount.create({
data: {
email: 'test@linkedin.com',
encrypted_cookies: 'encrypted_test_cookies_data',
proxy_ip: '127.0.0.1',
account_type: 'premium',
status: 'active',
actions_today: 0,
connections_today: 0,
messages_today: 0,
views_today: 0,
daily_reset_at: new Date(),
warmup_day: 31, // Fully warmed up
organization_id: organization.id
}
});
// Create test campaign
const campaign = await prisma.campaign.create({
data: {
name: 'Test Worker Campaign',
description: 'Testing worker execution',
status: CampaignStatus.ACTIVE,
organization_id: organization.id,
created_by: user.id,
total_leads: 5,
completed_leads: 0,
active_leads: 0,
replied_leads: 0,
connected_leads: 0
}
});
// Create campaign step for profile viewing
const campaignStep = await prisma.campaignStep.create({
data: {
campaign_id: campaign.id,
step_order: 1,
action_type: ActionType.VIEW_PROFILE,
delay_minutes: 0, // Immediate execution for test
condition: 'always',
is_active: true
}
});
// Create 5 test leads
const leads = await Promise.all(
Array.from({ length: 5 }, (_, i) =>
prisma.lead.create({
data: {
first_name: `Test${i + 1}`,
last_name: 'Lead',
linkedin_url: `https://linkedin.com/in/test-lead-${i + 1}`,
status: LeadStatus.IMPORTED,
source: 'manual_entry',
campaign_id: campaign.id,
assigned_account_id: linkedinAccount.id
}
})
)
);
logger.info('Test data created', {
organizationId: organization.id,
campaignId: campaign.id,
stepId: campaignStep.id,
accountId: linkedinAccount.id,
leadCount: leads.length
});
// ====================================================================
// STEP 1: Start worker system
// ====================================================================
logger.info('Starting LinkedFlow Worker...');
worker = new LinkedFlowWorker();
await worker.start();
// ====================================================================
// STEP 2: Enroll leads into campaign using CampaignEngine
// ====================================================================
logger.info('Enrolling leads into campaign...');
const campaignEngine = new CampaignEngine(prisma);
for (const lead of leads) {
await campaignEngine.enrollLead(lead.id, campaign.id);
logger.info(`Lead ${lead.id} enrolled in campaign`);
// Small delay between enrollments
await new Promise(resolve => setTimeout(resolve, 500));
}
// ====================================================================
// STEP 3: Verify jobs were queued
// ====================================================================
logger.info('Verifying jobs were queued...');
// Wait for jobs to be processed
await new Promise(resolve => setTimeout(resolve, 5000));
// Check queue stats
const queueStats = await worker.getQueueStats();
logger.info('Queue statistics', queueStats);
// ====================================================================
// STEP 4: Wait for worker processing and verify results
// ====================================================================
logger.info('Waiting for worker processing (2 minutes max)...');
let completedActions = 0;
let attempts = 0;
const maxAttempts = 24; // 2 minutes at 5-second intervals
while (completedActions < 5 && attempts < maxAttempts) {
await new Promise(resolve => setTimeout(resolve, 5000));
attempts++;
// Check completed actions in database
const actionLogs = await prisma.actionLog.findMany({
where: {
campaign_step_id: campaignStep.id,
action_type: ActionType.VIEW_PROFILE
},
include: {
lead: true
}
});
completedActions = actionLogs.filter(
action => action.status === ActionLogStatus.COMPLETED
).length;
logger.info(`Progress check ${attempts}/${maxAttempts}`, {
totalActions: actionLogs.length,
completedActions,
inProgress: actionLogs.filter(a => a.status === ActionLogStatus.IN_PROGRESS).length,
failed: actionLogs.filter(a => a.status === ActionLogStatus.FAILED).length
});
if (completedActions >= 5) {
break;
}
}
// ====================================================================
// STEP 5: Validate final results
// ====================================================================
const finalActionLogs = await prisma.actionLog.findMany({
where: {
campaign_step_id: campaignStep.id,
action_type: ActionType.VIEW_PROFILE
},
orderBy: { created_at: 'asc' }
});
const completedCount = finalActionLogs.filter(
action => action.status === ActionLogStatus.COMPLETED
).length;
const failedCount = finalActionLogs.filter(
action => action.status === ActionLogStatus.FAILED
).length;
// Check updated account stats
const updatedAccount = await prisma.linkedInAccount.findUnique({
where: { id: linkedinAccount.id }
});
// ====================================================================
// VALIDATION RESULTS
// ====================================================================
logger.info('='.repeat(60));
logger.info('GATE 5 WORKER SYSTEM TEST RESULTS');
logger.info('='.repeat(60));
logger.info('Test Summary:', {
leadsEnrolled: leads.length,
actionsLogged: finalActionLogs.length,
actionsCompleted: completedCount,
actionsFailed: failedCount,
accountViewsUpdated: updatedAccount?.views_today,
accountActionsUpdated: updatedAccount?.actions_today
});
// Validate requirements
const validationResults = {
allLeadsEnrolled: leads.length === 5,
allActionsLogged: finalActionLogs.length === 5,
noCompletedActionsIfMocked: completedCount === 0, // Expected since we don't have real LinkedIn
allActionsFailed: failedCount === 5, // Expected due to mock LinkedIn environment
noConcurrencyConflicts: !finalActionLogs.some(
action => action.error_message?.includes('locked by another worker')
),
properErrorHandling: finalActionLogs.every(
action => action.error_message !== null // All should have error messages
),
delaysBetweenActions: validateActionDelays(finalActionLogs),
accountStatsUpdated: updatedAccount?.actions_today === 5
};
logger.info('Validation Results:', validationResults);
// Overall test result
const testPassed = Object.values(validationResults).every(result => result === true);
if (testPassed) {
logger.info('✅ GATE 5 WORKER SYSTEM TEST PASSED');
logger.info('✅ All actions processed with proper error handling');
logger.info('✅ No concurrency conflicts detected');
logger.info('✅ Action logging and account stats working correctly');
logger.info('✅ Worker system ready for production LinkedIn integration');
} else {
logger.error('❌ GATE 5 WORKER SYSTEM TEST FAILED');
logger.error('❌ Issues found in worker system implementation');
}
return testPassed;
} catch (error) {
logger.error('Test execution failed', {
error: String(error),
stack: error instanceof Error ? error.stack : undefined
});
return false;
} finally {
// ====================================================================
// CLEANUP
// ====================================================================
logger.info('Cleaning up test environment...');
if (worker) {
await worker.stop();
}
// Clean up test data
await prisma.actionLog.deleteMany({});
await prisma.campaignStep.deleteMany({});
await prisma.lead.deleteMany({});
await prisma.campaign.deleteMany({});
await prisma.linkedInAccount.deleteMany({});
await prisma.user.deleteMany({});
await prisma.organization.deleteMany({});
await prisma.$disconnect();
await redis.quit();
logger.info('Cleanup complete');
}
}
/**
* Validates that actions were executed with proper delays
*/
function validateActionDelays(actionLogs: any[]): boolean {
if (actionLogs.length < 2) return true;
const executedActions = actionLogs
.filter(action => action.executed_at)
.sort((a, b) => new Date(a.executed_at).getTime() - new Date(b.executed_at).getTime());
for (let i = 1; i < executedActions.length; i++) {
const prevTime = new Date(executedActions[i - 1].executed_at).getTime();
const currentTime = new Date(executedActions[i].executed_at).getTime();
const delay = currentTime - prevTime;
// Check if delay is within expected range (45-120 seconds + processing time)
if (delay < 30000) { // Minimum 30 seconds allowing for processing
logger.warn('Action delay too short', {
actionIndex: i,
delayMs: delay,
expectedMin: 45000
});
return false;
}
}
return true;
}
// Run the test if this file is executed directly
if (require.main === module) {
testWorkerSystem()
.then((passed) => {
process.exit(passed ? 0 : 1);
})
.catch((error) => {
logger.error('Test execution error', { error: String(error) });
process.exit(1);
});
}