-
Notifications
You must be signed in to change notification settings - Fork 871
/
Copy pathtest.js
187 lines (170 loc) · 5.91 KB
/
test.js
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
const baseFields = {
pool: 'string',
chain: 'string',
project: 'string',
symbol: 'string',
};
const adapter = global.adapter;
const apy = global.apy;
const poolsUrl = global.poolsUrl;
const uniquePoolIdentifiersDB = global.uniquePoolIdentifiersDB;
const protocols = global.protocolsSlug;
describe(`Running ${process.env.npm_config_adapter} Test`, () => {
describe('Check for allowed field names', () => {
const optionalFields = [
'apy',
'apyBase',
'apyReward',
'underlyingTokens',
'rewardTokens',
'poolMeta',
'url',
'apyBaseBorrow',
'apyRewardBorrow',
'totalSupplyUsd',
'totalBorrowUsd',
'ltv',
'borrowable',
'borrowFactor',
'debtCeilingUsd',
'mintedCoin',
'apyBase7d',
'apyRewardFake',
'apyRewardBorrowFake',
'il7d',
'volumeUsd1d',
'volumeUsd7d',
'apyBaseInception',
];
const fields = [...Object.keys(baseFields), ...optionalFields, 'tvlUsd'];
apy.forEach((pool) => {
test(`Expects pool id ${
pool.pool
} to contain only allowed keys: ${fields} and has: ${Object.keys(
pool
)}`, () => {
expect(Object.keys(pool).every((f) => fields.includes(f))).toBe(true);
});
});
});
test("Check if link to the pool's page exist", () => {
const poolsLink = apy[0].url || poolsUrl;
expect(typeof poolsLink).toBe('string');
});
test('Check for unique pool ids', () => {
const poolIds = apy.map((pool) => pool.pool);
const uniquePoolIds = [...new Set(poolIds)];
expect(poolIds).toEqual(uniquePoolIds);
});
describe('Check apy data types', () => {
const apyFields = ['apy', 'apyBase', 'apyReward'];
apy.forEach((pool) => {
test(`Expects pool with id ${pool.pool} to have at least one number apy field`, () => {
expect(
apyFields.map((field) => Number.isFinite(pool[field]))
).toContain(true);
});
});
});
describe('Check tvl data type', () => {
apy.forEach((pool) => {
test(`tvlUsd field of pool with id ${pool.pool} should be number `, () => {
expect(Number.isFinite(pool.tvlUsd)).toBe(true);
});
});
});
describe('Check tokens data types', () => {
const tokenFields = ['rewardTokens', 'underlyingTokens'];
apy.forEach((pool) => {
tokenFields.forEach((field) => {
if (pool[field]) {
test(`${field} field of pool with id ${pool.pool} should be an Array of strings`, () => {
expect(Array.isArray(pool[field])).toBe(true);
const isStringArray =
pool[field].map((v) => typeof v).filter((v) => v === 'string')
.length === pool[field].length;
expect(isStringArray).toBe(true);
});
}
});
});
});
describe('Check other fields data types', () => {
apy.forEach((pool) => {
test(`Expect other fields of pool with id ${pool.pool} to match thier data types`, () => {
Object.entries(baseFields).map(([field, type]) => {
expect(typeof pool[field]).toBe(type);
});
});
});
});
describe('Check if pool has a rewardApy then rewardTokens must also exist', () => {
apy.forEach((pool) => {
test(`The pool ${pool.pool} is expected to have a rewardTokens field`, () => {
if (pool.apyReward)
expect((pool.rewardTokens || []).length).toBeGreaterThan(0);
});
});
});
describe('Check if pool id already used by other project', () => {
const uniqueIds = new Set(apy.map(({ pool }) => pool));
const duplicatedPoolIds = [...uniqueIds].filter((p) =>
uniquePoolIdentifiersDB.has(p)
);
test('Print duplicated pool IDs and their existing projects', () => {
if (duplicatedPoolIds.length > 0) {
console.log('\nDuplicated pool IDs found:');
duplicatedPoolIds.forEach((poolId) => {
console.log(`Pool ID: ${poolId} is already used by another project`);
});
}
expect(duplicatedPoolIds.length).toBe(0);
});
});
test('Check project field is constant in all pools and if folder name and project field in pool objects matches the information in /protocols slug', () => {
expect(new Set(apy.map((p) => p.project)).size).toBe(1);
expect(
protocolsSlug.includes(apy[0].project) && apy[0].project === adapter
).toBe(true);
});
describe('Check additional field data rules', () => {
// All fields added here are treated as optional
// If a field is present, it will be checked against its rules
let additionalFieldRules = {
totalSupplyUsd: {
type: 'number',
},
totalBorrowUsd: {
type: 'number',
},
ltv: {
min: 0,
max: 1,
},
};
apy.forEach((pool) => {
Object.entries(additionalFieldRules).map(([field, rule]) => {
if (pool[field] !== undefined) {
if (rule.type !== undefined) {
test(`${field} field of pool with id ${pool.pool} should be a ${rule.type}`, () => {
expect(typeof pool[field]).toBe(rule.type);
});
}
if (rule.max !== undefined && rule.min !== undefined) {
test(`${field} field of pool with id ${pool.pool} should be in the range of ${rule.min}-${rule.max}`, () => {
expect(pool[field]).toBeLessThanOrEqual(rule.max);
});
} else if (rule.min !== undefined && rule.max === undefined) {
test(`${field} field of pool with id ${pool.pool} should be greater than or equal to ${rule.min}`, () => {
expect(pool[field]).toBeGreaterThanOrEqual(rule.min);
});
} else if (rule.max !== undefined && rule.min === undefined) {
test(`${field} field of pool with id ${pool.pool} should be less than or equal to ${rule.max}`, () => {
expect(pool[field]).toBeLessThanOrEqual(rule.max);
});
}
}
});
});
});
});