157 lines
5.4 KiB
JavaScript
157 lines
5.4 KiB
JavaScript
![]() |
// Test script for reality check functionality
|
||
|
console.log("Testing reality check functionality...");
|
||
|
|
||
|
// Mock event data for testing
|
||
|
const mockEvent = {
|
||
|
type: "Feature",
|
||
|
geometry: {
|
||
|
type: "Point",
|
||
|
coordinates: [2.3522, 48.8566] // Paris coordinates
|
||
|
},
|
||
|
properties: {
|
||
|
id: "test-event-123",
|
||
|
label: "Test Traffic Jam",
|
||
|
what: "traffic.jam",
|
||
|
type: "unscheduled",
|
||
|
start: "2025-09-21T18:00:00Z",
|
||
|
stop: "2025-09-21T20:00:00Z",
|
||
|
createdate: "2025-09-21T17:00:00Z" // More than 1 hour ago
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// Test checkIfNeedsRealityCheck function
|
||
|
function testCheckIfNeedsRealityCheck() {
|
||
|
console.log("Testing checkIfNeedsRealityCheck function...");
|
||
|
|
||
|
// Should return true for traffic events older than 1 hour
|
||
|
const needsCheck = checkIfNeedsRealityCheck(mockEvent);
|
||
|
console.log(`Event needs reality check: ${needsCheck}`);
|
||
|
|
||
|
// Should return false for events with reality_check already set
|
||
|
const eventWithRealityCheck = JSON.parse(JSON.stringify(mockEvent));
|
||
|
eventWithRealityCheck.properties.reality_check = "2025-09-21T19:00:00Z | testuser | confirmed";
|
||
|
const needsCheckAgain = checkIfNeedsRealityCheck(eventWithRealityCheck);
|
||
|
console.log(`Event with reality_check needs another check: ${needsCheckAgain}`);
|
||
|
|
||
|
// Should return false for non-traffic events
|
||
|
const nonTrafficEvent = JSON.parse(JSON.stringify(mockEvent));
|
||
|
nonTrafficEvent.properties.what = "music.concert";
|
||
|
const needsCheckNonTraffic = checkIfNeedsRealityCheck(nonTrafficEvent);
|
||
|
console.log(`Non-traffic event needs reality check: ${needsCheckNonTraffic}`);
|
||
|
|
||
|
// Should return false for recent events
|
||
|
const recentEvent = JSON.parse(JSON.stringify(mockEvent));
|
||
|
recentEvent.properties.createdate = new Date().toISOString();
|
||
|
const needsCheckRecent = checkIfNeedsRealityCheck(recentEvent);
|
||
|
console.log(`Recent event needs reality check: ${needsCheckRecent}`);
|
||
|
}
|
||
|
|
||
|
// Test localStorage functionality
|
||
|
function testLocalStorage() {
|
||
|
console.log("Testing localStorage functionality...");
|
||
|
|
||
|
// Clear existing data
|
||
|
localStorage.removeItem('oedb_username');
|
||
|
localStorage.removeItem('oedb_points');
|
||
|
localStorage.removeItem('oedb_contributions');
|
||
|
|
||
|
// Test username storage
|
||
|
const testUsername = "testuser";
|
||
|
localStorage.setItem('oedb_username', testUsername);
|
||
|
console.log(`Username stored: ${localStorage.getItem('oedb_username')}`);
|
||
|
|
||
|
// Test points system
|
||
|
awardPoints(3);
|
||
|
console.log(`Points after first contribution: ${localStorage.getItem('oedb_points')}`);
|
||
|
awardPoints(3);
|
||
|
console.log(`Points after second contribution: ${localStorage.getItem('oedb_points')}`);
|
||
|
|
||
|
// Test contribution storage
|
||
|
saveContribution("test-event-123", true);
|
||
|
saveContribution("test-event-456", false);
|
||
|
const contributions = JSON.parse(localStorage.getItem('oedb_contributions') || '[]');
|
||
|
console.log(`Number of contributions: ${contributions.length}`);
|
||
|
console.log(`First contribution: ${JSON.stringify(contributions[0])}`);
|
||
|
}
|
||
|
|
||
|
// Test UI updates
|
||
|
function testUIUpdates() {
|
||
|
console.log("Testing UI updates...");
|
||
|
|
||
|
// Test user info panel
|
||
|
updateUserInfoDisplay();
|
||
|
const panel = document.getElementById('user-info-panel');
|
||
|
console.log(`User info panel created: ${panel !== null}`);
|
||
|
|
||
|
// Test popup content generation
|
||
|
const popupContent = generateTestPopupContent();
|
||
|
console.log("Popup content generated");
|
||
|
|
||
|
// Create a test div to display the popup content
|
||
|
const testDiv = document.createElement('div');
|
||
|
testDiv.innerHTML = popupContent;
|
||
|
testDiv.style.position = 'fixed';
|
||
|
testDiv.style.top = '50%';
|
||
|
testDiv.style.left = '50%';
|
||
|
testDiv.style.transform = 'translate(-50%, -50%)';
|
||
|
testDiv.style.backgroundColor = 'white';
|
||
|
testDiv.style.padding = '20px';
|
||
|
testDiv.style.boxShadow = '0 0 10px rgba(0,0,0,0.5)';
|
||
|
testDiv.style.zIndex = '1000';
|
||
|
testDiv.style.maxWidth = '400px';
|
||
|
testDiv.style.borderRadius = '4px';
|
||
|
|
||
|
// Add close button
|
||
|
const closeButton = document.createElement('button');
|
||
|
closeButton.textContent = 'Close Test';
|
||
|
closeButton.style.marginTop = '10px';
|
||
|
closeButton.style.padding = '5px 10px';
|
||
|
closeButton.style.backgroundColor = '#ddd';
|
||
|
closeButton.style.border = 'none';
|
||
|
closeButton.style.borderRadius = '4px';
|
||
|
closeButton.style.cursor = 'pointer';
|
||
|
closeButton.onclick = function() {
|
||
|
document.body.removeChild(testDiv);
|
||
|
};
|
||
|
|
||
|
testDiv.appendChild(closeButton);
|
||
|
document.body.appendChild(testDiv);
|
||
|
}
|
||
|
|
||
|
// Helper function to generate test popup content
|
||
|
function generateTestPopupContent() {
|
||
|
let popupContent = `
|
||
|
<h3>${mockEvent.properties.label}</h3>
|
||
|
<p>Type: ${mockEvent.properties.what}</p>
|
||
|
<p>Start: ${mockEvent.properties.start}</p>
|
||
|
<p>End: ${mockEvent.properties.stop}</p>
|
||
|
<div class="reality-check">
|
||
|
<p>Is this traffic event still present?</p>
|
||
|
<div class="reality-check-buttons">
|
||
|
<button class="confirm-btn" onclick="alert('Confirm clicked')">Yes, still there</button>
|
||
|
<button class="deny-btn" onclick="alert('Deny clicked')">No, it's gone</button>
|
||
|
</div>
|
||
|
</div>
|
||
|
`;
|
||
|
|
||
|
return popupContent;
|
||
|
}
|
||
|
|
||
|
// Run tests
|
||
|
function runTests() {
|
||
|
console.log("Running tests...");
|
||
|
testCheckIfNeedsRealityCheck();
|
||
|
testLocalStorage();
|
||
|
testUIUpdates();
|
||
|
console.log("Tests completed!");
|
||
|
}
|
||
|
|
||
|
// Execute tests when loaded in browser
|
||
|
if (typeof window !== 'undefined') {
|
||
|
// Wait for DOM to be ready
|
||
|
if (document.readyState === 'complete') {
|
||
|
runTests();
|
||
|
} else {
|
||
|
window.addEventListener('load', runTests);
|
||
|
}
|
||
|
}
|