054ce10985
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
65 lines
6.4 KiB
JavaScript
65 lines
6.4 KiB
JavaScript
// Stack POL page switch + Stack POL Pro (2026-11-09), end to end against a LOCAL copy. node qa/stackpro.mjs
|
|
// Boots server.js on its own port with a throwaway DATA_DIR (JSON store, devCode sign-in). Nothing touches production.
|
|
// Pro is NEW MONEY only: "Get Pro" opens a waiting order; only a package of $10 or more, mined after the order, by the member's own
|
|
// position, fulfils it; one package fulfils one order (a pass and Pro never ride the same purchase); credits never count.
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
import { spawn } from 'node:child_process';
|
|
|
|
const SITE = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
|
|
const PORT = Number(process.env.SP_PORT || 8803);
|
|
const B = 'http://127.0.0.1:' + PORT;
|
|
const DATA = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-stackpro-')); fs.mkdirSync(path.join(DATA, 'uploads'));
|
|
let fails = 0; const ok = (cond, label, extra) => { console.log((cond ? 'PASS ' : 'FAIL ') + label + (extra ? ' ' + extra : '')); if (!cond) fails++; };
|
|
const sleep = ms => new Promise(r => setTimeout(r, ms));
|
|
const srv = spawn(process.execPath, ['server.js'], { cwd: SITE, stdio: ['ignore', 'pipe', 'pipe'],
|
|
env: Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA, NODE_ENV: 'development', ADMIN_EMAIL: 'qa-admin@example.com', ADMIN_PASSWORD: 'localtest', DEV_POL_USD: '0.10' }) });
|
|
let log = ''; srv.stdout.on('data', d => log += d); srv.stderr.on('data', d => log += d);
|
|
async function api(p, o, headers) {
|
|
const opts = { method: (o && 'data' in o) ? 'POST' : 'GET', headers: Object.assign({ 'Content-Type': 'application/json' }, headers || {}) };
|
|
if (o && 'data' in o) opts.body = JSON.stringify(o.data);
|
|
const r = await fetch(B + p, opts); let j = null; try { j = await r.json(); } catch (e) {} return { status: r.status, j };
|
|
}
|
|
async function signIn(email, username) {
|
|
const s = await api('/api/auth/email/start', { data: { email, fts: Date.now() - 5000 } });
|
|
const v = await fetch(B + '/api/auth/email/verify', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, code: s.j && s.j.devCode }) });
|
|
const cookie = (v.headers.getSetCookie ? v.headers.getSetCookie() : [v.headers.get('set-cookie')]).map(c => String(c).split(';')[0]).join('; ');
|
|
if (username) await api('/api/my/profile', { data: { username } }, { Cookie: cookie });
|
|
return { Cookie: cookie };
|
|
}
|
|
const buy = (email, cents, extra) => api('/api/dev/gamepass-purchase', { data: Object.assign({ email, cents }, extra || {}) });
|
|
const optin = () => { try { return JSON.parse(fs.readFileSync(path.join(DATA, 'stack-optin.json'), 'utf8')); } catch (e) { return {}; } };
|
|
try {
|
|
for (let i = 0; i < 40; i++) { try { if ((await fetch(B + '/api/config')).ok) break; } catch (e) {} await sleep(500); }
|
|
const A = 'sp-a@example.com', Bm = 'sp-b@example.com';
|
|
ok((await api('/api/my/stackpol')).status === 401, 'signed out: 401');
|
|
const ha = await signIn(A, 'stacka'), hb = await signIn(Bm, 'stackb');
|
|
let r = await api('/api/my/stackpol', null, ha); ok(r.status === 200 && r.j.on === true && r.j.url === 'https://stackpol.com/stacka' && r.j.pro && r.j.pro.pro === false, 'GET: on by default, url, not pro', JSON.stringify(r.j));
|
|
r = await api('/api/my/stackpol', { data: { on: true } }, ha); ok(r.j.on === true && optin().users.stacka === true, 'switch on writes stack-optin.json');
|
|
r = await api('/api/my/stackpol', { data: { on: false } }, ha); ok(r.j.on === false && optin().users.stacka === false, 'switch off');
|
|
// a package with no Pro order unlocks nothing
|
|
await buy(A, 1000); r = await api('/api/my/stackpol', null, ha); ok(r.j.pro.pro === false, 'a $10 package without a Pro order does not unlock Pro');
|
|
// Get Pro opens a waiting order
|
|
r = await api('/api/my/stackpol', { data: { pro: 'buy' } }, ha); ok(r.j.buy && r.j.buy.needPay === true && r.j.pro.waiting === true && r.j.buy.usd === 10, 'Get Pro opens a waiting order ($10)', JSON.stringify(r.j.buy));
|
|
const orderTs = r.j.buy.orderTs;
|
|
r = await api('/api/my/stackpol', { data: { pro: 'buy' } }, ha); ok(r.j.buy.reused === true, 'Get Pro twice reuses the same order');
|
|
await buy(A, 500); r = await api('/api/my/stackpol', null, ha); ok(r.j.pro.pro === false && r.j.pro.waiting === true, 'a $5 package does not fulfil Pro');
|
|
await buy(A, 2000, { minedAt: orderTs - 60000 }); r = await api('/api/my/stackpol', null, ha); ok(r.j.pro.pro === false, 'a package mined BEFORE the order does not fulfil Pro');
|
|
const fx = await buy(A, 1000); r = await api('/api/my/stackpol', null, ha); ok(fx.j.order && fx.j.order.stackPro === true && r.j.pro.pro === true && r.j.pro.waiting === false, 'a $10 package after the order unlocks Pro', JSON.stringify(fx.j));
|
|
ok(optin().pro && optin().pro.stacka === true, 'Pro owner lands in stack-optin.json for the indexer');
|
|
const tx = '0x' + 'ab'.repeat(32);
|
|
// one package fulfils ONE order: B opens a pass AND a Pro order, a single $20 package unlocks only one (the higher price: Pro)
|
|
await api('/api/my/stackpol', { data: { pro: 'buy' } }, hb);
|
|
const gp = await api('/api/my/gamepass/buy', { data: {} }, hb); const gpw = await api('/api/my/gamepass', null, hb); ok(gpw.j.waiting === true || !gpw.j.enabled, 'B: a Game Pass order is waiting too (or passes are off locally)', JSON.stringify(gp.j).slice(0, 120));
|
|
await buy(Bm, 2000, { tx }); r = await api('/api/my/stackpol', null, hb);
|
|
const st = await api('/api/my/gamepass', null, hb);
|
|
ok(r.j.pro.pro === true, 'B: the $20 package unlocked Pro (the higher-priced order)');
|
|
ok(st.j.active === false && (st.j.waiting === true || !gpw.j.waiting), 'B: the same package did NOT also start a Game Pass (still waiting)', 'active=' + st.j.active + ' waiting=' + st.j.waiting);
|
|
await buy(Bm, 2000, { tx }); const st2 = await api('/api/my/gamepass', null, hb); ok(st2.j.active === false, 'replaying the same tx fulfils nothing');
|
|
r = await api('/api/my/stackpol', { data: { pro: 'cancel' } }, ha); ok(r.status === 200 && r.j.pro.pro === true, 'cancel after paying keeps Pro');
|
|
} catch (e) { console.log('FAIL exception', e && e.stack || e); fails++; }
|
|
finally { try { srv.kill(); } catch (e) {} try { fs.rmSync(DATA, { recursive: true, force: true }); } catch (e) {} }
|
|
if (fails) console.log(log.split('\n').filter(l => /stack|gamepass|error/i.test(l)).slice(-15).join('\n'));
|
|
console.log(fails ? '\n' + fails + ' FAILED' : '\nALL PASSED'); process.exit(fails ? 1 : 0);
|