Files
instantadpay/qa/weekend.e2e.mjs
T

95 lines
8.1 KiB
JavaScript

// Network Weekend end to end on a local copy (throwaway data, JSON store, no DB, no mail key): the /weekend page, the Home strip,
// the ribbon, /api/promodays, /api/network-deals (from a fake hub that serves the real network-hub deal list at the test time),
// and the DownlineHydra builders feed's key check. The test clock is IAP_TEST_NOW (server) + window.__NW_NOW (pages).
// node qa/weekend.e2e.mjs (needs ../../network-hub/lib/deals.js beside this repo, playwright from qa-tester)
import http from 'node:http'; import { spawn } from 'node:child_process'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path';
import { createRequire } from 'node:module'; import assert from 'node:assert';
const require = createRequire(import.meta.url);
const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/, '$1')), '..');
const { chromium } = require(process.env.PW || 'D:/Projects/MarketingAgent/qa-tester/node_modules/playwright');
const deals = require(path.resolve(ROOT, '../../network-hub/lib/deals.js'));
const KEY = 'k'.repeat(48);
const out = []; const ok = m => { out.push('ok ' + m); console.log('ok', m); };
async function run(nowIso, port) {
const hub = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify(deals.current(Date.parse(nowIso)))); });
await new Promise(r => hub.listen(0, r));
const data = fs.mkdtempSync(path.join(os.tmpdir(), 'iap-nw-')); fs.mkdirSync(path.join(data, 'uploads'));
const env = Object.assign({}, process.env, { PORT: String(port), DATA_DIR: data, ADMIN_PASSWORD: 'localtest', IAP_TEST_NOW: nowIso, HUB_URL: 'http://127.0.0.1:' + hub.address().port, WEEKEND_BUNDLE_KEY: KEY, NODE_ENV: 'test', DATABASE_URL: '', DOCSTORE_URL: '' });
const srv = spawn(process.execPath, ['server.js'], { cwd: ROOT, env, stdio: ['ignore', 'pipe', 'pipe'] });
let log = ''; srv.stdout.on('data', d => { log += d; }); srv.stderr.on('data', d => { log += d; });
const base = 'http://127.0.0.1:' + port;
try {
for (let i = 0; i < 40; i++) { try { if ((await fetch(base + '/api/config')).ok) break; } catch (e) {} await new Promise(r => setTimeout(r, 500)); }
return await fn(base, nowIso);
} finally { srv.kill(); hub.close(); fs.rmSync(data, { recursive: true, force: true }); if (process.env.QA_LOG) console.log(log.slice(-3000)); }
}
let fn;
(async () => {
const browser = await chromium.launch();
const page = async (base, nowIso, p) => {
const ctx = await browser.newContext(); const pg = await ctx.newPage(); const errs = [];
pg.on('pageerror', e => errs.push(e.message)); pg.on('console', m => { if (m.type() === 'error' && /Content Security Policy|Refused/.test(m.text())) errs.push(m.text()); });
await pg.addInitScript(t => { window.__NW_NOW = t; }, Date.parse(nowIso));
await pg.goto(base + p, { waitUntil: 'networkidle' }); await pg.waitForTimeout(800);
return { pg, ctx, errs };
};
// 1. Black Friday 09:00 CT
fn = async (base, nowIso) => {
const v = await (await fetch(base + '/api/promodays')).json();
assert.ok(v.live && v.today.name === 'Black Friday' && v.today.pct === 40 && v.lsCode === 'WEEKEND500'); ok('Black Friday /api/promodays live 40%');
const f = await (await fetch(base + '/api/friday')).json(); assert.ok(f.live && f.pct === 40 && f.name === 'Black Friday'); ok('/api/friday says Black Friday +40%');
const d = await (await fetch(base + '/api/network-deals')).json(); assert.strictEqual(d.self, 'iap'); assert.ok(d.events[0] && d.events[0].deals.length > 5); ok('/api/network-deals relays the hub');
assert.strictEqual((await fetch(base + '/api/network/weekend-builders')).status, 401);
assert.strictEqual((await fetch(base + '/api/network/weekend-builders', { headers: { 'X-Weekend-Key': 'x'.repeat(48) } })).status, 401);
const b = await fetch(base + '/api/network/weekend-builders', { headers: { 'X-Weekend-Key': KEY } }); assert.strictEqual(b.status, 200);
const bj = await b.json(); assert.ok(Array.isArray(bj.builders) && bj.cap === 24 && bj.start === '2026-11-27T06:00:00.000Z'); ok('builders feed: key required, shape right');
let { pg, ctx, errs } = await page(base, nowIso, '/weekend');
const state = await pg.textContent('#nwState'); assert.match(state, /It is Black Friday\. \+40% bonus ad credits on any \$5\+/); ok('/weekend live state');
assert.strictEqual(await pg.locator('.nw-day').count(), 4); assert.ok(await pg.locator('.nw-day.today').count() === 1);
assert.ok((await pg.locator('.nw-deal').count()) > 8); ok('/weekend day cards + every network deal');
assert.ok(await pg.locator('.nw-ribbon').count() === 1 && /Black Friday/.test(await pg.textContent('.nw-ribbon'))); ok('ribbon on /weekend (no self link)');
assert.strictEqual(await pg.locator('.nw-ribbon a').count(), 0);
await pg.screenshot({ path: path.join(ROOT, 'qa/out/weekend-live.png'), fullPage: true });
assert.deepStrictEqual(errs, []); await ctx.close();
({ pg, ctx, errs } = await page(base, nowIso, '/'));
assert.match(await pg.textContent('#nwStrip'), /Network Weekend: it\u2019s Black Friday\. \+40%/); ok('Home strip live');
assert.ok(/Ad credits buy advertising, not income\./.test(await pg.textContent('#nwStrip')));
assert.strictEqual(await pg.locator('.nw-ribbon').count(), 1); ok('ribbon on Home');
await pg.screenshot({ path: path.join(ROOT, 'qa/out/weekend-home.png') });
assert.deepStrictEqual(errs, []); await ctx.close();
};
await run('2026-11-27T15:00:00Z', 8811);
// 2. Wed 11-18 (today-ish): nothing shows anywhere
fn = async (base, nowIso) => {
const v = await (await fetch(base + '/api/promodays')).json(); assert.ok(!v.live && !v.published && v.days.length === 0); ok('before Thursday: /api/promodays publishes nothing');
const f = await (await fetch(base + '/api/friday')).json(); assert.strictEqual(f.pct, 20); ok('a normal week: Friday +20%');
let { pg, ctx, errs } = await page(base, nowIso, '/weekend');
assert.match(await pg.textContent('#nwState'), /Coming/); assert.strictEqual(await pg.locator('.nw-ribbon').count(), 0); assert.strictEqual(await pg.locator('.nw-deal').count(), 0); ok('/weekend dormant, no ribbon');
assert.deepStrictEqual(errs, []); await ctx.close();
({ pg, ctx, errs } = await page(base, nowIso, '/'));
assert.strictEqual(await pg.locator('#nwStrip:not([hidden])').count(), 0); assert.strictEqual(await pg.locator('.nw-ribbon').count(), 0); ok('Home: no strip, no ribbon');
assert.deepStrictEqual(errs, []); await ctx.close();
};
await run('2026-11-18T15:00:00Z', 8812);
// 3. Thanksgiving 20:00 CT: heads-up strip, no ribbon yet; Cyber Monday; Tuesday off
fn = async (base, nowIso) => {
let { pg, ctx } = await page(base, nowIso, '/');
assert.match(await pg.textContent('#nwStrip'), /starts at midnight Central/); assert.strictEqual(await pg.locator('.nw-ribbon').count(), 0); ok('Thanksgiving: heads-up strip, ribbon not yet');
await ctx.close();
};
await run('2026-11-27T02:00:00Z', 8813);
fn = async (base, nowIso) => {
const v = await (await fetch(base + '/api/promodays')).json(); assert.ok(v.live && v.today.pct === 30 && v.today.minUsd === 20); ok('Cyber Monday +30% $20+');
const { pg, ctx } = await page(base, nowIso, '/weekend'); assert.match(await pg.textContent('.nw-ribbon'), /Cyber Monday[\s\S]*\+30% bonus ad credits on any \$20\+/); ok('ribbon Cyber Monday text'); await ctx.close();
};
await run('2026-11-30T20:00:00Z', 8814);
fn = async (base, nowIso) => {
const v = await (await fetch(base + '/api/promodays')).json(); assert.ok(!v.live && v.today === null && v.passBonusLive === false); ok('Tue 12-01: weekend off; no pass days shown while sovTiers is off');
const { pg, ctx } = await page(base, nowIso, '/'); assert.strictEqual(await pg.locator('.nw-ribbon').count(), 0); assert.strictEqual(await pg.locator('#nwStrip:not([hidden])').count(), 0); ok('Tue: no ribbon, no strip'); await ctx.close();
};
await run('2026-12-01T15:00:00Z', 8815);
await browser.close();
console.log('ALL OK weekend e2e (' + out.length + ' checks)');
})().catch(e => { console.error('FAIL', e.message); process.exit(1); });