From: Matt Corallo Date: Fri, 7 Jan 2022 02:12:18 +0000 (+0000) Subject: Update tests to test human structs from TypeScript X-Git-Tag: v0.0.104.1~1^2~2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=7b5efec6dde25a47f7a03aaa11099a13ff1a42a3;p=ldk-java Update tests to test human structs from TypeScript --- diff --git a/ts/test/tests.mjs b/ts/test/tests.mjs deleted file mode 100644 index 96e50ec6..00000000 --- a/ts/test/tests.mjs +++ /dev/null @@ -1,24 +0,0 @@ -import * as ldk from "../bindings.mjs"; - -export async function run_tests(wasm_path) { - await ldk.initializeWasm(wasm_path); - const result = ldk.CResult_boolLightningErrorZ_ok(true); - console.assert(ldk.CResult_boolLightningErrorZ_is_ok(result)); - console.assert(ldk.CResult_boolLightningErrorZ_get_ok(result)); - ldk.CResult_boolLightningErrorZ_free(result); - console.assert(ldk.CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does - console.assert(ldk.CResult_boolLightningErrorZ_is_ok(result)); - console.assert(!ldk.CResult_boolLightningErrorZ_get_ok(result)); - ldk.CResult_boolLightningErrorZ_free(result); - - /*var pk_arr = []; - for (var i = 0; i < 33; i++) { pk_arr[i] = 42; } - const pk_bytes = encodeUint8Array(pk_arr); - const pk_res = wasm.TS_CResult_PublicKeyErrorZ_ok(pk_bytes); - console.assert(wasm.TS_CResult_PublicKeyErrorZ_is_ok(pk_res)); - const pk_res_bytes = wasm.TS_LDKCResult_PublicKeyErrorZ_get_ok(pk_res); - wasm.TS_CResult_PublicKeyErrorZ_free(pk_res);*/ - - console.log("pass"); - return true; -} diff --git a/ts/test/tests.mts b/ts/test/tests.mts new file mode 100644 index 00000000..85c1fb3c --- /dev/null +++ b/ts/test/tests.mts @@ -0,0 +1,60 @@ +import * as rawldk from "../bindings.mjs"; +import * as ldk from "../index.mjs"; + +const tests = []; + +tests.push(async () => { + const result = rawldk.CResult_boolLightningErrorZ_ok(true); + console.assert(rawldk.CResult_boolLightningErrorZ_is_ok(result)); + console.assert(rawldk.CResult_boolLightningErrorZ_get_ok(result)); + rawldk.CResult_boolLightningErrorZ_free(result); + console.assert(rawldk.CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does + console.assert(rawldk.CResult_boolLightningErrorZ_is_ok(result)); + console.assert(!rawldk.CResult_boolLightningErrorZ_get_ok(result)); + rawldk.CResult_boolLightningErrorZ_free(result); + + /*var pk_arr = []; + for (var i = 0; i < 33; i++) { pk_arr[i] = 42; } + const pk_bytes = encodeUint8Array(pk_arr); + const pk_res = wasm.TS_CResult_PublicKeyErrorZ_ok(pk_bytes); + console.assert(wasm.TS_CResult_PublicKeyErrorZ_is_ok(pk_res)); + const pk_res_bytes = wasm.TS_LDKCResult_PublicKeyErrorZ_get_ok(pk_res); + wasm.TS_CResult_PublicKeyErrorZ_free(pk_res);*/ + return true; +}); + +tests.push(async () => { + const ping = ldk.Ping.constructor_new(10, 2); + const new_ping = ldk.Ping.constructor_read(ping.write()); + if (!(new_ping instanceof ldk.Result_PingDecodeErrorZ_OK)) return false; + if (!new_ping.is_ok()) return false; + if (new_ping.res.get_byteslen() != 2) return false; + if (new_ping.res.get_ponglen() != 10) return false; + return true; +}); + +tests.push(async () => { + const outpoint = ldk.OutPoint.constructor_new(new Uint8Array(32), 4); + const read_outpoint = ldk.OutPoint.constructor_read(outpoint.write()); + if (!(read_outpoint instanceof ldk.Result_OutPointDecodeErrorZ_OK)) return false; + if (!read_outpoint.res.eq(outpoint)) return false; + if (read_outpoint.res.hash() != outpoint.hash()) return false; + const chan_id = read_outpoint.res.to_channel_id(); + if (chan_id.length != 32) return false; + if (chan_id[31] != 4) return false; + return true; +}); + +export async function run_tests(wasm_path) { + await rawldk.initializeWasm(wasm_path); + + var test_runs = []; + for (const test of tests) { + test_runs.push(test()); + } + const results = await Promise.all(test_runs); + console.log("test results: " + results); + const result = results.every((v) => { return v === true }); + console.log("all tests passed: " + result); + return result; +}