-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger_test.js
More file actions
168 lines (138 loc) · 5.51 KB
/
Copy pathtrigger_test.js
File metadata and controls
168 lines (138 loc) · 5.51 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
const axios = require('axios');
// Configuration
const BASE_URL = 'http://localhost:3000/api';
const TRIGGER_ENDPOINT = `${BASE_URL}/trigger`;
const TEST_PHONE_NUMBER = '+905559967545'; // Replace with a valid test phone number
const TEST_DURATION = 5000; // 5 seconds
// Colors for console output
const colors = {
reset: '\x1b[0m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m',
};
// Create a section header
const section = (title) => {
console.log('\n' + colors.bright + colors.blue + '='.repeat(80) + colors.reset);
console.log(colors.bright + colors.blue + ` ${title} ` + colors.reset);
console.log(colors.bright + colors.blue + '='.repeat(80) + colors.reset);
};
// Format successful operation output
const success = (message, data = null) => {
console.log(colors.green + '✓ ' + colors.reset + message);
if (data) {
console.log(' ' + colors.cyan + 'Data:' + colors.reset, typeof data === 'object' ? JSON.stringify(data, null, 2) : data);
}
};
// Format failed operation output
const failure = (message, error = null) => {
console.log(colors.red + '✗ ' + colors.reset + message);
if (error) {
const errorMessage = error.response?.data?.error || error.message || String(error);
console.log(' ' + colors.red + 'Error:' + colors.reset, errorMessage);
}
};
// Wait for specified milliseconds
const wait = async (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
// Function to start a trigger flow
async function startTriggerFlow() {
section('Starting Trigger Flow');
console.log(`Initiating video capture (${TEST_DURATION}ms) and notification to ${TEST_PHONE_NUMBER}...`);
try {
const response = await axios.post(`${TRIGGER_ENDPOINT}/start`, {
videoDuration: TEST_DURATION,
phoneNumber: TEST_PHONE_NUMBER,
customMessage: 'Test alert! View your video at: ',
});
success('Trigger flow started', response.data);
return response.data.triggerId;
} catch (error) {
failure('Failed to start trigger flow', error);
return null;
}
}
// Function to check trigger status
async function checkTriggerStatus(triggerId) {
section(`Checking Trigger Status (ID: ${triggerId})`);
try {
const response = await axios.get(`${TRIGGER_ENDPOINT}/${triggerId}`);
success('Retrieved trigger status', response.data);
return response.data;
} catch (error) {
failure(`Failed to get status for trigger ID ${triggerId}`, error);
return null;
}
}
// Function to poll trigger status until completion
async function pollTriggerStatus(triggerId, maxWaitTime = 120000) {
section(`Polling Trigger Status Until Completion (ID: ${triggerId})`);
const pollInterval = 5000; // 5 seconds
let elapsedTime = 0;
let status = null;
console.log(`Will poll every ${pollInterval/1000} seconds for up to ${maxWaitTime/1000} seconds...`);
while (elapsedTime < maxWaitTime) {
status = await checkTriggerStatus(triggerId);
if (!status) {
failure('Failed to get trigger status');
return null;
}
if (status.completed) {
success('Trigger flow completed', status);
return status;
}
console.log(colors.yellow + `Current step: ${status.currentStep}. Waiting ${pollInterval/1000} seconds...` + colors.reset);
await wait(pollInterval);
elapsedTime += pollInterval;
}
failure(`Timeout waiting for trigger ${triggerId} to complete`);
return status;
}
// Main test function
async function runTests() {
console.log(colors.bright + colors.cyan + '\nTRIGGER FLOW TEST' + colors.reset);
console.log(colors.cyan + new Date().toISOString() + colors.reset + '\n');
const triggerId = await startTriggerFlow();
if (triggerId) {
// Wait a moment before polling
await wait(2000);
// Monitor the process until completion
const finalStatus = await pollTriggerStatus(triggerId);
// Print final summary
section('TEST SUMMARY');
if (finalStatus) {
if (finalStatus.error) {
console.log(colors.bright + 'Trigger Flow: ' + colors.red + 'FAILED' + colors.reset);
console.log(` └─ Error: ${colors.red}${finalStatus.error}${colors.reset}`);
} else if (finalStatus.completed) {
console.log(colors.bright + 'Trigger Flow: ' + colors.green + 'SUCCESS' + colors.reset);
if (finalStatus.videoStatus) {
console.log(` └─ Video: ${colors.green}Recorded${colors.reset} (${finalStatus.videoStatus.path})`);
}
if (finalStatus.uploadedFileLink) {
console.log(` └─ Upload: ${colors.green}Successful${colors.reset}`);
console.log(` └─ Link: ${colors.cyan}${finalStatus.uploadedFileLink}${colors.reset}`);
}
if (finalStatus.smsStatus === true) {
console.log(` └─ SMS: ${colors.green}Sent${colors.reset} to ${TEST_PHONE_NUMBER}`);
} else {
console.log(` └─ SMS: ${colors.red}Failed${colors.reset}`);
}
} else {
console.log(colors.bright + 'Trigger Flow: ' + colors.yellow + 'INCOMPLETE' + colors.reset);
console.log(` └─ Current step: ${colors.yellow}${finalStatus.currentStep}${colors.reset}`);
}
} else {
console.log(colors.bright + 'Trigger Flow: ' + colors.red + 'UNKNOWN' + colors.reset);
console.log(` └─ Could not retrieve final status`);
}
}
}
// Run the test
runTests().catch(error => {
console.error(colors.red + 'Test execution failed:' + colors.reset, error);
});