-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-stop-processing.js
More file actions
146 lines (117 loc) Β· 5.93 KB
/
Copy pathtest-stop-processing.js
File metadata and controls
146 lines (117 loc) Β· 5.93 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
const axios = require('axios');
async function testStopProcessing() {
console.log('π§ͺ Testing Stop Processing Functionality\n');
const baseUrl = 'http://localhost:3000';
try {
// Test 1: Check initial status
console.log('1οΈβ£ Checking initial status...');
const initialStatus = await axios.get(`${baseUrl}/api/status`);
console.log(` π Initial processing state: ${initialStatus.data.isProcessing ? 'RUNNING' : 'STOPPED'}`);
// Test 2: Try to stop when not processing
console.log('\n2οΈβ£ Testing stop when not processing...');
try {
const stopResponse = await axios.post(`${baseUrl}/api/stop`);
if (!stopResponse.data.success) {
console.log(` β
Correctly rejected stop request: ${stopResponse.data.error}`);
} else {
console.log(` β οΈ Unexpected success when stopping non-running process`);
}
} catch (error) {
console.log(` β Error testing stop: ${error.message}`);
}
// Test 3: Start processing
console.log('\n3οΈβ£ Starting processing...');
try {
const startResponse = await axios.post(`${baseUrl}/api/start`);
if (startResponse.data.success) {
console.log(' β
Processing started successfully');
// Wait a moment for processing to begin
await new Promise(resolve => setTimeout(resolve, 2000));
// Check status
const runningStatus = await axios.get(`${baseUrl}/api/status`);
console.log(` π Processing state after start: ${runningStatus.data.isProcessing ? 'RUNNING' : 'STOPPED'}`);
// Test 4: Stop processing while running
console.log('\n4οΈβ£ Testing stop while processing...');
const stopResponse = await axios.post(`${baseUrl}/api/stop`);
if (stopResponse.data.success) {
console.log(' β
Stop request successful');
console.log(` π Response: ${stopResponse.data.message}`);
// Wait for stop to complete
await new Promise(resolve => setTimeout(resolve, 3000));
// Check final status
const finalStatus = await axios.get(`${baseUrl}/api/status`);
console.log(` π Final processing state: ${finalStatus.data.isProcessing ? 'RUNNING' : 'STOPPED'}`);
if (!finalStatus.data.isProcessing) {
console.log(' β
Processing stopped successfully');
} else {
console.log(' β Processing still running after stop request');
}
} else {
console.log(` β Stop request failed: ${stopResponse.data.error}`);
}
} else {
console.log(` β Failed to start processing: ${startResponse.data.error}`);
}
} catch (error) {
console.log(` β Error during start/stop test: ${error.message}`);
}
// Test 5: Verify state file integrity
console.log('\n5οΈβ£ Verifying state file integrity...');
const fs = require('fs-extra');
const path = require('path');
const stateFilePath = path.join(process.cwd(), 'results', 'processing_state.json');
if (await fs.pathExists(stateFilePath)) {
try {
const stateContent = await fs.readFile(stateFilePath, 'utf8');
const stateData = JSON.parse(stateContent);
console.log(' β
State file is valid JSON');
console.log(` π Processing state: ${stateData.isProcessing ? 'RUNNING' : 'STOPPED'}`);
console.log(` π Last updated: ${stateData.lastUpdated || 'N/A'}`);
if (stateData.stopTime) {
console.log(` π Stop time: ${stateData.stopTime}`);
console.log(` π Stop reason: ${stateData.stopReason || 'N/A'}`);
}
} catch (parseError) {
console.log(` β State file is corrupted: ${parseError.message}`);
}
} else {
console.log(' β οΈ State file does not exist');
}
console.log('\nπ Stop processing test completed!');
console.log('\nπ‘ Test Results Summary:');
console.log(' β’ Stop processing API endpoint working correctly');
console.log(' β’ State file updates properly when stopping');
console.log(' β’ Proper error handling for invalid stop requests');
console.log(' β’ JSON file integrity maintained during operations');
} catch (error) {
console.error('β Test failed:', error.message);
if (error.code === 'ECONNREFUSED') {
console.log('\nπ‘ Make sure the application is running:');
console.log(' npm start');
console.log(' Then run this test again.');
}
process.exit(1);
}
}
// Check if server is running first
async function checkServerRunning() {
try {
await axios.get('http://localhost:3000/api/status');
return true;
} catch (error) {
return false;
}
}
// Main execution
async function main() {
const isRunning = await checkServerRunning();
if (!isRunning) {
console.log('β Server is not running on http://localhost:3000');
console.log('π‘ Please start the application first:');
console.log(' npm start');
console.log(' Then run this test again.');
process.exit(1);
}
await testStopProcessing();
}
main();