Skip to content

Commit 1fb8c97

Browse files
docs: applying codacy suggestions (#3054)
Signed-off-by: Mariusz Jasuwienas <mariusz.jasuwienas@arianelabs.com>
1 parent c418d07 commit 1fb8c97

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

packages/server/tests/acceptance/conformityTests.spec.ts

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
setTransaction1559AndBlockHash,
2323
setTransaction2930AndBlockHash,
2424
} from './data/conformity/utils/constants';
25-
import { TestCases, UpdateParamFunction } from './data/conformity/utils/interfaces';
25+
import { JsonRpcResponse, TestCases, UpdateParamFunction } from './data/conformity/utils/interfaces';
2626
import { processFileContent, splitReqAndRes } from './data/conformity/utils/processors';
2727
import {
2828
createContractLegacyTransaction,
@@ -42,35 +42,58 @@ let relayOpenRpcData: OpenrpcDocument;
4242
relayOpenRpcData = await parseOpenRPCDocument(JSON.stringify(openRpcData));
4343
})().catch((error) => console.error('Error parsing OpenRPC document:', error));
4444

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);
7186
}
7287
};
7388

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+
7497
/**
7598
* To run the Ethereum Execution API tests as defined in the repository ethereum/execution-apis, it’s necessary
7699
* to execute them against a specifically configured node. This node must use:

0 commit comments

Comments
 (0)