@onekeyfe/hardware-cli 1.2.0-alpha.17 → 1.2.0-alpha.170

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -33,12 +33,13 @@ const createPeripheral = (id: string) => {
33
33
  state: 'connected',
34
34
  advertisement: {
35
35
  localName: `OneKey Pro 2 ${id}`,
36
- serviceUuids: ['fffd'],
36
+ serviceUuids: ['0001'],
37
37
  },
38
38
  discoverServices: jest.fn((_uuids, callback) => callback(null, [service])),
39
39
  connect: jest.fn(callback => callback()),
40
40
  disconnect: jest.fn(callback => callback()),
41
41
  },
42
+ service,
42
43
  write,
43
44
  notify,
44
45
  };
@@ -46,10 +47,74 @@ const createPeripheral = (id: string) => {
46
47
 
47
48
  describe('Noble BLE plugin notification routing', () => {
48
49
  afterEach(() => {
50
+ jest.useRealTimers();
49
51
  jest.resetModules();
50
52
  jest.clearAllMocks();
51
53
  });
52
54
 
55
+ test('does not enumerate Find My advertisements that expose FFFD', async () => {
56
+ jest.useFakeTimers({ doNotFake: ['performance'] });
57
+ const oneKey = createPeripheral('onekey-device');
58
+ const findMy = createPeripheral('find-my-device');
59
+ findMy.peripheral.advertisement.localName = 'Find My';
60
+ findMy.peripheral.advertisement.serviceUuids = ['fffd'];
61
+ const noble = new EventEmitter() as EventEmitter & {
62
+ state: string;
63
+ startScanning: jest.Mock;
64
+ stopScanning: jest.Mock;
65
+ };
66
+ noble.state = 'poweredOn';
67
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
68
+ callback?.();
69
+ noble.emit('discover', findMy.peripheral);
70
+ noble.emit('discover', oneKey.peripheral);
71
+ });
72
+ noble.stopScanning = jest.fn(callback => callback?.());
73
+ jest.doMock('@stoprocent/noble', () => noble);
74
+
75
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
76
+ const plugin = createNobleBlePlugin();
77
+ await plugin.init();
78
+ const devicesPromise = plugin.enumerate();
79
+ await Promise.resolve();
80
+ jest.runAllTimers();
81
+ await Promise.resolve();
82
+
83
+ await expect(devicesPromise).resolves.toEqual([
84
+ expect.objectContaining({ id: 'onekey-device' }),
85
+ ]);
86
+ jest.useRealTimers();
87
+ });
88
+
89
+ test('does not enumerate a name-only candidate without the communication service', async () => {
90
+ jest.useFakeTimers({ doNotFake: ['performance'] });
91
+ const nameOnly = createPeripheral('name-only-device');
92
+ nameOnly.peripheral.advertisement.serviceUuids = [];
93
+ const noble = new EventEmitter() as EventEmitter & {
94
+ state: string;
95
+ startScanning: jest.Mock;
96
+ stopScanning: jest.Mock;
97
+ };
98
+ noble.state = 'poweredOn';
99
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
100
+ callback?.();
101
+ noble.emit('discover', nameOnly.peripheral);
102
+ });
103
+ noble.stopScanning = jest.fn(callback => callback?.());
104
+ jest.doMock('@stoprocent/noble', () => noble);
105
+
106
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
107
+ const plugin = createNobleBlePlugin();
108
+ await plugin.init();
109
+ const devicesPromise = plugin.enumerate();
110
+ await Promise.resolve();
111
+ jest.runAllTimers();
112
+ await Promise.resolve();
113
+
114
+ await expect(devicesPromise).resolves.toEqual([]);
115
+ jest.useRealTimers();
116
+ });
117
+
53
118
  test('routes notifications to the receiver waiting for the same device', async () => {
54
119
  const deviceA = createPeripheral('device-a');
55
120
  const deviceB = createPeripheral('device-b');
@@ -112,6 +177,82 @@ describe('Noble BLE plugin notification routing', () => {
112
177
  expect(result).toBe('completed');
113
178
  });
114
179
 
180
+ test('disconnects an untracked peripheral when service discovery fails', async () => {
181
+ const device = createPeripheral('device-a');
182
+ device.peripheral.discoverServices.mockImplementation((_uuids, callback) =>
183
+ callback(new Error('service discovery failed'))
184
+ );
185
+ const noble = new EventEmitter() as EventEmitter & {
186
+ state: string;
187
+ startScanning: jest.Mock;
188
+ stopScanning: jest.Mock;
189
+ };
190
+ noble.state = 'poweredOn';
191
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
192
+ callback?.();
193
+ noble.emit('discover', device.peripheral);
194
+ });
195
+ noble.stopScanning = jest.fn(callback => callback?.());
196
+ jest.doMock('@stoprocent/noble', () => noble);
197
+
198
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
199
+ const plugin = createNobleBlePlugin();
200
+ await plugin.init();
201
+
202
+ await expect(plugin.connect('device-a')).rejects.toThrow('service discovery failed');
203
+ expect(device.peripheral.disconnect).toHaveBeenCalledTimes(1);
204
+ });
205
+
206
+ test('rejects a vendor-specific service containing the OneKey short UUID', async () => {
207
+ const device = createPeripheral('device-a');
208
+ device.service.uuid = 'abcd0001-1234-5678-9012-abcdefabcdef';
209
+ const noble = new EventEmitter() as EventEmitter & {
210
+ state: string;
211
+ startScanning: jest.Mock;
212
+ stopScanning: jest.Mock;
213
+ };
214
+ noble.state = 'poweredOn';
215
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
216
+ callback?.();
217
+ noble.emit('discover', device.peripheral);
218
+ });
219
+ noble.stopScanning = jest.fn(callback => callback?.());
220
+ jest.doMock('@stoprocent/noble', () => noble);
221
+
222
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
223
+ const plugin = createNobleBlePlugin();
224
+ await plugin.init();
225
+
226
+ await expect(plugin.connect('device-a')).rejects.toThrow('No BLE service found');
227
+ });
228
+
229
+ test('unsubscribes and disconnects an untracked peripheral when notification setup fails', async () => {
230
+ const device = createPeripheral('device-a');
231
+ device.notify.subscribe.mockImplementation(callback =>
232
+ callback(new Error('notification setup failed'))
233
+ );
234
+ const noble = new EventEmitter() as EventEmitter & {
235
+ state: string;
236
+ startScanning: jest.Mock;
237
+ stopScanning: jest.Mock;
238
+ };
239
+ noble.state = 'poweredOn';
240
+ noble.startScanning = jest.fn((_services, _duplicates, callback) => {
241
+ callback?.();
242
+ noble.emit('discover', device.peripheral);
243
+ });
244
+ noble.stopScanning = jest.fn(callback => callback?.());
245
+ jest.doMock('@stoprocent/noble', () => noble);
246
+
247
+ const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
248
+ const plugin = createNobleBlePlugin();
249
+ await plugin.init();
250
+
251
+ await expect(plugin.connect('device-a')).rejects.toThrow('notification setup failed');
252
+ expect(device.notify.unsubscribe).toHaveBeenCalled();
253
+ expect(device.peripheral.disconnect).toHaveBeenCalledTimes(1);
254
+ });
255
+
115
256
  test('uses withoutResponse for normal and high-volume writes', async () => {
116
257
  const device = createPeripheral('device-a');
117
258
  const noble = new EventEmitter() as EventEmitter & {
@@ -163,7 +304,11 @@ describe('Noble BLE plugin notification routing', () => {
163
304
 
164
305
  await (plugin.send as any)('device-a', 'aa', { withoutResponse: false });
165
306
 
166
- expect(device.write.write).toHaveBeenCalledWith(expect.any(Buffer), false, expect.any(Function));
307
+ expect(device.write.write).toHaveBeenCalledWith(
308
+ expect.any(Buffer),
309
+ false,
310
+ expect.any(Function)
311
+ );
167
312
  });
168
313
 
169
314
  test('does not add a fixed delay between 192-byte writes', async () => {
@@ -0,0 +1,64 @@
1
+ import { prepareSession } from '../cli';
2
+ import { resolvePassphraseByChoice } from '../sdk';
3
+ import { preloadSessionFromKeychain } from '../session';
4
+
5
+ jest.mock('../session', () => ({
6
+ clearSessionFromKeychain: jest.fn(),
7
+ preloadSessionFromKeychain: jest.fn(),
8
+ }));
9
+
10
+ describe('CLI wallet session', () => {
11
+ test('maps the Attach PIN choice to an on-device Attach PIN response', async () => {
12
+ await expect(resolvePassphraseByChoice('4')).resolves.toEqual({
13
+ value: '',
14
+ passphraseOnDevice: false,
15
+ attachPinOnDevice: true,
16
+ });
17
+ });
18
+
19
+ test('uses the public wallet identity without persisting an internal session id', async () => {
20
+ const sdk = {
21
+ searchDevices: jest.fn().mockResolvedValue({
22
+ success: true,
23
+ payload: [
24
+ {
25
+ connectId: 'pro2-connect-id',
26
+ deviceId: 'device-id',
27
+ deviceType: 'pro2',
28
+ features: {
29
+ deviceId: 'device-id',
30
+ deviceType: 'pro2',
31
+ unlocked: true,
32
+ passphraseProtection: true,
33
+ },
34
+ },
35
+ ],
36
+ }),
37
+ getFeatures: jest.fn(),
38
+ openWalletSession: jest.fn().mockResolvedValue({
39
+ success: true,
40
+ payload: {
41
+ protocol: 'V2',
42
+ walletType: 'hidden',
43
+ deviceId: 'device-id',
44
+ passphraseState: 'wallet-state',
45
+ resumed: false,
46
+ },
47
+ }),
48
+ };
49
+ const globalOpts: Record<string, unknown> = {};
50
+ (preloadSessionFromKeychain as jest.Mock).mockResolvedValueOnce(undefined);
51
+
52
+ await expect(prepareSession(sdk as never, globalOpts)).resolves.toBe('wallet-state');
53
+
54
+ expect(sdk.openWalletSession).toHaveBeenCalledWith('pro2-connect-id', {
55
+ mode: 'select-hidden',
56
+ });
57
+ expect(sdk.getFeatures).not.toHaveBeenCalled();
58
+ expect(globalOpts).toMatchObject({
59
+ connectId: 'pro2-connect-id',
60
+ deviceId: 'device-id',
61
+ passphraseState: 'wallet-state',
62
+ });
63
+ });
64
+ });
@@ -6,6 +6,7 @@ describe('upload-wallpaper CLI command', () => {
6
6
 
7
7
  expect(command).toBeDefined();
8
8
  expect(command?.description()).toBe('Upload and activate a Pro2 wallpaper');
9
+ expect(command?.options.some(option => option.long === '--jpeg' && option.required)).toBe(true);
9
10
  });
10
11
 
11
12
  test('reports effective transfer speed from encoded bytes and elapsed time', () => {