-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathe2e.spec.ts
More file actions
419 lines (368 loc) · 13.4 KB
/
e2e.spec.ts
File metadata and controls
419 lines (368 loc) · 13.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { deepStrictEqual } from 'assert';
import { join } from 'path';
import { Effect, Exit, Layer, pipe, Schedule } from 'effect';
import { Command, FileSystem } from '@effect/platform';
import { NodeContext } from '@effect/platform-node';
import { afterAll, beforeAll, describe, expect, it } from '@effect/vitest';
import { rootDir } from '@blocksense/base-utils';
import {
entriesOf,
fromEntries,
valuesOf,
} from '@blocksense/base-utils/array-iter';
import {
type EthereumAddress,
parseEthereumAddress,
} from '@blocksense/base-utils/evm';
import type { NewFeedsConfig } from '@blocksense/config-types/data-feeds-config';
import type { SequencerConfigV2 } from '@blocksense/config-types/node-config';
import { createViemClient } from '@blocksense/contracts/viem';
import {
parseProcessesStatus,
ProcessComposeLive,
} from '../../utils/environment-managers/process-compose-manager';
import type { EnvironmentManagerService } from '../../utils/environment-managers/types';
import { EnvironmentManager } from '../../utils/environment-managers/types';
import {
createGatewayController,
gateEffect,
installGateway,
} from '../../utils/services/gateway';
import type { FeedsValueAndRound } from '../../utils/services/onchain';
import { getDataFeedsInfoFromNetwork } from '../../utils/services/onchain';
import type { SequencerService } from '../../utils/services/sequencer';
import { Sequencer } from '../../utils/services/sequencer';
import { expectedPCStatuses03 } from './expected-service-status';
describe.sequential('E2E Tests with process-compose', () => {
const testScenario = `wit`;
const testEnvironment = `e2e-${testScenario}`;
const network = 'ink_sepolia';
const failFastGateway = createGatewayController();
installGateway(
failFastGateway,
'Skipping remaining tests because the gate test failed',
);
let sequencer: SequencerService;
let processCompose: EnvironmentManagerService;
let hasProcessComposeStarted = false;
let sequencerConfig: SequencerConfigV2;
let feedsConfig: NewFeedsConfig;
let feedIds: bigint[];
let contractAddress: EthereumAddress;
let initialFeedsInfo: FeedsValueAndRound;
let existingFiles: string[] = [];
const dirPath = join(rootDir, '/apps/e2e-tests/src/test-scenarios/wit/');
const deleteTestFiles = () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const allFiles = yield* fs.readDirectory(dirPath);
const newFiles = allFiles.filter(file => !existingFiles.includes(file));
if (newFiles.length > 0) {
for (const file of newFiles) {
yield* fs.remove(join(dirPath, file), {
force: true,
recursive: true,
});
console.log(
`Cleaning up ${newFiles.length} files created during tests:`,
`${newFiles.map(f => `\n - ${f}`).join('')}`,
);
}
}
});
beforeAll(async () => {
// track files created during the tests
existingFiles = await Effect.runPromise(
FileSystem.FileSystem.pipe(
Effect.flatMap(fs => fs.readDirectory(dirPath)),
Effect.provide(NodeContext.layer),
),
);
const res = await pipe(
Effect.gen(function* () {
processCompose = yield* EnvironmentManager;
yield* processCompose.start(testScenario);
hasProcessComposeStarted = true;
if (!process.listenerCount('SIGINT')) {
process.once('SIGINT', () => {
if (hasProcessComposeStarted) {
Effect.runPromise(
processCompose
.stop()
.pipe(Effect.catchAll(() => Effect.succeed(undefined)))
.pipe(() =>
deleteTestFiles().pipe(Effect.provide(NodeContext.layer)),
),
).finally(async () => {
process.exit(130);
});
} else {
process.exit(130);
}
});
}
sequencer = yield* Sequencer;
}),
Effect.provide(Layer.merge(ProcessComposeLive, Sequencer.Live)),
Effect.runPromiseExit,
);
if (Exit.isFailure(res)) {
throw new Error(`Failed to start test environment: ${testEnvironment}`);
}
});
afterAll(() =>
Effect.gen(function* () {
if (hasProcessComposeStarted) {
yield* processCompose.stop();
}
yield* deleteTestFiles();
})
.pipe(Effect.provide(NodeContext.layer))
.pipe(Effect.runPromise),
);
it.live('Test processes state shortly after start', () =>
gateEffect(
failFastGateway,
Effect.gen(function* () {
const equal = yield* Effect.retry(
processCompose
.getProcessesStatus()
.pipe(
Effect.tap(processes =>
Effect.try(() =>
deepStrictEqual(processes, expectedPCStatuses03),
),
),
),
{
schedule: Schedule.fixed(1000),
times: 90,
},
);
// still validate the result
expect(equal).toBeTruthy();
}).pipe(Effect.provide(ProcessComposeLive)),
'Gate test failed: processes are not in expected state',
),
);
it.live('Test sequencer configs are available and in correct format', () =>
Effect.gen(function* () {
sequencerConfig = yield* sequencer.getConfig();
feedsConfig = yield* sequencer.getFeedsConfig();
expect(sequencerConfig).toBeTypeOf('object');
expect(feedsConfig).toBeTypeOf('object');
contractAddress = parseEthereumAddress(
sequencerConfig.providers[network].contracts.find(
c => c.name === 'AggregatedDataFeedStore',
)?.address,
);
const allow_feeds = sequencerConfig.providers[network].allow_feeds;
feedIds = allow_feeds?.length
? (allow_feeds as bigint[])
: feedsConfig.feeds.map(feed => {
const stride = BigInt(feed.stride) << 120n;
return stride | feed.id;
});
}).pipe(
Effect.tap(
Effect.gen(function* () {
const url = sequencerConfig.providers[network].url;
// Fetch the initial round data for the feeds from the local network ( anvil )
initialFeedsInfo = yield* getDataFeedsInfoFromNetwork(
feedIds,
contractAddress,
url,
);
// Enable the provider which is disabled by default ( ink_sepolia )
yield* sequencer.enableProvider(network);
}),
),
),
);
it.live('Test sports db yields metrics', () =>
Effect.gen(function* () {
yield* Effect.retry(
sequencer.fetchUpdatesToNetworksMetric().pipe(
Effect.filterOrFail(updates => {
// TODO: how to look for stride too?
return valuesOf(updates[network]).every(v => v >= 1);
}),
),
{
schedule: Schedule.fixed(10000),
times: 30,
},
);
const processes = yield* parseProcessesStatus();
expect(processes).toEqual(expectedPCStatuses03);
}),
);
it.live('Test feeds data is updated on the local network', () =>
Effect.gen(function* () {
const url = sequencerConfig.providers[network].url;
// Save map of initial rounds for each feed
const initialRounds = fromEntries(
entriesOf(initialFeedsInfo).map(([id, data]) => [id, data.round]),
);
// Get feeds information from the local network ( anvil )
// for the same round as the initial one, to confirm it is not being overwritten
const initialFeedsInfoLocal = yield* getDataFeedsInfoFromNetwork(
feedIds,
contractAddress,
url,
initialRounds,
);
const latestFeedsInfoLocal = yield* getDataFeedsInfoFromNetwork(
feedIds,
contractAddress,
url,
);
expect(initialFeedsInfo).toEqual(initialFeedsInfoLocal);
expect(initialFeedsInfoLocal[feedIds[0].toString()].round).toEqual(
latestFeedsInfoLocal[feedIds[0].toString()].round - 1,
);
expect(feedIds.length).toEqual(1);
for (const feedId of feedIds) {
const value = latestFeedsInfoLocal[feedId.toString()].value;
const stride = feedId >> 120n;
const joinedValue = Array.isArray(value)
? `0x${value.map(val => val.slice(2)).join('')}`
: value;
expect(BigInt((joinedValue.length - 2) / 2 / 32)).toEqual(2n ** stride);
yield* Command.make(
'just',
'dev',
'decoder',
'generate-decoder',
'--wit-path',
'apps/e2e-tests/src/test-scenarios/wit/sports.wit',
'--output-dir',
'apps/e2e-tests/src/test-scenarios/wit/generated-decoders',
'--stride',
stride.toString(),
).pipe(Command.string());
yield* Command.make(
'forge',
'build',
'--root',
rootDir + '/apps/e2e-tests/src/test-scenarios/wit',
'generated-decoders',
).pipe(Command.string);
const contracts = yield* FileSystem.FileSystem.pipe(
Effect.flatMap(fs =>
fs
.readDirectory(dirPath + 'generated-decoders')
.pipe(Effect.map(files => files.map(file => file))),
),
);
expect(contracts.length).toBeGreaterThan(0);
for (const contractFile of contracts) {
const deployResult = yield* Command.make(
'forge',
'create',
'--rpc-url',
'http://localhost:8500',
// sequencerConfig.providers[network].url,
// 1st account private key from anvil
'--private-key',
'0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
'--root',
rootDir + '/apps/e2e-tests/src/test-scenarios/wit',
`generated-decoders/${contractFile}:${contractFile.replace('.sol', '')}`,
'--broadcast',
).pipe(Command.string());
// Extract contract address from the deploy result
const contractAddressMatch = deployResult.match(
/Deployed to:\s*(0x[a-fA-F0-9]{40})/,
);
if (!contractAddressMatch) {
throw new Error(
'Failed to extract contract address from deploy result',
);
}
const contractAddress = contractAddressMatch[1];
expect(contractAddress).toMatch(/^0x[a-fA-F0-9]{40}$/);
const codeResult = yield* Command.make(
'cast',
'code',
'--rpc-url',
'http://localhost:8500',
contractAddress,
).pipe(Command.string);
expect(codeResult).not.toEqual('0x');
expect(codeResult.length).toBeGreaterThan(10);
const abi = yield* Command.make(
'forge',
'inspect',
'--root',
rootDir + '/apps/e2e-tests/src/test-scenarios/wit',
'generated-decoders/SSZDecoder.sol',
'abi',
'--json',
)
.pipe(Command.string)
.pipe(Effect.map(JSON.parse));
const viemClient = createViemClient(new URL('http://localhost:8500'));
const decoded = (yield* Effect.tryPromise(() =>
viemClient.readContract({
address: contractAddress as EthereumAddress,
abi,
functionName: 'decode',
args: [joinedValue],
}),
)) as {
eventName: string;
season: string;
homeTeam: string;
awayTeam: string;
homeScore: bigint;
awayScore: bigint;
};
const eventId = yield* Effect.tryPromise({
try: () =>
fetch(
'https://www.thesportsdb.com/api/v1/json/123/eventslast.php?id=133602',
{
method: 'GET',
},
).then(res =>
res.json().then(data => data.results[0].idEvent as string),
),
catch: () => {
throw new Error('Failed to fetch data from TheSportsDB API');
},
});
const event = yield* Effect.tryPromise({
try: () =>
fetch(
`https://www.thesportsdb.com/api/v1/json/123/lookupevent.php?id=${eventId}`,
{
method: 'GET',
},
).then(res =>
res.json().then(data => {
return {
name: data.events[0].strEvent,
season: data.events[0].strSeason,
homeTeam: data.events[0].strHomeTeam,
awayTeam: data.events[0].strAwayTeam,
homeScore: data.events[0].intHomeScore,
awayScore: data.events[0].intAwayScore,
};
}),
),
catch: () => {
throw new Error('Failed to fetch data from TheSportsDB API');
},
});
expect(decoded.eventName).toEqual(event.name);
expect(decoded.season).toEqual(event.season);
expect(decoded.homeTeam).toEqual(event.homeTeam);
expect(decoded.awayTeam).toEqual(event.awayTeam);
expect(BigInt(decoded.homeScore)).toEqual(BigInt(event.homeScore));
expect(BigInt(decoded.awayScore)).toEqual(BigInt(event.awayScore));
}
}
}).pipe(Effect.provide(NodeContext.layer)),
);
});