|
1 | 1 | import { fromByteArray } from 'base64-js'; |
2 | 2 | import { ConnectionPriority, type BleManager, type Characteristic, type Descriptor, type Device } from 'react-native-ble-plx'; |
3 | 3 | import { ReactNativeBleTransport } from './ReactNativeBleTransport'; |
| 4 | +import { DiagnosticLog } from '@/diagnostics/DiagnosticLog'; |
4 | 5 |
|
5 | 6 | const descriptor = (value: string): Descriptor => ({ value } as Descriptor); |
6 | 7 |
|
@@ -29,6 +30,149 @@ function manager(overrides: Record<string, unknown> = {}): BleManager { |
29 | 30 | } |
30 | 31 |
|
31 | 32 | describe('ReactNativeBleTransport', () => { |
| 33 | + it('records discovery status separately from the selected-PC connection', async () => { |
| 34 | + const log = new DiagnosticLog(); |
| 35 | + let scanCallback!: (error: Error | null, value: Device | null) => void; |
| 36 | + const found = jest.fn(); |
| 37 | + const candidate = device({ isConnected: jest.fn(async () => false) }); |
| 38 | + const transport = new ReactNativeBleTransport(manager({ startDeviceScan: jest.fn((_uuids, _options, callback) => { scanCallback = callback; }) }), 'android', 100, undefined, log); |
| 39 | + const stop = transport.scan(found, jest.fn()); |
| 40 | + scanCallback(null, candidate); |
| 41 | + await waitFor(() => found.mock.calls.length === 1); |
| 42 | + expect(log.snapshot().map((entry) => entry.code).reverse()).toEqual([ |
| 43 | + 'ble_probe_connect_started', 'ble_probe_connect_succeeded', |
| 44 | + 'ble_probe_services_started', 'ble_probe_services_succeeded', |
| 45 | + 'ble_status_read_started', 'ble_status_read_succeeded', |
| 46 | + ]); |
| 47 | + stop(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('records synchronous listener registration failure', async () => { |
| 51 | + const log = new DiagnosticLog(); |
| 52 | + const connected = device({ monitorCharacteristicForService: jest.fn(() => { throw new Error('private'); }) }); |
| 53 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), 'ios', 100, undefined, log); |
| 54 | + await transport.connect('ble-1'); |
| 55 | + expect(() => transport.subscribe(jest.fn(), jest.fn())).toThrow(); |
| 56 | + expect(log.snapshot()[0]?.code).toBe('ble_notifications_failed'); |
| 57 | + expect(log.export()).not.toContain('private'); |
| 58 | + await transport.disconnect(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('records the Android connection stages in order through descriptor readiness', async () => { |
| 62 | + const log = new DiagnosticLog(); |
| 63 | + const connected = device(); |
| 64 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), 'android', 100, undefined, log); |
| 65 | + await transport.connect('private-address'); |
| 66 | + const remove = transport.subscribe(jest.fn(), jest.fn()); |
| 67 | + await transport.notificationsReady(); |
| 68 | + expect(log.snapshot().map((entry) => entry.code).reverse()).toEqual([ |
| 69 | + 'ble_connect_started', 'ble_connect_succeeded', |
| 70 | + 'ble_priority_started', 'ble_priority_succeeded', |
| 71 | + 'ble_mtu_started', 'ble_mtu_succeeded', |
| 72 | + 'ble_services_started', 'ble_services_succeeded', |
| 73 | + 'ble_notifications_started', 'ble_notifications_succeeded', |
| 74 | + 'ble_notification_ready_started', 'ble_notification_ready_succeeded', |
| 75 | + ]); |
| 76 | + expect(log.export()).not.toContain('private-address'); |
| 77 | + remove(); |
| 78 | + await transport.disconnect(); |
| 79 | + }); |
| 80 | + |
| 81 | + it.each([ |
| 82 | + ['connect', 'connectToDevice'], ['mtu', 'requestMTU'], ['services', 'discoverAllServicesAndCharacteristics'], |
| 83 | + ] as const)('identifies a %s failure without exporting native error details', async (stage, method) => { |
| 84 | + const log = new DiagnosticLog(); |
| 85 | + const failure = jest.fn(async () => { throw new Error('private native payload and address'); }); |
| 86 | + const connected = device(stage === 'connect' ? {} : { [method]: failure }); |
| 87 | + const native = manager({ connectToDevice: stage === 'connect' ? failure : jest.fn(async () => connected) }); |
| 88 | + const transport = new ReactNativeBleTransport(native, 'android', 100, undefined, log); |
| 89 | + await expect(transport.connect('private-address')).rejects.toThrow(); |
| 90 | + expect(log.snapshot()[0]).toMatchObject({ code: `ble_${stage}_failed`, level: 'warning' }); |
| 91 | + expect(log.export()).not.toContain('private'); |
| 92 | + await transport.disconnect(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('records a bounded MTU timeout as an MTU failure', async () => { |
| 96 | + const log = new DiagnosticLog(); |
| 97 | + const connected = device({ requestMTU: jest.fn(() => new Promise<Device>(() => undefined)) }); |
| 98 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), 'android', 1, undefined, log); |
| 99 | + await expect(transport.connect('ble-1')).rejects.toThrow('timed out'); |
| 100 | + expect(log.snapshot()[0]?.code).toBe('ble_mtu_failed'); |
| 101 | + await transport.disconnect(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('does not report a cancelled or late connection as a failure or success', async () => { |
| 105 | + const log = new DiagnosticLog(); |
| 106 | + let resolve!: (value: Device) => void; |
| 107 | + const connected = device(); |
| 108 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(() => new Promise<Device>((done) => { resolve = done; })) }), 'android', 100, undefined, log); |
| 109 | + const result = transport.connect('ble-1'); |
| 110 | + const rejected = expect(result).rejects.toThrow(); |
| 111 | + while (!resolve) await Promise.resolve(); |
| 112 | + await transport.disconnect(); |
| 113 | + resolve(connected); |
| 114 | + await rejected; |
| 115 | + expect(log.snapshot().map((entry) => entry.code)).toEqual(['ble_connect_started']); |
| 116 | + }); |
| 117 | + |
| 118 | + it('records optional priority failure but continues, and skips Android-only stages on iOS', async () => { |
| 119 | + for (const platform of ['android', 'ios'] as const) { |
| 120 | + const log = new DiagnosticLog(); |
| 121 | + const connected = device({ requestConnectionPriority: jest.fn(async () => { throw new Error('private'); }) }); |
| 122 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), platform, 100, undefined, log); |
| 123 | + await transport.connect('ble-1'); |
| 124 | + await transport.notificationsReady(); |
| 125 | + const codes = log.snapshot().map((entry) => entry.code); |
| 126 | + expect(codes).toContain('ble_services_succeeded'); |
| 127 | + if (platform === 'android') expect(codes).toContain('ble_priority_failed'); |
| 128 | + else expect(codes.some((code) => /priority|mtu|notification_ready/.test(code))).toBe(false); |
| 129 | + await transport.disconnect(); |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + it.each(['rejected read', 'disabled descriptor'] as const)('records notification readiness failure for %s', async (reason) => { |
| 134 | + const log = new DiagnosticLog(); |
| 135 | + const connected = device({ readDescriptorForService: jest.fn(async () => { |
| 136 | + if (reason === 'rejected read') throw new Error('private descriptor error'); |
| 137 | + return descriptor('AAA='); |
| 138 | + }) }); |
| 139 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), 'android', 100, undefined, log); |
| 140 | + await transport.connect('ble-1'); |
| 141 | + await expect(transport.notificationsReady()).rejects.toThrow(); |
| 142 | + expect(log.snapshot()[0]?.code).toBe('ble_notification_ready_failed'); |
| 143 | + expect(log.export()).not.toContain('private'); |
| 144 | + await transport.disconnect(); |
| 145 | + }); |
| 146 | + |
| 147 | + it('ignores stale notification diagnostic callbacks after unsubscribe', async () => { |
| 148 | + const log = new DiagnosticLog(); |
| 149 | + let fail!: () => void; |
| 150 | + const connected = device({ monitorCharacteristicForService: jest.fn((_service, _characteristic, listener) => { |
| 151 | + fail = () => listener(Object.assign(new Error('private'), { |
| 152 | + errorCode: 0 as const, attErrorCode: null, iosErrorCode: null, androidErrorCode: null, reason: 'private', |
| 153 | + }), null); |
| 154 | + return { remove: jest.fn() }; |
| 155 | + }) }); |
| 156 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), 'ios', 100, undefined, log); |
| 157 | + await transport.connect('ble-1'); |
| 158 | + const remove = transport.subscribe(jest.fn(), jest.fn()); |
| 159 | + fail(); |
| 160 | + expect(log.snapshot()[0]?.code).toBe('ble_notifications_failed'); |
| 161 | + const count = log.snapshot().length; |
| 162 | + remove(); |
| 163 | + fail(); |
| 164 | + expect(log.snapshot()).toHaveLength(count); |
| 165 | + expect(log.export()).not.toContain('private'); |
| 166 | + await transport.disconnect(); |
| 167 | + }); |
| 168 | + |
| 169 | + it('does not let diagnostic observer failures affect connection', async () => { |
| 170 | + const connected = device(); |
| 171 | + const transport = new ReactNativeBleTransport(manager({ connectToDevice: jest.fn(async () => connected) }), 'ios', 100, undefined, { addConnectionStage: () => { throw new Error('observer'); } }); |
| 172 | + await expect(transport.connect('ble-1')).resolves.toBeUndefined(); |
| 173 | + await transport.disconnect(); |
| 174 | + }); |
| 175 | + |
32 | 176 | it('does not construct the native manager during launch or pre-initialization cleanup', async () => { |
33 | 177 | const factory = jest.fn(() => manager()); |
34 | 178 | const transport = new ReactNativeBleTransport(null, 'ios', 10_000, factory); |
|
0 commit comments