const FormData = require('form-data'); const fs = require('fs'); const fetch = require('node-fetch'); /** * Test script for the Event Upload API * This demonstrates how to upload events from edge devices */ const API_BASE_URL = 'http://localhost:3000'; async function testEventUpload() { console.log('🧪 Testing Event Upload API'); console.log('============================'); try { // Step 1: Register a test user console.log('1ļøāƒ£ Registering test user...'); const registerResponse = await fetch(`${API_BASE_URL}/api/v1/auth/register-email`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: 'eventtest@example.com', password: 'TestPassword123', displayName: 'Event Test User', }), }); if (!registerResponse.ok) { const error = await registerResponse.json(); if (error.message?.includes('already exists')) { console.log(' User already exists, continuing...'); } else { throw new Error(`Registration failed: ${error.message}`); } } else { console.log(' āœ… User registered successfully'); } // Step 2: Login to get JWT token console.log('2ļøāƒ£ Logging in to get JWT token...'); const loginResponse = await fetch(`${API_BASE_URL}/api/v1/auth/login-email`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: 'eventtest@example.com', password: 'TestPassword123', }), }); if (!loginResponse.ok) { const error = await loginResponse.json(); throw new Error(`Login failed: ${error.message}`); } const loginData = await loginResponse.json(); const jwtToken = loginData.accessToken; console.log(' āœ… Login successful, got JWT token'); // Step 3: Register a device (if not already registered) console.log('3ļøāƒ£ Registering test device...'); // First add the device to inventory const addToInventoryResponse = await fetch(`${API_BASE_URL}/api/v1/devices/inventory`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}`, }, body: JSON.stringify({ hardwareId: 'EVENT_TEST_DEVICE_001', deviceModel: 'Test Camera v1.0', }), }); // Register the device const deviceResponse = await fetch(`${API_BASE_URL}/api/v1/devices/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${jwtToken}`, }, body: JSON.stringify({ hardwareId: 'EVENT_TEST_DEVICE_001', }), }); let deviceId; if (deviceResponse.ok) { const deviceData = await deviceResponse.json(); deviceId = deviceData.device.id; console.log(` āœ… Device registered: ${deviceId}`); } else { const error = await deviceResponse.json(); if (error.message?.includes('already registered')) { console.log(' Device already registered, fetching device list...'); // Get user's devices const devicesResponse = await fetch(`${API_BASE_URL}/api/v1/devices`, { headers: { 'Authorization': `Bearer ${jwtToken}`, }, }); if (devicesResponse.ok) { const devicesData = await devicesResponse.json(); if (devicesData.devices.length > 0) { deviceId = devicesData.devices[0].id; console.log(` āœ… Using existing device: ${deviceId}`); } } } if (!deviceId) { throw new Error(`Device registration failed: ${error.message}`); } } // Step 4: Create a test file console.log('4ļøāƒ£ Creating test image file...'); const testImageData = Buffer.from('fake-image-data-for-testing'); // Step 5: Upload an event console.log('5ļøāƒ£ Uploading event...'); const form = new FormData(); form.append('file', testImageData, { filename: 'motion_capture.jpg', contentType: 'image/jpeg', }); const eventData = { eventType: 'motion', eventTimestamp: new Date().toISOString(), metadata: { deviceId: deviceId, location: 'front_door', confidence: 0.95, temperature: 22.5, }, }; form.append('eventData', JSON.stringify(eventData)); const uploadResponse = await fetch(`${API_BASE_URL}/api/v1/events/upload`, { method: 'POST', headers: { 'Authorization': `Bearer ${jwtToken}`, }, body: form, }); if (!uploadResponse.ok) { const error = await uploadResponse.json(); throw new Error(`Event upload failed: ${error.message}`); } const uploadResult = await uploadResponse.json(); console.log(' āœ… Event uploaded successfully!'); console.log(` Event ID: ${uploadResult.rawEventId}`); console.log(` Message: ${uploadResult.message}`); // Step 6: Verify the event was saved console.log('6ļøāƒ£ Verifying event was saved...'); const eventsResponse = await fetch(`${API_BASE_URL}/api/v1/events/user`, { headers: { 'Authorization': `Bearer ${jwtToken}`, }, }); if (eventsResponse.ok) { const eventsData = await eventsResponse.json(); console.log(` āœ… Found ${eventsData.events.length} events for user`); if (eventsData.events.length > 0) { const latestEvent = eventsData.events[0]; console.log(` Latest event: ${latestEvent.eventType} at ${latestEvent.eventTimestamp}`); } } console.log('\nšŸŽ‰ Event Upload API Test Completed Successfully!'); console.log('================================================'); } catch (error) { console.error('āŒ Test failed:', error.message); console.log('\nšŸ’” Make sure:'); console.log(' - Backend server is running (npm run start:dev)'); console.log(' - Database is accessible and migrated'); console.log(' - AWS credentials are configured (for actual S3/SQS operations)'); process.exit(1); } } // Handle missing node-fetch gracefully if (typeof fetch === 'undefined') { console.log('šŸ“¦ Installing required dependencies...'); console.log('Run: npm install node-fetch@2 form-data'); console.log('Then run this script again: node test-event-upload.js'); process.exit(1); } testEventUpload();