@onekeyfe/hardware-cli 1.2.0-alpha.1 → 1.2.0-alpha.100
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.
- package/dist/cli.d.ts +83 -1
- package/dist/cli.js +501 -142
- package/dist/deviceSelection.d.ts +3 -0
- package/dist/deviceSelection.js +11 -0
- package/dist/deviceStateCommands.d.ts +19 -0
- package/dist/deviceStateCommands.js +62 -0
- package/dist/pinentry.d.ts +1 -0
- package/dist/sdk.d.ts +12 -2
- package/dist/sdk.js +22 -11
- package/dist/session.d.ts +2 -9
- package/dist/session.js +3 -22
- package/dist/transports/nobleBlePlugin.d.ts +2 -0
- package/dist/transports/nobleBlePlugin.js +371 -0
- package/package.json +8 -6
- package/src/__tests__/cli-version.test.ts +8 -0
- package/src/__tests__/device-selection.test.ts +29 -0
- package/src/__tests__/device-state-commands.test.ts +118 -0
- package/src/__tests__/firmware-update-legacy-command.test.ts +18 -0
- package/src/__tests__/firmware-update-v4-command.test.ts +101 -0
- package/src/__tests__/noble-ble-plugin.test.ts +370 -0
- package/src/__tests__/wallet-session.test.ts +64 -0
- package/src/__tests__/wallpaper-upload-command.test.ts +46 -0
- package/src/cli.ts +687 -173
- package/src/deviceSelection.ts +13 -0
- package/src/deviceStateCommands.ts +71 -0
- package/src/pinentry.ts +1 -0
- package/src/sdk.ts +29 -13
- package/src/session.ts +2 -24
- package/src/transports/nobleBlePlugin.ts +487 -0
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
type MockCharacteristic = EventEmitter & {
|
|
4
|
+
uuid: string;
|
|
5
|
+
unsubscribe: jest.Mock;
|
|
6
|
+
subscribe: jest.Mock;
|
|
7
|
+
write: jest.Mock;
|
|
8
|
+
removeAllListeners: jest.Mock;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const createCharacteristic = (uuid: string): MockCharacteristic => {
|
|
12
|
+
const characteristic = new EventEmitter() as MockCharacteristic;
|
|
13
|
+
characteristic.uuid = uuid;
|
|
14
|
+
characteristic.unsubscribe = jest.fn(callback => callback());
|
|
15
|
+
characteristic.subscribe = jest.fn(callback => callback());
|
|
16
|
+
characteristic.write = jest.fn((_buffer, _withoutResponse, callback) => callback());
|
|
17
|
+
characteristic.removeAllListeners = jest.fn(
|
|
18
|
+
characteristic.removeAllListeners.bind(characteristic)
|
|
19
|
+
);
|
|
20
|
+
return characteristic;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const createPeripheral = (id: string) => {
|
|
24
|
+
const write = createCharacteristic('0002');
|
|
25
|
+
const notify = createCharacteristic('0003');
|
|
26
|
+
const service = {
|
|
27
|
+
uuid: '0001',
|
|
28
|
+
discoverCharacteristics: jest.fn((_uuids, callback) => callback(null, [write, notify])),
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
peripheral: {
|
|
32
|
+
id,
|
|
33
|
+
state: 'connected',
|
|
34
|
+
advertisement: {
|
|
35
|
+
localName: `OneKey Pro 2 ${id}`,
|
|
36
|
+
serviceUuids: ['0001'],
|
|
37
|
+
},
|
|
38
|
+
discoverServices: jest.fn((_uuids, callback) => callback(null, [service])),
|
|
39
|
+
connect: jest.fn(callback => callback()),
|
|
40
|
+
disconnect: jest.fn(callback => callback()),
|
|
41
|
+
},
|
|
42
|
+
service,
|
|
43
|
+
write,
|
|
44
|
+
notify,
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
describe('Noble BLE plugin notification routing', () => {
|
|
49
|
+
afterEach(() => {
|
|
50
|
+
jest.useRealTimers();
|
|
51
|
+
jest.resetModules();
|
|
52
|
+
jest.clearAllMocks();
|
|
53
|
+
});
|
|
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
|
+
|
|
118
|
+
test('routes notifications to the receiver waiting for the same device', async () => {
|
|
119
|
+
const deviceA = createPeripheral('device-a');
|
|
120
|
+
const deviceB = createPeripheral('device-b');
|
|
121
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
122
|
+
state: string;
|
|
123
|
+
startScanning: jest.Mock;
|
|
124
|
+
stopScanning: jest.Mock;
|
|
125
|
+
};
|
|
126
|
+
noble.state = 'poweredOn';
|
|
127
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
128
|
+
callback?.();
|
|
129
|
+
noble.emit('discover', deviceA.peripheral);
|
|
130
|
+
noble.emit('discover', deviceB.peripheral);
|
|
131
|
+
});
|
|
132
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
133
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
134
|
+
|
|
135
|
+
const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
|
|
136
|
+
const plugin = createNobleBlePlugin();
|
|
137
|
+
await plugin.init();
|
|
138
|
+
await plugin.connect('device-a');
|
|
139
|
+
await plugin.connect('device-b');
|
|
140
|
+
|
|
141
|
+
const receiveA = plugin.receive('device-a');
|
|
142
|
+
const receiveB = plugin.receive('device-b');
|
|
143
|
+
deviceB.notify.emit('data', Buffer.from('bb', 'hex'));
|
|
144
|
+
deviceA.notify.emit('data', Buffer.from('aa', 'hex'));
|
|
145
|
+
|
|
146
|
+
await expect(Promise.all([receiveA, receiveB])).resolves.toEqual(['aa', 'bb']);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('finishes disconnect cleanup when Noble never calls unsubscribe back', async () => {
|
|
150
|
+
const device = createPeripheral('device-a');
|
|
151
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
152
|
+
state: string;
|
|
153
|
+
startScanning: jest.Mock;
|
|
154
|
+
stopScanning: jest.Mock;
|
|
155
|
+
};
|
|
156
|
+
noble.state = 'poweredOn';
|
|
157
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
158
|
+
callback?.();
|
|
159
|
+
noble.emit('discover', device.peripheral);
|
|
160
|
+
});
|
|
161
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
162
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
163
|
+
|
|
164
|
+
const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
|
|
165
|
+
const plugin = createNobleBlePlugin();
|
|
166
|
+
await plugin.init();
|
|
167
|
+
await plugin.connect('device-a');
|
|
168
|
+
device.notify.unsubscribe.mockImplementation(() => undefined);
|
|
169
|
+
|
|
170
|
+
const result = await Promise.race([
|
|
171
|
+
plugin.disconnect('device-a').then(() => 'completed'),
|
|
172
|
+
new Promise(resolve => {
|
|
173
|
+
setTimeout(() => resolve('blocked'), 300);
|
|
174
|
+
}),
|
|
175
|
+
]);
|
|
176
|
+
|
|
177
|
+
expect(result).toBe('completed');
|
|
178
|
+
});
|
|
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
|
+
|
|
256
|
+
test('uses withoutResponse for normal and high-volume writes', async () => {
|
|
257
|
+
const device = createPeripheral('device-a');
|
|
258
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
259
|
+
state: string;
|
|
260
|
+
startScanning: jest.Mock;
|
|
261
|
+
stopScanning: jest.Mock;
|
|
262
|
+
};
|
|
263
|
+
noble.state = 'poweredOn';
|
|
264
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
265
|
+
callback?.();
|
|
266
|
+
noble.emit('discover', device.peripheral);
|
|
267
|
+
});
|
|
268
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
269
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
270
|
+
|
|
271
|
+
const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
|
|
272
|
+
const plugin = createNobleBlePlugin();
|
|
273
|
+
await plugin.init();
|
|
274
|
+
await plugin.connect('device-a');
|
|
275
|
+
|
|
276
|
+
await plugin.send('device-a', 'aa');
|
|
277
|
+
await plugin.send('device-a', 'bb');
|
|
278
|
+
|
|
279
|
+
expect(device.write.write.mock.calls.map(([, withoutResponse]) => withoutResponse)).toEqual([
|
|
280
|
+
true,
|
|
281
|
+
true,
|
|
282
|
+
]);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test('uses acknowledged writes when requested by firmware upload', async () => {
|
|
286
|
+
const device = createPeripheral('device-a');
|
|
287
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
288
|
+
state: string;
|
|
289
|
+
startScanning: jest.Mock;
|
|
290
|
+
stopScanning: jest.Mock;
|
|
291
|
+
};
|
|
292
|
+
noble.state = 'poweredOn';
|
|
293
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
294
|
+
callback?.();
|
|
295
|
+
noble.emit('discover', device.peripheral);
|
|
296
|
+
});
|
|
297
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
298
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
299
|
+
|
|
300
|
+
const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
|
|
301
|
+
const plugin = createNobleBlePlugin();
|
|
302
|
+
await plugin.init();
|
|
303
|
+
await plugin.connect('device-a');
|
|
304
|
+
|
|
305
|
+
await (plugin.send as any)('device-a', 'aa', { withoutResponse: false });
|
|
306
|
+
|
|
307
|
+
expect(device.write.write).toHaveBeenCalledWith(
|
|
308
|
+
expect.any(Buffer),
|
|
309
|
+
false,
|
|
310
|
+
expect.any(Function)
|
|
311
|
+
);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
test('does not add a fixed delay between 192-byte writes', async () => {
|
|
315
|
+
const device = createPeripheral('device-a');
|
|
316
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
317
|
+
state: string;
|
|
318
|
+
startScanning: jest.Mock;
|
|
319
|
+
stopScanning: jest.Mock;
|
|
320
|
+
};
|
|
321
|
+
const wait = jest.fn(() => Promise.resolve());
|
|
322
|
+
noble.state = 'poweredOn';
|
|
323
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
324
|
+
callback?.();
|
|
325
|
+
noble.emit('discover', device.peripheral);
|
|
326
|
+
});
|
|
327
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
328
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
329
|
+
jest.doMock('@onekeyfe/hd-shared', () => ({
|
|
330
|
+
...jest.requireActual('@onekeyfe/hd-shared'),
|
|
331
|
+
wait,
|
|
332
|
+
}));
|
|
333
|
+
|
|
334
|
+
const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
|
|
335
|
+
const plugin = createNobleBlePlugin();
|
|
336
|
+
await plugin.init();
|
|
337
|
+
await plugin.connect('device-a');
|
|
338
|
+
|
|
339
|
+
await plugin.send('device-a', 'aa'.repeat(193));
|
|
340
|
+
|
|
341
|
+
expect(device.write.write).toHaveBeenCalledTimes(2);
|
|
342
|
+
expect(wait).not.toHaveBeenCalled();
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
test('preserves a short final BLE packet without padding', async () => {
|
|
346
|
+
const device = createPeripheral('device-a');
|
|
347
|
+
const noble = new EventEmitter() as EventEmitter & {
|
|
348
|
+
state: string;
|
|
349
|
+
startScanning: jest.Mock;
|
|
350
|
+
stopScanning: jest.Mock;
|
|
351
|
+
};
|
|
352
|
+
noble.state = 'poweredOn';
|
|
353
|
+
noble.startScanning = jest.fn((_services, _duplicates, callback) => {
|
|
354
|
+
callback?.();
|
|
355
|
+
noble.emit('discover', device.peripheral);
|
|
356
|
+
});
|
|
357
|
+
noble.stopScanning = jest.fn(callback => callback?.());
|
|
358
|
+
jest.doMock('@stoprocent/noble', () => noble);
|
|
359
|
+
|
|
360
|
+
const { createNobleBlePlugin } = await import('../transports/nobleBlePlugin');
|
|
361
|
+
const plugin = createNobleBlePlugin();
|
|
362
|
+
await plugin.init();
|
|
363
|
+
await plugin.connect('device-a');
|
|
364
|
+
|
|
365
|
+
await plugin.send('device-a', 'aabb');
|
|
366
|
+
|
|
367
|
+
const packet = device.write.write.mock.calls[0][0] as Buffer;
|
|
368
|
+
expect(packet).toEqual(Buffer.from('aabb', 'hex'));
|
|
369
|
+
});
|
|
370
|
+
});
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { buildWallpaperUploadMetrics, program } from '../cli';
|
|
2
|
+
|
|
3
|
+
describe('upload-wallpaper CLI command', () => {
|
|
4
|
+
test('exposes a command backed by the SDK wallpaper API', () => {
|
|
5
|
+
const command = program.commands.find(item => item.name() === 'upload-wallpaper');
|
|
6
|
+
|
|
7
|
+
expect(command).toBeDefined();
|
|
8
|
+
expect(command?.description()).toBe('Upload and activate a Pro2 wallpaper');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
test('reports effective transfer speed from encoded bytes and elapsed time', () => {
|
|
12
|
+
expect(
|
|
13
|
+
buildWallpaperUploadMetrics({
|
|
14
|
+
totalBytes: 2_473_984,
|
|
15
|
+
transferredBytes: 2_473_984,
|
|
16
|
+
startedAt: 1_000,
|
|
17
|
+
endedAt: 2_000,
|
|
18
|
+
lastProgress: 100,
|
|
19
|
+
})
|
|
20
|
+
).toEqual({
|
|
21
|
+
totalBytes: 2_473_984,
|
|
22
|
+
transferredBytes: 2_473_984,
|
|
23
|
+
totalSeconds: 1,
|
|
24
|
+
transferKiBPerSecond: 2416,
|
|
25
|
+
lastProgress: 100,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('uses confirmed bytes for an interrupted transfer rate', () => {
|
|
30
|
+
expect(
|
|
31
|
+
buildWallpaperUploadMetrics({
|
|
32
|
+
totalBytes: 1_855_500,
|
|
33
|
+
transferredBytes: 631_800,
|
|
34
|
+
startedAt: 1_000,
|
|
35
|
+
endedAt: 101_000,
|
|
36
|
+
lastProgress: 34,
|
|
37
|
+
})
|
|
38
|
+
).toEqual({
|
|
39
|
+
totalBytes: 1_855_500,
|
|
40
|
+
transferredBytes: 631_800,
|
|
41
|
+
totalSeconds: 100,
|
|
42
|
+
transferKiBPerSecond: 6.17,
|
|
43
|
+
lastProgress: 34,
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|