aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
Diffstat (limited to 'tools')
-rw-r--r--tools/names-pay-page-link-test.js58
-rw-r--r--tools/pay-page-test.js84
2 files changed, 142 insertions, 0 deletions
diff --git a/tools/names-pay-page-link-test.js b/tools/names-pay-page-link-test.js
new file mode 100644
index 0000000..5e18a43
--- /dev/null
+++ b/tools/names-pay-page-link-test.js
@@ -0,0 +1,58 @@
+// Receive → More options → Payment page: the link to the name's public pay
+// page (v3.coinos.io/<name>/receive, see pay-page-test), to copy or open.
+// Run: bun tools/names-pay-page-link-test.js
+import assert from 'node:assert/strict';
+import puppeteer from './puppeteer.js';
+
+const app = await Bun.file('src/app.js').text();
+const domHelper = app.slice(app.indexOf('function h(tag,'), app.indexOf('// ---------------------------------------------------------------- morphing'));
+const entry = `
+import { namesFeature } from './src/features/names.js';
+${domHelper}
+const state = { names: { byDomain: { 'coinos.io': { name: 'alice', domain: 'coinos.io' } } } };
+const ui = { screen: 'wallet', namesMore: 'menu' };
+const wallet = { loadFeatureState: (k, d) => (k in state ? state[k] : d), saveFeatureState: (k, v) => { state[k] = v; }, nostrSign: async (e) => e, nostrPubkey: () => 'aa'.repeat(32), _cacheKey: () => 'w1' };
+const ctx = {
+ h, ui, wallet, render, toast: () => {}, copy: () => {}, copyBtn: (x, l) => h('button', { class: 'btn-sm' }, l), brandHeader: () => null, goBack: () => {}, setAccount: () => {},
+ hook(name, ...a) {
+ if (name === 'arkReady') return true;
+ if (name === 'arkMakeInvoice') return Promise.resolve({ invoice: 'lnbc10u1testinvoice', amountSat: a[0], expiresAt: Date.now() + (window.__life || 3600_000) });
+ return null;
+ },
+};
+const feature = namesFeature(ctx);
+function render() { document.querySelector('#app').replaceChildren(feature.receiveModes()[0].render(h('div', { class: 'seg' }))); }
+window.test = { render };
+render();
+`;
+const bundle = await Bun.build({
+ entrypoints: ['names-page-entry'], target: 'browser',
+ plugins: [{ name: 'names-page', setup(build) {
+ build.onResolve({ filter: /^names-page-entry$/ }, () => ({ path: 'entry', namespace: 'names-page' }));
+ build.onLoad({ filter: /.*/, namespace: 'names-page' }, () => ({ contents: entry, loader: 'js', resolveDir: process.cwd() }));
+ } }],
+});
+assert(bundle.success, bundle.logs.join('\n'));
+const html = `<!doctype html><meta charset="utf-8"><style>${await Bun.file('src/style.css').text()}</style><div id="app"></div><script type="module">${await bundle.outputs[0].text()}</script>`;
+const server = Bun.serve({ port: 0, fetch: () => new Response(html, { headers: { 'content-type': 'text/html' } }) });
+const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: true, args: ['--no-sandbox'] });
+const page = await browser.newPage();
+const errors = [];
+page.on('pageerror', (e) => errors.push(e.message));
+const pause = (ms) => new Promise((r) => setTimeout(r, ms));
+const check = (name, value, detail = '') => { assert(value, name + (detail ? ' — ' + detail : '')); console.log(' ✓ ' + name); };
+const text = () => page.evaluate(() => document.body.innerText.replace(/\s+/g, ' '));
+try {
+ await page.goto(server.url.href);
+ await page.waitForFunction(() => /Payment page/.test(document.body.innerText));
+ check('More options lists the payment page', /A link for your website/.test(await text()));
+ await page.evaluate(() => [...document.querySelectorAll('.item')].find((d) => /Payment page/.test(d.textContent)).click());
+ await pause(50);
+ check('...which shows the link for this name', /https:\/\/v3\.coinos\.io\/alice\/receive/.test(await text()), await text());
+ check('...with Copy and Open page', await page.evaluate(() => ['Copy', 'Open page'].every((l) => [...document.querySelectorAll('button')].some((b) => b.textContent.trim() === l))));
+ check('no page errors', !errors.length, errors.join(' | '));
+ console.log('\n✅ the payment page link is in More options');
+} finally {
+ await browser.close();
+ server.stop(true);
+}
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);
+}