@acala-network/chopsticks-testing 1.0.2-1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/check.d.ts +74 -2
- package/dist/cjs/check.js +98 -27
- package/dist/esm/check.d.ts +74 -2
- package/dist/esm/check.js +107 -29
- package/package.json +10 -7
package/dist/cjs/check.d.ts
CHANGED
|
@@ -1,38 +1,110 @@
|
|
|
1
|
-
import { ApiPromise } from '@polkadot/api';
|
|
2
|
-
import { Codec } from '@polkadot/types/types';
|
|
1
|
+
import type { ApiPromise } from '@polkadot/api';
|
|
2
|
+
import type { Codec } from '@polkadot/types/types';
|
|
3
|
+
/**
|
|
4
|
+
* Defines a filter for blockchain events
|
|
5
|
+
* Can be either a string (section name) or an object with method and section
|
|
6
|
+
*/
|
|
3
7
|
export type EventFilter = string | {
|
|
4
8
|
method: string;
|
|
5
9
|
section: string;
|
|
6
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Configuration options for data redaction
|
|
13
|
+
*/
|
|
7
14
|
export type RedactOptions = {
|
|
15
|
+
/** Redact numbers with optional precision */
|
|
8
16
|
number?: boolean | number;
|
|
17
|
+
/** Redact 32-byte hex values */
|
|
9
18
|
hash?: boolean;
|
|
19
|
+
/** Redact any hex values with 0x prefix */
|
|
10
20
|
hex?: boolean;
|
|
21
|
+
/** Redact base58 addresses */
|
|
11
22
|
address?: boolean;
|
|
23
|
+
/** Regex pattern for keys whose values should be redacted */
|
|
12
24
|
redactKeys?: RegExp;
|
|
25
|
+
/** Regex pattern for keys that should be removed */
|
|
13
26
|
removeKeys?: RegExp;
|
|
14
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Function type for test assertions
|
|
30
|
+
*/
|
|
15
31
|
export type ExpectFn = (value: any) => {
|
|
16
32
|
toMatchSnapshot: (msg?: string) => void;
|
|
17
33
|
toMatch(value: any, msg?: string): void;
|
|
18
34
|
toMatchObject(value: any, msg?: string): void;
|
|
19
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Main class for checking and validating blockchain data
|
|
38
|
+
* Provides a fluent interface for data transformation, filtering, and assertion
|
|
39
|
+
*/
|
|
20
40
|
export declare class Checker {
|
|
21
41
|
#private;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new Checker instance
|
|
44
|
+
* @param expectFn - Function for making test assertions
|
|
45
|
+
* @param value - Value to check
|
|
46
|
+
* @param message - Optional message for assertions
|
|
47
|
+
*/
|
|
22
48
|
constructor(expectFn: ExpectFn, value: any, message?: string);
|
|
49
|
+
/** Convert the checked value to human-readable format */
|
|
23
50
|
toHuman(): this;
|
|
51
|
+
/** Convert the checked value to hexadecimal format */
|
|
24
52
|
toHex(): this;
|
|
53
|
+
/** Convert the checked value to JSON format */
|
|
25
54
|
toJson(): this;
|
|
55
|
+
/**
|
|
56
|
+
* Set a message for test assertions
|
|
57
|
+
* @param message - Message to use in assertions
|
|
58
|
+
*/
|
|
26
59
|
message(message: string): this;
|
|
60
|
+
/**
|
|
61
|
+
* Filter blockchain events based on provided filters
|
|
62
|
+
* @param filters - Event filters to apply
|
|
63
|
+
*/
|
|
27
64
|
filterEvents(...filters: EventFilter[]): this;
|
|
65
|
+
/**
|
|
66
|
+
* Apply redaction rules to the checked value
|
|
67
|
+
* @param options - Redaction options
|
|
68
|
+
*/
|
|
28
69
|
redact(options?: RedactOptions): this;
|
|
70
|
+
/**
|
|
71
|
+
* Add a transformation function to the processing pipeline
|
|
72
|
+
* @param fn - Transformation function
|
|
73
|
+
*/
|
|
29
74
|
map(fn: (value: any) => any): this;
|
|
75
|
+
/**
|
|
76
|
+
* Apply a function to the current Checker instance
|
|
77
|
+
* @param fn - Function to apply
|
|
78
|
+
*/
|
|
30
79
|
pipe(fn?: (value: Checker) => Checker): Checker;
|
|
80
|
+
/**
|
|
81
|
+
* Get the final processed value
|
|
82
|
+
* @returns Processed value after applying all transformations
|
|
83
|
+
*/
|
|
31
84
|
value(): Promise<any>;
|
|
85
|
+
/**
|
|
86
|
+
* Assert that the value matches a snapshot
|
|
87
|
+
* @param msg - Optional message for the assertion
|
|
88
|
+
*/
|
|
32
89
|
toMatchSnapshot(msg?: string): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Assert that the value matches an expected value
|
|
92
|
+
* @param value - Expected value
|
|
93
|
+
* @param msg - Optional message for the assertion
|
|
94
|
+
*/
|
|
33
95
|
toMatch(value: any, msg?: string): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Assert that the value matches an expected object structure
|
|
98
|
+
* @param value - Expected object structure
|
|
99
|
+
* @param msg - Optional message for the assertion
|
|
100
|
+
*/
|
|
34
101
|
toMatchObject(value: any, msg?: string): Promise<void>;
|
|
35
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates a set of checking utilities with a provided assertion function
|
|
105
|
+
* @param expectFn - Function for making test assertions
|
|
106
|
+
* @returns Object containing various checking utilities
|
|
107
|
+
*/
|
|
36
108
|
export declare const withExpect: (expectFn: ExpectFn) => {
|
|
37
109
|
check: (value: any, msg?: string) => Checker;
|
|
38
110
|
checkEvents: ({ events }: {
|
package/dist/cjs/check.js
CHANGED
|
@@ -66,29 +66,49 @@ function _class_private_method_init(obj, privateSet) {
|
|
|
66
66
|
_check_private_redeclaration(obj, privateSet);
|
|
67
67
|
privateSet.add(obj);
|
|
68
68
|
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Processes a Codec or array of Codecs with a given transformation function
|
|
71
|
+
* @param codec - Single Codec or array of Codecs to process
|
|
72
|
+
* @param fn - Transformation function to apply to each Codec
|
|
73
|
+
* @returns Processed value(s)
|
|
74
|
+
*/ const processCodecOrArray = (codec, fn)=>Array.isArray(codec) ? codec.map(fn) : fn(codec);
|
|
75
|
+
/**
|
|
76
|
+
* Converts Codec data to human-readable format
|
|
77
|
+
* @param codec - Codec data to convert
|
|
78
|
+
*/ const toHuman = (codec)=>processCodecOrArray(codec, (c)=>c?.toHuman?.() ?? c);
|
|
79
|
+
/**
|
|
80
|
+
* Converts Codec data to hexadecimal format
|
|
81
|
+
* @param codec - Codec data to convert
|
|
82
|
+
*/ const toHex = (codec)=>processCodecOrArray(codec, (c)=>c?.toHex?.() ?? c);
|
|
83
|
+
/**
|
|
84
|
+
* Converts Codec data to JSON format
|
|
85
|
+
* @param codec - Codec data to convert
|
|
86
|
+
*/ const toJson = (codec)=>processCodecOrArray(codec, (c)=>c?.toJSON?.() ?? c);
|
|
73
87
|
var _expectFn = /*#__PURE__*/ new WeakMap(), _value = /*#__PURE__*/ new WeakMap(), _pipeline = /*#__PURE__*/ new WeakMap(), _format = /*#__PURE__*/ new WeakMap(), _message = /*#__PURE__*/ new WeakMap(), _redactOptions = /*#__PURE__*/ new WeakMap(), _redact = /*#__PURE__*/ new WeakSet();
|
|
74
88
|
class Checker {
|
|
75
|
-
toHuman() {
|
|
89
|
+
/** Convert the checked value to human-readable format */ toHuman() {
|
|
76
90
|
_class_private_field_set(this, _format, 'human');
|
|
77
91
|
return this;
|
|
78
92
|
}
|
|
79
|
-
toHex() {
|
|
93
|
+
/** Convert the checked value to hexadecimal format */ toHex() {
|
|
80
94
|
_class_private_field_set(this, _format, 'hex');
|
|
81
95
|
return this;
|
|
82
96
|
}
|
|
83
|
-
toJson() {
|
|
97
|
+
/** Convert the checked value to JSON format */ toJson() {
|
|
84
98
|
_class_private_field_set(this, _format, 'json');
|
|
85
99
|
return this;
|
|
86
100
|
}
|
|
87
|
-
|
|
101
|
+
/**
|
|
102
|
+
* Set a message for test assertions
|
|
103
|
+
* @param message - Message to use in assertions
|
|
104
|
+
*/ message(message) {
|
|
88
105
|
_class_private_field_set(this, _message, message);
|
|
89
106
|
return this;
|
|
90
107
|
}
|
|
91
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Filter blockchain events based on provided filters
|
|
110
|
+
* @param filters - Event filters to apply
|
|
111
|
+
*/ filterEvents(...filters) {
|
|
92
112
|
this.toHuman();
|
|
93
113
|
_class_private_field_get(this, _pipeline).push((value)=>{
|
|
94
114
|
let data = value.map(({ event: { index: _, ...event } })=>event);
|
|
@@ -97,7 +117,8 @@ class Checker {
|
|
|
97
117
|
return filters.some((filter)=>{
|
|
98
118
|
if (typeof filter === 'string') {
|
|
99
119
|
return evt.section === filter;
|
|
100
|
-
}
|
|
120
|
+
}
|
|
121
|
+
if ('method' in filter) {
|
|
101
122
|
const { section, method } = filter;
|
|
102
123
|
return evt.section === section && evt.method === method;
|
|
103
124
|
}
|
|
@@ -108,7 +129,10 @@ class Checker {
|
|
|
108
129
|
});
|
|
109
130
|
return this;
|
|
110
131
|
}
|
|
111
|
-
|
|
132
|
+
/**
|
|
133
|
+
* Apply redaction rules to the checked value
|
|
134
|
+
* @param options - Redaction options
|
|
135
|
+
*/ redact(options = {
|
|
112
136
|
number: 2,
|
|
113
137
|
hash: true
|
|
114
138
|
}) {
|
|
@@ -118,14 +142,23 @@ class Checker {
|
|
|
118
142
|
});
|
|
119
143
|
return this;
|
|
120
144
|
}
|
|
121
|
-
|
|
145
|
+
/**
|
|
146
|
+
* Add a transformation function to the processing pipeline
|
|
147
|
+
* @param fn - Transformation function
|
|
148
|
+
*/ map(fn) {
|
|
122
149
|
_class_private_field_get(this, _pipeline).push(fn);
|
|
123
150
|
return this;
|
|
124
151
|
}
|
|
125
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Apply a function to the current Checker instance
|
|
154
|
+
* @param fn - Function to apply
|
|
155
|
+
*/ pipe(fn) {
|
|
126
156
|
return fn ? fn(this) : this;
|
|
127
157
|
}
|
|
128
|
-
|
|
158
|
+
/**
|
|
159
|
+
* Get the final processed value
|
|
160
|
+
* @returns Processed value after applying all transformations
|
|
161
|
+
*/ async value() {
|
|
129
162
|
let value = await _class_private_field_get(this, _value);
|
|
130
163
|
switch(_class_private_field_get(this, _format)){
|
|
131
164
|
case 'human':
|
|
@@ -144,16 +177,32 @@ class Checker {
|
|
|
144
177
|
value = _class_private_method_get(this, _redact, redact).call(this, value);
|
|
145
178
|
return value;
|
|
146
179
|
}
|
|
147
|
-
|
|
180
|
+
/**
|
|
181
|
+
* Assert that the value matches a snapshot
|
|
182
|
+
* @param msg - Optional message for the assertion
|
|
183
|
+
*/ async toMatchSnapshot(msg) {
|
|
148
184
|
return _class_private_field_get(this, _expectFn).call(this, await this.value()).toMatchSnapshot(msg ?? _class_private_field_get(this, _message));
|
|
149
185
|
}
|
|
150
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Assert that the value matches an expected value
|
|
188
|
+
* @param value - Expected value
|
|
189
|
+
* @param msg - Optional message for the assertion
|
|
190
|
+
*/ async toMatch(value, msg) {
|
|
151
191
|
return _class_private_field_get(this, _expectFn).call(this, await this.value()).toMatch(value, msg ?? _class_private_field_get(this, _message));
|
|
152
192
|
}
|
|
153
|
-
|
|
193
|
+
/**
|
|
194
|
+
* Assert that the value matches an expected object structure
|
|
195
|
+
* @param value - Expected object structure
|
|
196
|
+
* @param msg - Optional message for the assertion
|
|
197
|
+
*/ async toMatchObject(value, msg) {
|
|
154
198
|
return _class_private_field_get(this, _expectFn).call(this, await this.value()).toMatchObject(value, msg ?? _class_private_field_get(this, _message));
|
|
155
199
|
}
|
|
156
|
-
|
|
200
|
+
/**
|
|
201
|
+
* Creates a new Checker instance
|
|
202
|
+
* @param expectFn - Function for making test assertions
|
|
203
|
+
* @param value - Value to check
|
|
204
|
+
* @param message - Optional message for assertions
|
|
205
|
+
*/ constructor(expectFn, value, message){
|
|
157
206
|
_class_private_method_init(this, _redact);
|
|
158
207
|
_class_private_field_init(this, _expectFn, {
|
|
159
208
|
writable: true,
|
|
@@ -195,7 +244,7 @@ function redact(value) {
|
|
|
195
244
|
const redactAddress = _class_private_field_get(this, _redactOptions).address === true;
|
|
196
245
|
const processNumber = (value)=>{
|
|
197
246
|
if (precision > 0) {
|
|
198
|
-
const rounded = parseFloat(value.toPrecision(precision));
|
|
247
|
+
const rounded = Number.parseFloat(value.toPrecision(precision));
|
|
199
248
|
if (rounded === value) {
|
|
200
249
|
return rounded;
|
|
201
250
|
}
|
|
@@ -216,7 +265,7 @@ function redact(value) {
|
|
|
216
265
|
if (typeof obj === 'string') {
|
|
217
266
|
if (redactNumber && obj.match(/0x000000[0-9a-f]{26}/)) {
|
|
218
267
|
// this is very likely u128 encoded in hex
|
|
219
|
-
const num = parseInt(obj);
|
|
268
|
+
const num = Number.parseInt(obj);
|
|
220
269
|
return processNumber(num);
|
|
221
270
|
}
|
|
222
271
|
if (redactHash && obj.match(/0x[0-9a-f]{64}/)) {
|
|
@@ -229,7 +278,7 @@ function redact(value) {
|
|
|
229
278
|
return '(address)';
|
|
230
279
|
}
|
|
231
280
|
if (redactNumber && obj.match(/^-?[\d,]+$/)) {
|
|
232
|
-
const num = parseInt(obj.replace(/,/g, ''));
|
|
281
|
+
const num = Number.parseInt(obj.replace(/,/g, ''));
|
|
233
282
|
return processNumber(num);
|
|
234
283
|
}
|
|
235
284
|
return obj;
|
|
@@ -258,7 +307,11 @@ function redact(value) {
|
|
|
258
307
|
return process(value);
|
|
259
308
|
}
|
|
260
309
|
const withExpect = (expectFn)=>{
|
|
261
|
-
|
|
310
|
+
/**
|
|
311
|
+
* Create a new Checker instance
|
|
312
|
+
* @param value - Value to check
|
|
313
|
+
* @param msg - Optional message for assertions
|
|
314
|
+
*/ const check = (value, msg)=>{
|
|
262
315
|
if (value instanceof Checker) {
|
|
263
316
|
if (msg) {
|
|
264
317
|
return value.message(msg);
|
|
@@ -267,14 +320,32 @@ const withExpect = (expectFn)=>{
|
|
|
267
320
|
}
|
|
268
321
|
return new Checker(expectFn, value, msg);
|
|
269
322
|
};
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
323
|
+
/**
|
|
324
|
+
* Check blockchain events with filtering and redaction
|
|
325
|
+
* @param events - Events to check
|
|
326
|
+
* @param filters - Event filters to apply
|
|
327
|
+
*/ const checkEvents = ({ events }, ...filters)=>check(events, 'events').filterEvents(...filters).redact();
|
|
328
|
+
/**
|
|
329
|
+
* Check system events with filtering and redaction
|
|
330
|
+
* @param api - Polkadot API instance
|
|
331
|
+
* @param filters - Event filters to apply
|
|
332
|
+
*/ const checkSystemEvents = ({ api }, ...filters)=>check(api.query.system.events(), 'system events').filterEvents(...filters).redact();
|
|
333
|
+
/**
|
|
334
|
+
* Check Upward Message Passing (UMP) messages
|
|
335
|
+
* @param api - Polkadot API instance
|
|
336
|
+
*/ const checkUmp = ({ api })=>check(api.query.parachainSystem.upwardMessages(), 'ump').map((value)=>api.createType('Vec<XcmVersionedXcm>', value).toJSON());
|
|
337
|
+
/**
|
|
338
|
+
* Check HRMP (Horizontal Relay-routed Message Passing) messages
|
|
339
|
+
* @param api - Polkadot API instance
|
|
340
|
+
*/ const checkHrmp = ({ api })=>check(api.query.parachainSystem.hrmpOutboundMessages(), 'hrmp').map((value)=>value.map(({ recipient, data })=>({
|
|
274
341
|
data: api.createType('(XcmpMessageFormat, XcmVersionedXcm)', data).toJSON(),
|
|
275
342
|
recipient
|
|
276
343
|
})));
|
|
277
|
-
|
|
344
|
+
/**
|
|
345
|
+
* Check a value in hexadecimal format
|
|
346
|
+
* @param value - Value to check
|
|
347
|
+
* @param msg - Optional message for assertions
|
|
348
|
+
*/ const checkHex = (value, msg)=>check(value, msg).toHex();
|
|
278
349
|
return {
|
|
279
350
|
check,
|
|
280
351
|
checkEvents,
|
package/dist/esm/check.d.ts
CHANGED
|
@@ -1,38 +1,110 @@
|
|
|
1
|
-
import { ApiPromise } from '@polkadot/api';
|
|
2
|
-
import { Codec } from '@polkadot/types/types';
|
|
1
|
+
import type { ApiPromise } from '@polkadot/api';
|
|
2
|
+
import type { Codec } from '@polkadot/types/types';
|
|
3
|
+
/**
|
|
4
|
+
* Defines a filter for blockchain events
|
|
5
|
+
* Can be either a string (section name) or an object with method and section
|
|
6
|
+
*/
|
|
3
7
|
export type EventFilter = string | {
|
|
4
8
|
method: string;
|
|
5
9
|
section: string;
|
|
6
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* Configuration options for data redaction
|
|
13
|
+
*/
|
|
7
14
|
export type RedactOptions = {
|
|
15
|
+
/** Redact numbers with optional precision */
|
|
8
16
|
number?: boolean | number;
|
|
17
|
+
/** Redact 32-byte hex values */
|
|
9
18
|
hash?: boolean;
|
|
19
|
+
/** Redact any hex values with 0x prefix */
|
|
10
20
|
hex?: boolean;
|
|
21
|
+
/** Redact base58 addresses */
|
|
11
22
|
address?: boolean;
|
|
23
|
+
/** Regex pattern for keys whose values should be redacted */
|
|
12
24
|
redactKeys?: RegExp;
|
|
25
|
+
/** Regex pattern for keys that should be removed */
|
|
13
26
|
removeKeys?: RegExp;
|
|
14
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Function type for test assertions
|
|
30
|
+
*/
|
|
15
31
|
export type ExpectFn = (value: any) => {
|
|
16
32
|
toMatchSnapshot: (msg?: string) => void;
|
|
17
33
|
toMatch(value: any, msg?: string): void;
|
|
18
34
|
toMatchObject(value: any, msg?: string): void;
|
|
19
35
|
};
|
|
36
|
+
/**
|
|
37
|
+
* Main class for checking and validating blockchain data
|
|
38
|
+
* Provides a fluent interface for data transformation, filtering, and assertion
|
|
39
|
+
*/
|
|
20
40
|
export declare class Checker {
|
|
21
41
|
#private;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new Checker instance
|
|
44
|
+
* @param expectFn - Function for making test assertions
|
|
45
|
+
* @param value - Value to check
|
|
46
|
+
* @param message - Optional message for assertions
|
|
47
|
+
*/
|
|
22
48
|
constructor(expectFn: ExpectFn, value: any, message?: string);
|
|
49
|
+
/** Convert the checked value to human-readable format */
|
|
23
50
|
toHuman(): this;
|
|
51
|
+
/** Convert the checked value to hexadecimal format */
|
|
24
52
|
toHex(): this;
|
|
53
|
+
/** Convert the checked value to JSON format */
|
|
25
54
|
toJson(): this;
|
|
55
|
+
/**
|
|
56
|
+
* Set a message for test assertions
|
|
57
|
+
* @param message - Message to use in assertions
|
|
58
|
+
*/
|
|
26
59
|
message(message: string): this;
|
|
60
|
+
/**
|
|
61
|
+
* Filter blockchain events based on provided filters
|
|
62
|
+
* @param filters - Event filters to apply
|
|
63
|
+
*/
|
|
27
64
|
filterEvents(...filters: EventFilter[]): this;
|
|
65
|
+
/**
|
|
66
|
+
* Apply redaction rules to the checked value
|
|
67
|
+
* @param options - Redaction options
|
|
68
|
+
*/
|
|
28
69
|
redact(options?: RedactOptions): this;
|
|
70
|
+
/**
|
|
71
|
+
* Add a transformation function to the processing pipeline
|
|
72
|
+
* @param fn - Transformation function
|
|
73
|
+
*/
|
|
29
74
|
map(fn: (value: any) => any): this;
|
|
75
|
+
/**
|
|
76
|
+
* Apply a function to the current Checker instance
|
|
77
|
+
* @param fn - Function to apply
|
|
78
|
+
*/
|
|
30
79
|
pipe(fn?: (value: Checker) => Checker): Checker;
|
|
80
|
+
/**
|
|
81
|
+
* Get the final processed value
|
|
82
|
+
* @returns Processed value after applying all transformations
|
|
83
|
+
*/
|
|
31
84
|
value(): Promise<any>;
|
|
85
|
+
/**
|
|
86
|
+
* Assert that the value matches a snapshot
|
|
87
|
+
* @param msg - Optional message for the assertion
|
|
88
|
+
*/
|
|
32
89
|
toMatchSnapshot(msg?: string): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Assert that the value matches an expected value
|
|
92
|
+
* @param value - Expected value
|
|
93
|
+
* @param msg - Optional message for the assertion
|
|
94
|
+
*/
|
|
33
95
|
toMatch(value: any, msg?: string): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Assert that the value matches an expected object structure
|
|
98
|
+
* @param value - Expected object structure
|
|
99
|
+
* @param msg - Optional message for the assertion
|
|
100
|
+
*/
|
|
34
101
|
toMatchObject(value: any, msg?: string): Promise<void>;
|
|
35
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* Creates a set of checking utilities with a provided assertion function
|
|
105
|
+
* @param expectFn - Function for making test assertions
|
|
106
|
+
* @returns Object containing various checking utilities
|
|
107
|
+
*/
|
|
36
108
|
export declare const withExpect: (expectFn: ExpectFn) => {
|
|
37
109
|
check: (value: any, msg?: string) => Checker;
|
|
38
110
|
checkEvents: ({ events }: {
|
package/dist/esm/check.js
CHANGED
|
@@ -1,36 +1,64 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Processes a Codec or array of Codecs with a given transformation function
|
|
3
|
+
* @param codec - Single Codec or array of Codecs to process
|
|
4
|
+
* @param fn - Transformation function to apply to each Codec
|
|
5
|
+
* @returns Processed value(s)
|
|
6
|
+
*/ const processCodecOrArray = (codec, fn)=>Array.isArray(codec) ? codec.map(fn) : fn(codec);
|
|
7
|
+
/**
|
|
8
|
+
* Converts Codec data to human-readable format
|
|
9
|
+
* @param codec - Codec data to convert
|
|
10
|
+
*/ const toHuman = (codec)=>processCodecOrArray(codec, (c)=>c?.toHuman?.() ?? c);
|
|
11
|
+
/**
|
|
12
|
+
* Converts Codec data to hexadecimal format
|
|
13
|
+
* @param codec - Codec data to convert
|
|
14
|
+
*/ const toHex = (codec)=>processCodecOrArray(codec, (c)=>c?.toHex?.() ?? c);
|
|
15
|
+
/**
|
|
16
|
+
* Converts Codec data to JSON format
|
|
17
|
+
* @param codec - Codec data to convert
|
|
18
|
+
*/ const toJson = (codec)=>processCodecOrArray(codec, (c)=>c?.toJSON?.() ?? c);
|
|
19
|
+
/**
|
|
20
|
+
* Main class for checking and validating blockchain data
|
|
21
|
+
* Provides a fluent interface for data transformation, filtering, and assertion
|
|
22
|
+
*/ export class Checker {
|
|
6
23
|
#expectFn;
|
|
7
24
|
#value;
|
|
8
25
|
#pipeline = [];
|
|
9
26
|
#format = 'json';
|
|
10
27
|
#message;
|
|
11
28
|
#redactOptions;
|
|
12
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Creates a new Checker instance
|
|
31
|
+
* @param expectFn - Function for making test assertions
|
|
32
|
+
* @param value - Value to check
|
|
33
|
+
* @param message - Optional message for assertions
|
|
34
|
+
*/ constructor(expectFn, value, message){
|
|
13
35
|
this.#expectFn = expectFn;
|
|
14
36
|
this.#value = value;
|
|
15
37
|
this.#message = message;
|
|
16
38
|
}
|
|
17
|
-
toHuman() {
|
|
39
|
+
/** Convert the checked value to human-readable format */ toHuman() {
|
|
18
40
|
this.#format = 'human';
|
|
19
41
|
return this;
|
|
20
42
|
}
|
|
21
|
-
toHex() {
|
|
43
|
+
/** Convert the checked value to hexadecimal format */ toHex() {
|
|
22
44
|
this.#format = 'hex';
|
|
23
45
|
return this;
|
|
24
46
|
}
|
|
25
|
-
toJson() {
|
|
47
|
+
/** Convert the checked value to JSON format */ toJson() {
|
|
26
48
|
this.#format = 'json';
|
|
27
49
|
return this;
|
|
28
50
|
}
|
|
29
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Set a message for test assertions
|
|
53
|
+
* @param message - Message to use in assertions
|
|
54
|
+
*/ message(message) {
|
|
30
55
|
this.#message = message;
|
|
31
56
|
return this;
|
|
32
57
|
}
|
|
33
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Filter blockchain events based on provided filters
|
|
60
|
+
* @param filters - Event filters to apply
|
|
61
|
+
*/ filterEvents(...filters) {
|
|
34
62
|
this.toHuman();
|
|
35
63
|
this.#pipeline.push((value)=>{
|
|
36
64
|
let data = value.map(({ event: { index: _, ...event } })=>event);
|
|
@@ -39,7 +67,8 @@ export class Checker {
|
|
|
39
67
|
return filters.some((filter)=>{
|
|
40
68
|
if (typeof filter === 'string') {
|
|
41
69
|
return evt.section === filter;
|
|
42
|
-
}
|
|
70
|
+
}
|
|
71
|
+
if ('method' in filter) {
|
|
43
72
|
const { section, method } = filter;
|
|
44
73
|
return evt.section === section && evt.method === method;
|
|
45
74
|
}
|
|
@@ -50,7 +79,10 @@ export class Checker {
|
|
|
50
79
|
});
|
|
51
80
|
return this;
|
|
52
81
|
}
|
|
53
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Apply redaction rules to the checked value
|
|
84
|
+
* @param options - Redaction options
|
|
85
|
+
*/ redact(options = {
|
|
54
86
|
number: 2,
|
|
55
87
|
hash: true
|
|
56
88
|
}) {
|
|
@@ -71,7 +103,7 @@ export class Checker {
|
|
|
71
103
|
const redactAddress = this.#redactOptions.address === true;
|
|
72
104
|
const processNumber = (value)=>{
|
|
73
105
|
if (precision > 0) {
|
|
74
|
-
const rounded = parseFloat(value.toPrecision(precision));
|
|
106
|
+
const rounded = Number.parseFloat(value.toPrecision(precision));
|
|
75
107
|
if (rounded === value) {
|
|
76
108
|
return rounded;
|
|
77
109
|
}
|
|
@@ -92,7 +124,7 @@ export class Checker {
|
|
|
92
124
|
if (typeof obj === 'string') {
|
|
93
125
|
if (redactNumber && obj.match(/0x000000[0-9a-f]{26}/)) {
|
|
94
126
|
// this is very likely u128 encoded in hex
|
|
95
|
-
const num = parseInt(obj);
|
|
127
|
+
const num = Number.parseInt(obj);
|
|
96
128
|
return processNumber(num);
|
|
97
129
|
}
|
|
98
130
|
if (redactHash && obj.match(/0x[0-9a-f]{64}/)) {
|
|
@@ -105,7 +137,7 @@ export class Checker {
|
|
|
105
137
|
return '(address)';
|
|
106
138
|
}
|
|
107
139
|
if (redactNumber && obj.match(/^-?[\d,]+$/)) {
|
|
108
|
-
const num = parseInt(obj.replace(/,/g, ''));
|
|
140
|
+
const num = Number.parseInt(obj.replace(/,/g, ''));
|
|
109
141
|
return processNumber(num);
|
|
110
142
|
}
|
|
111
143
|
return obj;
|
|
@@ -133,14 +165,23 @@ export class Checker {
|
|
|
133
165
|
};
|
|
134
166
|
return process(value);
|
|
135
167
|
}
|
|
136
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Add a transformation function to the processing pipeline
|
|
170
|
+
* @param fn - Transformation function
|
|
171
|
+
*/ map(fn) {
|
|
137
172
|
this.#pipeline.push(fn);
|
|
138
173
|
return this;
|
|
139
174
|
}
|
|
140
|
-
|
|
175
|
+
/**
|
|
176
|
+
* Apply a function to the current Checker instance
|
|
177
|
+
* @param fn - Function to apply
|
|
178
|
+
*/ pipe(fn) {
|
|
141
179
|
return fn ? fn(this) : this;
|
|
142
180
|
}
|
|
143
|
-
|
|
181
|
+
/**
|
|
182
|
+
* Get the final processed value
|
|
183
|
+
* @returns Processed value after applying all transformations
|
|
184
|
+
*/ async value() {
|
|
144
185
|
let value = await this.#value;
|
|
145
186
|
switch(this.#format){
|
|
146
187
|
case 'human':
|
|
@@ -159,18 +200,37 @@ export class Checker {
|
|
|
159
200
|
value = this.#redact(value);
|
|
160
201
|
return value;
|
|
161
202
|
}
|
|
162
|
-
|
|
203
|
+
/**
|
|
204
|
+
* Assert that the value matches a snapshot
|
|
205
|
+
* @param msg - Optional message for the assertion
|
|
206
|
+
*/ async toMatchSnapshot(msg) {
|
|
163
207
|
return this.#expectFn(await this.value()).toMatchSnapshot(msg ?? this.#message);
|
|
164
208
|
}
|
|
165
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Assert that the value matches an expected value
|
|
211
|
+
* @param value - Expected value
|
|
212
|
+
* @param msg - Optional message for the assertion
|
|
213
|
+
*/ async toMatch(value, msg) {
|
|
166
214
|
return this.#expectFn(await this.value()).toMatch(value, msg ?? this.#message);
|
|
167
215
|
}
|
|
168
|
-
|
|
216
|
+
/**
|
|
217
|
+
* Assert that the value matches an expected object structure
|
|
218
|
+
* @param value - Expected object structure
|
|
219
|
+
* @param msg - Optional message for the assertion
|
|
220
|
+
*/ async toMatchObject(value, msg) {
|
|
169
221
|
return this.#expectFn(await this.value()).toMatchObject(value, msg ?? this.#message);
|
|
170
222
|
}
|
|
171
223
|
}
|
|
172
|
-
|
|
173
|
-
|
|
224
|
+
/**
|
|
225
|
+
* Creates a set of checking utilities with a provided assertion function
|
|
226
|
+
* @param expectFn - Function for making test assertions
|
|
227
|
+
* @returns Object containing various checking utilities
|
|
228
|
+
*/ export const withExpect = (expectFn)=>{
|
|
229
|
+
/**
|
|
230
|
+
* Create a new Checker instance
|
|
231
|
+
* @param value - Value to check
|
|
232
|
+
* @param msg - Optional message for assertions
|
|
233
|
+
*/ const check = (value, msg)=>{
|
|
174
234
|
if (value instanceof Checker) {
|
|
175
235
|
if (msg) {
|
|
176
236
|
return value.message(msg);
|
|
@@ -179,14 +239,32 @@ export const withExpect = (expectFn)=>{
|
|
|
179
239
|
}
|
|
180
240
|
return new Checker(expectFn, value, msg);
|
|
181
241
|
};
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
242
|
+
/**
|
|
243
|
+
* Check blockchain events with filtering and redaction
|
|
244
|
+
* @param events - Events to check
|
|
245
|
+
* @param filters - Event filters to apply
|
|
246
|
+
*/ const checkEvents = ({ events }, ...filters)=>check(events, 'events').filterEvents(...filters).redact();
|
|
247
|
+
/**
|
|
248
|
+
* Check system events with filtering and redaction
|
|
249
|
+
* @param api - Polkadot API instance
|
|
250
|
+
* @param filters - Event filters to apply
|
|
251
|
+
*/ const checkSystemEvents = ({ api }, ...filters)=>check(api.query.system.events(), 'system events').filterEvents(...filters).redact();
|
|
252
|
+
/**
|
|
253
|
+
* Check Upward Message Passing (UMP) messages
|
|
254
|
+
* @param api - Polkadot API instance
|
|
255
|
+
*/ const checkUmp = ({ api })=>check(api.query.parachainSystem.upwardMessages(), 'ump').map((value)=>api.createType('Vec<XcmVersionedXcm>', value).toJSON());
|
|
256
|
+
/**
|
|
257
|
+
* Check HRMP (Horizontal Relay-routed Message Passing) messages
|
|
258
|
+
* @param api - Polkadot API instance
|
|
259
|
+
*/ const checkHrmp = ({ api })=>check(api.query.parachainSystem.hrmpOutboundMessages(), 'hrmp').map((value)=>value.map(({ recipient, data })=>({
|
|
186
260
|
data: api.createType('(XcmpMessageFormat, XcmVersionedXcm)', data).toJSON(),
|
|
187
261
|
recipient
|
|
188
262
|
})));
|
|
189
|
-
|
|
263
|
+
/**
|
|
264
|
+
* Check a value in hexadecimal format
|
|
265
|
+
* @param value - Value to check
|
|
266
|
+
* @param msg - Optional message for assertions
|
|
267
|
+
*/ const checkHex = (value, msg)=>check(value, msg).toHex();
|
|
190
268
|
return {
|
|
191
269
|
check,
|
|
192
270
|
checkEvents,
|
package/package.json
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks-testing",
|
|
3
|
-
"version": "1.0.2
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"author": "Acala Developers <hello@acala.network>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=v20"
|
|
9
|
+
},
|
|
7
10
|
"scripts": {
|
|
8
11
|
"clean": "rm -rf dist tsconfig.tsbuildinfo",
|
|
9
12
|
"build": "yarn clean && ../../package-build.sh",
|
|
10
13
|
"depcheck": "npx depcheck"
|
|
11
14
|
},
|
|
12
15
|
"dependencies": {
|
|
13
|
-
"@acala-network/chopsticks-utils": "1.0.2
|
|
14
|
-
"@polkadot/api": "^
|
|
15
|
-
"@polkadot/types": "^
|
|
16
|
+
"@acala-network/chopsticks-utils": "1.0.2",
|
|
17
|
+
"@polkadot/api": "^15.0",
|
|
18
|
+
"@polkadot/types": "^15.0"
|
|
16
19
|
},
|
|
17
20
|
"devDependencies": {
|
|
18
|
-
"@swc/cli": "0.5.
|
|
19
|
-
"@swc/core": "^1.
|
|
20
|
-
"typescript": "^5.
|
|
21
|
+
"@swc/cli": "0.5.2",
|
|
22
|
+
"@swc/core": "^1.10.1",
|
|
23
|
+
"typescript": "^5.7.2"
|
|
21
24
|
},
|
|
22
25
|
"files": [
|
|
23
26
|
"dist/esm/**",
|