#!/usr/bin/env node /** * Test Geofence Email Notifications * * This script tests the complete geofence notification flow: * 1. Creates a test geofence * 2. Simulates MQTT location updates * 3. Triggers geofence events * 4. Sends email notifications */ const Database = require('better-sqlite3'); const path = require('path'); const { v4: uuidv4 } = require('uuid'); const dbPath = path.join(__dirname, '..', 'data', 'database.sqlite'); const locationsDbPath = path.join(__dirname, '..', 'data', 'locations.sqlite'); console.log('๐Ÿงช Geofence Email Notification Test\n'); // Test configuration const TEST_CONFIG = { deviceId: '10', userId: 'admin-001', // Geofence in Frankfurt geofenceName: 'Email Test Zone', geofenceCenter: { lat: 50.1109, lon: 8.6821, }, geofenceRadius: 500, }; // Create location that triggers ENTER event function createEnterLocation() { const db = new Database(locationsDbPath); try { const timestamp = new Date().toISOString(); const stmt = db.prepare(` INSERT INTO Location ( latitude, longitude, timestamp, user_id, username, marker_label, first_name, last_name, display_time, chat_id ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `); const result = stmt.run( TEST_CONFIG.geofenceCenter.lat, TEST_CONFIG.geofenceCenter.lon, timestamp, 0, TEST_CONFIG.deviceId, TEST_CONFIG.deviceId, null, null, null, 0 ); return { id: result.lastInsertRowid, latitude: TEST_CONFIG.geofenceCenter.lat, longitude: TEST_CONFIG.geofenceCenter.lon, timestamp, }; } finally { db.close(); } } async function runTest() { const db = new Database(dbPath); let geofenceId = null; try { // Step 1: Create geofence console.log('๐Ÿ“ Step 1: Creating test geofence...'); geofenceId = uuidv4(); db.prepare(` INSERT INTO Geofence ( id, name, description, shape_type, center_latitude, center_longitude, radius_meters, owner_id, device_id, color ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `).run( geofenceId, TEST_CONFIG.geofenceName, 'Test geofence for email notifications', 'circle', TEST_CONFIG.geofenceCenter.lat, TEST_CONFIG.geofenceCenter.lon, TEST_CONFIG.geofenceRadius, TEST_CONFIG.userId, TEST_CONFIG.deviceId, '#10b981' ); console.log(`โœ“ Created geofence: "${TEST_CONFIG.geofenceName}"`); console.log(` ID: ${geofenceId}`); console.log(` Center: ${TEST_CONFIG.geofenceCenter.lat}, ${TEST_CONFIG.geofenceCenter.lon}`); console.log(` Radius: ${TEST_CONFIG.geofenceRadius}m\n`); // Step 2: Simulate MQTT location (ENTER) console.log('๐Ÿ“ก Step 2: Simulating MQTT location update...'); console.log(' (Device enters the geofence zone)\n'); const location = createEnterLocation(); console.log(`โœ“ Location created (ID: ${location.id})`); console.log(` This location is INSIDE the geofence\n`); // Step 3: Create ENTER event manually console.log('๐Ÿ”” Step 3: Creating ENTER event...'); const eventResult = db.prepare(` INSERT INTO GeofenceEvent ( geofence_id, device_id, location_id, event_type, latitude, longitude, distance_from_center, timestamp ) VALUES (?, ?, ?, ?, ?, ?, ?, ?) `).run( geofenceId, TEST_CONFIG.deviceId, location.id, 'enter', location.latitude, location.longitude, 0, location.timestamp ); const eventId = eventResult.lastInsertRowid; console.log(`โœ“ Created ENTER event (ID: ${eventId})\n`); // Step 4: Update status db.prepare(` INSERT INTO GeofenceStatus (device_id, geofence_id, is_inside, last_checked_at) VALUES (?, ?, 1, ?) `).run(TEST_CONFIG.deviceId, geofenceId, new Date().toISOString()); // Step 5: Trigger email notification console.log('๐Ÿ“ง Step 4: Triggering email notification...'); console.log(' This requires the Next.js server to be running!\n'); // Import and call notification function // Since we can't easily import ESM modules, we'll use a workaround console.log('โš ๏ธ To test email notifications, you need to:'); console.log(' 1. Start the dev server: npm run dev'); console.log(' 2. The MQTT subscriber will automatically process new locations'); console.log(' 3. OR manually trigger via the notification service\n'); console.log('๐Ÿ“Š Test Summary:'); console.log(` โœ“ Geofence created: ${geofenceId}`); console.log(` โœ“ Location created: ${location.id}`); console.log(` โœ“ Event created: ${eventId}`); console.log(` โœ“ Notification status: Pending (notification_sent = 0)\n`); console.log('๐Ÿ’ก Next Steps:'); console.log(' 1. Keep this geofence for testing'); console.log(' 2. Send real MQTT location with OwnTracks'); console.log(' 3. OR run the full MQTT integration test\n'); console.log('โœ… Test data prepared successfully!'); console.log(` Geofence ID: ${geofenceId}`); console.log(` Delete with: sqlite3 data/database.sqlite "DELETE FROM Geofence WHERE id = '${geofenceId}'"`); } catch (error) { console.error('\nโŒ Test failed:', error); if (geofenceId) { db.prepare('DELETE FROM Geofence WHERE id = ?').run(geofenceId); console.log('โœ“ Cleaned up test data'); } process.exit(1); } finally { db.close(); } } runTest();