Skip to content

Commit e03ff88

Browse files
committed
Move tests out of defs.ts
1 parent b43cbfc commit e03ff88

8 files changed

Lines changed: 169 additions & 162 deletions

File tree

wasm-wrappers/js-bindings-test/tests/defs.ts

Lines changed: 1 addition & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,13 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
// Here we have some definitions that are used by other tests, as well as tests
17-
// that check those definitions (if needed).
16+
// Here we have some general definitions that are used by other tests.
1817

1918
import {
20-
Amount,
21-
Network,
2219
make_private_key,
2320
public_key_from_private_key,
24-
make_default_account_privkey,
25-
make_receiving_address,
26-
pubkey_to_pubkeyhash_address,
27-
encode_input_for_utxo,
28-
encode_input_for_withdraw_from_delegation,
29-
encode_lock_until_height,
30-
encode_output_lock_then_transfer,
31-
encode_output_create_stake_pool,
32-
encode_stake_pool_data,
3321
} from "../../pkg/wasm_wrappers.js";
3422

35-
import {
36-
TEXT_ENCODER,
37-
assert_eq_arrays,
38-
get_err_msg,
39-
} from "./utils.js";
40-
4123
// Taken from TESTNET_FORK_HEIGHT_5_ORDERS_V1 in common/src/chain/config/builder.rs.
4224
// This will be updated to the actual height after we choose one.
4325
export const ORDERS_V1_TESTNET_FORK_HEIGHT = 999_999_999;
@@ -71,131 +53,6 @@ export const get_predefined_pub_key = (function () {
7153
}
7254
})();
7355

