diff options
Diffstat (limited to 'tests/integration/multi-mint.mjs')
| -rw-r--r-- | tests/integration/multi-mint.mjs | 247 |
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 @@ | |||
| 1 | import { execSync } from 'child_process'; | ||
| 2 | |||
| 3 | const IP = process.env.TOLLGATE_IP || '10.192.45.1'; | ||
| 4 | const API_PORT = 2121; | ||
| 5 | const BASE = `http://${IP}:${API_PORT}`; | ||
| 6 | const MINTS_EXPECTED = [ | ||
| 7 | 'https://mint.minibits.cash/Bitcoin', | ||
| 8 | 'https://mint.coinos.io', | ||
| 9 | 'https://21mint.me', | ||
| 10 | 'https://mint.lnvoltz.com', | ||
| 11 | ]; | ||
| 12 | let passed = 0, failed = 0, skipped = 0; | ||
| 13 | |||
| 14 | function assert(condition, test) { | ||
| 15 | if (condition) { console.log(` \u2713 ${test}`); passed++; } | ||
| 16 | else { console.log(` \u2717 ${test}`); failed++; } | ||
| 17 | } | ||
| 18 | function skip(test, reason) { | ||
| 19 | console.log(` \u25CB ${test} (SKIPPED: ${reason})`); skipped++; | ||
| 20 | } | ||
| 21 | function run(cmd) { | ||
| 22 | try { return execSync(cmd, { encoding: 'utf8', timeout: 30000 }); } | ||
| 23 | catch (e) { return e.stdout || null; } | ||
| 24 | } | ||
| 25 | function 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 | } | ||
| 31 | function 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 | } | ||
| 42 | function sleep(ms) { return new Promise(r => setTimeout(r, ms)); } | ||
| 43 | |||
| 44 | console.log(`\n========================================`); | ||
| 45 | console.log(` Multi-Mint Integration Test`); | ||
| 46 | console.log(` Target: ${IP}:${API_PORT}`); | ||
| 47 | console.log(`========================================\n`); | ||
| 48 | |||
| 49 | // ===== Pre-flight: wait for board to be ready ===== | ||
| 50 | console.log('--- Pre-flight: Board Readiness ---'); | ||
| 51 | const discovery = jsonRetry(`${BASE}/`, 8, 3000); | ||
| 52 | if (!discovery) { | ||
| 53 | console.log(' FATAL: Board not responding after 8 retries. Aborting.'); | ||
| 54 | process.exit(2); | ||
| 55 | } | ||
| 56 | console.log(' Board is responding!\n'); | ||
| 57 | |||
| 58 | // ===== SECTION 1: Configuration ===== | ||
| 59 | console.log('--- Section 1: Configuration ---'); | ||
| 60 | |||
| 61 | assert(discovery !== null, 'GET / returns valid JSON'); | ||
| 62 | assert(discovery && discovery.kind === 10021, 'Discovery has kind=10021'); | ||
| 63 | assert(discovery && discovery.tags && discovery.tags.some(t => t[0] === 'metric' && t[1] === 'milliseconds'), 'Metric is milliseconds'); | ||
| 64 | |||
| 65 | const priceTag = discovery && discovery.tags && discovery.tags.find(t => t[0] === 'price_per_step'); | ||
| 66 | assert(priceTag && priceTag[1] === 'cashu', 'Price tag uses cashu unit'); | ||
| 67 | assert(priceTag && priceTag[2] === '1', 'Price is 1 sat'); | ||
| 68 | assert(priceTag && priceTag[5] === '1', 'Price step count is 1'); | ||
| 69 | |||
| 70 | // ===== SECTION 2: Mint List ===== | ||
| 71 | console.log('\n--- Section 2: Mint List ---'); | ||
| 72 | |||
| 73 | // Batch fetch mints immediately after discovery (board is unstable) | ||
| 74 | const mintsRaw = run(`curl -s --connect-timeout 5 ${BASE}/mints`); | ||
| 75 | let mints = null; | ||
| 76 | try { mints = mintsRaw ? JSON.parse(mintsRaw) : null; } catch { mints = null; } | ||
| 77 | assert(mints !== null, 'GET /mints returns valid JSON'); | ||
| 78 | assert(Array.isArray(mints), '/mints returns an array'); | ||
| 79 | assert(mints && mints.length === MINTS_EXPECTED.length, `/mints has ${MINTS_EXPECTED.length} entries (got ${mints ? mints.length : 0})`); | ||
| 80 | |||
| 81 | if (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 ===== | ||
| 92 | console.log('\n--- Section 3: Health Status ---'); | ||
| 93 | |||
| 94 | const hasHostInternet = run('ping -c 1 -W 3 8.8.8.8 2>/dev/null'); | ||
| 95 | const 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 | |||
| 103 | if (!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 ===== | ||
| 129 | console.log('\n--- Section 4: Payment Routing ---'); | ||
| 130 | |||
| 131 | const badTokenResp = run(`curl -s --connect-timeout 5 -X POST -d "cashuAtest123" ${BASE}/`); | ||
| 132 | assert(badTokenResp !== null, 'POST / with bad token returns response'); | ||
| 133 | assert(badTokenResp && badTokenResp.includes('payment-error-invalid'), 'Bad token rejected with payment-error-invalid'); | ||
| 134 | |||
| 135 | const emptyBodyResp = run(`curl -s --connect-timeout 5 -X POST -d "" ${BASE}/`); | ||
| 136 | assert(emptyBodyResp && emptyBodyResp.includes('payment-error-invalid'), 'Empty body rejected'); | ||
| 137 | |||
| 138 | const noPrefixResp = run(`curl -s --connect-timeout 5 -X POST -d "not_a_cashu_token" ${BASE}/`); | ||
| 139 | assert(noPrefixResp && noPrefixResp.includes('payment-error-invalid'), 'Non-cashu body rejected'); | ||
| 140 | |||
| 141 | // Test with a V3 token structure but fake proofs | ||
| 142 | const 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 | |||
| 146 | const fakeTokenResp = run(`curl -s --connect-timeout 5 -X POST -d "${fakeV3Token}" ${BASE}/`); | ||
| 147 | if (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 | ||
| 168 | const 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 | |||
| 172 | const badMintResp = run(`curl -s --connect-timeout 5 -X POST -d "${badMintToken}" ${BASE}/`); | ||
| 173 | assert(badMintResp && badMintResp.includes('payment-error-mint-not-accepted'), | ||
| 174 | 'Token from non-accepted mint rejected'); | ||
| 175 | |||
| 176 | // ===== SECTION 5: Wallet Status ===== | ||
| 177 | console.log('\n--- Section 5: Wallet Status ---'); | ||
| 178 | |||
| 179 | const wallet = jsonRetry(`${BASE}/wallet`, 3, 1000); | ||
| 180 | assert(wallet !== null, 'GET /wallet returns valid JSON'); | ||
| 181 | assert(wallet && typeof wallet.balance === 'number', 'Wallet has balance field'); | ||
| 182 | assert(wallet && typeof wallet.proof_count === 'number', 'Wallet has proof_count field'); | ||
| 183 | assert(wallet && Array.isArray(wallet.proofs), 'Wallet has proofs array'); | ||
| 184 | assert(wallet && wallet.balance >= 0, 'Balance is non-negative'); | ||
| 185 | assert(wallet && wallet.proof_count >= 0, 'Proof count is non-negative'); | ||
| 186 | |||
| 187 | // ===== SECTION 6: Session / Usage ===== | ||
| 188 | console.log('\n--- Section 6: Session / Usage ---'); | ||
| 189 | |||
| 190 | const usage = json(`${BASE}/usage`); | ||
| 191 | assert(usage !== null, 'GET /usage returns valid JSON'); | ||
| 192 | |||
| 193 | const whoami = run(`curl -s --connect-timeout 5 ${BASE}/whoami`); | ||
| 194 | assert(whoami !== null, 'GET /whoami returns response'); | ||
| 195 | assert(whoami && whoami.includes('mac='), '/whoami returns mac=...'); | ||
| 196 | |||
| 197 | // ===== SECTION 7: Dynamic Mint Status ===== | ||
| 198 | console.log('\n--- Section 7: Dynamic Mint Status Transitions ---'); | ||
| 199 | |||
| 200 | if (!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 ===== | ||
| 229 | console.log('\n--- Section 8: Portal Multi-Mint UI ---'); | ||
| 230 | |||
| 231 | const portal = run(`curl -s --connect-timeout 5 http://${IP}/`); | ||
| 232 | assert(portal && portal.includes('TollGate'), 'Portal HTML contains TollGate'); | ||
| 233 | assert(portal && portal.includes('SUPPORTED MINTS') || portal && portal.includes('mint-list'), 'Portal has mint list section'); | ||
| 234 | |||
| 235 | for (const mintUrl of MINTS_EXPECTED) { | ||
| 236 | const shortUrl = mintUrl.replace('https://', ''); | ||
| 237 | assert(portal && portal.includes(shortUrl), `Portal lists ${shortUrl}`); | ||
| 238 | } | ||
| 239 | |||
| 240 | assert(portal && portal.includes('mint-dot'), 'Portal has mint status dots'); | ||
| 241 | assert(portal && portal.includes(':2121/mints'), 'Portal JS fetches mints from API server'); | ||
| 242 | |||
| 243 | // ===== Summary ===== | ||
| 244 | console.log(`\n========================================`); | ||
| 245 | console.log(` Results: ${passed} passed, ${failed} failed, ${skipped} skipped`); | ||
| 246 | console.log(`========================================\n`); | ||
| 247 | process.exit(failed > 0 ? 1 : 0); | ||