1
0
forked from icd/rentgen
rentgen/tests/test-lib.js

56 lines
1.8 KiB
JavaScript

// Test library for Marionette-based extension verification
// This JavaScript code runs in the browser context via Marionette
/**
* Check if test content script is loaded
* The content script is automatically injected by manifest.json when ENABLE_TESTS=true
* @returns {Promise<boolean>} - True if content script is loaded
*/
async function waitForTestContentScript() {
// Wait for content script to set the marker
let attempts = 0;
while (attempts < 50) {
if (document.body && document.body.getAttribute('data-rentgen-injected') === 'true') {
return true;
}
await new Promise(resolve => setTimeout(resolve, 100));
attempts++;
}
return false;
}
/**
* Test that background script returns badge count correctly
* Tests real Rentgen functionality: counting third-party domains
* @returns {Promise<number|null>} - Badge count (number of third-party domains) or null on failure
*/
async function testBadgeCount() {
// Wait for content script to be loaded
const loaded = await waitForTestContentScript();
if (!loaded) {
return -1; // Content script not loaded
}
// Dispatch test request to content script
document.dispatchEvent(new CustomEvent('rentgen_test_request', {
detail: { timestamp: Date.now() }
}));
// Wait for background response with badge count
return new Promise((resolve) => {
let attempts = 0;
const checkInterval = setInterval(() => {
attempts++;
const badgeCount = document.body.getAttribute('data-rentgen-badge-count');
if (badgeCount !== null) {
clearInterval(checkInterval);
resolve(parseInt(badgeCount));
} else if (attempts > 50) {
clearInterval(checkInterval);
resolve(null);
}
}, 100);
});
}