// Import necessary modules const mqtt = require('mqtt'); // MQTT broker configuration const brokerUrl = 'mqtt://localhost'; // Broker URL (change if needed) const options = { clientId: 'test_buzzer_manager', clean: true }; // Set up MQTT client const client = mqtt.connect(brokerUrl, options); // Variables for tracking test results let testResults = { buzzerActivity: false, confirmationReceived: false, statusBlocked: false, statusUnblocked: false, tiltAdded: false, tiltIgnored: false, tiltRemoved: false, unlockConfirmation: false }; // Subscribe to topics to capture the responses from the buzzer manager client.on('connect', () => { console.log('[INFO] Connected to MQTT broker for testing'); // Subscribe to all topics related to the buzzer manager client.subscribe('brainblast/buzzer/#', (err) => { if (err) console.error('[ERROR] Failed to subscribe to topics for testing'); else console.log('[INFO] Subscribed to topics successfully'); }); // Run the test sequence after a short delay setTimeout(runTestSequence, 500); }); // Capture and process incoming MQTT messages client.on('message', (topic, message) => { const payload = JSON.parse(message.toString()); console.log(`[INFO] Message received on ${topic}: ${message.toString()}`); // Track the test results based on the topics and payloads if (topic.startsWith('brainblast/buzzer/activity') && payload.buzzer_id === 1) { testResults.buzzerActivity = true; } if (topic.startsWith(`brainblast/buzzer/confirmation/1`) && payload.status === "received") { testResults.confirmationReceived = true; } if (topic === 'brainblast/buzzer/status' && payload.status === "blocked") { testResults.statusBlocked = true; } if (topic === 'brainblast/buzzer/status' && payload.status === "unblocked") { testResults.statusUnblocked = true; } if (topic === 'brainblast/buzzer/unlock/confirmation' && payload.status === "received") { testResults.unlockConfirmation = true; } if (topic === 'brainblast/buzzer/activity' && payload.buzzer_id === 2 && payload.status === "blocked") { testResults.tiltIgnored = true; } if (topic.startsWith('brainblast/buzzer/status') && payload.message.includes('Tilt removed')) { testResults.tiltRemoved = true; } }); // Function to run the complete test sequence function runTestSequence() { console.log('[INFO] Starting test sequence...'); // 1. Simulate a buzzer press (buzzer 1, color red) console.log('[TEST] Simulating buzzer press (ID 1, color #FF0000)...'); client.publish('brainblast/buzzer/pressed/1', JSON.stringify({ buzzer_id: 1, color: "#FF0000" })); // 2. Simulate a second buzzer press (buzzer 2, color blue) to check blocking setTimeout(() => { console.log('[TEST] Simulating second buzzer press (ID 2, color #0000FF)...'); client.publish('brainblast/buzzer/pressed/2', JSON.stringify({ buzzer_id: 2, color: "#0000FF" })); }, 1000); // 3. Simulate adding a buzzer to tilt mode (buzzer 2) setTimeout(() => { console.log('[TEST] Adding buzzer ID 2 to tilt mode...'); client.publish('brainblast/buzzer/tilt', JSON.stringify({ buzzer_id: 2, status: "add" })); testResults.tiltAdded = true; }, 1500); // 4. Simulate pressing a buzzer in tilt mode (should be ignored) setTimeout(() => { console.log('[TEST] Simulating tilt buzzer press (ID 2, color #0000FF)...'); client.publish('brainblast/buzzer/pressed/2', JSON.stringify({ buzzer_id: 2, color: "#0000FF" })); }, 2000); // 5. Remove tilt mode from buzzer 2 setTimeout(() => { console.log('[TEST] Removing tilt mode for buzzer ID 2...'); client.publish('brainblast/buzzer/tilt', JSON.stringify({ buzzer_id: 2, status: "remove" })); }, 2500); // 6. Unlock buzzers to reset state setTimeout(() => { console.log('[TEST] Unlocking buzzers...'); client.publish('brainblast/buzzer/unlock', '{}'); }, 3000); // 7. Display results setTimeout(() => { console.log('[INFO] Test sequence complete. Results:'); console.log(`1. Buzzer activity detected for buzzer 1: ${testResults.buzzerActivity ? 'PASSED' : 'FAILED'}`); console.log(`2. Confirmation received for buzzer 1: ${testResults.confirmationReceived ? 'PASSED' : 'FAILED'}`); console.log(`3. Buzzer 1 status set to "blocked": ${testResults.statusBlocked ? 'PASSED' : 'FAILED'}`); console.log(`4. Buzzer status set to "unblocked": ${testResults.statusUnblocked ? 'PASSED' : 'FAILED'}`); console.log(`5. Tilt mode added for buzzer 2: ${testResults.tiltAdded ? 'PASSED' : 'FAILED'}`); console.log(`6. Tilted buzzer press ignored: ${testResults.tiltIgnored ? 'PASSED' : 'FAILED'}`); console.log(`7. Tilt mode removed for buzzer 2: ${testResults.tiltRemoved ? 'PASSED' : 'FAILED'}`); console.log(`8. Unlock confirmation received: ${testResults.unlockConfirmation ? 'PASSED' : 'FAILED'}`); client.end(); // End the MQTT connection }, 4000); }