[TS] Fix HTML test runner to properly await and fully check result
[ldk-java] / ts / test / index.html
index 42f6815874d0693af62a6795a8941cee046b05c2..3fc55a6d2ef22697d83b9725343cd8de0942ce91 100644 (file)
@@ -1,60 +1,25 @@
 <!DOCTYPE html>
-<!-- add.html -->
 <html>
 <head></head>
 <body>
+<h1><div id="results">Running LDK-TS Tests...</div></h1>
+<script type="text/javascript">
+       var test_runner;
+</script>
 <script type="module">
-       (async() => {
-               const imports = {};
-               imports.wasi_snapshot_preview1 = {
-                       "fd_write" : () => {
-                               console.log("ABORT");
-                       },
-                       "random_get" : () => {
-                               console.log("ABORT");
-                       },
-                       "environ_sizes_get" : () => {
-                               console.log("wasi_snapshot_preview1:environ_sizes_get");
-                       },
-                       "proc_exit" : () => {
-                               console.log("wasi_snapshot_preview1:proc_exit");
-                       },
-                       "environ_get" : () => {
-                               console.log("wasi_snapshot_preview1:environ_get");
-                       },
-               };
-               imports.env = {};
-
-               const memory = new WebAssembly.Memory({initial: 256});
-               imports.env.memoryBase = 0;
-               imports.env.memory = memory;
-               imports.env.tableBase = 0;
-               imports.env.table = new WebAssembly.Table({ initial: 4, element: 'anyfunc' });
-
-               imports.env["abort"] = function() {
-                       console.error("ABORT");
-               };
-               imports.env["js_free"] = function(argument) {
-                       console.log('integer passed from wasm:', argument);
-               };
-               imports.env["js_invoke_function"] = function(fn, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10) {
-                       console.log('integer passed from wasm:', argument);
-               };
-
-               const stream = fetch('../../liblightningjs.wasm');
-               const { instance: wasmInstance } = await WebAssembly.instantiateStreaming(stream, imports);
-               const wasm = wasmInstance.exports;
-
-               const result = wasm.TS_CResult_boolLightningErrorZ_ok(true);
-               console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
-               console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
-               wasm.TS_CResult_boolLightningErrorZ_free(result);
-               console.assert(wasm.TS_CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does
-               console.assert(wasm.TS_LDKCResult_boolLightningErrorZ_result_ok(result));
-               console.assert(!wasm.TS_LDKCResult_boolLightningErrorZ_get_ok(result));
-               wasm.TS_CResult_boolLightningErrorZ_free(result);
-               console.log("pass");
-       })();
+       import { run_tests } from './tests.mjs';
+       test_runner = run_tests;
+       try {
+               const result = await run_tests('../liblightningjs.wasm');
+               if (result === true) {
+                       document.getElementById("results").innerHTML = "All Tests Passed (note free-time errors may still occurr)!";
+               } else {
+                       document.getElementById("results").innerHTML = "Some Tests Failed!";
+               }
+       } catch (e) {
+               console.error(e);
+               document.getElementById("results").innerHTML = "Test failed with exception: " + e;
+       }
 </script>
 </body>
 </html>