-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathbalances.state-changes-e2e.ts
More file actions
88 lines (80 loc) · 2.97 KB
/
balances.state-changes-e2e.ts
File metadata and controls
88 lines (80 loc) · 2.97 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
import fetch from 'node-fetch';
import { config } from '../config/env.config';
const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));
async function getJson(url: string): Promise<any | undefined> {
for (let i = 0; i < 30; i++) {
try {
const resp = await fetch(url);
if (resp.ok) {
return await resp.json();
}
} catch (_) {
// ignore and retry
}
await sleep(1000);
}
return undefined;
}
function pickBalance(payload: any): string | undefined {
if (!payload || typeof payload !== 'object') return undefined;
// Primary shape used by CI shell script: top-level balance
if (typeof payload.balance === 'string') return payload.balance;
if (typeof payload.balance === 'number') return String(payload.balance);
// Fallbacks in case the shape is wrapped
if (payload.data) {
if (typeof payload.data.balance === 'string') return payload.data.balance;
if (typeof payload.data.balance === 'number') return String(payload.data.balance);
if (payload.data.account && payload.data.account.balance) {
const b = payload.data.account.balance;
if (typeof b === 'string') return b;
if (typeof b === 'number') return String(b);
}
}
return undefined;
}
async function fetchBalance(baseUrl: string, address: string): Promise<string> {
const url = `${baseUrl}/accounts/${address}`;
const payload = await getJson(url);
if (!payload) throw new Error(`No payload from ${url}`);
const bal = pickBalance(payload);
if (!bal) throw new Error(`No balance field in response from ${url}`);
return bal;
}
async function fetchBalanceV2(baseUrl: string, address: string): Promise<string> {
const url = `${baseUrl}/v2/accounts/${address}`;
// Try v2 first; if not available or not yet indexed, fallback to v1 for parity
let payload = await getJson(url);
if (!payload) {
const v1Url = `${baseUrl}/accounts/${address}`;
payload = await getJson(v1Url);
if (!payload) throw new Error(`No payload from ${url}`);
}
const bal = pickBalance(payload);
if (!bal) throw new Error(`No balance field in v2 response from ${url}`);
return bal;
}
describe('State changes: balances parity (v1 vs v2)', () => {
const base = config.apiServiceUrl;
const alice = config.aliceAddress;
const bob = config.bobAddress;
it('Alice balance matches between v1 and v2', async () => {
let v1 = await fetchBalance(base, alice);
let v2 = await fetchBalanceV2(base, alice);
for (let i = 0; i < 20 && v1 !== v2; i++) {
await sleep(1000);
v1 = await fetchBalance(base, alice);
v2 = await fetchBalanceV2(base, alice);
}
expect(v1).toBe(v2);
});
it('Bob balance matches between v1 and v2', async () => {
let v1 = await fetchBalance(base, bob);
let v2 = await fetchBalanceV2(base, bob);
for (let i = 0; i < 20 && v1 !== v2; i++) {
await sleep(1000);
v1 = await fetchBalance(base, bob);
v2 = await fetchBalanceV2(base, bob);
}
expect(v1).toBe(v2);
});
});