|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import type { SafeLockReturn } from '../safeLock'; |
| 4 | +import { SessionCookiePoller } from '../SessionCookiePoller'; |
| 5 | + |
| 6 | +describe('SessionCookiePoller', () => { |
| 7 | + beforeEach(() => { |
| 8 | + vi.useFakeTimers(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + vi.useRealTimers(); |
| 13 | + vi.restoreAllMocks(); |
| 14 | + }); |
| 15 | + |
| 16 | + describe('shared lock coordination', () => { |
| 17 | + it('accepts an external lock for coordination with other components', () => { |
| 18 | + const sharedLock: SafeLockReturn = { |
| 19 | + acquireLockAndRun: vi.fn().mockResolvedValue(undefined), |
| 20 | + }; |
| 21 | + |
| 22 | + const poller = new SessionCookiePoller(sharedLock); |
| 23 | + const callback = vi.fn().mockResolvedValue(undefined); |
| 24 | + |
| 25 | + poller.startPollingForSessionToken(callback); |
| 26 | + |
| 27 | + // Verify the shared lock is used |
| 28 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledWith(callback); |
| 29 | + |
| 30 | + poller.stopPollingForSessionToken(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('creates internal lock when none provided (backward compatible)', () => { |
| 34 | + // Should not throw when no lock is provided |
| 35 | + const poller = new SessionCookiePoller(); |
| 36 | + expect(poller).toBeInstanceOf(SessionCookiePoller); |
| 37 | + }); |
| 38 | + |
| 39 | + it('enables focus handler and poller to share the same lock', () => { |
| 40 | + // This test demonstrates the shared lock pattern used in AuthCookieService |
| 41 | + const sharedLock: SafeLockReturn = { |
| 42 | + acquireLockAndRun: vi.fn().mockImplementation(async (cb: () => Promise<unknown>) => { |
| 43 | + return cb(); |
| 44 | + }), |
| 45 | + }; |
| 46 | + |
| 47 | + const poller = new SessionCookiePoller(sharedLock); |
| 48 | + const pollerCallback = vi.fn().mockResolvedValue('poller-result'); |
| 49 | + |
| 50 | + // Poller uses the shared lock |
| 51 | + poller.startPollingForSessionToken(pollerCallback); |
| 52 | + |
| 53 | + // Simulate focus handler also using the shared lock (like AuthCookieService does) |
| 54 | + const focusCallback = vi.fn().mockResolvedValue('focus-result'); |
| 55 | + void sharedLock.acquireLockAndRun(focusCallback); |
| 56 | + |
| 57 | + // Both should use the same lock instance |
| 58 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledTimes(2); |
| 59 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledWith(pollerCallback); |
| 60 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledWith(focusCallback); |
| 61 | + |
| 62 | + poller.stopPollingForSessionToken(); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('startPollingForSessionToken', () => { |
| 67 | + it('executes callback immediately on start', () => { |
| 68 | + const sharedLock: SafeLockReturn = { |
| 69 | + acquireLockAndRun: vi.fn().mockResolvedValue(undefined), |
| 70 | + }; |
| 71 | + |
| 72 | + const poller = new SessionCookiePoller(sharedLock); |
| 73 | + const callback = vi.fn().mockResolvedValue(undefined); |
| 74 | + |
| 75 | + poller.startPollingForSessionToken(callback); |
| 76 | + |
| 77 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledWith(callback); |
| 78 | + |
| 79 | + poller.stopPollingForSessionToken(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('prevents multiple concurrent polling sessions', () => { |
| 83 | + const sharedLock: SafeLockReturn = { |
| 84 | + acquireLockAndRun: vi.fn().mockResolvedValue(undefined), |
| 85 | + }; |
| 86 | + |
| 87 | + const poller = new SessionCookiePoller(sharedLock); |
| 88 | + const callback = vi.fn().mockResolvedValue(undefined); |
| 89 | + |
| 90 | + poller.startPollingForSessionToken(callback); |
| 91 | + poller.startPollingForSessionToken(callback); // Second call should be ignored |
| 92 | + |
| 93 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledTimes(1); |
| 94 | + |
| 95 | + poller.stopPollingForSessionToken(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('stopPollingForSessionToken', () => { |
| 100 | + it('allows restart after stop', async () => { |
| 101 | + const sharedLock: SafeLockReturn = { |
| 102 | + acquireLockAndRun: vi.fn().mockResolvedValue(undefined), |
| 103 | + }; |
| 104 | + |
| 105 | + const poller = new SessionCookiePoller(sharedLock); |
| 106 | + const callback = vi.fn().mockResolvedValue(undefined); |
| 107 | + |
| 108 | + // Start and stop |
| 109 | + poller.startPollingForSessionToken(callback); |
| 110 | + poller.stopPollingForSessionToken(); |
| 111 | + |
| 112 | + // Clear mock to check restart |
| 113 | + vi.mocked(sharedLock.acquireLockAndRun).mockClear(); |
| 114 | + |
| 115 | + // Should be able to start again |
| 116 | + poller.startPollingForSessionToken(callback); |
| 117 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledTimes(1); |
| 118 | + |
| 119 | + poller.stopPollingForSessionToken(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('polling interval', () => { |
| 124 | + it('schedules next poll after callback completes', async () => { |
| 125 | + const sharedLock: SafeLockReturn = { |
| 126 | + acquireLockAndRun: vi.fn().mockResolvedValue(undefined), |
| 127 | + }; |
| 128 | + |
| 129 | + const poller = new SessionCookiePoller(sharedLock); |
| 130 | + const callback = vi.fn().mockResolvedValue(undefined); |
| 131 | + |
| 132 | + poller.startPollingForSessionToken(callback); |
| 133 | + |
| 134 | + // Initial call |
| 135 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledTimes(1); |
| 136 | + |
| 137 | + // Wait for first interval (5 seconds) |
| 138 | + await vi.advanceTimersByTimeAsync(5000); |
| 139 | + |
| 140 | + // Should have scheduled another call |
| 141 | + expect(sharedLock.acquireLockAndRun).toHaveBeenCalledTimes(2); |
| 142 | + |
| 143 | + poller.stopPollingForSessionToken(); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments