1
0
forked from icd/rentgen
rentgen/tests/test-lib.js
Jacek Wielemborek e6f025335a fix: remove test content script from production manifest
- Removed content_scripts entry that injected test code into all pages
- Modified test-lib.js to dynamically inject content script only during tests
- Test code no longer affects production users

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-26 18:13:48 +01:00

59 lines
1.9 KiB
JavaScript

// Test library for Marionette-based extension verification
// This JavaScript code runs in the browser context via Marionette
/**
* Inject test content script into the page
* @returns {Promise<boolean>} - True if injection successful
*/
async function injectTestContentScript() {
// Read the content script file
const response = await fetch(browser.runtime.getURL('lib/tests/test-content-script.js'));
const scriptCode = await response.text();
// Inject it into the page
const script = document.createElement('script');
script.textContent = scriptCode;
document.documentElement.appendChild(script);
script.remove();
// Wait a bit for script to initialize
await new Promise(resolve => setTimeout(resolve, 100));
return document.body.getAttribute('data-rentgen-injected') === 'true';
}
/**
* Test that background script performs computation correctly
* @param {number} testValue - Input value for computation
* @returns {Promise<number|null>} - Computed result or null on failure
*/
async function testBackgroundComputation(testValue) {
// Inject content script first
const injected = await injectTestContentScript();
if (!injected) {
return -1; // Content script not loaded
}
// Dispatch test request to content script
document.dispatchEvent(new CustomEvent('rentgen_test_request', {
detail: { value: testValue, timestamp: Date.now() }
}));
// Wait for background response
return new Promise((resolve) => {
let attempts = 0;
const checkInterval = setInterval(() => {
attempts++;
const computed = document.body.getAttribute('data-rentgen-computed');
if (computed) {
clearInterval(checkInterval);
resolve(parseInt(computed));
} else if (attempts > 50) {
clearInterval(checkInterval);
resolve(null);
}
}, 100);
});
}