|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { BackgroundRefresh } from './background-refresh'; |
| 3 | +import { getFullDashboardData } from '../../lib/github'; |
| 4 | + |
| 5 | +vi.mock('../../lib/github', () => ({ |
| 6 | + getFullDashboardData: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +describe('BackgroundRefresh Mock Integrations', () => { |
| 10 | + let service: BackgroundRefresh; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + service = BackgroundRefresh.getInstance(); |
| 14 | + service.reset(); |
| 15 | + vi.clearAllMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + it('mocks standard asynchronous imports using stubs for background refresh', async () => { |
| 19 | + vi.mocked(getFullDashboardData).mockResolvedValue( |
| 20 | + {} as unknown as Awaited<ReturnType<typeof getFullDashboardData>> |
| 21 | + ); |
| 22 | + |
| 23 | + service.triggerRefresh('john_doe'); |
| 24 | + |
| 25 | + expect(service.isJobActive('john_doe')).toBe(true); |
| 26 | + expect(getFullDashboardData).toHaveBeenCalledWith('john_doe', { bypassCache: true }); |
| 27 | + }); |
| 28 | + |
| 29 | + it('tests service loading paths to ensure pending state overlays render', async () => { |
| 30 | + let resolvePromise!: (val: unknown) => void; |
| 31 | + const promise = new Promise((resolve) => { |
| 32 | + resolvePromise = resolve; |
| 33 | + }); |
| 34 | + vi.mocked(getFullDashboardData).mockReturnValue( |
| 35 | + promise as unknown as ReturnType<typeof getFullDashboardData> |
| 36 | + ); |
| 37 | + |
| 38 | + service.triggerRefresh('pending_user'); |
| 39 | + expect(service.isJobActive('pending_user')).toBe(true); |
| 40 | + |
| 41 | + resolvePromise({}); |
| 42 | + await promise; |
| 43 | + |
| 44 | + // Small microtask tick to let the finally block run |
| 45 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 46 | + |
| 47 | + expect(service.isJobActive('pending_user')).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('asserts local cache layers are queried before triggering database retrievals', () => { |
| 51 | + // Check if cache is stale or not. If not stale, we should not trigger database retrievals. |
| 52 | + const lastSyncedTenSecsAgo = new Date(Date.now() - 10000).toISOString(); |
| 53 | + const isStale = service.isStale(lastSyncedTenSecsAgo); |
| 54 | + |
| 55 | + expect(isStale).toBe(false); |
| 56 | + |
| 57 | + if (isStale) { |
| 58 | + service.triggerRefresh('cached_user'); |
| 59 | + } |
| 60 | + |
| 61 | + expect(getFullDashboardData).not.toHaveBeenCalled(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('verifies correct fallback procedures during fake endpoint timeout blocks', async () => { |
| 65 | + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 66 | + vi.mocked(getFullDashboardData).mockRejectedValue(new Error('timeout')); |
| 67 | + |
| 68 | + service.triggerRefresh('timeout_user'); |
| 69 | + expect(service.isJobActive('timeout_user')).toBe(true); |
| 70 | + |
| 71 | + // Wait for promise rejection to propagate |
| 72 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 73 | + |
| 74 | + expect(service.isJobActive('timeout_user')).toBe(false); |
| 75 | + expect(consoleErrorSpy).toHaveBeenCalled(); |
| 76 | + consoleErrorSpy.mockRestore(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('asserts complete cache sync is written on success callbacks', async () => { |
| 80 | + const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {}); |
| 81 | + vi.mocked(getFullDashboardData).mockResolvedValue({ success: true } as unknown as Awaited< |
| 82 | + ReturnType<typeof getFullDashboardData> |
| 83 | + >); |
| 84 | + |
| 85 | + service.triggerRefresh('sync_user'); |
| 86 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 87 | + |
| 88 | + expect(consoleInfoSpy).toHaveBeenCalledWith( |
| 89 | + expect.stringContaining('Successfully completed background refresh for: sync_user') |
| 90 | + ); |
| 91 | + consoleInfoSpy.mockRestore(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments