diff options
Diffstat (limited to 'tools/pay-page-test.js')
| -rw-r--r-- | tools/pay-page-test.js | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/tools/pay-page-test.js b/tools/pay-page-test.js new file mode 100644 index 0000000..c463959 --- /dev/null +++ b/tools/pay-page-test.js @@ -0,0 +1,84 @@ +// v3.coinos.io/<name>/receive: a visitor pays a coinos name over Lightning +// from a page of its own (no wallet on this site). The registrar is faked. +// Run: bun tools/pay-page-test.js +import assert from 'node:assert/strict'; +import puppeteer from './puppeteer.js'; +import { buildPayPage } from '../build.js'; + +const page_ = await buildPayPage({ minify: true }); +const server = Bun.serve({ port: 0, fetch() { return new Response(page_, { headers: { 'Content-Type': 'text/html' } }); } }); +const PR = 'lnbc210n1pjtestinvoiceqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq'; +const PK = 'e21a18f58b31baee9291b295c24cbe7aa1787f141af4c7f513a40b4b80262a20'; +const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] }); +const check = (name, ok, detail = '') => { assert(ok, name + (detail ? ' — ' + detail : '')); console.log(' ✓ ' + name); }; + +async function open(path, { verify = true } = {}) { + const page = await browser.newPage(); + const seen = { cb: [], verify: 0, errors: [] }; + page.on('pageerror', (e) => seen.errors.push(e.message)); + await page.setRequestInterception(true); + page.on('request', (r) => { + const u = new URL(r.url()); + const json = (o, status = 200) => r.respond({ status, contentType: 'application/json', headers: { 'Access-Control-Allow-Origin': '*' }, body: JSON.stringify(o) }); + if (u.origin === server.url.origin) return r.continue(); + if (u.host !== 'names.coinos.io') return r.abort(); + if (u.pathname === '/lnurlp/shopkeeper') return json({ tag: 'payRequest', callback: 'https://names.coinos.io/lnurlp/shopkeeper/cb?domain=coinos.io', minSendable: 1000, maxSendable: 500000000, metadata: '[]', commentAllowed: 200 }); + if (u.pathname === '/name/shopkeeper') return json({ name: 'shopkeeper', taken: true, pubkey: PK }); + if (u.pathname === '/lnurlp/shopkeeper/cb') { seen.cb.push(Object.fromEntries(u.searchParams)); return json({ pr: PR, routes: [], ...(verify ? { verify: 'https://names.coinos.io/lnurlp/verify/' + 'ab'.repeat(32) } : {}) }); } + if (u.pathname.startsWith('/lnurlp/verify/')) { seen.verify++; return json({ status: 'OK', settled: seen.verify >= 2, preimage: null, pr: PR }); } + if (u.pathname.startsWith('/lnurlp/')) return json({ status: 'ERROR', reason: 'unknown address' }, 404); + return r.abort(); + }); + // no relays in the test: the profile lookup just finds nothing + await page.evaluateOnNewDocument(() => { window.WebSocket = class { constructor() { setTimeout(() => this.onerror?.({}), 0); } send() {} close() {} }; }); + await page.goto(server.url.origin + path); + return { page, seen }; +} + +try { + { + const { page, seen } = await open('/ShopKeeper/receive?amount=2100'); + await page.waitForSelector('#amount', { timeout: 5000 }); + const text = await page.evaluate(() => document.body.innerText); + check('the page names the address', text.includes('[email protected]'), text); + const qr = await page.$eval('.qr', (a) => a.getAttribute('href')); + check('...with a QR any Lightning wallet can open (LNURL)', /^lightning:LNURL1[0-9A-Z]+$/.test(qr), qr); + check('...and the amount from the link filled in', (await page.$eval('#amount', (i) => i.value)) === '2100'); + await page.$eval('#amount', (i) => { i.value = '21'; }); + await page.type('#note', 'for the prints'); + await page.click('button.primary'); + await page.waitForSelector('#invoice', { timeout: 5000 }); + check('a set amount asks the registrar for that many msat, with the note', seen.cb.length === 1 && seen.cb[0].amount === '21000' && seen.cb[0].comment === 'for the prints', JSON.stringify(seen.cb)); + const inv = await page.$eval('#invoice .qr', (a) => a.getAttribute('href')); + check('...and shows its invoice as a QR', inv === 'lightning:' + PR.toUpperCase(), inv); + check('...with an open-in-wallet link', (await page.$eval('#invoice a.button', (a) => a.getAttribute('href'))) === 'lightning:' + PR); + await page.waitForSelector('#paid', { timeout: 10000 }); + check('it says Paid once the registrar says the invoice settled', (await page.evaluate(() => document.body.innerText)).includes('21 sats sent to [email protected]')); + check('no page errors', !seen.errors.length, seen.errors.join(' | ')); + await page.close(); + } + { + const { page } = await open('/shopkeeper/pay', { verify: false }); + await page.waitForSelector('#amount', { timeout: 5000 }); + await page.type('#amount', '500'); + await page.click('button.primary'); + await page.waitForSelector('#invoice', { timeout: 5000 }); + check('/pay works too; an invoice nobody can watch says to pay it from the wallet', (await page.$eval('#status', (s) => s.textContent)) === 'Pay it from your Lightning wallet.'); + await page.click('button.link'); + check('...and "another amount" goes back', !!(await page.waitForSelector('#amount', { timeout: 3000 }))); + await page.$eval('#amount', (i) => { i.value = '0'; }); + await page.click('button.primary'); + check('an amount under the minimum is refused on the page', /at least 1 sats/.test(await page.$eval('#err', (e) => e.textContent))); + await page.close(); + } + { + const { page } = await open('/nobody/receive'); + await page.waitForFunction(() => document.querySelector('.err'), { timeout: 5000 }); + check('an unknown name says so', (await page.$eval('.err', (e) => e.textContent)) === '[email protected] is not a coinos address.'); + await page.close(); + } + console.log('\n✅ a coinos name has a pay page'); +} finally { + await browser.close(); + server.stop(true); +} |
