forked from icd/rentgen
- Remove red() function (not critical for tests) - Extract testUrl() helper for testing URLs with badge count - Add assertBadgeCount() for cleaner assertions - Use testCases array for easier test case management - Support '>0' as expected value for dynamic counts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
141 lines
4.3 KiB
JavaScript
Executable File
141 lines
4.3 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
/**
|
|
* test_verify.mjs - Extension badge count verification test using Selenium WebDriver
|
|
*
|
|
* 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 { Builder } from 'selenium-webdriver';
|
|
import firefox from 'selenium-webdriver/firefox.js';
|
|
import { readFileSync } from 'fs';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join } from 'path';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = dirname(__filename);
|
|
|
|
// Load test library once
|
|
const testLibPath = join(__dirname, 'test-lib.js');
|
|
const testLib = readFileSync(testLibPath, 'utf-8');
|
|
|
|
/**
|
|
* Test badge count for a specific URL
|
|
* @param {WebDriver} driver - Selenium WebDriver instance
|
|
* @param {string} url - URL to test
|
|
* @param {number} waitMs - How long to wait after page load
|
|
* @returns {Promise<number|null>} Badge count or null on failure
|
|
*/
|
|
async function testUrl(driver, url, waitMs = 5000) {
|
|
await driver.get(url);
|
|
await driver.sleep(waitMs);
|
|
|
|
const badgeCount = await driver.executeAsyncScript(`
|
|
const testLibCode = arguments[0];
|
|
const callback = arguments[1];
|
|
|
|
eval(testLibCode);
|
|
testBadgeCount().then(callback).catch(err => callback(null));
|
|
`, testLib);
|
|
|
|
return badgeCount;
|
|
}
|
|
|
|
/**
|
|
* Verify badge count matches expected value
|
|
* @param {number|null} actual - Actual badge count
|
|
* @param {number|string} expected - Expected value (number or '>0')
|
|
* @returns {boolean} Whether assertion passed
|
|
*/
|
|
function assertBadgeCount(actual, expected) {
|
|
if (actual === null) return false;
|
|
if (expected === '>0') return actual > 0;
|
|
return actual === expected;
|
|
}
|
|
|
|
async function testBadgeCount() {
|
|
let driver = null;
|
|
|
|
try {
|
|
const extensionPath = join(__dirname, '..');
|
|
|
|
console.log(' Launching Firefox with extension...');
|
|
|
|
const options = new firefox.Options();
|
|
if (process.env.HEADLESS !== 'false') {
|
|
options.addArguments('-headless');
|
|
}
|
|
|
|
driver = await new Builder()
|
|
.forBrowser('firefox')
|
|
.setFirefoxOptions(options)
|
|
.build();
|
|
|
|
console.log(' Installing extension...');
|
|
await driver.installAddon(extensionPath, true);
|
|
|
|
// Test cases: [url, expectedBadgeCount, waitMs, name]
|
|
const testCases = [
|
|
['https://news.ycombinator.com', 0, 5000, 'news.ycombinator.com'],
|
|
['https://pudelek.pl', '>0', 10000, 'pudelek.pl']
|
|
];
|
|
|
|
const results = {};
|
|
|
|
for (const [url, expected, waitMs, name] of testCases) {
|
|
console.log(` Testing ${name} (expected: ${expected === '>0' ? '>0' : expected} third-party domains)...`);
|
|
const badgeCount = await testUrl(driver, url, waitMs);
|
|
results[name] = badgeCount;
|
|
console.log(` → Badge count: ${badgeCount}`);
|
|
|
|
if (!assertBadgeCount(badgeCount, expected)) {
|
|
await driver.quit();
|
|
return {
|
|
success: false,
|
|
error: `${name}: expected ${expected}, got ${badgeCount}`
|
|
};
|
|
}
|
|
}
|
|
|
|
await driver.quit();
|
|
return { success: true, results };
|
|
|
|
} catch (error) {
|
|
if (driver) {
|
|
await driver.quit();
|
|
}
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
console.log('Starting Rentgen badge count verification test...');
|
|
console.log('');
|
|
|
|
const result = await testBadgeCount();
|
|
|
|
console.log('');
|
|
if (!result.success) {
|
|
console.log(`FAIL: ${result.error}`);
|
|
process.exit(1);
|
|
}
|
|
|
|
console.log('PASS: Badge count test succeeded!');
|
|
const resultNames = Object.keys(result.results);
|
|
for (const name of resultNames) {
|
|
console.log(` - ${name}: ${result.results[name]} third-party domains`);
|
|
}
|
|
process.exit(0);
|
|
}
|
|
|
|
// Handle signals
|
|
process.on('SIGINT', () => process.exit(130));
|
|
process.on('SIGTERM', () => process.exit(143));
|
|
|
|
main().catch(error => {
|
|
console.error(`ERROR: ${error.message}`);
|
|
process.exit(1);
|
|
});
|