diff --git a/background.ts b/background.ts index 2a39f22..8d6150e 100644 --- a/background.ts +++ b/background.ts @@ -1,4 +1,4 @@ -import { init } from "./memory"; +import { init, getMemory } from "./memory"; // Use global browser object directly (available in extension context) declare const browser: any; @@ -7,23 +7,25 @@ declare const ENABLE_TESTS: boolean; init(); // Test verification handler for Marionette tests -// This proves the background script is executing and can communicate with content scripts +// Tests real Rentgen functionality: counting third-party domains if (ENABLE_TESTS) { browser.runtime.onMessage.addListener((message: any, sender: any, sendResponse: any) => { if (message.type === 'RENTGEN_TEST_VERIFICATION') { - // Perform a computation to prove the background script is running - // This is not just an echo - we're doing actual processing - const inputValue = message.inputValue || 0; - const computed = (inputValue * 2) + 3; + // Get the origin from message (sent by content script) + const origin = message.origin; - // Send back a response with computed value and metadata + // Access the memory to get clusters for this origin + const memory = getMemory(); + const clusters = memory.getClustersForOrigin(origin); + const badgeCount = Object.keys(clusters).length; + + // Send back the badge count (number of third-party domains) const response = { success: true, - computed: computed, - formula: `(${inputValue} * 2) + 3 = ${computed}`, - backgroundTimestamp: Date.now(), - receivedFrom: message.url || 'unknown', - originalInput: inputValue + badgeCount: badgeCount, + origin: origin, + clusterIds: Object.keys(clusters), + backgroundTimestamp: Date.now() }; sendResponse(response); diff --git a/tests/test-content-script.js b/tests/test-content-script.js index 507b67c..6f4ac73 100644 --- a/tests/test-content-script.js +++ b/tests/test-content-script.js @@ -27,33 +27,34 @@ document.addEventListener('rentgen_test_request', async (event) => { // Extract test data from event const testData = event.detail || {}; - const inputValue = testData.value || 42; const timestamp = testData.timestamp || Date.now(); - // Send message to background script and wait for response - // This proves background script is running and responsive + // Send message to background script to get badge count for this origin + // This tests real Rentgen functionality: counting third-party domains const response = await browser.runtime.sendMessage({ type: 'RENTGEN_TEST_VERIFICATION', - inputValue: inputValue, - timestamp: timestamp, + origin: window.location.origin, url: window.location.href, - title: document.title + title: document.title, + timestamp: timestamp }); // Store the response from background in DOM - // This provides undeniable proof of bidirectional communication + // This provides the badge count (number of third-party domains) if (response && response.success) { document.body.setAttribute('data-rentgen-verified', 'true'); - document.body.setAttribute('data-rentgen-computed', String(response.computed)); - document.body.setAttribute('data-rentgen-formula', response.formula); + document.body.setAttribute('data-rentgen-badge-count', String(response.badgeCount)); + document.body.setAttribute('data-rentgen-origin', response.origin); + document.body.setAttribute('data-rentgen-cluster-ids', JSON.stringify(response.clusterIds)); document.body.setAttribute('data-rentgen-background-timestamp', String(response.backgroundTimestamp)); // Also dispatch a custom event with the results document.dispatchEvent(new CustomEvent('rentgen_test_complete', { detail: { success: true, - computed: response.computed, - formula: response.formula, + badgeCount: response.badgeCount, + origin: response.origin, + clusterIds: response.clusterIds, backgroundTimestamp: response.backgroundTimestamp } })); diff --git a/tests/test-lib.js b/tests/test-lib.js index 6ab61d4..5c96350 100644 --- a/tests/test-lib.js +++ b/tests/test-lib.js @@ -23,11 +23,11 @@ async function injectTestContentScript() { } /** - * Test that background script performs computation correctly - * @param {number} testValue - Input value for computation - * @returns {Promise} - Computed result or null on failure + * Test that background script returns badge count correctly + * Tests real Rentgen functionality: counting third-party domains + * @returns {Promise} - Badge count (number of third-party domains) or null on failure */ -async function testBackgroundComputation(testValue) { +async function testBadgeCount() { // Inject content script first const injected = await injectTestContentScript(); if (!injected) { @@ -36,19 +36,19 @@ async function testBackgroundComputation(testValue) { // Dispatch test request to content script document.dispatchEvent(new CustomEvent('rentgen_test_request', { - detail: { value: testValue, timestamp: Date.now() } + detail: { timestamp: Date.now() } })); - // Wait for background response + // Wait for background response with badge count return new Promise((resolve) => { let attempts = 0; const checkInterval = setInterval(() => { attempts++; - const computed = document.body.getAttribute('data-rentgen-computed'); + const badgeCount = document.body.getAttribute('data-rentgen-badge-count'); - if (computed) { + if (badgeCount !== null) { clearInterval(checkInterval); - resolve(parseInt(computed)); + resolve(parseInt(badgeCount)); } else if (attempts > 50) { clearInterval(checkInterval); resolve(null); diff --git a/tests/test_verify.py b/tests/test_verify.py index f61a660..d55fddb 100755 --- a/tests/test_verify.py +++ b/tests/test_verify.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 """ -test_verify.py - Minimal extension verification test +test_verify.py - Extension badge count verification test -Verifies the extension background script is executing by testing -bidirectional communication with a simple addition operation. +Verifies Rentgen's core functionality: counting third-party domains. +Tests two scenarios: +1. Site without third-party domains (news.ycombinator.com) → badge = 0 +2. Site with third-party domains (pudelek.pl) → badge > 0 """ import sys @@ -50,8 +52,8 @@ def start_webext(): return webext.pid -def test_addition(): - """Test background script via Marionette. Returns (success, result).""" +def test_badge_count(): + """Test badge count for sites with/without third-party domains. Returns (success, results_dict).""" try: from marionette_driver.marionette import Marionette @@ -62,32 +64,47 @@ def test_addition(): client = Marionette(host='localhost', port=2828) client.start_session() - # Navigate to any page (needed for content script injection) - client.navigate("https://example.com") - time.sleep(5) - - # Test: background should compute (17 * 2) + 3 = 37 - test_value = 17 - expected = 37 - # Load test library test_lib_path = os.path.join(os.path.dirname(__file__), 'test-lib.js') with open(test_lib_path, 'r') as f: test_lib = f.read() - # Execute test - result = client.execute_script( - test_lib + "\nreturn testBackgroundComputation(arguments[0]);", - script_args=[test_value], + results = {} + + # Test 1: Site without third-party domains + print(" Testing news.ycombinator.com (expected: 0 third-party domains)...") + client.navigate("https://news.ycombinator.com") + time.sleep(5) # Wait for page load and tracking detection + + badge_count_hn = client.execute_script( + test_lib + "\nreturn testBadgeCount();", script_timeout=10000 ) + results['hn'] = badge_count_hn + print(f" → Badge count: {badge_count_hn}") + + # Test 2: Site with third-party domains + print(" Testing pudelek.pl (expected: >0 third-party domains)...") + client.navigate("https://pudelek.pl") + time.sleep(10) # Wait longer for page load and tracking detection + + badge_count_pudelek = client.execute_script( + test_lib + "\nreturn testBadgeCount();", + script_timeout=10000 + ) + results['pudelek'] = badge_count_pudelek + print(f" → Badge count: {badge_count_pudelek}") client.close() - if result == expected: - return True, expected + # Verify results + if badge_count_hn is None or badge_count_pudelek is None: + return False, "Test timed out or failed to get badge count" + + if badge_count_hn == 0 and badge_count_pudelek > 0: + return True, results else: - return False, result + return False, f"Unexpected results: HN={badge_count_hn} (expected 0), Pudelek={badge_count_pudelek} (expected >0)" except Exception as e: return False, str(e) @@ -107,17 +124,24 @@ def cleanup(xvfb_pid, webext_pid): def main(): """Main test.""" + print("Starting Rentgen badge count verification test...") + print() + xvfb_pid = start_xvfb() webext_pid = start_webext() - success, result = test_addition() + success, result = test_badge_count() cleanup(xvfb_pid, webext_pid) + print() if not success: - print(red(f"FAIL: Expected 37, got {result}")) + print(red(f"FAIL: {result}")) return 1 + print(f"PASS: Badge count test succeeded!") + print(f" - news.ycombinator.com: {result['hn']} third-party domains") + print(f" - pudelek.pl: {result['pudelek']} third-party domains") return 0