Files
instantadpay/qa/line-origin.mjs
T

69 lines
7.2 KiB
JavaScript

// My line: directs' email + origin + search (Marty, 2026-11-12). Boots a throwaway server, makes a sponsor and three directs who
// join through tagged links (?src=), then checks in a real browser that "Your line" shows each direct's email, where they came
// from and how they joined, that the Activity drop-down shows the same for a direct, and that the search box narrows the list.
// Screenshot in qa/out/line-origin.png. node qa/line-origin.mjs
import { spawn } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
const PW = process.env.PW || 'D:/Projects/MarketingAgent/qa-tester/node_modules/playwright/index.js';
const { chromium } = (await import('file:///' + PW.replace(/^\/+/, ''))).default;
const PORT = 8797, B = 'http://127.0.0.1:' + PORT;
const DATA = path.join(os.tmpdir(), 'iap-line-origin-' + Date.now()); fs.mkdirSync(DATA, { recursive: true });
const ok = [], bad = []; const t = (n, c, x) => (c ? ok : bad).push(n + (c || !x ? '' : ' -> ' + x));
const srv = spawn(process.execPath, ['server.js'], { env: { ...process.env, PORT: String(PORT), DATA_DIR: DATA, ADMIN_PASSWORD: 'localtest', ADMIN_EMAIL: 'admin@example.com', NODE_ENV: 'test', DATABASE_URL: '' }, stdio: 'ignore' });
const bye = c => { try { srv.kill(); } catch (e) {} setTimeout(() => { try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} process.exit(c); }, 500); };
for (let i = 0; i < 60; i++) { try { if ((await fetch(B + '/api/stats')).ok) break; } catch (e) {} await new Promise(r => setTimeout(r, 500)); }
const DEV = 'c0ffee00c0ffee00c0ffee00c0ffee00';
const cookiesOf = r => (r.headers.getSetCookie ? r.headers.getSetCookie() : [r.headers.get('set-cookie')]).filter(Boolean).map(c => String(c).split(';')[0]);
const jf = async (p, body, cookie, extra) => { const r = await fetch(B + p, { method: body ? 'POST' : 'GET', redirect: 'manual', headers: Object.assign({ 'Content-Type': 'application/json', Cookie: cookie }, extra || {}), body: body ? JSON.stringify(body) : undefined }); let b = {}; try { b = await r.json(); } catch (e) {} return { r, b }; };
async function signIn(email, jarIn, dev, ip) { // each test person gets their own device id, or the one-account-per-person guard blocks them
const jar0 = ['iap.dev=' + (dev || DEV), ...(jarIn || [])].join('; ');
const xf = { 'X-Forwarded-For': ip || '203.0.113.1' }; // a different test address per person (the per-connection sign-up guard)
const st = await jf('/api/auth/email/start', { email, fts: Date.now() - 20000 }, jar0, xf);
const code = st.b.devCode || st.b.code; if (!code) { console.log('no dev code for', email, JSON.stringify(st.b)); bye(2); }
const vf = await jf('/api/auth/email/verify', { email, code }, jar0, xf);
if (vf.b && vf.b.error) console.log('verify', email, vf.b.error);
return [jar0, ...cookiesOf(vf.r)].join('; ');
}
const jar = await signIn('sponsor@example.com');
await jf('/api/my/profile', { username: 'sponsorone' }, jar);
const me = (await jf('/api/me', null, jar)).b; const tok = me.username || me.refCode; t('sponsor has a link token', !!tok, JSON.stringify(me).slice(0, 120));
const directs = [['ann@example.com', '?src=mailergold', 'https://mailer.gold/x'], ['bob@example.com', '?src=pam-mail', ''], ['cat@example.com', '', 'https://massifly.com/feed']];
for (const [email, q, ref] of directs) {
const j = await jf('/join/' + tok + q, null, '', Object.assign({ 'X-Forwarded-For': '198.51.100.' + (10 + directs.findIndex(d => d[0] === email)) }, ref ? { Referer: ref } : {}));
await signIn(email, cookiesOf(j.r), DEV.slice(0, 26) + String(directs.findIndex(d => d[0] === email)).padStart(6, '0'), '198.51.100.' + (10 + directs.findIndex(d => d[0] === email)));
}
const me2 = (await jf('/api/my/dashboard', null, jar)).b; t('the 3 directs are under the sponsor', (me2.referrals || []).length === 3, 'tok=' + tok + ' referrals=' + JSON.stringify(me2.referrals || []).slice(0, 300));
if ((me2.referrals || []).length !== 3) {
try { const d = JSON.parse(fs.readFileSync(path.join(DATA, 'accounts.json'), 'utf8')); console.log(JSON.stringify(Object.values(d.byEmail || {}).map(x => [x.email, x.username, x.sponsorRef, x.joinedRef, x.joinedVia]))); } catch (e) { console.log('no accounts.json', fs.readdirSync(DATA).join(',')); }
console.log(bad.join('\n')); bye(1);
}
const br = await chromium.launch(); const ctx = await br.newContext({ viewport: { width: 1280, height: 900 } });
await ctx.addCookies(jar.split('; ').map(kv => { const [name, ...v] = kv.split('='); return { name, value: v.join('='), url: B }; }));
await ctx.addInitScript(() => { try { sessionStorage.setItem('iap.welcome.v1', '1'); } catch (e) {} });
const page = await ctx.newPage();
await page.goto(B + '/my#line', { waitUntil: 'domcontentloaded' }); await page.waitForTimeout(4000);
for (const sel of ['#unameSkip', '[data-close]', '.modal .x']) { try { await page.click(sel, { timeout: 800 }); } catch (e) {} }
await page.evaluate(() => { document.querySelectorAll('.modal-back,.lgate,.iap-welcome').forEach(m => m.hidden = true); const b = document.querySelector('[data-pane="line"]'); if (b) b.click(); }); await page.waitForTimeout(1500);
const rows = await page.$$eval('#rosterWrap table.roster tr', xs => xs.map(x => x.innerText.replace(/\s+/g, ' ')));
t('Your line lists the 3 directs', rows.length === 3, rows.length);
t('each row shows the email', ['ann@', 'bob@', 'cat@'].every(e => rows.some(r => r.includes(e))), JSON.stringify(rows));
t('tagged link with referrer: from mailer.gold/mailergold', rows.some(r => r.includes('ann@') && r.includes('from mailer.gold/mailergold')), JSON.stringify(rows));
t('tagged link, no referrer: from pam-mail', rows.some(r => r.includes('bob@') && r.includes('from pam-mail')), JSON.stringify(rows));
t('untagged: from massifly.com', rows.some(r => r.includes('cat@') && r.includes('from massifly.com')), JSON.stringify(rows));
await (await page.$('#lineRosterCard')).screenshot({ path: 'qa/out/line-origin.png' });
await page.fill('#rosterWrap .lst-q', 'mailergold'); await page.waitForTimeout(600);
const after = await page.$$eval('#rosterWrap table.roster tr', xs => xs.filter(x => !x.hidden).map(x => x.innerText));
t('search by source narrows to 1', after.length === 1 && after[0].includes('ann@'), after.length);
const focused = await page.evaluate(() => document.activeElement && document.activeElement.classList.contains('lst-q'));
t('the search box keeps focus while typing', focused);
// Activity drop-down on a direct in the three-level card
const act = await page.$('#lineageWrap [data-act]');
if (act) { await act.click(); await page.waitForTimeout(1200); const box = await page.$eval('#lineageWrap .lin-act:not([hidden])', e => e.innerText).catch(() => ''); t('Activity shows Email + Came for a direct', /Email/.test(box) && /Came/.test(box) && /@example\.com/.test(box), box.slice(0, 200)); }
else t('three-level card has an Activity button', false, 'none');
const lv = await page.$eval('#lineageWrap', e => e.innerText).catch(() => ''); t('three-level card shows the origin line', /from (mailer\.gold\/mailergold|pam-mail|massifly\.com)/.test(lv), lv.slice(0, 200));
await br.close();
console.log(ok.map(x => ' ok ' + x).join('\n')); if (bad.length) console.log(bad.map(x => ' FAIL ' + x).join('\n'));
bye(bad.length ? 1 : 0);