-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbluetooth-audio.service.test.ts
More file actions
198 lines (159 loc) · 6.81 KB
/
bluetooth-audio.service.test.ts
File metadata and controls
198 lines (159 loc) · 6.81 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
/* eslint-disable @typescript-eslint/no-explicit-any */
import 'react-native';
// Mock @livekit/react-native-webrtc before any imports that use it
jest.mock('@livekit/react-native-webrtc', () => ({
RTCAudioSession: {
audioSessionDidActivate: jest.fn(),
audioSessionDidDeactivate: jest.fn(),
},
}));
// Mock CallKeep service before importing modules that use it
jest.mock('@/services/callkeep.service.ios', () => ({
callKeepService: {
setup: jest.fn().mockResolvedValue(undefined),
startCall: jest.fn().mockResolvedValue('test-uuid'),
endCall: jest.fn().mockResolvedValue(undefined),
isCallActiveNow: jest.fn().mockReturnValue(false),
getCurrentCallUUID: jest.fn().mockReturnValue(null),
setMuteStateCallback: jest.fn(),
},
}));
// Mock dependencies first before importing the service
jest.mock('react-native-ble-manager', () => ({
__esModule: true,
default: {
start: jest.fn(),
checkState: jest.fn(),
scan: jest.fn(),
stopScan: jest.fn(),
connect: jest.fn(),
disconnect: jest.fn(),
isPeripheralConnected: jest.fn(),
getConnectedPeripherals: jest.fn(),
getDiscoveredPeripherals: jest.fn(),
removeAllListeners: jest.fn(),
removePeripheral: jest.fn(),
},
}));
jest.mock('@/lib/storage', () => ({
getItem: jest.fn(),
setItem: jest.fn(),
removeItem: jest.fn(),
}));
jest.mock('@/services/audio.service', () => ({
audioService: {
playConnectedDeviceSound: jest.fn(),
},
}));
jest.mock('@/stores/app/livekit-store', () => {
const actions = {
toggleMicrophone: jest.fn(),
setMicrophoneEnabled: jest.fn(),
};
return {
useLiveKitStore: {
getState: jest.fn(() => actions),
},
};
});
import { bluetoothAudioService } from '../bluetooth-audio.service';
describe('BluetoothAudioService Refactoring', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should be defined and accessible', () => {
expect(bluetoothAudioService).toBeDefined();
expect(typeof bluetoothAudioService.destroy).toBe('function');
});
it('should have singleton instance pattern', () => {
// Both calls should return the same instance
const instance1 = bluetoothAudioService;
const instance2 = bluetoothAudioService;
expect(instance1).toBe(instance2);
});
it('should have required methods for Bluetooth management', () => {
expect(typeof bluetoothAudioService.startScanning).toBe('function');
expect(typeof bluetoothAudioService.stopScanning).toBe('function');
expect(typeof bluetoothAudioService.connectToDevice).toBe('function');
expect(typeof bluetoothAudioService.disconnectDevice).toBe('function');
});
describe('Preferred Device Connection Refactoring', () => {
it('should have private attemptPreferredDeviceConnection method', () => {
const service = bluetoothAudioService as any;
expect(typeof service.attemptPreferredDeviceConnection).toBe('function');
});
it('should have private attemptReconnectToPreferredDevice method for iOS support', () => {
const service = bluetoothAudioService as any;
expect(typeof service.attemptReconnectToPreferredDevice).toBe('function');
});
it('should track hasAttemptedPreferredDeviceConnection flag for single-call semantics', () => {
const service = bluetoothAudioService as any;
// Initially should be false
expect(service.hasAttemptedPreferredDeviceConnection).toBe(false);
// Can be set to true (simulating attempt)
service.hasAttemptedPreferredDeviceConnection = true;
expect(service.hasAttemptedPreferredDeviceConnection).toBe(true);
});
it('should reset flags on destroy method', () => {
const service = bluetoothAudioService as any;
// Set flags to true
service.hasAttemptedPreferredDeviceConnection = true;
service.isInitialized = true;
// Call destroy
bluetoothAudioService.destroy();
// Verify flags are reset for single-call logic
expect(service.hasAttemptedPreferredDeviceConnection).toBe(false);
expect(service.isInitialized).toBe(false);
});
it('should support iOS state change handling through attemptReconnectToPreferredDevice', () => {
const service = bluetoothAudioService as any;
// Set up scenario: connection was previously attempted
service.hasAttemptedPreferredDeviceConnection = true;
// Verify the method exists for iOS poweredOn state handling
expect(typeof service.attemptReconnectToPreferredDevice).toBe('function');
// This method should be called when Bluetooth state changes to poweredOn on iOS
// It resets the flag and attempts preferred device connection again
});
});
describe('Single-Call Logic Validation', () => {
it('should implement single-call semantics for preferred device connection', () => {
const service = bluetoothAudioService as any;
// Simulate first call - should set flag
service.hasAttemptedPreferredDeviceConnection = false;
// In actual implementation, attemptPreferredDeviceConnection would set this to true
// Simulate second call - should not execute due to flag
expect(service.hasAttemptedPreferredDeviceConnection).toBe(false);
// After first attempt
service.hasAttemptedPreferredDeviceConnection = true;
expect(service.hasAttemptedPreferredDeviceConnection).toBe(true);
// Second attempt should be blocked by this flag
});
it('should allow re-attempting connection after destroy', () => {
const service = bluetoothAudioService as any;
// Simulate connection attempt
service.hasAttemptedPreferredDeviceConnection = true;
// Destroy service (resets flags)
bluetoothAudioService.destroy();
// Flag should be reset, allowing new attempts
expect(service.hasAttemptedPreferredDeviceConnection).toBe(false);
});
});
describe('Microphone Control Delegation', () => {
it('should delegate mute toggle to livekitStore', async () => {
const service = bluetoothAudioService as any;
// Call private method via casting to any
await service.handleMuteToggle();
const storeMock = require('@/stores/app/livekit-store').useLiveKitStore.getState();
expect(storeMock.toggleMicrophone).toHaveBeenCalled();
});
it('should delegate setMicrophoneEnabled to livekitStore', async () => {
const service = bluetoothAudioService as any;
// Call private method via casting to any
await service.setMicrophoneEnabled(true);
const storeMock = require('@/stores/app/livekit-store').useLiveKitStore.getState();
expect(storeMock.setMicrophoneEnabled).toHaveBeenCalledWith(true);
await service.setMicrophoneEnabled(false);
expect(storeMock.setMicrophoneEnabled).toHaveBeenCalledWith(false);
});
});
});