1
0
forked from icd/rentgen

refactor: make test code conditional via ENABLE_TESTS env var

Test code in background.ts and test-content-script.js now only builds
when ENABLE_TESTS=true is set. Production builds (default) exclude test
code completely via esbuild's define feature.

Changes:
- esbuild.config.js: conditionally add test entrypoints and define ENABLE_TESTS
- background.ts: wrap test message listener in if (ENABLE_TESTS) block
- Dockerfile: add test_builder stage that builds with ENABLE_TESTS=true
- package-lock.json: updated from npm install

Verified with typecheck and lint.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Jacek Wielemborek 2025-10-27 15:21:03 +00:00
parent e6f025335a
commit 7d57d3cc07
4 changed files with 401 additions and 134 deletions

View File

@ -20,12 +20,32 @@ RUN npm install
# Copy source code (respecting .dockerignore) # Copy source code (respecting .dockerignore)
COPY . . COPY . .
# Build the extension for Firefox (default) # Build the extension for Firefox (default) - without tests
RUN npm run build RUN npm run build
# Create the package # Create the package
RUN npm run create-package RUN npm run create-package
# Test builder stage - builds with ENABLE_TESTS=true
FROM node:lts AS test_builder
WORKDIR /app
# Copy package files for dependency installation
COPY package.json package-lock.json ./
# Install dependencies
RUN npm install
# Copy source code
COPY . .
# Build with tests enabled
RUN ENABLE_TESTS=true npm run build
# Create the package
RUN npm run create-package
# Code quality stage - for running quality checks # Code quality stage - for running quality checks
FROM builder AS code_quality FROM builder AS code_quality
RUN npm run typecheck && npm run lint RUN npm run typecheck && npm run lint
@ -47,8 +67,8 @@ FROM node:lts AS runtime
WORKDIR /app WORKDIR /app
# Copy built extension from builder # Copy built extension from test_builder (includes test code)
COPY --from=builder /app /app COPY --from=test_builder /app /app
# Install Firefox and Xvfb for headless execution (cached layer) # Install Firefox and Xvfb for headless execution (cached layer)
RUN apt-get update && apt-get install -y \ RUN apt-get update && apt-get install -y \

View File

@ -2,29 +2,32 @@ import { init } from "./memory";
// Use global browser object directly (available in extension context) // Use global browser object directly (available in extension context)
declare const browser: any; declare const browser: any;
declare const ENABLE_TESTS: boolean;
init(); init();
// Test verification handler for Marionette tests // Test verification handler for Marionette tests
// This proves the background script is executing and can communicate with content scripts // This proves the background script is executing and can communicate with content scripts
browser.runtime.onMessage.addListener((message: any, sender: any, sendResponse: any) => { if (ENABLE_TESTS) {
if (message.type === 'RENTGEN_TEST_VERIFICATION') { browser.runtime.onMessage.addListener((message: any, sender: any, sendResponse: any) => {
// Perform a computation to prove the background script is running if (message.type === 'RENTGEN_TEST_VERIFICATION') {
// This is not just an echo - we're doing actual processing // Perform a computation to prove the background script is running
const inputValue = message.inputValue || 0; // This is not just an echo - we're doing actual processing
const computed = (inputValue * 2) + 3; const inputValue = message.inputValue || 0;
const computed = (inputValue * 2) + 3;
// Send back a response with computed value and metadata // Send back a response with computed value and metadata
const response = { const response = {
success: true, success: true,
computed: computed, computed: computed,
formula: `(${inputValue} * 2) + 3 = ${computed}`, formula: `(${inputValue} * 2) + 3 = ${computed}`,
backgroundTimestamp: Date.now(), backgroundTimestamp: Date.now(),
receivedFrom: message.url || 'unknown', receivedFrom: message.url || 'unknown',
originalInput: inputValue originalInput: inputValue
}; };
sendResponse(response); sendResponse(response);
return true; // Keep channel open for async response return true; // Keep channel open for async response
} }
}); });
}

View File

@ -8,6 +8,8 @@ const watch = process.argv.includes('--watch') && {
}, },
}; };
const ENABLE_TESTS = process.env.ENABLE_TESTS === 'true';
// see https://github.com/evanw/esbuild/issues/806#issuecomment-779138268 // see https://github.com/evanw/esbuild/issues/806#issuecomment-779138268
let skipReactImports = { let skipReactImports = {
name: 'skipReactImports', name: 'skipReactImports',
@ -41,18 +43,23 @@ let skipReactImports = {
}, },
}; };
const entryPoints = [
'components/toolbar/toolbar.tsx',
'components/sidebar/sidebar.tsx',
'components/report-window/report-window.tsx',
'background.ts',
'diag.tsx',
'styles/global.scss',
'styles/fonts.scss',
];
if (ENABLE_TESTS) {
entryPoints.push('tests/test-content-script.js');
}
esbuild esbuild
.build({ .build({
entryPoints: [ entryPoints,
'components/toolbar/toolbar.tsx',
'components/sidebar/sidebar.tsx',
'components/report-window/report-window.tsx',
'background.ts',
'tests/test-content-script.js',
'diag.tsx',
'styles/global.scss',
'styles/fonts.scss',
],
bundle: true, bundle: true,
// minify: true, // minify: true,
outdir: './lib', outdir: './lib',
@ -61,6 +68,7 @@ esbuild
define: { define: {
PLUGIN_NAME: '"Rentgen"', PLUGIN_NAME: '"Rentgen"',
PLUGIN_URL: '"https://addons.mozilla.org/pl/firefox/addon/rentgen/"', PLUGIN_URL: '"https://addons.mozilla.org/pl/firefox/addon/rentgen/"',
ENABLE_TESTS: String(ENABLE_TESTS),
}, },
external: ['react', 'react-dom', 'survey-react'], external: ['react', 'react-dom', 'survey-react'],
watch, watch,

440
package-lock.json generated

File diff suppressed because it is too large Load Diff