forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcameraPermissions.spec.ts
More file actions
272 lines (220 loc) · 7.66 KB
/
Copy pathcameraPermissions.spec.ts
File metadata and controls
272 lines (220 loc) · 7.66 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { describe, it, expect, beforeEach, vi } from 'vitest';
import type { PermissionStatus } from './cameraPermissions';
import {
requestCameraPermission,
stopCameraStream,
checkCameraPermission,
getUserFriendlyErrorMessage,
} from './cameraPermissions';
describe('cameraPermissions Utilities', () => {
describe('requestCameraPermission', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should request camera permission with default options', async () => {
const mockStream = { getTracks: () => [] } as any;
vi.spyOn(navigator.mediaDevices, 'getUserMedia').mockResolvedValue(
mockStream
);
const stream = await requestCameraPermission();
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith(
expect.objectContaining({
video: expect.any(Object),
audio: false,
})
);
expect(stream).toBe(mockStream);
});
it('should use front camera when specified', async () => {
const mockStream = { getTracks: () => [] } as any;
vi.spyOn(navigator.mediaDevices, 'getUserMedia').mockResolvedValue(
mockStream
);
await requestCameraPermission({
video: { facingMode: 'user' },
audio: false,
});
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({
video: { facingMode: 'user' },
audio: false,
});
});
it('should use back camera when specified', async () => {
const mockStream = { getTracks: () => [] } as any;
vi.spyOn(navigator.mediaDevices, 'getUserMedia').mockResolvedValue(
mockStream
);
await requestCameraPermission({
video: { facingMode: 'environment' },
audio: false,
});
expect(navigator.mediaDevices.getUserMedia).toHaveBeenCalledWith({
video: { facingMode: 'environment' },
audio: false,
});
});
it('should handle NotAllowedError (permission denied)', async () => {
const error = new DOMException(
'Permission denied',
'NotAllowedError'
);
vi.spyOn(navigator.mediaDevices, 'getUserMedia').mockRejectedValue(
error
);
await expect(requestCameraPermission()).rejects.toThrow();
});
it('should handle NotFoundError (no camera)', async () => {
const error = new DOMException('No camera found', 'NotFoundError');
vi.spyOn(navigator.mediaDevices, 'getUserMedia').mockRejectedValue(
error
);
await expect(requestCameraPermission()).rejects.toThrow();
});
it('should throw error if getUserMedia is not available', async () => {
const originalMediaDevices = navigator.mediaDevices;
Object.defineProperty(navigator, 'mediaDevices', {
value: undefined,
writable: true,
});
try {
await requestCameraPermission();
expect.fail('Should have thrown an error');
} catch (error: any) {
expect(error.message).toContain('not supported');
} finally {
Object.defineProperty(navigator, 'mediaDevices', {
value: originalMediaDevices,
writable: true,
});
}
});
it('should enforce HTTPS for non-localhost', async () => {
const originalLocation = window.location;
Object.defineProperty(window, 'location', {
value: { protocol: 'http:', hostname: 'example.com' },
writable: true,
});
try {
await requestCameraPermission();
expect.fail('Should have thrown an error');
} catch (error: any) {
expect(error.message).toContain('HTTPS');
} finally {
Object.defineProperty(window, 'location', {
value: originalLocation,
writable: true,
});
}
});
it('should allow HTTP for localhost', async () => {
const mockStream = { getTracks: () => [] } as any;
const originalLocation = window.location;
Object.defineProperty(window, 'location', {
value: { protocol: 'http:', hostname: 'localhost' },
writable: true,
});
try {
vi.spyOn(navigator.mediaDevices, 'getUserMedia').mockResolvedValue(
mockStream
);
const stream = await requestCameraPermission();
expect(stream).toBe(mockStream);
} finally {
Object.defineProperty(window, 'location', {
value: originalLocation,
writable: true,
});
}
});
});
describe('stopCameraStream', () => {
it('should stop all tracks in a stream', () => {
const mockTrack1 = { stop: vi.fn() };
const mockTrack2 = { stop: vi.fn() };
const mockStream = {
getTracks: () => [mockTrack1, mockTrack2],
} as any;
stopCameraStream(mockStream);
expect(mockTrack1.stop).toHaveBeenCalled();
expect(mockTrack2.stop).toHaveBeenCalled();
});
it('should handle stream with no tracks', () => {
const mockStream = {
getTracks: () => [],
} as any;
expect(() => stopCameraStream(mockStream)).not.toThrow();
});
});
describe('checkCameraPermission', () => {
it('should check camera permission status', async () => {
const mockPermissionStatus = {
state: 'granted' as PermissionStatus,
onchange: null,
};
vi.spyOn(navigator.permissions, 'query').mockResolvedValue(
mockPermissionStatus as any
);
const status = await checkCameraPermission();
expect(status).toBe('granted');
expect(navigator.permissions.query).toHaveBeenCalledWith({
name: 'camera',
});
});
it('should return prompt if permission status is unknown', async () => {
vi.spyOn(navigator.permissions, 'query').mockRejectedValue(
new Error('Not supported')
);
const status = await checkCameraPermission();
expect(status).toBe('prompt');
});
it('should return denied status', async () => {
const mockPermissionStatus = {
state: 'denied' as PermissionStatus,
onchange: null,
};
vi.spyOn(navigator.permissions, 'query').mockResolvedValue(
mockPermissionStatus as any
);
const status = await checkCameraPermission();
expect(status).toBe('denied');
});
});
describe('getUserFriendlyErrorMessage', () => {
it('should format permission denied error', () => {
const error: any = new Error();
error.permissionError = {
type: 'permission-denied',
message: 'Camera permission denied',
};
const message = getUserFriendlyErrorMessage(error);
expect(message).toBe('Camera permission denied');
});
it('should format not found error', () => {
const error: any = new Error();
error.permissionError = {
type: 'not-found',
message: 'No camera device found',
};
const message = getUserFriendlyErrorMessage(error);
expect(message).toBe('No camera device found');
});
it('should format not secure error', () => {
const error: any = new Error();
error.permissionError = {
type: 'not-secure',
message: 'Camera access requires HTTPS',
};
const message = getUserFriendlyErrorMessage(error);
expect(message).toBe('Camera access requires HTTPS');
});
it('should return default message for unknown error', () => {
const error = new Error('Unknown error');
const message = getUserFriendlyErrorMessage(error);
expect(message).toBe('Unable to access camera. Please try again.');
});
it('should handle null or undefined errors', () => {
const message = getUserFriendlyErrorMessage(null);
expect(message).toBe('Unable to access camera. Please try again.');
});
});
});