-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: enhance
index.js
after review
- Loading branch information
Showing
1 changed file
with
92 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,159 +1,127 @@ | ||
import initMithrilClient, { | ||
MithrilClient, | ||
} from "@mithril-dev/mithril-client-wasm" | ||
} from "@mithril-dev/mithril-client-wasm"; | ||
|
||
function display_test_result_in_dom(step_number, step_name, result, error) { | ||
let div = document.createElement("div") | ||
div.id = step_name | ||
div.title = result | ||
div.innerHTML = `Result test n°${step_number}: ${result}; function_name: ${step_name}${ | ||
async function run_test(test_name, test_number, fun) { | ||
try { | ||
const result = await fun(); | ||
display_test_result_in_dom(test_name, test_number, "OK"); | ||
return result; | ||
} catch (error) { | ||
handle_error(test_name, test_number, error); | ||
} | ||
} | ||
|
||
function display_test_result_in_dom(test_name, test_number, result, error) { | ||
let div = document.createElement("div"); | ||
div.id = test_name; | ||
div.title = result; | ||
div.innerHTML = `Result test n°${test_number}: ${result}; function_name: ${test_name}${ | ||
error ? `; reason: ${error}` : "" | ||
}` | ||
document.body.appendChild(div) | ||
}`; | ||
document.body.appendChild(div); | ||
} | ||
|
||
function handle_error(step_number, step_name, error) { | ||
display_test_result_in_dom(step_number, step_name, "FAILED", error) | ||
console.error(`Error at step ${step_number} (${step_name}):`, error) | ||
add_finished_div() | ||
function handle_error(test_name, test_number, error) { | ||
display_test_result_in_dom(test_name, test_number, "FAILED", error); | ||
console.error(`Error at step ${test_number} (${test_name}):`, error); | ||
add_finished_div(); | ||
throw new Error( | ||
`Stopping script due to error at step ${step_number}: ${error}` | ||
) | ||
`Stopping script due to error at step ${test_number}: ${error}` | ||
); | ||
} | ||
|
||
function add_finished_div() { | ||
let div = document.createElement("div") | ||
div.id = "tests_finished" | ||
document.body.appendChild(div) | ||
let div = document.createElement("div"); | ||
div.id = "tests_finished"; | ||
document.body.appendChild(div); | ||
} | ||
|
||
await initMithrilClient() | ||
const aggregator_endpoint = process.env.AGGREGATOR_ENDPOINT | ||
const genesis_verification_key = process.env.GENESIS_VERIFICATION_KEY | ||
let client | ||
let test_name | ||
let test_number = 1 | ||
await initMithrilClient(); | ||
const aggregator_endpoint = process.env.AGGREGATOR_ENDPOINT; | ||
const genesis_verification_key = process.env.GENESIS_VERIFICATION_KEY; | ||
let client; | ||
let test_number = 1; | ||
|
||
// Test 'MithrilClient constructor' | ||
try { | ||
test_name = "constructor" | ||
client = new MithrilClient(aggregator_endpoint, genesis_verification_key) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
await run_test("constructor", test_number, async () => { | ||
client = new MithrilClient(aggregator_endpoint, genesis_verification_key); | ||
}); | ||
|
||
// Test 'list_snapshots' function | ||
let snapshots | ||
try { | ||
test_number++ | ||
test_name = "list_snapshots" | ||
snapshots = await client.list_snapshots() | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log("snapshots", snapshots) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
let snapshots; | ||
test_number++; | ||
await run_test("list_snapshots", test_number, async () => { | ||
snapshots = await client.list_snapshots(); | ||
console.log("snapshots", snapshots); | ||
}); | ||
|
||
// Test 'get_snapshot' function | ||
try { | ||
test_number++ | ||
test_name = "get_snapshot" | ||
const snapshot = await client.get_snapshot(snapshots[0].digest) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log("snapshot", snapshot) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
test_number++; | ||
await run_test("get_snapshot", test_number, async () => { | ||
const snapshot = await client.get_snapshot(snapshots[0].digest); | ||
console.log("snapshot", snapshot); | ||
}); | ||
|
||
// Test 'list_mithril_stake_distributions' function | ||
let mithril_stake_distributions | ||
try { | ||
test_number++ | ||
test_name = "list_mithril_stake_distributions" | ||
mithril_stake_distributions = await client.list_mithril_stake_distributions() | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log("mithril_stake_distributions", mithril_stake_distributions) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
let mithril_stake_distributions; | ||
test_number++; | ||
await run_test("list_mithril_stake_distributions", test_number, async () => { | ||
mithril_stake_distributions = await client.list_mithril_stake_distributions(); | ||
console.log("mithril_stake_distributions", mithril_stake_distributions); | ||
}); | ||
|
||
// Test 'get_mithril_stake_distribution' function | ||
let mithril_stake_distribution | ||
try { | ||
test_number++ | ||
test_name = "get_mithril_stake_distribution" | ||
let mithril_stake_distribution; | ||
test_number++; | ||
await run_test("get_mithril_stake_distribution", test_number, async () => { | ||
mithril_stake_distribution = await client.get_mithril_stake_distribution( | ||
mithril_stake_distributions[0].hash | ||
) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log("mithril_stake_distribution", mithril_stake_distribution) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
); | ||
console.log("mithril_stake_distribution", mithril_stake_distribution); | ||
}); | ||
|
||
// Test 'get_mithril_certificate' function | ||
let certificate | ||
try { | ||
test_number++ | ||
test_name = "get_mithril_certificate" | ||
let certificate; | ||
test_number++; | ||
await run_test("get_mithril_certificate", test_number, async () => { | ||
certificate = await client.get_mithril_certificate( | ||
mithril_stake_distribution.certificate_hash | ||
) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log("certificate", certificate) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
); | ||
console.log("certificate", certificate); | ||
}); | ||
|
||
// Test 'verify_certificate_chain' function | ||
let last_certificate_from_chain | ||
try { | ||
test_number++ | ||
test_name = "verify_certificate_chain" | ||
let last_certificate_from_chain; | ||
test_number++; | ||
await run_test("verify_certificate_chain", test_number, async () => { | ||
last_certificate_from_chain = await client.verify_certificate_chain( | ||
certificate.hash | ||
) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log("last_certificate_from_chain", last_certificate_from_chain) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
); | ||
console.log("last_certificate_from_chain", last_certificate_from_chain); | ||
}); | ||
|
||
// Test 'compute_mithril_stake_distribution_message' function | ||
let mithril_stake_distribution_message | ||
try { | ||
test_number++ | ||
test_name = "compute_mithril_stake_distribution_message" | ||
mithril_stake_distribution_message = | ||
await client.compute_mithril_stake_distribution_message( | ||
mithril_stake_distribution | ||
) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
console.log( | ||
"mithril_stake_distribution_message", | ||
mithril_stake_distribution_message | ||
) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
let mithril_stake_distribution_message; | ||
test_number++; | ||
await run_test( | ||
"compute_mithril_stake_distribution_message", | ||
test_number, | ||
async () => { | ||
mithril_stake_distribution_message = | ||
await client.compute_mithril_stake_distribution_message( | ||
mithril_stake_distribution | ||
); | ||
console.log( | ||
"mithril_stake_distribution_message", | ||
mithril_stake_distribution_message | ||
); | ||
} | ||
); | ||
|
||
// Test 'verify_message_match_certificate' function | ||
try { | ||
test_number++ | ||
test_name = "verify_message_match_certificate" | ||
test_number++; | ||
await run_test("verify_message_match_certificate", test_number, async () => { | ||
const valid_stake_distribution_message = | ||
await client.verify_message_match_certificate( | ||
mithril_stake_distribution_message, | ||
last_certificate_from_chain | ||
) | ||
display_test_result_in_dom(test_number, test_name, "OK") | ||
); | ||
console.log( | ||
"valid_stake_distribution_message", | ||
valid_stake_distribution_message | ||
) | ||
} catch (error) { | ||
handle_error(test_number, test_name, error) | ||
} | ||
); | ||
}); | ||
|
||
add_finished_div() | ||
add_finished_div(); |