Skip to content

Commit 74d6748

Browse files
committed
test: simple tests framework
1 parent a06df0f commit 74d6748

File tree

9 files changed

+155
-31
lines changed

9 files changed

+155
-31
lines changed

packages/js-evo-sdk/.mocharc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require: tests/bootstrap.cjs
1+
require:
2+
- tests/bootstrap.cjs
23
recursive: true
34
timeout: 8000
4-

packages/js-evo-sdk/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
"scripts": {
2424
"build": "rm -rf dist && tsc -p tsconfig.json && webpack --config webpack.config.cjs",
2525
"lint": "eslint .",
26-
"test": "yarn build && yarn run test:unit && yarn run test:functional",
27-
"test:unit": "mocha tests/unit/**/*.spec.mjs && karma start ./tests/karma/karma.conf.cjs --single-run",
28-
"test:functional": "mocha tests/functional/**/*.spec.mjs && karma start ./tests/karma/karma.functional.conf.cjs --single-run"
26+
"test": "yarn run test:unit",
27+
"test:unit": "mocha tests/unit/**/*.spec.mjs && karma start ./tests/karma/karma.conf.cjs --single-run"
2928
},
3029
"devDependencies": {
3130
"assert": "^2.0.0",
@@ -44,6 +43,8 @@
4443
"mocha": "^11.1.0",
4544
"path-browserify": "^1.0.1",
4645
"process": "^0.11.10",
46+
"sinon": "^17.0.1",
47+
"sinon-chai": "^3.7.0",
4748
"string_decoder": "^1.3.0",
4849
"terser-webpack-plugin": "^5.3.10",
4950
"ts-loader": "^9.5.0",

packages/js-evo-sdk/src/tokens/facade.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,17 @@ export class TokensFacade {
3838
return wasm.get_identities_token_balances_with_proof_info(this.sdk.wasm, identityIds, tokenId);
3939
}
4040

41-
identityTokenInfos(identityId: string, tokenIds?: string[] | null, opts: { limit?: number; offset?: number } = {}): Promise<any> {
41+
identityTokenInfos(identityId: string, tokenIds: string[], opts: { limit?: number; offset?: number } = {}): Promise<any> {
4242
const { limit, offset } = opts;
43-
return wasm.get_identity_token_infos(this.sdk.wasm, identityId, tokenIds ?? null, limit ?? null, offset ?? null);
43+
return wasm.get_identity_token_infos(this.sdk.wasm, identityId, tokenIds);
4444
}
4545

4646
identitiesTokenInfos(identityIds: string[], tokenId: string): Promise<any> {
4747
return wasm.get_identities_token_infos(this.sdk.wasm, identityIds, tokenId);
4848
}
4949

50-
identityTokenInfosWithProof(identityId: string, tokenIds?: string[] | null, opts: { limit?: number; offset?: number } = {}): Promise<any> {
51-
const { limit, offset } = opts;
52-
return wasm.get_identity_token_infos_with_proof_info(this.sdk.wasm, identityId, tokenIds ?? null, limit ?? null, offset ?? null);
50+
identityTokenInfosWithProof(identityId: string, tokenIds: string[]): Promise<any> {
51+
return wasm.get_identity_token_infos_with_proof_info(this.sdk.wasm, identityId, tokenIds);
5352
}
5453

5554
identitiesTokenInfosWithProof(identityIds: string[], tokenId: string): Promise<any> {

packages/js-evo-sdk/tests/bootstrap.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ chai.use(dirtyChai);
88
const { expect } = chai;
99
const g = (typeof globalThis !== 'undefined') ? globalThis : global;
1010
g.expect = expect;
11-

packages/js-evo-sdk/tests/karma/karma.conf.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ module.exports = (config) => {
1010
],
1111
preprocessors: {
1212
'../bootstrap.cjs': ['webpack'],
13+
'../../../js-dash-sdk/src/test/bootstrap.js': ['webpack'],
1314
'../unit/**/*.spec.mjs': ['webpack'],
1415
},
1516
});
1617
};
17-

packages/js-evo-sdk/tests/karma/karma.functional.conf.cjs

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/js-evo-sdk/tests/karma/options.cjs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2+
const path = require('path');
23
const webpack = require('webpack');
34
const karmaMocha = require('karma-mocha');
45
const karmaMochaReporter = require('karma-mocha-reporter');
@@ -12,6 +13,21 @@ module.exports = {
1213
webpack: {
1314
mode: 'development',
1415
devtool: 'eval',
16+
module: {
17+
rules: [
18+
{
19+
test: /\.ts$/,
20+
use: {
21+
loader: 'ts-loader',
22+
options: {
23+
transpileOnly: true,
24+
compilerOptions: { declaration: false, emitDeclarationOnly: false },
25+
},
26+
},
27+
exclude: /node_modules/,
28+
},
29+
],
30+
},
1531
// No special wasm handling needed (WASM is inlined in dist)
1632
plugins: [
1733
new webpack.ProvidePlugin({
@@ -20,7 +36,12 @@ module.exports = {
2036
}),
2137
],
2238
resolve: {
23-
extensions: ['.mjs', '.js'],
39+
extensions: ['.mjs', '.js', '.ts'],
40+
extensionAlias: { '.js': ['.ts', '.js'] },
41+
alias: {
42+
// Replace real wasm-sdk with a local stub for browser unit tests
43+
'@dashevo/wasm-sdk': path.resolve(__dirname, '../stubs/wasm-sdk-stub.mjs'),
44+
},
2445
fallback: {
2546
fs: false,
2647
path: require.resolve('path-browserify'),
@@ -65,4 +86,3 @@ module.exports = {
6586
},
6687
},
6788
};
68-
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Sinon-based stub for '@dashevo/wasm-sdk' used by unit tests
2+
// Lightweight stub for '@dashevo/wasm-sdk' used by Karma unit tests
3+
// Returns structured records and tracks calls for verification without external libs.
4+
5+
const calls = [];
6+
const record = (name, args) => ({ __stub: true, called: name, args: Array.from(args) });
7+
8+
export default async function initWasmSdk() { return undefined; }
9+
10+
export function __getCalls() { return calls.slice(); }
11+
export function __clearCalls() { calls.length = 0; }
12+
13+
// Minimal builder used by EvoSDK.connect()
14+
export class WasmSdkBuilder {
15+
static new_testnet() { return new WasmSdkBuilder('testnet', false); }
16+
static new_mainnet() { return new WasmSdkBuilder('mainnet', false); }
17+
static new_testnet_trusted() { return new WasmSdkBuilder('testnet', true); }
18+
static new_mainnet_trusted() { return new WasmSdkBuilder('mainnet', true); }
19+
constructor(network, trusted) { this.network = network; this.trusted = trusted; }
20+
with_version() { return this; }
21+
with_proofs() { return this; }
22+
with_settings() { return this; }
23+
build() { return new WasmSdk(); }
24+
}
25+
26+
export class WasmSdk {}
27+
28+
// Documents
29+
export function get_documents(...args) { const r = record('get_documents', args); calls.push(r); return Promise.resolve(r); }
30+
export function get_documents_with_proof_info(...args) { const r = record('get_documents_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
31+
export function get_document(...args) { const r = record('get_document', args); calls.push(r); return Promise.resolve(r); }
32+
export function get_document_with_proof_info(...args) { const r = record('get_document_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
33+
34+
// Identities
35+
export function identity_fetch(...args) { const r = record('identity_fetch', args); calls.push(r); return Promise.resolve(r); }
36+
export function identity_fetch_with_proof_info(...args) { const r = record('identity_fetch_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
37+
export function identity_fetch_unproved(...args) { const r = record('identity_fetch_unproved', args); calls.push(r); return Promise.resolve(r); }
38+
export function get_identity_keys(...args) { const r = record('get_identity_keys', args); calls.push(r); return Promise.resolve(r); }
39+
40+
// Contracts
41+
export function data_contract_fetch(...args) { const r = record('data_contract_fetch', args); calls.push(r); return Promise.resolve(r); }
42+
export function data_contract_fetch_with_proof_info(...args) { const r = record('data_contract_fetch_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
43+
export function get_data_contract_history(...args) { const r = record('get_data_contract_history', args); calls.push(r); return Promise.resolve(r); }
44+
export function get_data_contract_history_with_proof_info(...args) { const r = record('get_data_contract_history_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
45+
export function get_data_contracts(...args) { const r = record('get_data_contracts', args); calls.push(r); return Promise.resolve(r); }
46+
export function get_data_contracts_with_proof_info(...args) { const r = record('get_data_contracts_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
47+
48+
// Tokens (queries)
49+
export function get_token_price_by_contract(...args) { const r = record('get_token_price_by_contract', args); calls.push(r); return Promise.resolve(r); }
50+
export function get_token_total_supply(...args) { const r = record('get_token_total_supply', args); calls.push(r); return Promise.resolve(r); }
51+
export function get_token_total_supply_with_proof_info(...args) { const r = record('get_token_total_supply_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
52+
export function get_token_statuses(...args) { const r = record('get_token_statuses', args); calls.push(r); return Promise.resolve(r); }
53+
export function get_token_statuses_with_proof_info(...args) { const r = record('get_token_statuses_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
54+
export function get_identities_token_balances(...args) { const r = record('get_identities_token_balances', args); calls.push(r); return Promise.resolve(r); }
55+
export function get_identities_token_balances_with_proof_info(...args) { const r = record('get_identities_token_balances_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
56+
export function get_identity_token_infos(...args) { const r = record('get_identity_token_infos', args); calls.push(r); return Promise.resolve(r); }
57+
export function get_identities_token_infos(...args) { const r = record('get_identities_token_infos', args); calls.push(r); return Promise.resolve(r); }
58+
export function get_identity_token_infos_with_proof_info(...args) { const r = record('get_identity_token_infos_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
59+
export function get_identities_token_infos_with_proof_info(...args) { const r = record('get_identities_token_infos_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
60+
export function get_token_direct_purchase_prices(...args) { const r = record('get_token_direct_purchase_prices', args); calls.push(r); return Promise.resolve(r); }
61+
export function get_token_direct_purchase_prices_with_proof_info(...args) { const r = record('get_token_direct_purchase_prices_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
62+
export function get_token_contract_info(...args) { const r = record('get_token_contract_info', args); calls.push(r); return Promise.resolve(r); }
63+
export function get_token_contract_info_with_proof_info(...args) { const r = record('get_token_contract_info_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
64+
export function get_token_perpetual_distribution_last_claim(...args) { const r = record('get_token_perpetual_distribution_last_claim', args); calls.push(r); return Promise.resolve(r); }
65+
export function get_token_perpetual_distribution_last_claim_with_proof_info(...args) { const r = record('get_token_perpetual_distribution_last_claim_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
66+
67+
// DPNS helpers (pure)
68+
export function dpns_convert_to_homograph_safe(input) { return String(input).toLowerCase(); }
69+
export function dpns_is_valid_username(label) { return /^[a-z0-9-_]{3,63}$/.test(String(label)); }
70+
export function dpns_is_contested_username(label) { return false; }
71+
export function dpns_is_name_available(...args) { const r = record('dpns_is_name_available', args); calls.push(r); return Promise.resolve(true); }
72+
export function dpns_resolve_name(...args) { const r = record('dpns_resolve_name', args); calls.push(r); return Promise.resolve(r); }
73+
export function dpns_register_name(...args) { const r = record('dpns_register_name', args); calls.push(r); return Promise.resolve(r); }
74+
export function get_dpns_usernames(...args) { const r = record('get_dpns_usernames', args); calls.push(r); return Promise.resolve(r); }
75+
export function get_dpns_username(...args) { const r = record('get_dpns_username', args); calls.push(r); return Promise.resolve(r); }
76+
export function get_dpns_usernames_with_proof_info(...args) { const r = record('get_dpns_usernames_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
77+
export function get_dpns_username_with_proof_info(...args) { const r = record('get_dpns_username_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
78+
export function get_dpns_username_by_name(...args) { const r = record('get_dpns_username_by_name', args); calls.push(r); return Promise.resolve(r); }
79+
export function get_dpns_username_by_name_with_proof_info(...args) { const r = record('get_dpns_username_by_name_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
80+
81+
// Epoch
82+
export function get_epochs_info(...args) { const r = record('get_epochs_info', args); calls.push(r); return Promise.resolve(r); }
83+
export function get_epochs_info_with_proof_info(...args) { const r = record('get_epochs_info_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
84+
export function get_finalized_epoch_infos(...args) { const r = record('get_finalized_epoch_infos', args); calls.push(r); return Promise.resolve(r); }
85+
export function get_finalized_epoch_infos_with_proof_info(...args) { const r = record('get_finalized_epoch_infos_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
86+
export function get_current_epoch(...args) { const r = record('get_current_epoch', args); calls.push(r); return Promise.resolve(r); }
87+
export function get_current_epoch_with_proof_info(...args) { const r = record('get_current_epoch_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
88+
export function get_evonodes_proposed_epoch_blocks_by_ids(...args) { const r = record('get_evonodes_proposed_epoch_blocks_by_ids', args); calls.push(r); return Promise.resolve(r); }
89+
export function get_evonodes_proposed_epoch_blocks_by_ids_with_proof_info(...args) { const r = record('get_evonodes_proposed_epoch_blocks_by_ids_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
90+
export function get_evonodes_proposed_epoch_blocks_by_range(...args) { const r = record('get_evonodes_proposed_epoch_blocks_by_range', args); calls.push(r); return Promise.resolve(r); }
91+
export function get_evonodes_proposed_epoch_blocks_by_range_with_proof_info(...args) { const r = record('get_evonodes_proposed_epoch_blocks_by_range_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
92+
93+
// Protocol
94+
export function get_protocol_version_upgrade_state(...args) { const r = record('get_protocol_version_upgrade_state', args); calls.push(r); return Promise.resolve(r); }
95+
export function get_protocol_version_upgrade_state_with_proof_info(...args) { const r = record('get_protocol_version_upgrade_state_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
96+
export function get_protocol_version_upgrade_vote_status(...args) { const r = record('get_protocol_version_upgrade_vote_status', args); calls.push(r); return Promise.resolve(r); }
97+
export function get_protocol_version_upgrade_vote_status_with_proof_info(...args) { const r = record('get_protocol_version_upgrade_vote_status_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
98+
99+
// System
100+
export function get_status(...args) { const r = record('get_status', args); calls.push(r); return Promise.resolve(r); }
101+
export function get_current_quorums_info(...args) { const r = record('get_current_quorums_info', args); calls.push(r); return Promise.resolve(r); }
102+
export function get_total_credits_in_platform(...args) { const r = record('get_total_credits_in_platform', args); calls.push(r); return Promise.resolve(r); }
103+
export function get_total_credits_in_platform_with_proof_info(...args) { const r = record('get_total_credits_in_platform_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
104+
export function get_prefunded_specialized_balance(...args) { const r = record('get_prefunded_specialized_balance', args); calls.push(r); return Promise.resolve(r); }
105+
export function get_prefunded_specialized_balance_with_proof_info(...args) { const r = record('get_prefunded_specialized_balance_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
106+
export function wait_for_state_transition_result(...args) { const r = record('wait_for_state_transition_result', args); calls.push(r); return Promise.resolve(r); }
107+
export function get_path_elements(...args) { const r = record('get_path_elements', args); calls.push(r); return Promise.resolve(r); }
108+
export function get_path_elements_with_proof_info(...args) { const r = record('get_path_elements_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
109+
110+
// Group
111+
export function get_contested_resources(...args) { const r = record('get_contested_resources', args); calls.push(r); return Promise.resolve(r); }
112+
export function get_contested_resources_with_proof_info(...args) { const r = record('get_contested_resources_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
113+
export function get_contested_resource_voters_for_identity(...args) { const r = record('get_contested_resource_voters_for_identity', args); calls.push(r); return Promise.resolve(r); }
114+
export function get_contested_resource_voters_for_identity_with_proof_info(...args) { const r = record('get_contested_resource_voters_for_identity_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
115+
116+
// Voting (queries)
117+
export function get_contested_resource_vote_state(...args) { const r = record('get_contested_resource_vote_state', args); calls.push(r); return Promise.resolve(r); }
118+
export function get_contested_resource_vote_state_with_proof_info(...args) { const r = record('get_contested_resource_vote_state_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
119+
export function get_contested_resource_identity_votes(...args) { const r = record('get_contested_resource_identity_votes', args); calls.push(r); return Promise.resolve(r); }
120+
export function get_contested_resource_identity_votes_with_proof_info(...args) { const r = record('get_contested_resource_identity_votes_with_proof_info', args); calls.push(r); return Promise.resolve(r); }
121+
export function get_vote_polls_by_end_date(...args) { const r = record('get_vote_polls_by_end_date', args); calls.push(r); return Promise.resolve(r); }
122+
export function get_vote_polls_by_end_date_with_proof_info(...args) { const r = record('get_vote_polls_by_end_date_with_proof_info', args); calls.push(r); return Promise.resolve(r); }

0 commit comments

Comments
 (0)