forked from input-output-hk/daedalus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
134 lines (112 loc) · 3.69 KB
/
utils.ts
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
import expect from 'expect';
import createIPCMock from 'electron-mock-ipc';
import chalk from 'chalk';
import {
IpcChannel,
IpcReceiver,
IpcSender,
} from '../source/common/ipc/lib/IpcChannel';
import {
GET_INIT_LEDGER_CONNECT_CHANNEL,
GET_CARDANO_ADA_APP_CHANNEL,
GET_EXTENDED_PUBLIC_KEY_CHANNEL,
GET_HARDWARE_WALLET_CONNECTION_CHANNEL,
DEVICE_NOT_CONNECTED,
} from '../source/common/ipc/api';
import { createChannels } from '../source/main/ipc/createHardwareWalletIPCChannels';
import { handleHardwareWalletRequests } from '../source/main/ipc/getHardwareWalletChannel';
export const { ipcMain, ipcRenderer } = createIPCMock();
export class MockIpcChannel<Incoming, Outgoing> extends IpcChannel<
Incoming,
Outgoing
> {
async send(
message: Outgoing,
sender: IpcSender = ipcRenderer,
receiver: IpcReceiver = ipcRenderer
): Promise<Incoming> {
return super.send(message, sender, receiver);
}
async request(
message: Outgoing,
sender: IpcSender = ipcMain,
receiver: IpcReceiver = ipcMain
): Promise<Incoming> {
return super.request(message, sender, receiver);
}
onReceive(
handler: (message: Incoming) => Promise<Outgoing>,
receiver: IpcReceiver = ipcMain
): void {
super.onReceive(handler, receiver);
}
onRequest(
handler: (arg0: Incoming) => Promise<Outgoing>,
receiver: IpcReceiver = ipcMain
): void {
super.onRequest(handler, receiver);
}
}
export const createAndRegisterHardwareWalletChannels = () =>
// @ts-ignore Argument of type 'ipcRenderer' is not assignable to parameter of type 'BrowserWindow'.
handleHardwareWalletRequests(ipcRenderer, createChannels(MockIpcChannel));
export const initLedgerChannel = () => {
const initLedgerConnectChannel = new MockIpcChannel(
GET_INIT_LEDGER_CONNECT_CHANNEL
);
initLedgerConnectChannel.request({}, ipcRenderer, ipcMain);
};
export const createCardanoAppChannel = () =>
new MockIpcChannel(GET_CARDANO_ADA_APP_CHANNEL);
export const createGetPublicKeyChannel = () =>
new MockIpcChannel(GET_EXTENDED_PUBLIC_KEY_CHANNEL);
export const createHardwareWalletConnectionChannel = () =>
new MockIpcChannel(GET_HARDWARE_WALLET_CONNECTION_CHANNEL);
export const requestLaunchingCardanoAppOnLedger = (deviceId: string) =>
new Promise((resolve, reject) => {
const cardanoAppChannel = createCardanoAppChannel();
const run = async () => {
try {
const cardanoAppChannelResponse = await cardanoAppChannel.request(
{ path: deviceId },
ipcRenderer,
ipcRenderer
);
return resolve(cardanoAppChannelResponse);
} catch (err) {
if (err.code === DEVICE_NOT_CONNECTED) {
return reject(err);
}
setTimeout(run, 1000);
}
};
run();
});
interface Result {
disconnected: boolean;
deviceType: string;
deviceId: string | null;
deviceModel: string;
deviceName: string;
path: string;
}
export const createSequentialResult = (sequence: Array<Partial<Result>>) => {
const common = {
disconnected: expect.any(Boolean),
deviceType: expect.any(String),
deviceId: null,
deviceModel: expect.any(String),
deviceName: expect.any(String),
path: expect.any(String),
product: expect.any(String),
};
const result = sequence.map((s) => ({ ...common, ...s }));
return () => [result.shift(), result.length === 0];
};
export const log = (message: string) =>
console.log(chalk.whiteBright.bgBlackBright.bold(message)); // eslint-disable-line no-console
export const createTestInstructions = (messages: string[]) => {
messages.forEach((m, i) => log(`${i + 1} - ${m}`));
};
export const waitForZombieMessages = () =>
new Promise((resolve) => setTimeout(resolve, 1000));