Repository navigation
Expand file tree
/
Copy pathbackground-refresh.test.ts
More file actions
161 lines (110 loc) · 4.68 KB
/
Copy pathbackground-refresh.test.ts
File metadata and controls
161 lines (110 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { BackgroundRefresh } from './background-refresh';
import { getFullDashboardData } from '../../lib/github';
vi.mock('../../lib/github', () => ({
getFullDashboardData: vi.fn(),
}));
describe('BackgroundRefresh Unit Tests', () => {
let service: BackgroundRefresh;
beforeEach(() => {
service = BackgroundRefresh.getInstance();
service.reset();
vi.clearAllMocks();
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
it('behaves as a singleton and returns the same instance', () => {
const anotherInstance = BackgroundRefresh.getInstance();
expect(service).toBe(anotherInstance);
});
describe('isStale', () => {
it('returns true when lastSyncedAt is undefined', () => {
expect(service.isStale(undefined)).toBe(true);
});
it('returns true when lastSyncedAt is an invalid date string', () => {
expect(service.isStale('invalid-date')).toBe(true);
});
it('returns true when lastSyncedAt is older than 10 minutes', () => {
const now = Date.now();
const elevenMinutesAgo = new Date(now - 11 * 60 * 1000).toISOString();
expect(service.isStale(elevenMinutesAgo)).toBe(true);
});
it('returns false when lastSyncedAt is within 10 minutes', () => {
const now = Date.now();
const nineMinutesAgo = new Date(now - 9 * 60 * 1000).toISOString();
expect(service.isStale(nineMinutesAgo)).toBe(false);
});
});
describe('triggerRefresh', () => {
it('sanitizes username (trims and converts to lowercase)', async () => {
vi.mocked(getFullDashboardData).mockResolvedValue({} as never);
await service.triggerRefresh(' TestUser ');
expect(getFullDashboardData).toHaveBeenCalledWith(' TestUser ', {
forceRefresh: true,
});
expect(service.isJobActive('testuser')).toBe(false);
});
it('prevents concurrent duplicate jobs for the same user', async () => {
let resolvePromise!: () => void;
const pendingPromise = new Promise<void>((resolve) => {
resolvePromise = resolve;
});
vi.mocked(getFullDashboardData).mockReturnValue(
pendingPromise as unknown as ReturnType<typeof getFullDashboardData>
);
const firstCall = service.triggerRefresh('user1');
const secondCall = service.triggerRefresh('user1');
await Promise.resolve();
expect(service.isJobActive('user1')).toBe(true);
expect(getFullDashboardData).toHaveBeenCalledTimes(1);
resolvePromise();
await firstCall;
await secondCall;
expect(service.isJobActive('user1')).toBe(false);
});
it('removes the user from active jobs on failure', async () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
vi.mocked(getFullDashboardData).mockRejectedValue(new Error('Network error'));
service.triggerRefresh('user-fail');
expect(service.isJobActive('user-fail')).toBe(true);
await vi.runAllTimersAsync();
expect(service.isJobActive('user-fail')).toBe(false);
expect(consoleErrorSpy).toHaveBeenCalled();
consoleErrorSpy.mockRestore();
});
it('handles expired tokens or authentication errors gracefully', async () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const mockedApi = vi.mocked(getFullDashboardData);
const authError = new Error('Bad credentials');
Object.assign(authError, {
status: 401,
});
mockedApi.mockRejectedValueOnce(authError);
service.triggerRefresh('invalid_token_user');
await vi.runAllTimersAsync();
expect(service.isJobActive('invalid_token_user')).toBe(false);
consoleErrorSpy.mockRestore();
});
it('recovers correctly from network dropouts during synchronization', async () => {
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const mockedApi = vi.mocked(getFullDashboardData);
mockedApi.mockRejectedValueOnce(new Error('Network timeout'));
service.triggerRefresh('offline_user');
expect(service.isJobActive('offline_user')).toBe(true);
await vi.runAllTimersAsync();
expect(service.isJobActive('offline_user')).toBe(false);
consoleErrorSpy.mockRestore();
});
});
describe('reset', () => {
it('clears all currently active jobs', () => {
vi.mocked(getFullDashboardData).mockReturnValue(new Promise(() => {}));
service.triggerRefresh('active-user');
expect(service.isJobActive('active-user')).toBe(true);
service.reset();
expect(service.isJobActive('active-user')).toBe(false);
});
});
});