@@ -4,8 +4,8 @@ import { parseOpenRPCDocument } from '@open-rpc/schema-utils-js';
4
4
import { expect } from 'chai' ;
5
5
import fs from 'fs' ;
6
6
import path from 'path' ;
7
+ import WebSocket from 'ws' ;
7
8
8
- // import WebSocket from 'ws';
9
9
import openRpcData from '../../../../docs/openrpc.json' ;
10
10
import CallerContract from '../contracts/Caller.json' ;
11
11
import LogsContract from '../contracts/Logs.json' ;
@@ -21,8 +21,9 @@ import {
21
21
setTransaction1559_2930AndBlockHash ,
22
22
setTransaction1559AndBlockHash ,
23
23
setTransaction2930AndBlockHash ,
24
+ WS_RELAY_URL ,
24
25
} from './data/conformity/utils/constants' ;
25
- import { TestCases , UpdateParamFunction } from './data/conformity/utils/interfaces' ;
26
+ import { TestCase , TestCases , UpdateParamFunction } from './data/conformity/utils/interfaces' ;
26
27
import { processFileContent , splitReqAndRes } from './data/conformity/utils/processors' ;
27
28
import {
28
29
createContractLegacyTransaction ,
@@ -32,7 +33,7 @@ import {
32
33
transaction2930 ,
33
34
} from './data/conformity/utils/transactions' ;
34
35
import { getLatestBlockHash , sendRequestToRelay , signAndSendRawTransaction } from './data/conformity/utils/utils' ;
35
- import { isResponseValid } from './data/conformity/utils/validations' ;
36
+ import { getMissingKeys , isResponseValid } from './data/conformity/utils/validations' ;
36
37
37
38
const directoryPath = path . resolve ( __dirname , '../../../../node_modules/execution-apis/tests' ) ;
38
39
const overwritesDirectoryPath = path . resolve ( __dirname , 'data/conformity/overwrites' ) ;
@@ -42,19 +43,48 @@ let relayOpenRpcData: OpenrpcDocument;
42
43
relayOpenRpcData = await parseOpenRPCDocument ( JSON . stringify ( openRpcData ) ) ;
43
44
} ) ( ) . catch ( ( error ) => console . error ( 'Error parsing OpenRPC document:' , error ) ) ;
44
45
46
+ /**
47
+ * Determines whether a given test case is expected to return an error response.
48
+ *
49
+ * @param testCase - The test case to evaluate.
50
+ * @returns {boolean } `true` if an error response is expected, otherwise `false`.
51
+ *
52
+ * @example
53
+ * ```typescript
54
+ * const tc = { status: 404, response: '{"error": "Not found"}' };
55
+ * console.log(isErrorResponseExpected(tc)); // true
56
+ * ```
57
+ */
58
+ const isErrorResponseExpected = function ( testCase : TestCase ) : boolean {
59
+ return ( testCase ?. status && testCase . status != 200 ) || ! ! JSON . parse ( testCase . response ) . error ;
60
+ } ;
61
+
62
+ /**
63
+ * Retrieves the JSON schema object for the result of a given method name from the OpenRPC data.
64
+ *
65
+ * @param name - The name of the method to look up.
66
+ * @returns {JSONSchemaObject | undefined } The method's result schema, or `undefined` if not found or invalid.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * const schema = getMethodSchema("eth_getBalance");
71
+ * console.log(schema); // JSON schema object or undefined
72
+ * ```
73
+ */
74
+ const getMethodSchema = function ( name : string ) : JSONSchemaObject | undefined {
75
+ const method = relayOpenRpcData . methods . find (
76
+ ( m : MethodOrReference ) : m is MethodObject => 'name' in m && m . name === name ,
77
+ ) ;
78
+ return method ?. result && 'schema' in method . result && typeof method . result . schema === 'object'
79
+ ? method . result . schema
80
+ : undefined ;
81
+ } ;
82
+
45
83
const synthesizeTestCases = function ( testCases : TestCases , updateParamIfNeeded : UpdateParamFunction ) {
46
84
for ( const testName in testCases ) {
47
85
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 ;
86
+ const isErrorStatusExpected = isErrorResponseExpected ( testCases [ testName ] ) ;
87
+ const schema = getMethodSchema ( testName . split ( ' ' ) [ 0 ] ) ;
58
88
try {
59
89
const req = updateParamIfNeeded ( testName , JSON . parse ( testCases [ testName ] . request ) ) ;
60
90
const res = await sendRequestToRelay ( RELAY_URL , req , false ) ;
@@ -240,7 +270,7 @@ describe('@api-conformity', async function () {
240
270
241
271
synthesizeTestCases ( TEST_CASES_BATCH_3 [ 'server' ] , updateParamIfNeeded ) ;
242
272
243
- /* describe('ws related rpc methods', async function () {
273
+ describe ( 'ws related rpc methods' , async function ( ) {
244
274
let webSocket : WebSocket ;
245
275
let contractAddress : string | null ;
246
276
let existingFilter : string ;
@@ -319,14 +349,14 @@ describe('@api-conformity', async function () {
319
349
} ) ;
320
350
await new Promise ( ( r ) => setTimeout ( r , 500 ) ) ;
321
351
322
- const hasMissingKeys: boolean = hasResponseFormatIssues (response, JSON.parse(testCases[testName].response));
352
+ const hasMissingKeys = getMissingKeys ( response , JSON . parse ( testCases [ testName ] . response ) ) . length > 0 ;
323
353
expect ( hasMissingKeys ) . to . be . false ;
324
354
} ) ;
325
355
}
326
356
} ;
327
357
328
358
synthesizeWsTestCases ( TEST_CASES_BATCH_3 [ 'ws-server' ] , updateParamIfNeeded ) ;
329
- });*/
359
+ } ) ;
330
360
} ) ;
331
361
332
362
describe ( '@conformity-batch-4 Ethereum execution apis tests' , async function ( ) {
0 commit comments