// Network Weekend checks (Marty 2026-11-17): node qa/weekend.test.mjs. No network: a stand-in Polygon RPC for skus(). // Every date gate runs on the WEEKEND_NOW test clock, never the machine clock. import http from 'node:http'; import os from 'node:os'; import fs from 'node:fs'; import path from 'node:path'; import { createRequire } from 'node:module'; const require = createRequire(import.meta.url); const { Interface } = require('ethers'); let pass = 0, fail = 0; const ok = (c, m) => { if (c) pass++; else { fail++; console.log('FAIL', m); } }; process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'vbbwk-')); delete process.env.DOCSTORE_URL; process.env.NETKB = '0'; delete process.env.OPENROUTER_API_KEY; const I = new Interface(['function skus(uint256) view returns (uint64 usdCents, uint64 units, bool active)']); const SKUS = { 1: [300, 10, true], 2: [1000, 40, true], 3: [200, 0, true] }; let down = false; const rpc = http.createServer((q, r) => { let b = ''; q.on('data', c => { b += c; }); q.on('end', () => { const j = JSON.parse(b); if (down) { r.writeHead(500); return r.end('x'); } const sku = Number(I.decodeFunctionData('skus', j.params[0].data)[0]); r.setHeader('Content-Type', 'application/json'); r.end(JSON.stringify({ jsonrpc: '2.0', id: j.id, result: I.encodeFunctionResult('skus', SKUS[sku] || [0, 0, false]) })); }); }); await new Promise(r => rpc.listen(0, r)); process.env.CHAIN_RPC = 'http://127.0.0.1:' + rpc.address().port; process.env.CONTRACT_ADDRESS = '0x00000000000000000000000000000000000B0B05'; const C = require('../config.js').get(); const weekend = require('../weekend.js'); const chainskus = require('../chainskus.js'); const netdeals = require('../netdeals.js'); const at = iso => { process.env.WEEKEND_NOW = iso; }; // 1. the date gate, exact Central midnights both ways for (const [iso, want] of [['2026-11-26T18:00:00-06:00', false], ['2026-11-26T23:59:59-06:00', false], ['2026-11-27T00:00:00-06:00', true], ['2026-11-28T12:00:00-06:00', true], ['2026-11-30T23:59:59-06:00', true], ['2026-12-01T00:00:00-06:00', false], ['2026-12-02T09:00:00-06:00', false]]) { at(iso); ok(weekend.on() === want, 'on() at ' + iso + ' = ' + want); ok(weekend.freePerDay(1) === (want ? 2 : 1), 'free credits at ' + iso); } ok(C.freeCreditsPerDay === 1, 'the setting stays 1 (FREE_CREDITS_PER_DAY untouched)'); // 2. live SKU values: env until the first read, then the chain; RPC down keeps the last good read chainskus.init(C, [1, 2, 3]); ok(chainskus.packs(C)[1].credits === 40, 'env 40 before/at first read'); await chainskus.refresh(); ok(chainskus.packs(C)[1].credits === 40 && chainskus.packs(C)[1].usd === 10, 'chain 40 for $10'); SKUS[2] = [1000, 50, true]; await chainskus.refresh(); ok(chainskus.packs(C)[1].credits === 50, 'owner set 50: pages show 50'); ok(chainskus.packs(C)[0].credits === 10 && chainskus.packs(C)[0].usd === 3, '$3 pack untouched'); down = true; await chainskus.refresh(); ok(chainskus.packs(C)[1].credits === 50, 'RPC down: last good read stays'); down = false; SKUS[2] = [1000, 40, false]; await chainskus.refresh(); ok(chainskus.packs(C)[1].credits === 40, 'inactive SKU on chain: env values'); // 3. the ribbon line follows the chain and the clock; nothing wrong if the signature is late const deal = { site: 'vbb', name: 'Viral Banner Blitz', text: 'hub text', byDay: { x: 'y' } }; SKUS[2] = [1000, 40, true]; await chainskus.refresh(); at('2026-11-20T12:00:00-06:00'); ok(weekend.dealLine(C, deal) === null, 'before: no VBB line'); at('2026-11-27T00:05:00-06:00'); let d = weekend.dealLine(C, deal); ok(d && d.text === 'Network members get 2 free post credits a day.' && !d.byDay, 'late signature: free credits only, no 50: ' + (d && d.text)); SKUS[2] = [1000, 50, true]; await chainskus.refresh(); d = weekend.dealLine(C, deal); ok(d && d.text === 'The $10 pack carries 50 credits instead of 40, and network members get 2 free post credits a day.', 'signed: ' + (d && d.text)); at('2026-12-01T00:00:00-06:00'); d = weekend.dealLine(C, deal); ok(d && /50 credits/.test(d.text) && !/free/.test(d.text), 'revert not signed yet Tuesday: the line says what the chain charges'); netdeals.setLocal(x => weekend.dealLine(C, x)); netdeals._set({ events: [{ id: 'e', name: 'Network Weekend', startsAt: '2026-11-27T06:00:00.000Z', endsAt: '2026-12-01T06:00:00.000Z', deals: [deal, { site: 'dlb', name: 'DLB', text: 't' }] }] }); let body = ''; const res = { writeHead() {}, end(s) { body = s; } }; SKUS[2] = [1000, 40, true]; await chainskus.refresh(); at('2026-11-20T00:00:00-06:00'); netdeals.handle({}, res, 'vbb'); ok(JSON.parse(body).events[0].deals.map(x => x.site).join() === 'dlb', 'feed: VBB line dropped when there is nothing true to say'); // 4. chatbot: live packs and the weekend fact only inside the window const bot = require('../chatbot.js'); at('2026-11-20T12:00:00-06:00'); ok(!/NETWORK WEEKEND/.test(bot.FACTS()), 'no weekend fact before'); ok(/1 free post credit each day/.test(bot.FACTS()), 'normal free credit fact'); SKUS[2] = [1000, 50, true]; await chainskus.refresh(); at('2026-11-28T09:00:00-06:00'); const F = bot.FACTS(); ok(/NETWORK WEEKEND/.test(F) && /50 credits instead of 40/.test(F) && /2 free post credits/.test(F), 'weekend fact from live values'); ok(/40 post credits for \$10/.test(bot.render('x 10 post credits for $3 or 40 post credits for $10 y')) === false && /50 post credits for \$10/.test(bot.render('10 post credits for $3 or 40 post credits for $10')), 'canned packs rendered live'); ok(/get 2 free post credits each day they sign in during Network Weekend/.test(bot.render('Network members get one free post credit each day they sign in.')), 'canned free credit rendered for the weekend'); at('2026-12-01T00:00:01-06:00'); ok(bot.render('get one free post credit each day they sign in') === 'get one free post credit each day they sign in', 'after: canned free credit unchanged'); ok(!/\u2014/.test(F) && !/up to/i.test(F.split('\n').find(l => /WEEKEND/.test(l))), 'no em dashes, no up-to in the weekend fact'); rpc.close(); console.log('weekend: ' + pass + ' passed, ' + fail + ' failed'); process.exit(fail ? 1 : 0);