|
| 1 | +import { describe, it, expect, beforeEach, vi, Mock } from 'vitest'; |
| 2 | +import { BackgroundRefresh } from './background-refresh'; |
| 3 | +import { getFullDashboardData } from '../../lib/github'; |
| 4 | + |
| 5 | +// Mock the external github fetch integration |
| 6 | +vi.mock('../../lib/github', () => ({ |
| 7 | + getFullDashboardData: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +describe('BackgroundRefresh - Theme Contrast Equivalent (Mode Cohesion)', () => { |
| 11 | + let refresher: BackgroundRefresh; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + // Note: since it's a singleton, we use getInstance and reset |
| 15 | + // but the class is exported as a singleton instance 'backgroundRefresh'. |
| 16 | + // We can just import the class and use getInstance. |
| 17 | + refresher = BackgroundRefresh['getInstance'](); |
| 18 | + refresher.reset(); |
| 19 | + vi.clearAllMocks(); |
| 20 | + }); |
| 21 | + |
| 22 | + it('Dual Environment Setup: seamlessly swaps between Fresh (Light) and Stale (Dark) cache modes', () => { |
| 23 | + vi.spyOn(Date, 'now').mockReturnValue(new Date('2024-01-01T12:00:00Z').getTime()); |
| 24 | + |
| 25 | + // Fresh Environment (Light Mode equivalent) - Synced 1 minute ago |
| 26 | + const freshTimestamp = new Date('2024-01-01T11:59:00Z').toISOString(); |
| 27 | + expect(refresher.isStale(freshTimestamp)).toBe(false); |
| 28 | + |
| 29 | + // Stale Environment (Dark Mode equivalent) - Synced 15 minutes ago |
| 30 | + const staleTimestamp = new Date('2024-01-01T11:45:00Z').toISOString(); |
| 31 | + expect(refresher.isStale(staleTimestamp)).toBe(true); |
| 32 | + |
| 33 | + vi.restoreAllMocks(); |
| 34 | + }); |
| 35 | + |
| 36 | + it('Behavior Adaptation Cohesion: accurately transitions user from Idle to Active Job state', () => { |
| 37 | + // Mock getFullDashboardData to return a promise that doesn't resolve immediately |
| 38 | + let resolveJob: (value?: unknown) => void; |
| 39 | + (getFullDashboardData as Mock).mockReturnValue( |
| 40 | + new Promise((resolve) => { |
| 41 | + resolveJob = resolve; |
| 42 | + }) |
| 43 | + ); |
| 44 | + |
| 45 | + // Idle State |
| 46 | + expect(refresher.isJobActive('transition_user')).toBe(false); |
| 47 | + |
| 48 | + // Trigger action |
| 49 | + refresher.triggerRefresh('transition_user'); |
| 50 | + |
| 51 | + // Active State |
| 52 | + expect(refresher.isJobActive('transition_user')).toBe(true); |
| 53 | + |
| 54 | + // Cleanup: Resolve the promise to not leave hanging promises |
| 55 | + resolveJob!(undefined); |
| 56 | + }); |
| 57 | + |
| 58 | + it('Contrast Verification (Concurrency): rigidly rejects colliding background jobs to preserve active state contrast', () => { |
| 59 | + let resolveJob1: (value?: unknown) => void; |
| 60 | + (getFullDashboardData as Mock).mockReturnValue( |
| 61 | + new Promise((resolve) => { |
| 62 | + resolveJob1 = resolve; |
| 63 | + }) |
| 64 | + ); |
| 65 | + |
| 66 | + // Trigger first job |
| 67 | + refresher.triggerRefresh('collide_user'); |
| 68 | + expect(getFullDashboardData).toHaveBeenCalledTimes(1); |
| 69 | + expect(refresher.isJobActive('collide_user')).toBe(true); |
| 70 | + |
| 71 | + // Attempt to trigger overlapping second job |
| 72 | + refresher.triggerRefresh('collide_user'); |
| 73 | + |
| 74 | + // The integration should NOT be called a second time |
| 75 | + expect(getFullDashboardData).toHaveBeenCalledTimes(1); |
| 76 | + |
| 77 | + resolveJob1!(undefined); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Configuration Preservation (Error Resilience): clears active state smoothly upon integration failures without getting stuck', async () => { |
| 81 | + // Force a failure |
| 82 | + (getFullDashboardData as Mock).mockReturnValue(Promise.reject(new Error('Network failure'))); |
| 83 | + |
| 84 | + refresher.triggerRefresh('error_user'); |
| 85 | + expect(refresher.isJobActive('error_user')).toBe(true); |
| 86 | + |
| 87 | + // Wait for the microtask queue to process the catch/finally blocks |
| 88 | + await new Promise(process.nextTick); |
| 89 | + |
| 90 | + // State should have smoothly transitioned back to idle |
| 91 | + expect(refresher.isJobActive('error_user')).toBe(false); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Foreground/Background Cohesion (Sanitization): flawlessly aligns active states across disparate formatting inputs', () => { |
| 95 | + (getFullDashboardData as Mock).mockReturnValue(Promise.resolve()); |
| 96 | + |
| 97 | + // Start job with messy formatting |
| 98 | + refresher.triggerRefresh(' Messy_User '); |
| 99 | + |
| 100 | + // Check state with clean formatting |
| 101 | + expect(refresher.isJobActive('messy_user')).toBe(true); |
| 102 | + |
| 103 | + // Check state with different messy formatting |
| 104 | + expect(refresher.isJobActive(' MESSY_USER ')).toBe(true); |
| 105 | + }); |
| 106 | +}); |
0 commit comments