-
Notifications
You must be signed in to change notification settings - Fork 754
/
jestGlobalSetup.ts
245 lines (219 loc) · 7.58 KB
/
jestGlobalSetup.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint-disable no-console */
/**
* Asynchronous Global Test Setup
* Run only once
* ref: order of execution jestGlobalSetup.ts -> jest.setup.ts -> fixtures.ts
*/
import { BaseUrl } from '../src/constants';
type DevnetStrategy = {
isDevnet: boolean;
isRS: boolean;
};
type ProviderType = {
sequencer: boolean;
rpc: boolean;
};
/**
* Global Setup Fixtures
*/
/* Default test config based on run `starknet-devnet --seed 0` */
const GS_DEFAULT_TEST_PROVIDER_URL = 'http://127.0.0.1:5050/';
const setIfNullish = (envName: string, setValue?: string | boolean) => {
process.env[envName] ??= setValue?.toString();
};
const localDevnetDetectionStrategy = async () => {
const setup = (strategy: DevnetStrategy) => {
setIfNullish('IS_LOCALHOST_DEVNET', strategy.isDevnet ? 'true' : 'false');
setIfNullish(
'IS_RPC_DEVNET',
strategy.isDevnet && (strategy.isRS || process.env.TEST_RPC_URL) ? 'true' : 'false'
);
setIfNullish(
'IS_SEQUENCER_DEVNET',
strategy.isDevnet && process.env.IS_RPC_DEVNET === 'false' ? 'true' : 'false'
);
return strategy;
};
const strategy: DevnetStrategy = {
isDevnet: false,
isRS: false,
};
// if is_alive work it is local devnet
const devnetResult = await fetch(`${GS_DEFAULT_TEST_PROVIDER_URL}is_alive`)
.then((res) => res.text())
.catch(() => '');
if (devnetResult !== 'Alive!!!') {
return setup(strategy);
}
strategy.isDevnet = true;
// if on base url RPC endpoint work it is devnet-rs else it devnet-py
try {
const response = await fetch(`${GS_DEFAULT_TEST_PROVIDER_URL}`, {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json' },
body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'starknet_syncing' }),
});
const json = await response.json();
strategy.isRS = json.jsonrpc === '2.0';
} catch (error) {
return setup(strategy);
}
return setup(strategy);
};
const sequencerOrRpc = async (devnetStrategy?: DevnetStrategy) => {
const setup = (providerType: ProviderType) => {
setIfNullish('IS_SEQUENCER', providerType.sequencer ? 'true' : 'false');
setIfNullish('IS_RPC', providerType.rpc ? 'true' : 'false');
setIfNullish(
'IS_SEQUENCER_GOERLI',
(process.env.TEST_PROVIDER_BASE_URL || process.env.TEST_RPC_URL || '').includes(
BaseUrl.SN_GOERLI
)
? 'true'
: 'false'
);
return providerType;
};
let result: ProviderType = { sequencer: false, rpc: false };
if (process.env.TEST_PROVIDER_BASE_URL) {
return setup({ ...result, sequencer: true });
}
if (process.env.TEST_RPC_URL) {
return setup({ ...result, rpc: true });
}
// nor sequencer nor rpc provided, try with local devnet strategy
if (devnetStrategy && devnetStrategy.isDevnet) {
result = { sequencer: !devnetStrategy.isRS, rpc: devnetStrategy.isRS };
if (result.sequencer) {
process.env.TEST_PROVIDER_BASE_URL = GS_DEFAULT_TEST_PROVIDER_URL;
} else if (result.rpc) {
process.env.TEST_RPC_URL = GS_DEFAULT_TEST_PROVIDER_URL;
}
}
return setup(result);
};
const setAccount = async (devnetStrategy: DevnetStrategy) => {
const fetchAccount = async (URL: string) => {
const response = await fetch(`${URL}predeployed_accounts`);
const accounts = await response.json();
process.env.TEST_ACCOUNT_ADDRESS = accounts[0].address;
process.env.TEST_ACCOUNT_PRIVATE_KEY = accounts[0].private_key;
process.env.INITIAL_BALANCE = accounts[0].initial_balance;
};
if (process.env.TEST_ACCOUNT_ADDRESS && process.env.TEST_ACCOUNT_PRIVATE_KEY) {
return true;
}
if (process.env.TEST_ACCOUNT_ADDRESS || process.env.TEST_ACCOUNT_PRIVATE_KEY) {
throw new Error(
'If you are providing one of you need to provide both: TEST_ACCOUNT_ADDRESS & TEST_ACCOUNT_PRIVATE_KEY'
);
}
const providedURL = process.env.TEST_PROVIDER_BASE_URL || process.env.TEST_RPC_URL;
if (devnetStrategy.isDevnet) {
// get account from devnet
try {
await fetchAccount(GS_DEFAULT_TEST_PROVIDER_URL);
return true;
} catch (error) {
console.error('Fetching account from devnet failed');
}
} else if (providedURL) {
// try to get it from remote devnet
try {
await fetchAccount(providedURL);
return true;
} catch (error) {
console.error(`Fetching account from provided url ${providedURL} failed`);
}
}
throw new Error(
'Setting Account using all known strategies failed, provide basic test parameters'
);
};
const verifySetup = (final?: boolean) => {
const warnings: string[] = [];
if (!process.env.TEST_ACCOUNT_ADDRESS) {
if (final) throw new Error('TEST_ACCOUNT_ADDRESS env is not provided');
else warnings.push('TEST_ACCOUNT_ADDRESS env is not provided!');
}
if (!process.env.TEST_ACCOUNT_PRIVATE_KEY) {
if (final) throw new Error('TEST_ACCOUNT_PRIVATE_KEY env is not provided');
else warnings.push('TEST_ACCOUNT_PRIVATE_KEY env is not provided!');
}
// TODO: revise after Sequencer removal
// if (!process.env.TEST_RPC_URL) {
// process.env.TEST_RPC_URL = getDefaultNodeUrl();
// console.warn('TEST_RPC_URL env is not provided');
// }
if (warnings.length > 0) {
console.log('\x1b[33m', warnings.join('\n'), '\x1b[0m');
delete process.env.TEST_ACCOUNT_ADDRESS;
delete process.env.TEST_ACCOUNT_PRIVATE_KEY;
return false;
}
if (!final) {
setIfNullish('IS_LOCALHOST_DEVNET', 'false');
setIfNullish('IS_RPC_DEVNET', 'false');
setIfNullish('IS_SEQUENCER_DEVNET', 'false');
setIfNullish('IS_RPC', process.env.TEST_RPC_URL ? 'true' : 'false');
setIfNullish('IS_SEQUENCER', process.env.TEST_PROVIDER_BASE_URL ? 'true' : 'false');
setIfNullish(
'IS_SEQUENCER_GOERLI',
(process.env.TEST_PROVIDER_BASE_URL || process.env.TEST_RPC_URL || '').includes(
BaseUrl.SN_GOERLI
)
? 'true'
: 'false'
);
}
console.table({
TEST_ACCOUNT_ADDRESS: process.env.TEST_ACCOUNT_ADDRESS,
TEST_ACCOUNT_PRIVATE_KEY: '****',
INITIAL_BALANCE: process.env.INITIAL_BALANCE,
TEST_PROVIDER_BASE_URL: process.env.TEST_PROVIDER_BASE_URL,
TEST_RPC_URL: process.env.TEST_RPC_URL,
});
console.table({
IS_LOCALHOST_DEVNET: process.env.IS_LOCALHOST_DEVNET,
IS_RPC_DEVNET: process.env.IS_RPC_DEVNET,
IS_SEQUENCER_DEVNET: process.env.IS_SEQUENCER_DEVNET,
IS_RPC: process.env.IS_RPC,
IS_SEQUENCER: process.env.IS_SEQUENCER,
IS_SEQUENCER_GOERLI: process.env.IS_SEQUENCER_GOERLI,
});
console.log('Global Test Environment is Ready');
return true;
};
const executeStrategy = async () => {
// 1. Assume setup is provided and ready;
console.log('Global Test Setup Started');
if (verifySetup()) {
console.log('Using Provided Test Setup');
return true;
}
// 2. Try to detect devnet setup
console.log('Basic test parameters are missing, Auto Setup Started');
const devnetStrategy = await localDevnetDetectionStrategy();
if (devnetStrategy.isDevnet) {
if (devnetStrategy.isRS) {
console.log('Detected Devnet-RS');
} else {
console.log('Detected Devnet-PY');
}
}
const providerType = await sequencerOrRpc(devnetStrategy);
if (providerType.sequencer) {
console.log('Detected Sequencer');
} else if (providerType.rpc) {
console.log('Detected RPC');
}
const isAccountSet = await setAccount(devnetStrategy);
if (isAccountSet) {
console.log('Detected Account');
}
return verifySetup(true);
};
export default async (_globalConfig: any, _projectConfig: any) => {
const isSet = await executeStrategy();
if (!isSet) console.error('Test Setup Environment is NOT Ready');
};