-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdeeplinks.test.ts
More file actions
185 lines (154 loc) · 5.68 KB
/
deeplinks.test.ts
File metadata and controls
185 lines (154 loc) · 5.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import { describe, it, expect } from 'vitest';
import {
parseDeeplink,
createDeeplink,
DeeplinkBuilder,
DeeplinkActions,
DEEPLINK_PREFIX,
} from '../deeplinks';
describe('parseDeeplink', () => {
describe('valid deeplinks', () => {
it('should parse simple action deeplink', () => {
const result = parseDeeplink('cap://record');
expect(result).toEqual({ action: 'record' });
});
it('should parse deeplink with query parameters', () => {
const result = parseDeeplink('cap://switch-microphone?deviceId=mic-123');
expect(result).toEqual({
action: 'switch-microphone',
deviceId: 'mic-123',
});
});
it('should parse deeplink with multiple parameters', () => {
const result = parseDeeplink('cap://switch-camera?deviceId=cam-456&format=1080p');
expect(result).toEqual({
action: 'switch-camera',
deviceId: 'cam-456',
format: '1080p',
});
});
it('should handle URL-encoded parameters', () => {
const result = parseDeeplink('cap://record?name=My%20Recording');
expect(result).toEqual({
action: 'record',
name: 'My Recording',
});
});
it('should ignore empty query values', () => {
const result = parseDeeplink('cap://record?empty=');
expect(result).toEqual({ action: 'record' });
});
it('should trim whitespace from URL', () => {
const result = parseDeeplink(' cap://pause ');
expect(result).toEqual({ action: 'pause' });
});
});
describe('invalid deeplinks', () => {
it('should return null for wrong prefix', () => {
expect(parseDeeplink('http://example.com')).toBeNull();
});
it('should return null for empty string', () => {
expect(parseDeeplink('')).toBeNull();
});
it('should return null for null/undefined', () => {
expect(parseDeeplink(null as unknown as string)).toBeNull();
expect(parseDeeplink(undefined as unknown as string)).toBeNull();
});
it('should return null for invalid action', () => {
expect(parseDeeplink('cap://invalid-action')).toBeNull();
});
it('should return null for malformed URL', () => {
expect(parseDeeplink('cap://')).toBeNull();
});
it('should handle malformed query string gracefully', () => {
// Invalid percent encoding should not throw
const result = parseDeeplink('cap://record?name=%ZZ');
expect(result?.action).toBe('record');
});
it('should return null for non-string input', () => {
expect(parseDeeplink(123 as unknown as string)).toBeNull();
});
});
});
describe('createDeeplink', () => {
it('should create simple deeplink', () => {
expect(createDeeplink('record')).toBe('cap://record');
});
it('should create deeplink with parameters', () => {
expect(createDeeplink('switch-microphone', { deviceId: 'mic-123' }))
.toBe('cap://switch-microphone?deviceId=mic-123');
});
it('should filter out undefined parameters', () => {
const result = createDeeplink('switch-camera', {
deviceId: 'cam-456',
unused: undefined,
});
expect(result).toBe('cap://switch-camera?deviceId=cam-456');
});
it('should filter out empty string parameters', () => {
const result = createDeeplink('record', { name: '' });
expect(result).toBe('cap://record');
});
it('should URL-encode special characters', () => {
const result = createDeeplink('record', { name: 'My Recording' });
expect(result).toBe('cap://record?name=My+Recording');
});
it('should handle no parameters', () => {
expect(createDeeplink('stop')).toBe('cap://stop');
});
});
describe('DeeplinkBuilder', () => {
it('should build simple deeplink', () => {
const result = new DeeplinkBuilder('record').build();
expect(result).toBe('cap://record');
});
it('should build deeplink with parameters', () => {
const result = new DeeplinkBuilder('switch-microphone')
.withDeviceId('mic-789')
.build();
expect(result).toBe('cap://switch-microphone?deviceId=mic-789');
});
it('should chain multiple parameters', () => {
const result = new DeeplinkBuilder('record')
.withParam('name', 'Test')
.withParam('format', 'mp4')
.build();
expect(result).toContain('cap://record?');
expect(result).toContain('name=Test');
expect(result).toContain('format=mp4');
});
it('should ignore empty parameters', () => {
const result = new DeeplinkBuilder('record')
.withParam('empty', '')
.build();
expect(result).toBe('cap://record');
});
});
describe('DeeplinkActions', () => {
it('should create startRecording deeplink', () => {
expect(DeeplinkActions.startRecording()).toBe('cap://record');
});
it('should create stopRecording deeplink', () => {
expect(DeeplinkActions.stopRecording()).toBe('cap://stop');
});
it('should create pauseRecording deeplink', () => {
expect(DeeplinkActions.pauseRecording()).toBe('cap://pause');
});
it('should create resumeRecording deeplink', () => {
expect(DeeplinkActions.resumeRecording()).toBe('cap://resume');
});
it('should create switchMicrophone deeplink with deviceId', () => {
expect(DeeplinkActions.switchMicrophone('mic-123'))
.toBe('cap://switch-microphone?deviceId=mic-123');
});
it('should throw error for switchMicrophone without deviceId', () => {
expect(() => DeeplinkActions.switchMicrophone('')).toThrow();
});
it('should create switchCamera deeplink with deviceId', () => {
expect(DeeplinkActions.switchCamera('cam-456'))
.toBe('cap://switch-camera?deviceId=cam-456');
});
it('should throw error for switchCamera without deviceId', () => {
expect(() => DeeplinkActions.switchCamera('')).toThrow();
});
});