/** * Integration test for Tampermonkey userscript functionality * Tests that the userscript logic works correctly in a browser context */ import puppeteer from 'puppeteer'; import { readFile } from 'fs/promises'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); describe('BSky Cleaner Userscript Tests', () => { let browser; let page; beforeAll(async () => { browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'], }); }); afterAll(async () => { if (browser) { await browser.close(); } }); beforeEach(async () => { page = await browser.newPage(); }); afterEach(async () => { if (page) { await page.close(); } }); test('should remove bookmark buttons with default settings', async () => { const htmlPath = join(__dirname, 'test-fixtures.html'); const html = await readFile(htmlPath, 'utf-8'); await page.setContent(html); // Simulate userscript logic const userscriptCode = await readFile( join(__dirname, '../../userscripts/bsky-cleaner.user.js'), 'utf-8' ); // Extract the core logic (without Tampermonkey header) const coreLogic = userscriptCode.replace(/^\/\/ ==UserScript==[\s\S]*?==\/UserScript==\s*/m, ''); // Create a testable version await page.evaluate( ` const SETTINGS = { enabled: true, mode: 'remove', removeBookmark: true, removeRepost: false, hideDisplayName: false, customSelectors: [], mutationObserver: false, debounceMs: 300, debug: false, dryRun: false, }; function applyRule(selector, action) { let count = 0; try { const elements = document.querySelectorAll(selector); for (const element of Array.from(elements)) { if (action === 'remove') { element.remove(); count++; } } } catch (error) { console.error('Error:', error); } return count; } // Apply bookmark removal if (SETTINGS.removeBookmark) { const selector = "button[aria-label*='Bookmark'], .action-bookmark, [data-testid*='bookmark']"; applyRule(selector, SETTINGS.mode); } `, coreLogic ); // Check that bookmark buttons are removed const bookmarkCount = await page.$$eval( "button[aria-label*='Bookmark'], .action-bookmark", (els) => els.length ); expect(bookmarkCount).toBe(0); }); test('should hide elements when mode is "hide"', async () => { const htmlPath = join(__dirname, 'test-fixtures.html'); const html = await readFile(htmlPath, 'utf-8'); await page.setContent(html); await page.evaluate(` const SETTINGS = { mode: 'hide', removeBookmark: true, }; const selector = "button[aria-label*='Bookmark'], .action-bookmark"; const elements = document.querySelectorAll(selector); elements.forEach(el => { el.style.setProperty('display', 'none', 'important'); }); `); // Check that elements are hidden (not removed) const bookmarkButtons = await page.$$("button[aria-label*='Bookmark'], .action-bookmark"); expect(bookmarkButtons.length).toBeGreaterThan(0); const isHidden = await page.evaluate(() => { const elements = document.querySelectorAll("button[aria-label*='Bookmark'], .action-bookmark"); return Array.from(elements).every((el) => { const style = window.getComputedStyle(el); return style.display === 'none'; }); }); expect(isHidden).toBe(true); }); test('should respect dryRun mode', async () => { const htmlPath = join(__dirname, 'test-fixtures.html'); const html = await readFile(htmlPath, 'utf-8'); await page.setContent(html); const initialCount = await page.$$eval( "button[aria-label*='Repost']", (els) => els.length ); // Simulate dry run - elements should not be removed const logs = []; await page.evaluate( ` const SETTINGS = { dryRun: true, mode: 'remove', removeRepost: true }; const selector = "button[aria-label*='Repost']"; const elements = document.querySelectorAll(selector); if (SETTINGS.dryRun) { for (const element of Array.from(elements)) { console.log('Would remove:', element.tagName); } } else { elements.forEach(el => el.remove()); } `, logs ); // Elements should still be present const finalCount = await page.$$eval("button[aria-label*='Repost']", (els) => els.length); expect(finalCount).toBe(initialCount); }); test('should apply custom selectors', async () => { const htmlPath = join(__dirname, 'test-fixtures.html'); const html = await readFile(htmlPath, 'utf-8'); await page.setContent(html); await page.evaluate(` const customSelectors = [ { selector: '.handle', action: 'remove' } ]; for (const rule of customSelectors) { const elements = document.querySelectorAll(rule.selector); elements.forEach(el => el.remove()); } `); const handleCount = await page.$$eval('.handle', (els) => els.length); expect(handleCount).toBe(0); }); test('should handle multiple rules correctly', async () => { const htmlPath = join(__dirname, 'test-fixtures.html'); const html = await readFile(htmlPath, 'utf-8'); await page.setContent(html); const initialBookmarkCount = await page.$$eval( "button[aria-label*='Bookmark'], .action-bookmark", (els) => els.length ); const initialRepostCount = await page.$$eval( "button[aria-label*='Repost']", (els) => els.length ); await page.evaluate(` const rules = [ { selector: "button[aria-label*='Bookmark'], .action-bookmark", action: 'remove' }, { selector: "button[aria-label*='Repost']", action: 'remove' } ]; for (const rule of rules) { const elements = document.querySelectorAll(rule.selector); elements.forEach(el => el.remove()); } `); const finalBookmarkCount = await page.$$eval( "button[aria-label*='Bookmark'], .action-bookmark", (els) => els.length ); const finalRepostCount = await page.$$eval( "button[aria-label*='Repost']", (els) => els.length ); expect(finalBookmarkCount).toBe(0); expect(finalRepostCount).toBe(0); expect(initialBookmarkCount).toBeGreaterThan(0); expect(initialRepostCount).toBeGreaterThan(0); }); }); if (import.meta.url === `file://${process.argv[1]}`) { console.log('Running userscript tests...'); console.log('Tests should be run with: npm test'); }