BrainBlast/services/test-buzzer-manager.js
2024-10-03 18:20:52 +00:00

162 lines
6.2 KiB
JavaScript

// 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,
tiltAddConfirmed: false,
tiltRemoveConfirmed: false,
tiltIgnored: false,
unlockConfirmation: false,
tiltUpdateAdd: false,
tiltUpdateRemove: 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.startsWith(`brainblast/buzzer/tilt/confirmation/2`) && payload.status === "received" && payload.action === "add") {
testResults.tiltAddConfirmed = true;
}
if (topic.startsWith(`brainblast/buzzer/tilt/confirmation/2`) && payload.status === "received" && payload.action === "remove") {
testResults.tiltRemoveConfirmed = true;
}
if (topic === `brainblast/buzzer/tilt/ignored/2` && payload.status === "tilt_ignored") {
testResults.tiltIgnored = true;
}
if (topic === 'brainblast/buzzer/status' && payload.status === "tilt_update") {
// Check for tilt update with added buzzer
if (payload.tilt_buzzers.includes(2) && payload.message.includes("added")) {
testResults.tiltUpdateAdd = true;
}
// Check for tilt update with removed buzzer
if (!payload.tilt_buzzers.includes(2) && payload.message.includes("removed")) {
testResults.tiltUpdateRemove = true;
}
}
if (topic === 'brainblast/buzzer/unlock/confirmation' && payload.status === "received") {
testResults.unlockConfirmation = 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"
}));
}, 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 add confirmed for buzzer 2: ${testResults.tiltAddConfirmed ? 'PASSED' : 'FAILED'}`);
console.log(`6. Tilted buzzer press ignored: ${testResults.tiltIgnored ? 'PASSED' : 'FAILED'}`);
console.log(`7. Tilt status update sent (add): ${testResults.tiltUpdateAdd ? 'PASSED' : 'FAILED'}`);
console.log(`8. Tilt mode remove confirmed for buzzer 2: ${testResults.tiltRemoveConfirmed ? 'PASSED' : 'FAILED'}`);
console.log(`9. Tilt status update sent (remove): ${testResults.tiltUpdateRemove ? 'PASSED' : 'FAILED'}`);
console.log(`10. Unlock confirmation received: ${testResults.unlockConfirmation ? 'PASSED' : 'FAILED'}`);
client.end(); // End the MQTT connection
}, 4000);
}