|
| 1 | +/** |
| 2 | + * Unit tests for getWindowIdFromContext function in popup.js |
| 3 | + * Tests the window ID bug fix that ensures the correct window ID is selected |
| 4 | + * based on URL hash parameters (quick-switch mode) vs current window (extension icon). |
| 5 | + */ |
| 6 | + |
| 7 | +import { getWindowIdFromContext } from '../js/popup.js'; |
| 8 | + |
| 9 | +describe('getWindowIdFromContext', () => { |
| 10 | + describe('Window ID from URL hash (quick-switch mode)', () => { |
| 11 | + it('should use windowId from hash when present and valid', () => { |
| 12 | + const urlString = 'popup.html#windowId=456&action=switch'; |
| 13 | + const currentWindowId = 100; |
| 14 | + |
| 15 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 16 | + |
| 17 | + expect(result).toBe(456); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should handle parameters in different order', () => { |
| 21 | + const urlString = 'popup.html#action=switch&tabId=10&windowId=777&sessionName=test'; |
| 22 | + const currentWindowId = 100; |
| 23 | + |
| 24 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 25 | + |
| 26 | + expect(result).toBe(777); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('Fallback to current window', () => { |
| 31 | + it('should use current window when windowId is not in hash', () => { |
| 32 | + const urlString = 'popup.html#action=switch'; |
| 33 | + const currentWindowId = 100; |
| 34 | + |
| 35 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 36 | + |
| 37 | + expect(result).toBe(100); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should use current window when windowId is "false"', () => { |
| 41 | + const urlString = 'popup.html#windowId=false'; |
| 42 | + const currentWindowId = 100; |
| 43 | + |
| 44 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 45 | + |
| 46 | + expect(result).toBe(100); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should use current window when no hash present', () => { |
| 50 | + const urlString = 'popup.html'; |
| 51 | + const currentWindowId = 100; |
| 52 | + |
| 53 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 54 | + |
| 55 | + expect(result).toBe(100); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('Invalid window ID values', () => { |
| 60 | + it('should return false when windowId is NaN', () => { |
| 61 | + const urlString = 'popup.html#windowId=invalid'; |
| 62 | + const currentWindowId = 100; |
| 63 | + |
| 64 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 65 | + |
| 66 | + expect(result).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return false when windowId is zero or negative', () => { |
| 70 | + expect(getWindowIdFromContext('popup.html#windowId=0', 100)).toBe(false); |
| 71 | + expect(getWindowIdFromContext('popup.html#windowId=-5', 100)).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return false when current window ID is invalid', () => { |
| 75 | + expect(getWindowIdFromContext('popup.html', 0)).toBe(false); |
| 76 | + expect(getWindowIdFromContext('popup.html', -1)).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should return false when currentWindowId is null and no hash value', () => { |
| 80 | + const result = getWindowIdFromContext('popup.html', null); |
| 81 | + expect(result).toBe(false); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('Bug fix verification', () => { |
| 86 | + it('BUG FIX: should prioritize hash windowId over currentWindowId in quick-switch mode', () => { |
| 87 | + // This is the core bug that was fixed |
| 88 | + // The popup window itself has ID 100, but we want to use the original window ID 456 |
| 89 | + const urlString = 'popup.html#windowId=456&action=switch'; |
| 90 | + const currentWindowId = 100; // This would be the popup window's ID |
| 91 | + |
| 92 | + const result = getWindowIdFromContext(urlString, currentWindowId); |
| 93 | + |
| 94 | + // MUST use 456 from hash, NOT 100 from currentWindowId |
| 95 | + expect(result).toBe(456); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should use currentWindowId when opened from extension icon (no hash)', () => { |
| 99 | + const result = getWindowIdFromContext('popup.html', 200); |
| 100 | + expect(result).toBe(200); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments