|
| 1 | +import assert from 'assert'; |
| 2 | +import { By, until, Key } from 'selenium-webdriver'; |
| 3 | +import FirefoxExtensionHelper from '../helpers/firefox-extension.js'; |
| 4 | + |
| 5 | +describe('PerfChaser Extension - Popup Interval Tests', function() { |
| 6 | + let helper; |
| 7 | + let driver; |
| 8 | + |
| 9 | + before(async function() { |
| 10 | + helper = new FirefoxExtensionHelper(); |
| 11 | + driver = await helper.init(); |
| 12 | + }); |
| 13 | + |
| 14 | + after(async function() { |
| 15 | + await helper.quit(); |
| 16 | + }); |
| 17 | + |
| 18 | + describe('Update Interval Controls', function() { |
| 19 | + it('should display the update interval number input with correct attributes', async function() { |
| 20 | + await driver.get(helper.popupUrl); |
| 21 | + |
| 22 | + const input = await driver.findElement(By.css('#update-interval-input')); |
| 23 | + assert.ok(input, 'Update interval input should exist'); |
| 24 | + |
| 25 | + const inputType = await input.getAttribute('type'); |
| 26 | + assert.strictEqual(inputType, 'number', 'Input should be type number'); |
| 27 | + |
| 28 | + const value = await input.getAttribute('value'); |
| 29 | + assert.strictEqual(value, '5', 'Default interval should be 5 seconds'); |
| 30 | + |
| 31 | + const min = await input.getAttribute('min'); |
| 32 | + const max = await input.getAttribute('max'); |
| 33 | + assert.strictEqual(min, '1', 'Min interval should be 1 second'); |
| 34 | + assert.strictEqual(max, '5', 'Max interval should be 5 seconds'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should allow changing the interval value', async function() { |
| 38 | + await driver.get(helper.popupUrl); |
| 39 | + |
| 40 | + const input = await driver.findElement(By.css('#update-interval-input')); |
| 41 | + |
| 42 | + // Clear and set new value |
| 43 | + await input.clear(); |
| 44 | + await input.sendKeys('2', Key.RETURN); |
| 45 | + |
| 46 | + const newValue = await input.getAttribute('value'); |
| 47 | + assert.strictEqual(newValue, '2', 'Interval value should be updated to 2'); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('Interval Application to Background Script', function() { |
| 52 | + it('should apply interval changes to sidebar updates', async function() { |
| 53 | + // This test verifies that changing the interval in the popup |
| 54 | + // correctly updates the background script's alarm timer |
| 55 | + |
| 56 | + // Step 1: Set a short interval (1 second) via the popup |
| 57 | + await driver.get(helper.popupUrl); |
| 58 | + const input = await driver.findElement(By.css('#update-interval-input')); |
| 59 | + await input.clear(); |
| 60 | + await input.sendKeys('1', Key.RETURN); |
| 61 | + |
| 62 | + // Step 2: Open sidebar and wait for initial data |
| 63 | + await driver.get(helper.sidebarUrl); |
| 64 | + await driver.wait(async () => { |
| 65 | + const rows = await driver.findElements(By.css('#tbody-processes tr')); |
| 66 | + return rows.length > 0; |
| 67 | + }, 10000, 'Expected initial process data to be loaded'); |
| 68 | + |
| 69 | + // Step 3: Set up a MutationObserver to detect table updates |
| 70 | + await helper.setupTableObserver(); |
| 71 | + |
| 72 | + const startTime = Date.now(); |
| 73 | + |
| 74 | + // Step 4: Wait for a table update to occur |
| 75 | + await driver.wait(async () => { |
| 76 | + const updateCount = await driver.executeScript('return window.__updateCount'); |
| 77 | + return updateCount > 0; |
| 78 | + }, 3000, 'Process table should update within 3 seconds with 1s interval'); |
| 79 | + |
| 80 | + const timeElapsed = Date.now() - startTime; |
| 81 | + |
| 82 | + // The update should happen within ~1-2 seconds (allowing some margin) |
| 83 | + assert.ok(timeElapsed < 2500, |
| 84 | + `Update should occur within ~2 seconds with 1s interval (took ${timeElapsed}ms)`); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should respect longer intervals', async function() { |
| 88 | + // Set interval to 5 seconds |
| 89 | + await driver.get(helper.popupUrl); |
| 90 | + const input = await driver.findElement(By.css('#update-interval-input')); |
| 91 | + await input.clear(); |
| 92 | + await input.sendKeys('5', Key.RETURN); |
| 93 | + |
| 94 | + // Go to sidebar |
| 95 | + await driver.get(helper.sidebarUrl); |
| 96 | + await driver.wait(async () => { |
| 97 | + const rows = await driver.findElements(By.css('#tbody-processes tr')); |
| 98 | + return rows.length > 0; |
| 99 | + }, 10000, 'Sidebar should load data'); |
| 100 | + |
| 101 | + // Set up MutationObserver to count table updates |
| 102 | + await helper.setupTableObserver(); |
| 103 | + |
| 104 | + // Wait for 7 seconds |
| 105 | + await driver.sleep(7000); |
| 106 | + |
| 107 | + // Check update count - should be at most 2 updates in 7 seconds with 5s interval |
| 108 | + // (one might happen right away, then one at ~5s, possibly one at ~10s but we stop at 7s) |
| 109 | + const updateCount = await driver.executeScript('return window.__updateCount'); |
| 110 | + |
| 111 | + assert.ok(updateCount <= 2, |
| 112 | + `With 5s interval, should have at most 2 updates in 7 seconds (got ${updateCount})`); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should immediately apply interval changes', async function() { |
| 116 | + // This test verifies that changing the interval immediately recreates the alarm |
| 117 | + // rather than waiting for the next scheduled update |
| 118 | + |
| 119 | + // Start with 5 second interval |
| 120 | + await driver.get(helper.popupUrl); |
| 121 | + let input = await driver.findElement(By.css('#update-interval-input')); |
| 122 | + await input.clear(); |
| 123 | + await input.sendKeys('5', Key.RETURN); |
| 124 | + |
| 125 | + // Wait a moment for the change to apply |
| 126 | + await driver.sleep(500); |
| 127 | + |
| 128 | + // Change to 1 second interval |
| 129 | + await driver.get(helper.popupUrl); |
| 130 | + input = await driver.findElement(By.css('#update-interval-input')); |
| 131 | + await input.clear(); |
| 132 | + await input.sendKeys('1', Key.RETURN); |
| 133 | + |
| 134 | + // Go to sidebar and set up MutationObserver |
| 135 | + await driver.get(helper.sidebarUrl); |
| 136 | + await driver.wait(async () => { |
| 137 | + const rows = await driver.findElements(By.css('#tbody-processes tr')); |
| 138 | + return rows.length > 0; |
| 139 | + }, 10000, 'Sidebar should load'); |
| 140 | + |
| 141 | + await helper.setupTableObserver(); |
| 142 | + |
| 143 | + // Should receive an update within ~2 seconds, not have to wait 5 seconds |
| 144 | + const startTime = Date.now(); |
| 145 | + await driver.wait(async () => { |
| 146 | + const updateCount = await driver.executeScript('return window.__updateCount'); |
| 147 | + return updateCount > 0; |
| 148 | + }, 3000, 'Should receive update quickly after interval change'); |
| 149 | + |
| 150 | + const timeElapsed = Date.now() - startTime; |
| 151 | + |
| 152 | + // Should not have to wait the old 5 second interval |
| 153 | + assert.ok(timeElapsed < 3000, |
| 154 | + `Update should occur within ~2s after changing to 1s interval (took ${timeElapsed}ms)`); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments