Skip to content
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

Refactor test's response schema validation #1151

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
20 changes: 19 additions & 1 deletion __tests__/config/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import ajvKeywords from 'ajv-keywords';
import { matchersWithOptions } from 'jest-json-schema';

import starknet_api_openrpc from 'starknet_specs/api/starknet_api_openrpc.json';
import starknet_metadata from 'starknet_specs/api/starknet_metadata.json';
import starknet_trace_api_openrpc from 'starknet_specs/api/starknet_trace_api_openrpc.json';
import starknet_write_api from 'starknet_specs/api/starknet_write_api.json';

import accountSchemas from '../schemas/account.json';
import componentSchemas from '../schemas/component.json';
import libSchemas from '../schemas/lib.json';
Expand All @@ -9,7 +14,16 @@ import rpcSchemas from '../schemas/rpc.json';
import { isBigInt } from '../../src/utils/num';

const matcherSchemas = [accountSchemas, libSchemas, providerSchemas, rpcSchemas];
const schemas = [...matcherSchemas, componentSchemas];
const starknetSchemas = [
{ $id: 'starknet_api_openrpc', ...starknet_api_openrpc },
{ $id: 'starknet_metadata', ...starknet_metadata },
{ $id: 'starknet_trace_api_openrpc', ...starknet_trace_api_openrpc },
{ $id: 'starknet_write_api', ...starknet_write_api },
// schema aliases to rectify faulty references from the spec
{ $id: 'starknet_api_openrpc.json', ...starknet_api_openrpc },
{ $id: 'api/starknet_api_openrpc.json', ...starknet_api_openrpc },
];
const schemas = [...matcherSchemas, ...starknetSchemas, componentSchemas];
const jestJsonMatchers = matchersWithOptions({ schemas }, (ajv: any) => {
// @ts-ignore
ajv.addKeyword({
Expand All @@ -35,6 +49,10 @@ export const initializeMatcher = (expect: jest.Expect) => {
});
expect(accountSchemas).toBeValidSchema();
expect(componentSchemas).toBeValidSchema();
expect(starknet_api_openrpc).toBeValidSchema();
expect(starknet_metadata).toBeValidSchema();
expect(starknet_trace_api_openrpc).toBeValidSchema();
expect(starknet_write_api).toBeValidSchema();
expect(libSchemas).toBeValidSchema();
expect(providerSchemas).toBeValidSchema();
expect(rpcSchemas).toBeValidSchema();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/schemas/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
"properties": {
"transaction_trace": {
"title": "the transaction's trace",
"$ref": "sequencerSchemas#/definitions/TransactionTraceResponse"
"$ref": "starknet_trace_api_openrpc#/components/schemas/TRANSACTION_TRACE"
},
"fee_estimation": {
"title": "the transaction's resources and fee",
Expand Down
2 changes: 1 addition & 1 deletion __tests__/schemas/lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"type": "object"
},
"abi": {
"type": "array"
"$ref": "starknet_metadata#/components/schemas/ABI"
}
},
"required": ["sierra_program", "contract_class_version", "entry_points_by_type", "abi"]
Expand Down
6 changes: 3 additions & 3 deletions __tests__/schemas/provider.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@
"required": ["transaction_hash"]
},
"getTransactionTrace": {
"$ref": "componentSchemas#/components/schemas/TRANSACTION_TRACE"
"$ref": "starknet_trace_api_openrpc#/components/schemas/TRANSACTION_TRACE"
},
"BlockWithTxReceipts": {
"oneOf": [
{
"$ref": "componentSchemas#/components/schemas/BLOCK_WITH_RECEIPTS"
"$ref": "starknet_api_openrpc#/components/schemas/BLOCK_WITH_RECEIPTS"
},
{
"$ref": "componentSchemas#/components/schemas/PENDING_BLOCK_WITH_RECEIPTS"
"$ref": "starknet_api_openrpc#/components/schemas/PENDING_BLOCK_WITH_RECEIPTS"
}
]
}
Expand Down
66 changes: 2 additions & 64 deletions __tests__/schemas/rpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,10 @@
"$id": "rpcSchemas",
"definitions": {
"GetSyncingStatsResponse": {
"oneOf": [
{
"type": "object",
"properties": {
"current_block_hash": {
"type": "string"
},
"current_block_num": {
"type": "number"
},
"highest_block_hash": {
"type": "string"
},
"highest_block_num": {
"type": "number"
},
"starting_block_hash": {
"type": "string"
},
"starting_block_num": {
"type": "number"
}
},
"required": [
"current_block_hash",
"current_block_num",
"highest_block_hash",
"highest_block_num",
"starting_block_hash",
"starting_block_num"
]
},
{
"type": "boolean"
}
]
"$ref": "starknet_api_openrpc#/components/schemas/SYNC_STATUS"
},
"StarknetEmittedEvent": {
"type": "object",
"properties": {
"from_address": {
"type": "string"
},
"keys": {
"type": "array",
"items": {
"type": "string"
}
},
"data": {
"type": "array",
"items": {
"type": "string"
}
},
"block_hash": {
"type": "string"
},
"block_number": {
"type": "number"
},
"transaction_hash": {
"type": "string"
}
},
"required": ["from_address", "keys", "data", "block_hash", "block_number", "transaction_hash"]
"$ref": "starknet_api_openrpc#/components/schemas/EMITTED_EVENT"
}
}
}
163 changes: 163 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"prettier": "^3.2.5",
"prettier-plugin-import-sort": "^0.0.7",
"semantic-release": "^23.0.5",
"starknet_specs": "github:starkware-libs/starknet-specs#v0.7.1",
"tsup": "^8.0.2",
"type-coverage": "^2.28.2",
"typedoc": "^0.25.7",
Expand Down