Skip to content

Commit df81ef7

Browse files
committed
chore: rename checkResponseFormat to hasResponseFormatIssues and update references with improved JSDoc documentation (#3886)
Signed-off-by: Michał Walczak <[email protected]>
1 parent 213fa2a commit df81ef7

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
transaction2930,
3535
} from './data/conformity/utils/transactions';
3636
import { getLatestBlockHash, sendRequestToRelay, signAndSendRawTransaction } from './data/conformity/utils/utils';
37-
import { checkResponseFormat, isResponseValid } from './data/conformity/utils/validations';
37+
import { hasResponseFormatIssues, isResponseValid } from './data/conformity/utils/validations';
3838

3939
const directoryPath = path.resolve(__dirname, '../../../../node_modules/execution-apis/tests');
4040
const overwritesDirectoryPath = path.resolve(__dirname, 'data/conformity/overwrites');
@@ -60,14 +60,14 @@ const synthesizeTestCases = function (testCases: TestCases, updateParamIfNeeded:
6060
try {
6161
const req = updateParamIfNeeded(testName, JSON.parse(testCases[testName].request));
6262
const res = await sendRequestToRelay(RELAY_URL, req, false);
63-
const hasMissingKeys: boolean = checkResponseFormat(res, JSON.parse(testCases[testName].response));
63+
const isResFormatInvalid: boolean = hasResponseFormatIssues(res, JSON.parse(testCases[testName].response));
6464

6565
if (schema && schema.pattern) {
6666
const check = isResponseValid(schema, res);
6767
expect(check).to.be.true;
6868
}
6969

70-
expect(hasMissingKeys).to.be.false;
70+
expect(isResFormatInvalid).to.be.false;
7171
expect(isErrorStatusExpected).to.be.false;
7272
} catch (e: any) {
7373
expect(isErrorStatusExpected).to.be.true;
@@ -318,7 +318,7 @@ describe('@api-conformity', async function () {
318318
});
319319
await new Promise((r) => setTimeout(r, 500));
320320

321-
const hasMissingKeys: boolean = checkResponseFormat(response, JSON.parse(testCases[testName].response));
321+
const hasMissingKeys: boolean = hasResponseFormatIssues(response, JSON.parse(testCases[testName].response));
322322
expect(hasMissingKeys).to.be.false;
323323
});
324324
}

packages/server/tests/acceptance/data/conformity/utils/processors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { expect } from 'chai';
44
import { FileContent } from './interfaces';
55
import { updateRequestParams } from './overwrites';
66
import { sendRequestToRelay } from './utils';
7-
import { checkResponseFormat, findSchema, isResponseValid } from './validations';
7+
import { findSchema, hasResponseFormatIssues, isResponseValid } from './validations';
88

99
/**
1010
* Splits a given input string into distinct segments representing the request, the response, and optional wildcard fields.
@@ -83,7 +83,7 @@ export async function processFileContent(relayUrl: string, directory: string, fi
8383

8484
if (needError) {
8585
console.log('Validating an error response.');
86-
const valid = checkResponseFormat(response, content.response, wildcards);
86+
const valid = hasResponseFormatIssues(response, content.response, wildcards);
8787
expect(valid).to.be.false;
8888
console.log('Error response validation finished.');
8989
} else {
@@ -99,7 +99,7 @@ export async function processFileContent(relayUrl: string, directory: string, fi
9999
}
100100
} else {
101101
console.log('Using response format check (key-by-key comparison).');
102-
const hasMissingKeys = checkResponseFormat(response, JSON.parse(content.response), wildcards);
102+
const hasMissingKeys = hasResponseFormatIssues(response, JSON.parse(content.response), wildcards);
103103
console.log(`Missing keys check result: ${hasMissingKeys}`);
104104
expect(hasMissingKeys).to.be.false;
105105
}

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,47 @@ const execApisOpenRpcData = require('../../../../../../../openrpc_exec_apis.json
1111
const ajv = new Ajv({ strict: false });
1212
addFormats(ajv);
1313

14-
export function checkResponseFormat(
14+
/**
15+
* Validates response format by comparing actual response against expected response structure.
16+
*
17+
* @param actualResponse - The actual response received from the API call
18+
* @param expectedResponse - The expected response structure to validate against (can be object, string, or ErrorResponse)
19+
* @param wildcards - Array of property paths to ignore during validation (default: empty array)
20+
* @returns {boolean} Returns true if the response format has issues (validation failed), false if format is valid
21+
*
22+
* @description
23+
* This function performs comprehensive response validation including:
24+
* - Parsing expected response if provided as string
25+
* - Checking error state consistency between actual and expected responses
26+
* - Missing key detection in response structure
27+
* - Deep value comparison with wildcard support
28+
*
29+
* @example
30+
* ```typescript
31+
* const actualResponse = { result: "0x123", id: 1 };
32+
* const expectedResponse = '{"result": "0x123", "id": 1}';
33+
* const hasIssues = hasResponseFormatIssues(actualResponse, expectedResponse);
34+
* console.log(hasIssues); // false - format is valid
35+
* ```
36+
*
37+
* @example
38+
* ```typescript
39+
* const actualResponse = { result: "0x123" };
40+
* const expectedResponse = { result: "0x123", id: 1 };
41+
* const hasIssues = hasResponseFormatIssues(actualResponse, expectedResponse);
42+
* console.log(hasIssues); // true - missing 'id' key
43+
* ```
44+
*
45+
* @example
46+
* ```typescript
47+
* const actualResponse = { result: "0x123", timestamp: "2023-01-01" };
48+
* const expectedResponse = { result: "0x123", timestamp: "2023-01-02" };
49+
* const wildcards = ["timestamp"];
50+
* const hasIssues = hasResponseFormatIssues(actualResponse, expectedResponse, wildcards);
51+
* console.log(hasIssues); // false - timestamp ignored due to wildcard
52+
* ```
53+
*/
54+
export function hasResponseFormatIssues(
1555
actualResponse: Record<string, unknown> | ErrorResponse | JsonRpcResponse,
1656
expectedResponse: Record<string, unknown> | string | ErrorResponse,
1757
wildcards: string[] = [],

0 commit comments

Comments
 (0)