]> git.bitcoin.ninja Git - ldk-java/commitdiff
Update tests to test human structs from TypeScript
authorMatt Corallo <git@bluematt.me>
Fri, 7 Jan 2022 02:12:18 +0000 (02:12 +0000)
committerMatt Corallo <git@bluematt.me>
Sat, 8 Jan 2022 04:16:38 +0000 (04:16 +0000)
ts/test/tests.mjs [deleted file]
ts/test/tests.mts [new file with mode: 0644]

diff --git a/ts/test/tests.mjs b/ts/test/tests.mjs
deleted file mode 100644 (file)
index 96e50ec..0000000
+++ /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 (file)
index 0000000..85c1fb3
--- /dev/null
@@ -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;
+}