Skip to content

Commit 7b5efec

Browse files
committed
Update tests to test human structs from TypeScript
1 parent 2fbce30 commit 7b5efec

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

ts/test/tests.mjs

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

ts/test/tests.mts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import * as rawldk from "../bindings.mjs";
2+
import * as ldk from "../index.mjs";
3+
4+
const tests = [];
5+
6+
tests.push(async () => {
7+
const result = rawldk.CResult_boolLightningErrorZ_ok(true);
8+
console.assert(rawldk.CResult_boolLightningErrorZ_is_ok(result));
9+
console.assert(rawldk.CResult_boolLightningErrorZ_get_ok(result));
10+
rawldk.CResult_boolLightningErrorZ_free(result);
11+
console.assert(rawldk.CResult_boolLightningErrorZ_ok(false) == result); // malloc doesn't need to guarantee this, but currently does
12+
console.assert(rawldk.CResult_boolLightningErrorZ_is_ok(result));
13+
console.assert(!rawldk.CResult_boolLightningErrorZ_get_ok(result));
14+
rawldk.CResult_boolLightningErrorZ_free(result);
15+
16+
/*var pk_arr = [];
17+
for (var i = 0; i < 33; i++) { pk_arr[i] = 42; }
18+
const pk_bytes = encodeUint8Array(pk_arr);
19+
const pk_res = wasm.TS_CResult_PublicKeyErrorZ_ok(pk_bytes);
20+
console.assert(wasm.TS_CResult_PublicKeyErrorZ_is_ok(pk_res));
21+
const pk_res_bytes = wasm.TS_LDKCResult_PublicKeyErrorZ_get_ok(pk_res);
22+
wasm.TS_CResult_PublicKeyErrorZ_free(pk_res);*/
23+
return true;
24+
});
25+
26+
tests.push(async () => {
27+
const ping = ldk.Ping.constructor_new(10, 2);
28+
const new_ping = ldk.Ping.constructor_read(ping.write());
29+
if (!(new_ping instanceof ldk.Result_PingDecodeErrorZ_OK)) return false;
30+
if (!new_ping.is_ok()) return false;
31+
if (new_ping.res.get_byteslen() != 2) return false;
32+
if (new_ping.res.get_ponglen() != 10) return false;
33+
return true;
34+
});
35+
36+
tests.push(async () => {
37+
const outpoint = ldk.OutPoint.constructor_new(new Uint8Array(32), 4);
38+
const read_outpoint = ldk.OutPoint.constructor_read(outpoint.write());
39+
if (!(read_outpoint instanceof ldk.Result_OutPointDecodeErrorZ_OK)) return false;
40+
if (!read_outpoint.res.eq(outpoint)) return false;
41+
if (read_outpoint.res.hash() != outpoint.hash()) return false;
42+
const chan_id = read_outpoint.res.to_channel_id();
43+
if (chan_id.length != 32) return false;
44+
if (chan_id[31] != 4) return false;
45+
return true;
46+
});
47+
48+
export async function run_tests(wasm_path) {
49+
await rawldk.initializeWasm(wasm_path);
50+
51+
var test_runs = [];
52+
for (const test of tests) {
53+
test_runs.push(test());
54+
}
55+
const results = await Promise.all(test_runs);
56+
console.log("test results: " + results);
57+
const result = results.every((v) => { return v === true });
58+
console.log("all tests passed: " + result);
59+
return result;
60+
}

0 commit comments

Comments
 (0)