From 5a7893e6dab14590c8f7cd55e65ab1f85d9bb053 Mon Sep 17 00:00:00 2001 From: Reynaldi Chernando Date: Tue, 17 Nov 2026 21:26:06 +0700 Subject: [PATCH] auto topup welcome info --- scripts/test-whats-new.mjs | 147 ++++++++++++++++++++++++++++++++----- src/css/styles.css | 120 +++++++++++++++++++++++++++++- src/js/app.js | 4 +- src/js/whats-new.js | 135 +++++++++++++++++++++++++++------- 4 files changed, 359 insertions(+), 47 deletions(-) diff --git a/scripts/test-whats-new.mjs b/scripts/test-whats-new.mjs index 4605bad..b185d4d 100644 --- a/scripts/test-whats-new.mjs +++ b/scripts/test-whats-new.mjs @@ -2,14 +2,16 @@ import fs from 'node:fs'; import vm from 'node:vm'; // ---- The What's new dialog ------------------------------------------------------ -// js/whats-new.js tells a returning user about the newest feature, once. It -// shows at startup to a signed-in account with at least one project, never over -// a build or another dialog. Show me closes it and points at the feature -// (fast mode's bolt button), Later just closes it, and closing it in any way -// saves the announcement's start date in puter.kv, under one key per user that -// a later announcement overwrites. Runs the real whats-new.js with a small -// fake jQuery, kv and FastMode, then checks the startup wiring in app.js and -// the bundle list. +// js/whats-new.js tells a returning user about recent features, one per load, +// each once. It shows at startup to a signed-in account with at least one +// project, never over a build or another dialog. Show me closes it and points +// at the feature (fast mode's bolt button), auto top-up's button opens Puter's +// billing page in a new tab, Later just closes it, and closing it in any way +// saves the announcement's start date in puter.kv, under one key per user. A +// load shows the oldest announcement newer than that, running today and for +// the account (auto top-up only for an account that buys its own credit). +// Runs the real whats-new.js with a small fake jQuery, kv, FastMode and clock, +// then checks the startup wiring in app.js and the bundle list. let failures = 0; function check(name, cond, detail) { @@ -21,10 +23,22 @@ const source = read('../src/js/whats-new.js'); const tick = () => new Promise((r) => setTimeout(r, 0)); const KV_KEY = 'whats_new_seen'; -const START = (source.match(/start: '(\d{4}-\d{2}-\d{2})'/) || [])[1]; -check('the announcement has a YYYY-MM-DD start', !!START); +const STARTS = [...source.matchAll(/^\s*start: '(\d{4}-\d{2}-\d{2})',$/gm)].map((m) => m[1]); +const ENDS = [...source.matchAll(/^\s*end: '(\d{4}-\d{2}-\d{2})',$/gm)].map((m) => m[1]); +const IDS = [...source.matchAll(/^\s*id: '([a-z-]+)',$/gm)].map((m) => m[1]); +check('the announcements are fast mode then auto top-up', IDS.join() === 'fast-mode,auto-top-up', IDS.join()); +check('each has a YYYY-MM-DD start and end, the end on or after the start', + STARTS.length === IDS.length && ENDS.length === IDS.length && STARTS.every((d, i) => ENDS[i] >= d)); +check('… and each starts later than the one before (kv keeps only the start)', + STARTS.every((d, i) => i === 0 || d > STARTS[i - 1]), STARTS.join()); +const START = STARTS[0]; +const TOPUP = STARTS[1]; +// The next day, as YYYY-MM-DD. +const dayAfter = (d) => { const t = new Date(d + 'T12:00:00'); t.setDate(t.getDate() + 1); return t.toISOString().slice(0, 10); }; +// Noon on the day auto top-up went out, when both are running. +const NOW = TOPUP + 'T12:00:00'; -function setup({ user = { uuid: 'u1', username: 'ann' }, paid = false, fast = true, kv = {}, kvFails = false, kvSetFails = false, otherDialog = false } = {}) { +function setup({ user = { uuid: 'u1', username: 'ann' }, paid = false, topUp = paid, fast = true, now = NOW, kv = {}, kvFails = false, kvSetFails = false, otherDialog = false } = {}) { const calls = { kvGet: [], kvSet: [], kvDel: [], track: [], released: 0, warn: [], showMe: [] }; const kvStore = new Map(Object.entries(kv)); const overlays = []; @@ -67,7 +81,9 @@ function setup({ user = { uuid: 'u1', username: 'ann' }, paid = false, fast = tr }; const env = { console: { ...console, warn: (...a) => calls.warn.push(a.join(' ')) }, - Date, Promise, $, document, + // The clock reads now, so the tests don't depend on the day they run. + Date: class extends Date { constructor(...a) { super(...(a.length ? a : [now])); } static now() { return new Date(now).getTime(); } }, + Promise, $, document, setTimeout: (fn) => fn(), requestAnimationFrame: (fn) => fn(), puter: { @@ -82,7 +98,8 @@ function setup({ user = { uuid: 'u1', username: 'ann' }, paid = false, fast = tr }; env.window = env; env.user = user ? { ...user } : null; - env.Branding = { isSubscribed: () => paid }; + // A team seat is paid but can't buy credit (topUp false). + env.Branding = { isSubscribed: () => paid, canTopUp: () => topUp }; // Show me records whether the dialog was still up when it ran. env.FastMode = { SPEED: '2.5x', COST: '2x', @@ -300,15 +317,111 @@ for (const junk of [1730000000000, true, { at: 1 }, '']) { !t.kvStore.has(KV_KEY) && (await t.W.maybeShow({ hasProjects: true })) === true); } -// --- Copy ---------------------------------------------------------------------------- +// --- Auto top-up ----------------------------------------------------------------------- { - const t = setup(); + const t = setup({ paid: true }); + check('a paid account that missed fast mode sees fast mode first', + (await t.W.maybeShow({ hasProjects: true })) === true && /Build faster with fast mode/.test(t.shown()[0].html)); + t.fire('.whats-new-later click'); + await tick(); + check('… and the next load, auto top-up', (await t.W.maybeShow({ hasProjects: true })) === true + && /Keep building with auto top-up/.test(t.shown()[0].html) + && t.calls.track.some((c) => c.name === 'Whats New Shown' && c.id === 'auto-top-up')); + + const html = t.shown()[0].html; + check('its main button is a link to Puter’s billing page, opened in a new tab', + /Turn on auto top-up<\/a>/.test(html)); + check('… after Later, with ✕, and no Show me', + (html.match(/`; return `
- +
- + ${go}
@@ -134,9 +204,8 @@ // The open dialog's close, for hide(). let closeOpen = null; - function open(a = ANNOUNCEMENT) { + function show(a) { if (document.querySelector('.whats-new-overlay')) return false; - if (a.when && !a.when()) return false; const $overlay = $(modalHtml(a, !!window.Branding?.isSubscribed(window.user))); let releaseFocus = null; let closed = false; @@ -149,7 +218,8 @@ setTimeout(() => { $overlay.remove(); // Once the dialog is gone, so it doesn't cover what it shows. - if (how === 'show') a.showMe(); + // An href opened in its new tab on the click. + if (how === 'show' && a.showMe) a.showMe(); }, 150); if (releaseFocus) releaseFocus(); // Gave way to another dialog, so not seen. @@ -176,6 +246,17 @@ return true; } + // From the console. The newest announcement this account gets, or the one + // with this id, whatever was seen and whatever the date. Not one the + // account doesn't get, since its button would lead nowhere. + function open(id) { + const a = id + ? ANNOUNCEMENTS.find((x) => x.id === id) + : [...ANNOUNCEMENTS].reverse().find((x) => !x.when || x.when()); + if (!a || (a.when && !a.when())) return false; + return show(a); + } + // Called by a dialog that comes first (the upgrade dialog). Closes this // one without marking it seen. function hide() { @@ -186,5 +267,5 @@ await puter.kv.del(KV_KEY); } - window.WhatsNew = { maybeShow, open: () => open(ANNOUNCEMENT), hide, reset }; + window.WhatsNew = { maybeShow, open, hide, reset }; })();