Files
XCDesktop/remote/test-full.js

90 lines
2.3 KiB
JavaScript

const WebSocket = require('ws');
const fs = require('fs');
const ws = new WebSocket('ws://127.0.0.1:3003/ws?password=wzw20040525');
let fileId = 'upload_test_' + Date.now();
const testContent = Buffer.from('Hello World Test File Content');
ws.on('open', () => {
console.log('=== Connected, starting upload test ===');
ws.send(JSON.stringify({
type: 'fileUploadStart',
fileId: fileId,
filename: 'test.txt',
totalChunks: 1,
fileSize: testContent.length,
requestId: 'req1'
}));
});
ws.on('message', (data, isBinary) => {
if (isBinary) {
console.log('Binary data received');
return;
}
const msg = JSON.parse(data.toString());
console.log('Received:', msg.type, msg.fileId || '');
if (msg.type === 'fileUploadStart') {
console.log('Session started, sending chunk...');
ws.send(JSON.stringify({
type: 'fileUploadChunk',
fileId: fileId,
chunkIndex: 0,
data: testContent.toString('base64'),
requestId: 'req2'
}));
}
if (msg.type === 'fileUploadChunk') {
console.log('Chunk sent, sending merge...');
ws.send(JSON.stringify({
type: 'fileUploadMerge',
fileId: fileId,
filename: 'test.txt',
totalChunks: 1,
requestId: 'req3'
}));
}
if (msg.type === 'fileUploadResult') {
console.log('=== Upload Result:', msg.success ? 'SUCCESS' : 'FAILED', msg.filename || msg.error);
if (msg.success) {
console.log('\n=== Testing download ===');
ws.send(JSON.stringify({
type: 'fileDownloadStart',
filename: 'test.txt',
filePath: 'test.txt',
allowSystem: true,
requestId: 'req4'
}));
} else {
ws.close();
process.exit(1);
}
}
if (msg.type === 'fileDownloadStart') {
console.log('Download started, size:', msg.size);
}
if (msg.type === 'fileDownloadChunk') {
console.log('Download chunk:', msg.chunkIndex, 'progress:', msg.progress + '%');
}
if (msg.type === 'fileDownloadComplete') {
console.log('=== Download Result:', msg.success ? 'SUCCESS' : 'FAILED');
console.log('=== ALL TESTS PASSED ===');
ws.close();
process.exit(0);
}
});
ws.on('error', (err) => { console.error('Error:', err.message); });
setTimeout(() => { console.log('=== TIMEOUT ==='); process.exit(1); }, 20000);