Files
location-mqtt-tracker-app/scripts/test-geofence-notification.js
Joachim Hummel b4bd967400 Add comprehensive geofence testing scripts
Added three test scripts for validating geofence functionality:

1. test-geofence.js: Database-level geofence testing
   - Creates test geofence and mock locations
   - Tests distance calculations and event generation
   - Validates state tracking (enter/exit detection)

2. test-geofence-notification.js: Email notification testing
   - Tests SMTP connection and email delivery
   - Sends formatted geofence notification email
   - Validates email template rendering

3. test-mqtt-geofence.js: Full MQTT integration testing
   - Publishes OwnTracks-formatted MQTT messages
   - Tests complete flow: MQTT → Geofence → Email
   - Simulates device entering/exiting zones

**NPM Scripts:**
- npm run test:geofence - Database and logic test
- npm run test:geofence:email - Email notification test
- npm run test:geofence:mqtt - Full MQTT integration test

**Other Changes:**
- Updated admin user email to joachim.hummel@gmail.com
- All scripts include cleanup instructions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-02 18:32:06 +00:00

156 lines
5.4 KiB
JavaScript

#!/usr/bin/env node
/**
* Test Geofence Email Notification
*
* This script sends a test email notification for a geofence event
* without needing the full MQTT/Next.js server stack.
*/
const nodemailer = require('nodemailer');
const { render } = require('@react-email/components');
console.log('📧 Geofence Email Notification Test\n');
// SMTP configuration from .env
const SMTP_CONFIG = {
host: process.env.SMTP_HOST || 'smtp-relay.brevo.com',
port: parseInt(process.env.SMTP_PORT || '587', 10),
secure: process.env.SMTP_SECURE === 'true',
auth: {
user: process.env.SMTP_USER || 'joachim.hummel@gmail.com',
pass: process.env.SMTP_PASS || 'xqwXW2Sr3ZNcITa1',
},
};
const FROM_EMAIL = process.env.SMTP_FROM_EMAIL || 'noreply@businesshelpdesk.biz.com';
const FROM_NAME = process.env.SMTP_FROM_NAME || 'Location Tracker';
const TO_EMAIL = 'joachim.hummel@gmail.com';
async function sendTestEmail() {
try {
console.log('🔧 Step 1: Creating SMTP transporter...');
console.log(` Host: ${SMTP_CONFIG.host}:${SMTP_CONFIG.port}`);
console.log(` From: ${FROM_NAME} <${FROM_EMAIL}>`);
console.log(` To: ${TO_EMAIL}\n`);
const transporter = nodemailer.createTransport(SMTP_CONFIG);
// Test connection
console.log('🔌 Step 2: Testing SMTP connection...');
await transporter.verify();
console.log('✓ SMTP connection successful!\n');
// Prepare email content
console.log('📝 Step 3: Preparing email...');
const emailData = {
username: 'Admin',
deviceName: 'Device A',
geofenceName: 'Test Zone',
timestamp: new Date().toISOString(),
latitude: 50.1109,
longitude: 8.6821,
distanceFromCenter: 0,
};
// Simple HTML email (since we can't easily import React Email in this context)
const html = `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geofence Benachrichtigung</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px; background-color: #f3f4f6;">
<div style="max-width: 600px; margin: 0 auto; background-color: white; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
<!-- Header -->
<div style="background-color: #2563eb; color: white; padding: 24px; text-align: center;">
<h1 style="margin: 0; font-size: 24px;">Geofence Benachrichtigung</h1>
</div>
<!-- Content -->
<div style="padding: 32px;">
<p style="color: #374151; font-size: 16px; line-height: 1.6; margin: 0 0 16px;">
Hallo ${emailData.username},
</p>
<p style="color: #16a34a; font-size: 18px; font-weight: 600; line-height: 1.6; margin: 0 0 24px;">
Ihr Gerät "${emailData.deviceName}" hat die Zone "${emailData.geofenceName}" betreten.
</p>
<!-- Details Box -->
<div style="background-color: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 16px; margin: 20px 0;">
<p style="color: #111827; font-size: 14px; font-weight: 600; margin: 0 0 12px; text-transform: uppercase; letter-spacing: 0.5px;">
Details:
</p>
<p style="color: #374151; font-size: 15px; line-height: 1.6; margin: 0 0 8px;">
<strong>Zeit:</strong> ${new Date(emailData.timestamp).toLocaleString('de-DE')}
</p>
<p style="color: #374151; font-size: 15px; line-height: 1.6; margin: 0 0 8px;">
<strong>Position:</strong> ${emailData.latitude}, ${emailData.longitude}
</p>
<p style="color: #374151; font-size: 15px; line-height: 1.6; margin: 0;">
<strong>Distanz vom Zentrum:</strong> ${emailData.distanceFromCenter} Meter
</p>
</div>
<p style="color: #6b7280; font-size: 14px; line-height: 1.5; margin: 24px 0 0; padding-top: 16px; border-top: 1px solid #e5e7eb;">
Diese Benachrichtigung wurde automatisch von Ihrem Location Tracker System gesendet.
</p>
</div>
<!-- Footer -->
<div style="background-color: #f9fafb; padding: 16px 32px; text-align: center; border-top: 1px solid #e5e7eb;">
<p style="color: #6b7280; font-size: 12px; margin: 0;">
Location Tracker © ${new Date().getFullYear()}
</p>
</div>
</div>
</body>
</html>
`;
console.log('✓ Email prepared\n');
// Send email
console.log('📤 Step 4: Sending email...');
const info = await transporter.sendMail({
from: `"${FROM_NAME}" <${FROM_EMAIL}>`,
to: TO_EMAIL,
subject: `${emailData.deviceName} hat ${emailData.geofenceName} betreten`,
html: html,
});
console.log('✅ Email sent successfully!\n');
console.log('📬 Details:');
console.log(` Message ID: ${info.messageId}`);
console.log(` Response: ${info.response}\n`);
console.log('💡 Check your inbox at: ' + TO_EMAIL);
console.log(' (May take a few seconds to arrive)');
} catch (error) {
console.error('\n❌ Failed to send email:', error.message);
if (error.code === 'EAUTH') {
console.error('\n💡 Authentication failed. Check your SMTP credentials in .env');
} else if (error.code === 'ETIMEDOUT' || error.code === 'ECONNECTION') {
console.error('\n💡 Connection timeout. Check your SMTP host and port.');
}
process.exit(1);
}
}
// Run test
console.log('Starting email notification test...\n');
sendTestEmail().then(() => {
console.log('\n✅ Test completed!');
process.exit(0);
}).catch((error) => {
console.error('Unexpected error:', error);
process.exit(1);
});