Skip to content

test: batch 2 conformity tests #4110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 78 additions & 62 deletions packages/server/tests/acceptance/conformityTests.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
// SPDX-License-Identifier: Apache-2.0
// import {
// JSONSchemaObject,
// MethodObject,
// MethodOrReference,
// OpenrpcDocument,
// } from '@open-rpc/meta-schema';
// import { parseOpenRPCDocument } from '@open-rpc/schema-utils-js';
// import { expect } from 'chai';
import { JSONSchemaObject, MethodObject, MethodOrReference, OpenrpcDocument } from '@open-rpc/meta-schema';
import { parseOpenRPCDocument } from '@open-rpc/schema-utils-js';
import { expect } from 'chai';
import fs from 'fs';
import path from 'path';
import WebSocket from 'ws';

// import WebSocket from 'ws';
import openRpcData from '../../../../docs/openrpc.json';
// import CallerContract from '../contracts/Caller.json';
// import LogsContract from '../contracts/Logs.json';
import CallerContract from '../contracts/Caller.json';
import LogsContract from '../contracts/Logs.json';
import {
chainId,
gasLimit,
Expand All @@ -26,9 +21,9 @@ import {
setTransaction1559_2930AndBlockHash,
setTransaction1559AndBlockHash,
setTransaction2930AndBlockHash,
// WS_RELAY_URL,
WS_RELAY_URL,
} from './data/conformity/utils/constants';
// import { TestCases, UpdateParamFunction } from './data/conformity/utils/interfaces';
import { TestCase, TestCases, UpdateParamFunction } from './data/conformity/utils/interfaces';
import { processFileContent, splitReqAndRes } from './data/conformity/utils/processors';
import {
createContractLegacyTransaction,
Expand All @@ -37,53 +32,74 @@ import {
transaction1559_2930,
transaction2930,
} from './data/conformity/utils/transactions';
import {
getLatestBlockHash,
// sendRequestToRelay,
signAndSendRawTransaction,
} from './data/conformity/utils/utils';
// import { hasResponseFormatIssues, isResponseValid } from './data/conformity/utils/validations';
import { getLatestBlockHash, sendRequestToRelay, signAndSendRawTransaction } from './data/conformity/utils/utils';
import { getMissingKeys, isResponseValid } from './data/conformity/utils/validations';

const directoryPath = path.resolve(__dirname, '../../../../node_modules/execution-apis/tests');
const overwritesDirectoryPath = path.resolve(__dirname, 'data/conformity/overwrites');

// let relayOpenRpcData: OpenrpcDocument;
// (async () => {
// relayOpenRpcData = await parseOpenRPCDocument(JSON.stringify(openRpcData));
// })().catch((error) => console.error('Error parsing OpenRPC document:', error));

// const synthesizeTestCases = function (testCases: TestCases, updateParamIfNeeded: UpdateParamFunction) {
// for (const testName in testCases) {
// it(`${testName}`, async function () {
// const isErrorStatusExpected: boolean =
// (testCases[testName]?.status && testCases[testName].status != 200) ||
// !!JSON.parse(testCases[testName].response).error;
// const method = relayOpenRpcData.methods.find(
// (m: MethodOrReference): m is MethodObject => 'name' in m && m.name === testName.split(' ')[0],
// );
// const schema: JSONSchemaObject | undefined =
// method?.result && 'schema' in method.result && typeof method.result.schema === 'object'
// ? method.result.schema
// : undefined;
// try {
// const req = updateParamIfNeeded(testName, JSON.parse(testCases[testName].request));
// const res = await sendRequestToRelay(RELAY_URL, req, false);
// const isResFormatInvalid: boolean = hasResponseFormatIssues(res, JSON.parse(testCases[testName].response));
//
// if (schema && schema.pattern) {
// const check = isResponseValid(schema, res);
// expect(check).to.be.true;
// }
//
// expect(isResFormatInvalid).to.be.false;
// expect(isErrorStatusExpected).to.be.false;
// } catch (e: any) {
// expect(isErrorStatusExpected).to.be.true;
// expect(e?.response?.status).to.equal(testCases[testName].status);
// }
// });
// }
// };
let relayOpenRpcData: OpenrpcDocument;
(async () => {
relayOpenRpcData = await parseOpenRPCDocument(JSON.stringify(openRpcData));
})().catch((error) => console.error('Error parsing OpenRPC document:', error));

/**
* Determines whether a given test case is expected to return an error response.
*
* @param testCase - The test case to evaluate.
* @returns {boolean} `true` if an error response is expected, otherwise `false`.
*
* @example
* ```typescript
* const tc = { status: 404, response: '{"error": "Not found"}' };
* console.log(isErrorResponseExpected(tc)); // true
* ```
*/
const isErrorResponseExpected = function (testCase: TestCase): boolean {
return (testCase?.status && testCase.status != 200) || !!JSON.parse(testCase.response).error;
};

/**
* Retrieves the JSON schema object for the result of a given method name from the OpenRPC data.
*
* @param name - The name of the method to look up.
* @returns {JSONSchemaObject | undefined} The method's result schema, or `undefined` if not found or invalid.
*
* @example
* ```typescript
* const schema = getMethodSchema("eth_getBalance");
* console.log(schema); // JSON schema object or undefined
* ```
*/
const getMethodSchema = function (name: string): JSONSchemaObject | undefined {
const method = relayOpenRpcData.methods.find(
(m: MethodOrReference): m is MethodObject => 'name' in m && m.name === name,
);
return method?.result && 'schema' in method.result && typeof method.result.schema === 'object'
? method.result.schema
: undefined;
};

const synthesizeTestCases = function (testCases: TestCases, updateParamIfNeeded: UpdateParamFunction) {
for (const testName in testCases) {
it(`${testName}`, async function () {
const isErrorStatusExpected = isErrorResponseExpected(testCases[testName]);
const schema = getMethodSchema(testName.split(' ')[0]);
try {
const req = updateParamIfNeeded(testName, JSON.parse(testCases[testName].request));
const res = await sendRequestToRelay(RELAY_URL, req, false);
if (schema && schema.pattern) {
const check = isResponseValid(schema, res);
expect(check).to.be.true;
}
expect(isErrorStatusExpected).to.be.false;
} catch (e: any) {
expect(isErrorStatusExpected).to.be.true;
expect(e?.response?.status).to.equal(testCases[testName].status);
}
});
}
};

/**
* To run the Ethereum Execution API tests as defined in the repository ethereum/execution-apis, it’s necessary
Expand Down Expand Up @@ -152,7 +168,7 @@ describe('@api-conformity', async function () {
//
// These test suites must be un-skipped. The code requires refactoring to resolve the
// static analysis issues before they can be re-enabled.
/* describe.skip('@conformity-batch-2 Ethereum execution apis tests', async function () {
describe('@conformity-batch-2 Ethereum execution apis tests', async function () {
this.timeout(240 * 1000);

let existingBlockFilter: string;
Expand Down Expand Up @@ -222,7 +238,7 @@ describe('@api-conformity', async function () {
synthesizeTestCases(TEST_CASES_BATCH_2, updateParamIfNeeded);
});

describe.skip('@conformity-batch-3 Ethereum execution apis tests', async function () {
describe('@conformity-batch-3 Ethereum execution apis tests', async function () {
this.timeout(240 * 1000);

let txHash: any;
Expand Down Expand Up @@ -333,7 +349,7 @@ describe('@api-conformity', async function () {
});
await new Promise((r) => setTimeout(r, 500));

const hasMissingKeys: boolean = hasResponseFormatIssues(response, JSON.parse(testCases[testName].response));
const hasMissingKeys = getMissingKeys(response, JSON.parse(testCases[testName].response)).length > 0;
expect(hasMissingKeys).to.be.false;
});
}
Expand All @@ -343,7 +359,7 @@ describe('@api-conformity', async function () {
});
});

describe.skip('@conformity-batch-4 Ethereum execution apis tests', async function () {
describe('@conformity-batch-4 Ethereum execution apis tests', async function () {
this.timeout(240 * 1000);

let existingCallerContractAddress: string | null;
Expand Down Expand Up @@ -543,12 +559,12 @@ describe('@api-conformity', async function () {
synthesizeTestCases(TEST_CASES_BATCH_4, updateParamIfNeeded);
});

describe.skip('@conformity-batch-5 Ethereum execution apis tests', async function () {
describe('@conformity-batch-5 Ethereum execution apis tests', async function () {
this.timeout(240 * 1000);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const TEST_CASES_BATCH_5 = require('./data/conformity-tests-batch-5.json');

const updateParamIfNeeded = (_testName: any, request: any) => request;
synthesizeTestCases(TEST_CASES_BATCH_5, updateParamIfNeeded);
});*/
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@
}
return true;
}

