-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
194 lines (157 loc) · 7.33 KB
/
test.js
File metadata and controls
194 lines (157 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
const { createWallet, parseNwcUrl, decodeBolt11, resolveLightningAddress, NWCWallet } = require('./lib');
let passed = 0;
let failed = 0;
function assert(condition, message) {
if (condition) {
passed++;
console.log(` ✅ ${message}`);
} else {
failed++;
console.log(` ❌ ${message}`);
}
}
function assertThrows(fn, message) {
try {
fn();
failed++;
console.log(` ❌ ${message} (did not throw)`);
} catch (_) {
passed++;
console.log(` ✅ ${message}`);
}
}
// ─── NWC URL Parsing ───
console.log('\n📡 NWC URL Parsing');
const testNwcUrl = 'nostr+walletconnect://962852f75958e8920c8dfeffd59baa8a75bc7029143a7cea82875772863b0721?relay=wss://relay.getalby.com/v1&secret=7ed367a99f9bde637f4f960f398ab310aaadb0e10ff8273ca6f97e136146272f';
const parsed = parseNwcUrl(testNwcUrl);
assert(parsed.walletPubkey === '962852f75958e8920c8dfeffd59baa8a75bc7029143a7cea82875772863b0721', 'extracts walletPubkey');
assert(parsed.relay === 'wss://relay.getalby.com/v1', 'extracts relay URL');
assert(parsed.secret === '7ed367a99f9bde637f4f960f398ab310aaadb0e10ff8273ca6f97e136146272f', 'extracts secret');
assertThrows(() => parseNwcUrl('https://example.com'), 'rejects non-NWC URL');
assertThrows(() => parseNwcUrl(''), 'rejects empty string');
assertThrows(() => parseNwcUrl(null), 'rejects null');
assertThrows(
() => parseNwcUrl('nostr+walletconnect://abc?relay=wss://x.com&secret=abc'),
'rejects bad pubkey length'
);
assertThrows(
() => parseNwcUrl('nostr+walletconnect://962852f75958e8920c8dfeffd59baa8a75bc7029143a7cea82875772863b0721?secret=7ed367a99f9bde637f4f960f398ab310aaadb0e10ff8273ca6f97e136146272f'),
'rejects missing relay'
);
// ─── Bolt11 Decoding ───
console.log('\n⚡ Bolt11 Amount Decoding');
// lnbc1u = 1 micro-BTC = 100 sats
const d1 = decodeBolt11('lnbc1u1ptest');
assert(d1.amountSats === 100, 'lnbc1u = 100 sats');
// lnbc210n = 210 nano-BTC = 21 sats
const d2 = decodeBolt11('lnbc210n1ptest');
assert(d2.amountSats === 21, 'lnbc210n = 21 sats');
// lnbc50u = 50 micro-BTC = 5000 sats
const d3 = decodeBolt11('lnbc50u1ptest');
assert(d3.amountSats === 5000, 'lnbc50u = 5000 sats');
// lnbc1m = 1 milli-BTC = 100000 sats
const d4 = decodeBolt11('lnbc1m1ptest');
assert(d4.amountSats === 100000, 'lnbc1m = 100000 sats');
// lnbc100n = 100 nano-BTC = 10 sats
const d5 = decodeBolt11('lnbc100n1ptest');
assert(d5.amountSats === 10, 'lnbc100n = 10 sats');
// lnbc2500u = 2500 micro-BTC = 250000 sats
const d6 = decodeBolt11('lnbc2500u1ptest');
assert(d6.amountSats === 250000, 'lnbc2500u = 250000 sats');
// lnbc10m = 10 milli-BTC = 1000000 sats
const d7 = decodeBolt11('lnbc10m1ptest');
assert(d7.amountSats === 1000000, 'lnbc10m = 1000000 sats');
// lnbc1500p = 1500 pico-BTC = 0.15 sats ≈ 0 sats (rounds)
const d8 = decodeBolt11('lnbc1500p1ptest');
assert(d8.amountSats === 0, 'lnbc1500p = 0 sats (sub-sat)');
// lnbc20n = 20 nano-BTC = 2 sats
const d9 = decodeBolt11('lnbc20n1ptest');
assert(d9.amountSats === 2, 'lnbc20n = 2 sats');
// Network detection
console.log('\n🌐 Network Detection');
assert(decodeBolt11('lnbc50u1ptest').network === 'mainnet', 'lnbc = mainnet');
assert(decodeBolt11('lntb50u1ptest').network === 'testnet', 'lntb = testnet');
// No amount (zero-amount invoice)
const d10 = decodeBolt11('lnbc1ptesttesttest');
assert(d10.amountSats === null, 'lnbc with no amount = null');
// Error cases
console.log('\n🚫 Bolt11 Error Cases');
assertThrows(() => decodeBolt11(''), 'rejects empty string');
assertThrows(() => decodeBolt11(null), 'rejects null');
assertThrows(() => decodeBolt11('notaninvoice'), 'rejects garbage');
// ─── createWallet interface ───
console.log('\n🔧 createWallet Interface');
const wallet = new NWCWallet(testNwcUrl);
assert(typeof wallet.getBalance === 'function', 'has getBalance()');
assert(typeof wallet.createInvoice === 'function', 'has createInvoice()');
assert(typeof wallet.payInvoice === 'function', 'has payInvoice()');
assert(typeof wallet.waitForPayment === 'function', 'has waitForPayment()');
assert(typeof wallet.decodeInvoice === 'function', 'has decodeInvoice()');
assert(typeof wallet.close === 'function', 'has close()');
// Test decodeInvoice works via wallet instance
const decoded = wallet.decodeInvoice('lnbc50u1ptest');
assert(decoded.amountSats === 5000, 'wallet.decodeInvoice works');
wallet.close();
// ─── Lightning Address tests ───
console.log('\n⚡ Lightning Address');
assert(typeof resolveLightningAddress === 'function', 'resolveLightningAddress is exported');
// Wallet has payAddress method
const walletForAddr = createWallet(testNwcUrl);
assert(typeof walletForAddr.payAddress === 'function', 'wallet has payAddress()');
walletForAddr.close();
// payAddress validation
const walletForAddrTest = createWallet(testNwcUrl);
// Test via promise catches
const addrTests = Promise.all([
walletForAddrTest.payAddress('invalid', { amountSats: 10 })
.then(() => assert(false, 'payAddress rejects invalid address'))
.catch(e => assert(e.message.includes('Invalid Lightning address'), 'payAddress rejects invalid address')),
walletForAddrTest.payAddress('user@domain.com', {})
.then(() => assert(false, 'payAddress requires amountSats'))
.catch(e => assert(e.message.includes('amountSats'), 'payAddress requires amountSats')),
walletForAddrTest.payAddress('user@domain.com', { amountSats: -5 })
.then(() => assert(false, 'payAddress rejects negative amount'))
.catch(e => assert(e.message.includes('amountSats'), 'payAddress rejects negative amount')),
]).then(() => walletForAddrTest.close());
// createWallet from env
console.log('\n🌍 createWallet from env');
process.env.NWC_URL = testNwcUrl;
const walletFromEnv = createWallet();
assert(walletFromEnv instanceof NWCWallet, 'createWallet() reads NWC_URL env');
walletFromEnv.close();
delete process.env.NWC_URL;
assertThrows(() => createWallet(), 'createWallet() throws without URL or env');
// ─── Batch Payment Methods ───
console.log('\n📦 Batch Payment Methods');
const walletForBatch = createWallet(testNwcUrl);
// payBatch validation
const batchTests = Promise.all([
// Test empty array
walletForBatch.payBatch([])
.then(() => assert(false, 'payBatch rejects empty array'))
.catch(e => assert(e.message.includes('required'), 'payBatch rejects empty array')),
// Test non-array
walletForBatch.payBatch('not an array')
.then(() => assert(false, 'payBatch rejects non-array'))
.catch(e => assert(e.message.includes('required'), 'payBatch rejects non-array')),
// payAddresses validation
walletForBatch.payAddresses([])
.then(() => assert(false, 'payAddresses rejects empty array'))
.catch(e => assert(e.message.includes('required'), 'payAddresses rejects empty array')),
]).then(() => {
// Test that methods exist with correct signatures
assert(typeof walletForBatch.payBatch === 'function', 'payBatch method exists');
assert(typeof walletForBatch.payAddresses === 'function', 'payAddresses method exists');
walletForBatch.close();
});
// ─── Summary (wait for async tests) ───
Promise.all([addrTests, batchTests]).then(() => {
console.log(`\n━━━━━━━━━━━━━━━━━━━━━━━━━━`);
console.log(`Results: ${passed} passed, ${failed} failed`);
if (failed > 0) {
process.exit(1);
} else {
console.log('All tests passed! ✅');
}
});