74-
// Some address.
75-
// It corresponds to `make_receiving_address(make_default_account_privkey(MNEMONIC,Network.Testnet), 0)`,
76-
// but most tests don't care.
77-
export const ADDRESS = "tmt1q9dn5m4svn8sds3fcy09kpxrefnu75xekgr5wa3n";
78-
7956
// Some token id.
8057
export const TOKEN_ID =
8158
"tmltk15tgfrs49rv88v8utcllqh0nvpaqtgvn26vdxhuner5m6ewg9c3msn9fxns";
82-
83-
// Some test inputs - a UTXO and a delegation withdrawal
84-
export const INPUTS = [
85-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86-
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
87-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
88-
];
89-
90-
// OutpointSourceId used in INPUTS.
91-
export const TX_OUTPOINT = new Uint8Array(33).fill(0)
92-
93-
export const OUTPUT_LOCK_THEN_TRANSFER = [
94-
1, 0, 145, 1, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193, 30, 91, 4,
95-
195, 202, 103, 207, 80, 217, 178, 0, 145, 1
96-
];
97-
98-
export const OUTPUT_CREATE_STAKE_POOL = [
99-
3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
101-
113, 2, 0, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193, 30, 91, 4, 195,
102-
202, 103, 207, 80, 217, 178, 0, 108, 245, 234, 97, 170, 9, 247, 158, 169,
103-
100, 84, 123, 235, 183, 147, 29, 136, 118, 203, 24, 146, 56, 60, 217, 2,
104-
198, 32, 133, 255, 240, 84, 123, 1, 91, 58, 110, 176, 100, 207, 6, 194,
105-
41, 193, 30, 91, 4, 195, 202, 103, 207, 80, 217, 178, 100, 0, 0,
106-
];
107-
108-
// Some tx outputs - LockThenTransfer and CreateStakePool
109-
export const OUTPUTS = [...OUTPUT_LOCK_THEN_TRANSFER, ...OUTPUT_CREATE_STAKE_POOL];
110-
111-
export async function test_predefined_address() {
112-
const account_private_key = make_default_account_privkey(
113-
MNEMONIC,
114-
Network.Testnet
115-
);
116-
console.log(`acc private key = ${account_private_key}`);
117-
118-
const receiving_privkey = make_receiving_address(account_private_key, 0);
119-
console.log(`receiving privkey = ${receiving_privkey}`);
120-
121-
const receiving_pubkey = public_key_from_private_key(receiving_privkey);
122-
const address = pubkey_to_pubkeyhash_address(
123-
receiving_pubkey,
124-
Network.Testnet
125-
);
126-
console.log(`address = ${address}`);
127-
if (address != ADDRESS) {
128-
throw new Error("Incorrect address generated");
129-
}
130-
}
131-
132-
export async function test_encode_predefined_inputs() {
133-
const tx_input = encode_input_for_utxo(TX_OUTPOINT, 1);
134-
const deleg_id =
135-
"mdelg1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqut3aj8";
136-
const tx_input2 = encode_input_for_withdraw_from_delegation(
137-
deleg_id,
138-
Amount.from_atoms("1"),
139-
BigInt(1),
140-
Network.Mainnet
141-
);
142-
const inputs = [...tx_input, ...tx_input2];
143-
assert_eq_arrays(inputs, INPUTS);
144-
}
145-
146-
export async function test_encode_predefined_outputs() {
147-
const vrf_public_key =
148-
"tvrfpk1qpk0t6np4gyl084fv328h6ahjvwcsaktrzfrs0xeqtrzpp0l7p28knrnn57";
149-
150-
const pool_data = encode_stake_pool_data(
151-
Amount.from_atoms("40000"),
152-
ADDRESS,
153-
vrf_public_key,
154-
ADDRESS,
155-
100,
156-
Amount.from_atoms("0"),
157-
Network.Testnet
158-
);
159-
const expected_pool_data = [
160-
2, 113, 2, 0, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193, 30, 91, 4,
161-
195, 202, 103, 207, 80, 217, 178, 0, 108, 245, 234, 97, 170, 9, 247, 158,
162-
169, 100, 84, 123, 235, 183, 147, 29, 136, 118, 203, 24, 146, 56, 60, 217,
163-
2, 198, 32, 133, 255, 240, 84, 123, 1, 91, 58, 110, 176, 100, 207, 6, 194,
164-
41, 193, 30, 91, 4, 195, 202, 103, 207, 80, 217, 178, 100, 0, 0,
165-
];
166-
167-
assert_eq_arrays(pool_data, expected_pool_data);
168-
169-
const lock = encode_lock_until_height(BigInt(100));
170-
const output = encode_output_lock_then_transfer(
171-
Amount.from_atoms("100"),
172-
ADDRESS,
173-
lock,
174-
Network.Testnet
175-
);
176-
177-
const pool_id =
178-
"tpool1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqza035u";
179-
try {
180-
const invalid_pool_data = TEXT_ENCODER.encode("invalid pool data");
181-
encode_output_create_stake_pool(
182-
pool_id,
183-
invalid_pool_data,
184-
Network.Testnet
185-
);
186-
throw new Error("Invalid pool data worked somehow!");
187-
} catch (e) {
188-
if (!get_err_msg(e).includes("Invalid stake pool data encoding")) {
189-
throw e;
190-
}
191-
console.log("Tested invalid pool data successfully");
192-
}
193-
const stake_pool_output = encode_output_create_stake_pool(
194-
pool_id,
195-
pool_data,
196-
Network.Testnet
197-
);
198-
const outputs = [...output, ...stake_pool_output];
199-
200-
assert_eq_arrays(outputs, OUTPUTS);
201-
}