const actualResponseKeys = extractKeys(actualResponse as Record<string, unknown>);
const expectedResponseKeys = extractKeys(parsedExpectedResponse as Record<string, unknown>);
const filteredExpectedKeys = expectedResponseKeys.filter((key) => !wildcards.includes(key));
const missingKeys = filteredExpectedKeys.filter((key) => !actualResponseKeys.includes(key));

const missingKeys = getMissingKeys(actualResponse as Record<string, unknown>, parsedExpectedResponse, wildcards);
if (missingKeys.length > 0) {
console.log(`Missing keys in response: ${JSON.stringify(missingKeys)}`);
return true;
Expand All @@ -98,6 +93,46 @@
return hasValuesMismatch(actualResponse, parsedExpectedResponse, wildcards);
}

/**
* Returns the list of keys that exist in `expectedResponse` but are missing in `actualResponse`,
* excluding any keys listed in `wildcards`.
*
* This function compares only key presence (as produced by `extractKeys`)—it does not parse,
* validate error states, or compare values. Any keys in `expectedResponse` that match entries
* in `wildcards` are ignored.
*
* @param actualResponse - The actual response object whose keys will be checked.
* @param expectedResponse - The reference object whose keys are considered required.
* @param wildcards - Array of key paths to ignore during the check (default: []).
* @returns {string[]} Array of key names/paths that are required by `expectedResponse`
* but absent from `actualResponse`.
*
* @example
* ```typescript
* const actual = { result: "0x123" };
* const expected = { result: "0x123", id: 1 };
* const missing = getMissingKeys(actual, expected);
* console.log(missing); // ["id"]
* ```
*
* @example
* ```typescript
* const actual = { result: "0x123", timestamp: "2023-01-01" };
* const expected = { result: "0x123", timestamp: "2023-01-02", id: 1 };
* const missing = getMissingKeys(actual, expected, ["timestamp"]);
* console.log(missing); // ["id"] // "timestamp" is ignored by wildcard
* ```
*/
export function getMissingKeys(

Check warning on line 126 in packages/server/tests/acceptance/data/conformity/utils/validations.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/server/tests/acceptance/data/conformity/utils/validations.ts#L126

Method getMissingKeys has 11 parameters (limit is 8)

Check warning on line 126 in packages/server/tests/acceptance/data/conformity/utils/validations.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

packages/server/tests/acceptance/data/conformity/utils/validations.ts#L126

Method getMissingKeys has a cyclomatic complexity of 9 (limit is 8)
actualResponse: Record<string, unknown>,
expectedResponse: Record<string, unknown>,
wildcards: string[] = [],
): string[] {
const actualResponseKeys = extractKeys(actualResponse);
const expectedResponseKeys = extractKeys(expectedResponse);
return expectedResponseKeys.filter((key) => !actualResponseKeys.includes(key) && !wildcards.includes(key));
}

/**
* Checks if the actual response is missing required error properties
*
Expand Down
Loading