@@ -22,7 +22,7 @@ import {
22
22
setTransaction1559AndBlockHash ,
23
23
setTransaction2930AndBlockHash ,
24
24
} from './data/conformity/utils/constants' ;
25
- import { TestCases , UpdateParamFunction } from './data/conformity/utils/interfaces' ;
25
+ import { JsonRpcResponse , TestCases , UpdateParamFunction } from './data/conformity/utils/interfaces' ;
26
26
import { processFileContent , splitReqAndRes } from './data/conformity/utils/processors' ;
27
27
import {
28
28
createContractLegacyTransaction ,
@@ -42,35 +42,58 @@ let relayOpenRpcData: OpenrpcDocument;
42
42
relayOpenRpcData = await parseOpenRPCDocument ( JSON . stringify ( openRpcData ) ) ;
43
43
} ) ( ) . catch ( ( error ) => console . error ( 'Error parsing OpenRPC document:' , error ) ) ;
44
44
45
- const synthesizeTestCases = function ( testCases : TestCases , updateParamIfNeeded : UpdateParamFunction ) {
46
- for ( const testName in testCases ) {
47
- it ( `${ testName } ` , async function ( ) {
48
- const isErrorStatusExpected : boolean =
49
- ( testCases [ testName ] ?. status && testCases [ testName ] . status != 200 ) ||
50
- ! ! JSON . parse ( testCases [ testName ] . response ) . error ;
51
- const method = relayOpenRpcData . methods . find (
52
- ( m : MethodOrReference ) : m is MethodObject => 'name' in m && m . name === testName . split ( ' ' ) [ 0 ] ,
53
- ) ;
54
- const schema : JSONSchemaObject | undefined =
55
- method ?. result && 'schema' in method . result && typeof method . result . schema === 'object'
56
- ? method . result . schema
57
- : undefined ;
58
- try {
59
- const req = updateParamIfNeeded ( testName , JSON . parse ( testCases [ testName ] . request ) ) ;
60
- const res = await sendRequestToRelay ( RELAY_URL , req , false ) ;
61
- if ( schema && schema . pattern ) {
62
- const check = isResponseValid ( schema , res ) ;
63
- expect ( check ) . to . be . true ;
64
- }
65
- expect ( isErrorStatusExpected ) . to . be . false ;
66
- } catch ( e : any ) {
67
- expect ( isErrorStatusExpected ) . to . be . true ;
68
- expect ( e ?. response ?. status ) . to . equal ( testCases [ testName ] . status ) ;
69
- }
70
- } ) ;
45
+ const getBaseName = ( testName : string ) => testName . split ( ' ' ) [ 0 ] ;
46
+
47
+ const findResultSchema = ( testName : string ) : JSONSchemaObject | undefined => {
48
+ const method = relayOpenRpcData . methods . find (
49
+ ( m : MethodOrReference ) : m is MethodObject => 'name' in m && m . name === getBaseName ( testName ) ,
50
+ ) ;
51
+ return method ?. result && 'schema' in method . result && typeof method . result . schema === 'object'
52
+ ? ( method . result . schema as JSONSchemaObject )
53
+ : undefined ;
54
+ } ;
55
+
56
+ const validateIfPattern = ( schema : JSONSchemaObject | undefined , res : JsonRpcResponse ) => {
57
+ if ( schema ?. pattern ) {
58
+ const check = isResponseValid ( schema , res ) ;
59
+ expect ( check ) . to . be . true ;
60
+ }
61
+ } ;
62
+
63
+ const runSingleTest = async (
64
+ testName : string ,
65
+ raw : TestCases [ string ] ,
66
+ updateParamIfNeeded : UpdateParamFunction ,
67
+ ) : Promise < void > => {
68
+ const parsedRequest = JSON . parse ( raw . request ) ;
69
+ const parsedResponse = JSON . parse ( raw . response ) ;
70
+ const hasOkStatus = raw ?. status && raw . status !== 200 ;
71
+ const hasError = Boolean ( ( parsedResponse as any ) ?. error ) ;
72
+ if ( ! hasOkStatus && ! hasError ) {
73
+ const req = updateParamIfNeeded ( testName , parsedRequest ) ;
74
+ const res = await sendRequestToRelay ( RELAY_URL , req , false ) ;
75
+ const schema = findResultSchema ( testName ) ;
76
+ validateIfPattern ( schema , res ) ;
77
+ return ;
78
+ }
79
+
80
+ try {
81
+ const req = updateParamIfNeeded ( testName , parsedRequest ) ;
82
+ await sendRequestToRelay ( RELAY_URL , req , false ) ;
83
+ expect . fail ( 'Expected request to fail, but it succeeded.' ) ;
84
+ } catch ( e : any ) {
85
+ expect ( e ?. response ?. status ) . to . equal ( raw . status ) ;
71
86
}
72
87
} ;
73
88
89
+ export const synthesizeTestCases = function ( testCases : TestCases , updateParamIfNeeded : UpdateParamFunction ) {
90
+ Object . keys ( testCases ) . forEach ( ( testName ) => {
91
+ it ( testName , async function ( ) {
92
+ await runSingleTest ( testName , testCases [ testName ] , updateParamIfNeeded ) ;
93
+ } ) ;
94
+ } ) ;
95
+ } ;
96
+
74
97
/**
75
98
* To run the Ethereum Execution API tests as defined in the repository ethereum/execution-apis, it’s necessary
76
99
* to execute them against a specifically configured node. This node must use:
0 commit comments