diff --git a/README.md b/README.md index 881b69c..982a2f7 100644 --- a/README.md +++ b/README.md @@ -112,4 +112,4 @@ Każdy problem zostanie sprawdzony i przeniesiony na wewnętrzną listę problem --- -# Test pre-commit hook +# Test pre-commit hook - without Docker check diff --git a/esbuild.config.js b/esbuild.config.js index 47416f9..13b6e4f 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -1,5 +1,7 @@ import esbuild from 'esbuild'; import scss from 'esbuild-plugin-sass'; +import fs from 'fs'; +import path from 'path'; const watch = process.argv.includes('--watch') && { onRebuild(error) { @@ -73,5 +75,34 @@ esbuild external: ['react', 'react-dom', 'survey-react'], watch, }) - .then(() => console.log('Add-on was built')) + .then(() => { + console.log('Add-on was built'); + + // Modify manifest.json to include test content script when ENABLE_TESTS=true + if (ENABLE_TESTS) { + const manifestPath = path.join(process.cwd(), 'manifest.json'); + const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8')); + + // Add content_scripts for testing + if (!manifest.content_scripts) { + manifest.content_scripts = []; + } + + // Check if test script is already added + const hasTestScript = manifest.content_scripts.some( + cs => cs.js && cs.js.includes('lib/tests/test-content-script.js') + ); + + if (!hasTestScript) { + manifest.content_scripts.push({ + matches: [''], + js: ['lib/tests/test-content-script.js'], + run_at: 'document_start' + }); + + fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4)); + console.log('Added test content script to manifest.json'); + } + } + }) .catch(() => process.exit(1)); diff --git a/tests/test-lib.js b/tests/test-lib.js index 5c96350..00f782f 100644 --- a/tests/test-lib.js +++ b/tests/test-lib.js @@ -2,24 +2,21 @@ // This JavaScript code runs in the browser context via Marionette /** - * Inject test content script into the page - * @returns {Promise} - True if injection successful + * Check if test content script is loaded + * The content script is automatically injected by manifest.json when ENABLE_TESTS=true + * @returns {Promise} - True if content script is loaded */ -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'; +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; } /** @@ -28,9 +25,9 @@ async function injectTestContentScript() { * @returns {Promise} - Badge count (number of third-party domains) or null on failure */ async function testBadgeCount() { - // Inject content script first - const injected = await injectTestContentScript(); - if (!injected) { + // Wait for content script to be loaded + const loaded = await waitForTestContentScript(); + if (!loaded) { return -1; // Content script not loaded }