upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/integration/multi-mint.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/multi-mint.mjs')
-rw-r--r--tests/integration/multi-mint.mjs247
1 files changed, 247 insertions, 0 deletions
diff --git a/tests/integration/multi-mint.mjs b/tests/integration/multi-mint.mjs
new file mode 100644
index 0000000..05c61fb
--- /dev/null
+++ b/tests/integration/multi-mint.mjs
@@ -0,0 +1,247 @@
1import { execSync } from 'child_process';
2
3const IP = process.env.TOLLGATE_IP || '10.192.45.1';
4const API_PORT = 2121;
5const BASE = `http://${IP}:${API_PORT}`;
6const MINTS_EXPECTED = [
7 'https://mint.minibits.cash/Bitcoin',
8 'https://mint.coinos.io',
9 'https://21mint.me',
10 'https://mint.lnvoltz.com',
11];
12let passed = 0, failed = 0, skipped = 0;
13
14function assert(condition, test) {
15 if (condition) { console.log(` \u2713 ${test}`); passed++; }
16 else { console.log(` \u2717 ${test}`); failed++; }
17}
18function skip(test, reason) {
19 console.log(` \u25CB ${test} (SKIPPED: ${reason})`); skipped++;
20}
21function run(cmd) {
22 try { return execSync(cmd, { encoding: 'utf8', timeout: 30000 }); }
23 catch (e) { return e.stdout || null; }
24}
25function json(url) {
26 const out = run(`curl -s --connect-timeout 5 ${url}`);
27 if (!out) return null;
28 try { return JSON.parse(out); }
29 catch { return null; }
30}
31function jsonRetry(url, retries = 5, delayMs = 2000) {
32 for (let i = 0; i < retries; i++) {
33 const result = json(url);
34 if (result !== null) return result;
35 if (i < retries - 1) {
36 console.log(` (retry ${i+1}/${retries}: ${url})`);
37 execSync(`sleep ${delayMs/1000}`);
38 }
39 }
40 return null;
41}
42function sleep(ms) { return new Promise(r => setTimeout(r, ms)); }
43
44console.log(`\n========================================`);
45console.log(` Multi-Mint Integration Test`);
46console.log(` Target: ${IP}:${API_PORT}`);
47console.log(`========================================\n`);
48
49// ===== Pre-flight: wait for board to be ready =====
50console.log('--- Pre-flight: Board Readiness ---');
51const discovery = jsonRetry(`${BASE}/`, 8, 3000);
52if (!discovery) {
53 console.log(' FATAL: Board not responding after 8 retries. Aborting.');
54 process.exit(2);
55}
56console.log(' Board is responding!\n');
57
58// ===== SECTION 1: Configuration =====
59console.log('--- Section 1: Configuration ---');
60
61assert(discovery !== null, 'GET / returns valid JSON');
62assert(discovery && discovery.kind === 10021, 'Discovery has kind=10021');
63assert(discovery && discovery.tags && discovery.tags.some(t => t[0] === 'metric' && t[1] === 'milliseconds'), 'Metric is milliseconds');
64
65const priceTag = discovery && discovery.tags && discovery.tags.find(t => t[0] === 'price_per_step');
66assert(priceTag && priceTag[1] === 'cashu', 'Price tag uses cashu unit');
67assert(priceTag && priceTag[2] === '1', 'Price is 1 sat');
68assert(priceTag && priceTag[5] === '1', 'Price step count is 1');
69
70// ===== SECTION 2: Mint List =====
71console.log('\n--- Section 2: Mint List ---');
72
73// Batch fetch mints immediately after discovery (board is unstable)
74const mintsRaw = run(`curl -s --connect-timeout 5 ${BASE}/mints`);
75let mints = null;
76try { mints = mintsRaw ? JSON.parse(mintsRaw) : null; } catch { mints = null; }
77assert(mints !== null, 'GET /mints returns valid JSON');
78assert(Array.isArray(mints), '/mints returns an array');
79assert(mints && mints.length === MINTS_EXPECTED.length, `/mints has ${MINTS_EXPECTED.length} entries (got ${mints ? mints.length : 0})`);
80
81if (mints && mints.length > 0) {
82 for (const expectedUrl of MINTS_EXPECTED) {
83 const found = mints.find(m => m.url === expectedUrl);
84 assert(found !== undefined, `Mint list contains ${expectedUrl}`);
85 if (found) {
86 assert(typeof found.reachable === 'boolean', `${expectedUrl} has boolean reachable field`);
87 }
88 }
89}
90
91// ===== SECTION 3: Health Status =====
92console.log('\n--- Section 3: Health Status ---');
93
94const hasHostInternet = run('ping -c 1 -W 3 8.8.8.8 2>/dev/null');
95const boardHasInternet = (() => {
96 if (!discovery) return false;
97 // If board has STA internet, mints would be reachable after initial probe
98 // Check by seeing if any mint is reachable
99 const m = jsonRetry(`${BASE}/mints`, 3, 1000);
100 return m && m.some(mi => mi.reachable === true);
101})();
102
103if (!boardHasInternet) {
104 skip('Mint reachability probes', 'Board has no internet connectivity');
105 skip('Reachable mint transitions', 'Board has no internet connectivity');
106
107 if (mints && mints.length > 0) {
108 const allUnreachable = mints.every(m => m.reachable === false);
109 assert(allUnreachable, 'All mints show reachable=false without internet');
110 }
111} else {
112 console.log(' Board has internet! Running live health probe tests...');
113
114 const reachableMints = mints ? mints.filter(m => m.reachable) : [];
115 const unreachableMints = mints ? mints.filter(m => !m.reachable) : [];
116
117 console.log(` Reachable: ${reachableMints.length}, Unreachable: ${unreachableMints.length}`);
118 assert(reachableMints.length > 0, `At least 1 mint is reachable (got ${reachableMints.length})`);
119
120 for (const m of reachableMints) {
121 console.log(` \u2713 REACHABLE: ${m.url}`);
122 }
123 for (const m of unreachableMints) {
124 console.log(` \u2717 UNREACHABLE: ${m.url}`);
125 }
126}
127
128// ===== SECTION 4: Payment Routing =====
129console.log('\n--- Section 4: Payment Routing ---');
130
131const badTokenResp = run(`curl -s --connect-timeout 5 -X POST -d "cashuAtest123" ${BASE}/`);
132assert(badTokenResp !== null, 'POST / with bad token returns response');
133assert(badTokenResp && badTokenResp.includes('payment-error-invalid'), 'Bad token rejected with payment-error-invalid');
134
135const emptyBodyResp = run(`curl -s --connect-timeout 5 -X POST -d "" ${BASE}/`);
136assert(emptyBodyResp && emptyBodyResp.includes('payment-error-invalid'), 'Empty body rejected');
137
138const noPrefixResp = run(`curl -s --connect-timeout 5 -X POST -d "not_a_cashu_token" ${BASE}/`);
139assert(noPrefixResp && noPrefixResp.includes('payment-error-invalid'), 'Non-cashu body rejected');
140
141// Test with a V3 token structure but fake proofs
142const fakeV3Token = 'cashuA' + Buffer.from(JSON.stringify({
143 token: [{ mint: 'https://mint.minibits.cash/Bitcoin', proofs: [{ amount: 1, id: 'fake', secret: 'fake', C: 'fake' }] }]
144})).toString('base64url');
145
146const fakeTokenResp = run(`curl -s --connect-timeout 5 -X POST -d "${fakeV3Token}" ${BASE}/`);
147if (fakeTokenResp) {
148 try {
149 const parsed = JSON.parse(fakeTokenResp);
150 if (parsed.tags && parsed.tags.some(t => t[0] === 'code')) {
151 const code = parsed.tags.find(t => t[0] === 'code')[1];
152 if (boardHasInternet) {
153 assert(code === 'payment-error-verification' || code === 'payment-error-token-spent',
154 'Fake V3 token rejected by mint verification (not locally)');
155 } else {
156 assert(code === 'payment-error-mint-not-accepted' || code === 'payment-error-verification',
157 'Fake V3 token rejected (mint unreachable or verification failed)');
158 }
159 } else {
160 skip('Fake V3 token code check', 'Response has unexpected format');
161 }
162 } catch {
163 skip('Fake V3 token parse', 'Non-JSON response');
164 }
165}
166
167// Test with token from non-accepted mint
168const badMintToken = 'cashuA' + Buffer.from(JSON.stringify({
169 token: [{ mint: 'https://evil-mint.example.com', proofs: [{ amount: 1, id: 'fake', secret: 'fake', C: 'fake' }] }]
170})).toString('base64url');
171
172const badMintResp = run(`curl -s --connect-timeout 5 -X POST -d "${badMintToken}" ${BASE}/`);
173assert(badMintResp && badMintResp.includes('payment-error-mint-not-accepted'),
174 'Token from non-accepted mint rejected');
175
176// ===== SECTION 5: Wallet Status =====
177console.log('\n--- Section 5: Wallet Status ---');
178
179const wallet = jsonRetry(`${BASE}/wallet`, 3, 1000);
180assert(wallet !== null, 'GET /wallet returns valid JSON');
181assert(wallet && typeof wallet.balance === 'number', 'Wallet has balance field');
182assert(wallet && typeof wallet.proof_count === 'number', 'Wallet has proof_count field');
183assert(wallet && Array.isArray(wallet.proofs), 'Wallet has proofs array');
184assert(wallet && wallet.balance >= 0, 'Balance is non-negative');
185assert(wallet && wallet.proof_count >= 0, 'Proof count is non-negative');
186
187// ===== SECTION 6: Session / Usage =====
188console.log('\n--- Section 6: Session / Usage ---');
189
190const usage = json(`${BASE}/usage`);
191assert(usage !== null, 'GET /usage returns valid JSON');
192
193const whoami = run(`curl -s --connect-timeout 5 ${BASE}/whoami`);
194assert(whoami !== null, 'GET /whoami returns response');
195assert(whoami && whoami.includes('mac='), '/whoami returns mac=...');
196
197// ===== SECTION 7: Dynamic Mint Status =====
198console.log('\n--- Section 7: Dynamic Mint Status Transitions ---');
199
200if (!boardHasInternet) {
201 skip('Reachable->unreachable transition', 'No internet');
202 skip('Unreachable->reachable recovery', 'No internet');
203 skip('Mint status callback triggers', 'No internet');
204 skip('Payment rejection for unreachable mints', 'No internet');
205} else {
206 // Wait for health probes to run and check if any mints became reachable
207 console.log(' Waiting 60s for health probes to complete...');
208 await sleep(60000);
209
210 const mintsAfterProbe = json(`${BASE}/mints`);
211 if (mintsAfterProbe) {
212 const reachableNow = mintsAfterProbe.filter(m => m.reachable);
213 console.log(` After 60s: ${reachableNow.length}/${mintsAfterProbe.length} mints reachable`);
214
215 // Compare with initial state
216 const initialReachable = mints ? mints.filter(m => m.reachable).length : 0;
217 if (reachableNow.length !== initialReachable) {
218 console.log(` \u271f Mint status changed: ${initialReachable} -> ${reachableNow.length} reachable`);
219 }
220
221 // Test payment only with a reachable mint
222 if (reachableNow.length > 0) {
223 console.log(` \u2713 Can attempt payment with reachable mint: ${reachableNow[0].url}`);
224 }
225 }
226}
227
228// ===== SECTION 8: Portal Multi-Mint UI =====
229console.log('\n--- Section 8: Portal Multi-Mint UI ---');
230
231const portal = run(`curl -s --connect-timeout 5 http://${IP}/`);
232assert(portal && portal.includes('TollGate'), 'Portal HTML contains TollGate');
233assert(portal && portal.includes('SUPPORTED MINTS') || portal && portal.includes('mint-list'), 'Portal has mint list section');
234
235for (const mintUrl of MINTS_EXPECTED) {
236 const shortUrl = mintUrl.replace('https://', '');
237 assert(portal && portal.includes(shortUrl), `Portal lists ${shortUrl}`);
238}
239
240assert(portal && portal.includes('mint-dot'), 'Portal has mint status dots');
241assert(portal && portal.includes(':2121/mints'), 'Portal JS fetches mints from API server');
242
243// ===== Summary =====
244console.log(`\n========================================`);
245console.log(` Results: ${passed} passed, ${failed} failed, ${skipped} skipped`);
246console.log(`========================================\n`);
247process.exit(failed > 0 ? 1 : 0);