wasm-wrappers/js-bindings-test/tests/test_address_generation.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,40 @@ import {
3434

3535
import {
3636
MNEMONIC,
37-
test_predefined_address,
3837
} from "./defs.js";
3938

39+
// Some address.
40+
// It corresponds to `make_receiving_address(make_default_account_privkey(MNEMONIC,Network.Testnet), 0)`,
41+
// but most tests don't care.
42+
export const ADDRESS = "tmt1q9dn5m4svn8sds3fcy09kpxrefnu75xekgr5wa3n";
43+
4044
export async function test_address_generation() {
45+
run_one_test(predefined_address_test);
46+
run_one_test(general_test);
47+
}
48+
49+
export async function predefined_address_test() {
50+
const account_private_key = make_default_account_privkey(
51+
MNEMONIC,
52+
Network.Testnet
53+
);
54+
console.log(`acc private key = ${account_private_key}`);
55+
56+
const receiving_privkey = make_receiving_address(account_private_key, 0);
57+
console.log(`receiving privkey = ${receiving_privkey}`);
58+
59+
const receiving_pubkey = public_key_from_private_key(receiving_privkey);
60+
const address = pubkey_to_pubkeyhash_address(
61+
receiving_pubkey,
62+
Network.Testnet
63+
);
64+
console.log(`address = ${address}`);
65+
if (address != ADDRESS) {
66+
throw new Error("Incorrect address generated");
67+
}
68+
}
69+
70+
export async function general_test() {
4171
const bad_priv_key = TEXT_ENCODER.encode("bad");
4272

4373
try {
@@ -123,6 +153,4 @@ export async function test_address_generation() {
123153
throw new Error("Incorrect address generated");
124154
}
125155
}
126-
127-
run_one_test(test_predefined_address);
128156
}

wasm-wrappers/js-bindings-test/tests/test_encode_other_inputs.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,42 @@ import {
3838
} from "./utils.js";
3939

4040
import {
41-
ADDRESS,
4241
TOKEN_ID,
43-
test_encode_predefined_inputs,
4442
} from "./defs.js";
43+
import {
44+
ADDRESS
45+
} from "./test_address_generation.js";
46+
47+
// Some test inputs - a UTXO and a delegation withdrawal
48+
export const INPUTS = [
49+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50+
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4,
52+
];
53+
54+
// OutpointSourceId used in INPUTS.
55+
export const TX_OUTPOINT = new Uint8Array(33).fill(0)
4556

4657
export async function test_encode_other_inputs() {
58+
run_one_test(predefined_inputs_test);
59+
run_one_test(general_test);
60+
}
61+
62+
async function predefined_inputs_test() {
63+
const tx_input = encode_input_for_utxo(TX_OUTPOINT, 1);
64+
const deleg_id =
65+
"mdelg1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqut3aj8";
66+
const tx_input2 = encode_input_for_withdraw_from_delegation(
67+
deleg_id,
68+
Amount.from_atoms("1"),
69+
BigInt(1),
70+
Network.Mainnet
71+
);
72+
const inputs = [...tx_input, ...tx_input2];
73+
assert_eq_arrays(inputs, INPUTS);
74+
}
75+
76+
export async function general_test() {
4777
try {
4878
encode_input_for_utxo(TEXT_ENCODER.encode("asd"), 1);
4979
throw new Error("Invalid outpoint encoding worked somehow!");
@@ -69,8 +99,6 @@ export async function test_encode_other_inputs() {
6999
console.log("Tested invalid delegation id in account successfully");
70100
}
71101

72-
run_one_test(test_encode_predefined_inputs);
73-
74102
const mint_tokens_input = encode_input_for_mint_tokens(
75103
TOKEN_ID,
76104
Amount.from_atoms("100"),

wasm-wrappers/js-bindings-test/tests/test_encode_other_outputs.ts

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
encode_output_lock_then_transfer,
2828
encode_output_token_lock_then_transfer,
2929
encode_stake_pool_data,
30+
encode_output_create_stake_pool,
3031
Amount,
3132
TotalSupply,
3233
FreezableToken,
@@ -43,12 +44,91 @@ import {
4344

4445
import {
4546
MNEMONIC,
46-
ADDRESS,
4747
TOKEN_ID,
48-
test_encode_predefined_outputs,
4948
} from "./defs.js";
49+
import { ADDRESS } from "./test_address_generation.js";
50+
51+
export const OUTPUT_LOCK_THEN_TRANSFER = [
52+
1, 0, 145, 1, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193, 30, 91, 4,
53+
195, 202, 103, 207, 80, 217, 178, 0, 145, 1
54+
];
55+
56+
export const OUTPUT_CREATE_STAKE_POOL = [
57+
3, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
59+
113, 2, 0, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193, 30, 91, 4, 195,
60+
202, 103, 207, 80, 217, 178, 0, 108, 245, 234, 97, 170, 9, 247, 158, 169,
61+
100, 84, 123, 235, 183, 147, 29, 136, 118, 203, 24, 146, 56, 60, 217, 2,
62+
198, 32, 133, 255, 240, 84, 123, 1, 91, 58, 110, 176, 100, 207, 6, 194,
63+
41, 193, 30, 91, 4, 195, 202, 103, 207, 80, 217, 178, 100, 0, 0,
64+
];
65+
66+
// Some tx outputs - LockThenTransfer and CreateStakePool
67+
export const OUTPUTS = [...OUTPUT_LOCK_THEN_TRANSFER, ...OUTPUT_CREATE_STAKE_POOL];
5068

5169
export async function test_encode_other_outputs() {
70+
run_one_test(predefined_outputs_test);
71+
run_one_test(general_test);
72+
}
73+
74+
async function predefined_outputs_test() {
75+
const vrf_public_key =
76+
"tvrfpk1qpk0t6np4gyl084fv328h6ahjvwcsaktrzfrs0xeqtrzpp0l7p28knrnn57";
77+
78+
const pool_data = encode_stake_pool_data(
79+
Amount.from_atoms("40000"),
80+
ADDRESS,
81+
vrf_public_key,
82+
ADDRESS,
83+
100,
84+
Amount.from_atoms("0"),
85+
Network.Testnet
86+
);
87+
const expected_pool_data = [
88+
2, 113, 2, 0, 1, 91, 58, 110, 176, 100, 207, 6, 194, 41, 193, 30, 91, 4,
89+
195, 202, 103, 207, 80, 217, 178, 0, 108, 245, 234, 97, 170, 9, 247, 158,
90+
169, 100, 84, 123, 235, 183, 147, 29, 136, 118, 203, 24, 146, 56, 60, 217,
91+
2, 198, 32, 133, 255, 240, 84, 123, 1, 91, 58, 110, 176, 100, 207, 6, 194,
92+
41, 193, 30, 91, 4, 195, 202, 103, 207, 80, 217, 178, 100, 0, 0,
93+
];
94+
95+
assert_eq_arrays(pool_data, expected_pool_data);
96+
97+
const lock = encode_lock_until_height(BigInt(100));
98+
const output = encode_output_lock_then_transfer(
99+
Amount.from_atoms("100"),
100+
ADDRESS,
101+
lock,
102+
Network.Testnet
103+
);
104+
105+
const pool_id =
106+
"tpool1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqza035u";
107+
try {
108+
const invalid_pool_data = TEXT_ENCODER.encode("invalid pool data");
109+
encode_output_create_stake_pool(
110+
pool_id,
111+
invalid_pool_data,
112+
Network.Testnet
113+
);
114+
throw new Error("Invalid pool data worked somehow!");
115+
} catch (e) {
116+
if (!get_err_msg(e).includes("Invalid stake pool data encoding")) {
117+
throw e;
118+
}
119+
console.log("Tested invalid pool data successfully");
120+
}
121+
const stake_pool_output = encode_output_create_stake_pool(
122+
pool_id,
123+
pool_data,
124+
Network.Testnet
125+
);
126+
const outputs = [...output, ...stake_pool_output];
127+
128+
assert_eq_arrays(outputs, OUTPUTS);
129+
}
130+
131+
export async function general_test() {
52132
try {
53133
encode_output_coin_burn(Amount.from_atoms("invalid amount"));
54134
throw new Error("Invalid value for amount worked somehow!");
@@ -419,6 +499,4 @@ export async function test_encode_other_outputs() {
419499
}
420500
console.log("Tested invalid description successfully");
421501
}
422-
423-
run_one_test(test_encode_predefined_outputs);
424502
}

wasm-wrappers/js-bindings-test/tests/test_htlc.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ import {
3737

3838
import {
3939
MNEMONIC,
40-
ADDRESS,
4140
TOKEN_ID,
41+
} from "./defs.js";
42+
import {
43+
ADDRESS
44+
} from "./test_address_generation.js";
45+
import {
4246
INPUTS,
43-
OUTPUTS,
4447
TX_OUTPOINT,
45-
} from "./defs.js";
48+
} from "./test_encode_other_inputs.js";
49+
import {
50+
OUTPUTS,
51+
} from "./test_encode_other_outputs.js";
4652

4753
export async function test_htlc() {
4854
const account_pubkey = make_default_account_privkey(

0 commit comments

Comments
 (0)