aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-09-10 23:09:14 -0700
committerAdam Soltys <[email protected]>2026-09-10 23:09:14 -0700
commit7539975beed48ed7aa9c35495d34f5b8b547cc93 (patch)
treed92aaea48ac7e3f4d0a5a8363602ef22c164e895 /tools
parent48e8e588f1a3dee26a2497d1a1757f2f294413a3 (diff)
downloadcoinosv3-7539975beed48ed7aa9c35495d34f5b8b547cc93.tar.gz
coinosv3-7539975beed48ed7aa9c35495d34f5b8b547cc93.zip
A transient failure shouldn't cost a face its thumbnail for good.
Pinguimdarelva's picture is 52KB on a CDN that answers in 700ms one minute and times out the next — I measured both from this machine. The thumbnail attempt hit one of the bad minutes, and `thumbFail` was written as if the host had refused us: never retried, so that avatar went back to fetching the original on every boot. Only a tainted canvas is a real refusal (no CORS header, nothing to be done), and that is remembered for good. Everything else — a load error, or a host that won't answer inside twenty seconds — is stamped with the time and tried again in six hours. The timeout also frees the slot: three thumbnails run at a time, and a hanging image used to hold one of them indefinitely. And zap tallies are remembered between sessions. They were asked of the relays on every boot, which takes seconds, so a room full of zapped messages painted bare and filled in afterwards. The totals (never the receipts) are kept locally and painted straight away; the moment the relays answer for a message, their word replaces it — so a tally that changed, or a receipt that was deleted, still settles correctly. Opening a DM thread doesn't forget what the chat had learned, and the store is capped at 400. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_019mrt228Upn9nNCwGixzSLx
Diffstat (limited to 'tools')
-rw-r--r--tools/zap-tally-cache-test.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/tools/zap-tally-cache-test.js b/tools/zap-tally-cache-test.js
new file mode 100644
index 0000000..98cfe7e
--- /dev/null
+++ b/tools/zap-tally-cache-test.js
@@ -0,0 +1,69 @@
+// Zap tallies are asked of the relays on every boot, which takes seconds. The
+// last known totals are remembered locally so a room of zapped messages
+// paints its chips straight away — and the relays' answer replaces them, so a
+// tally that changed (or a receipt that was deleted) still settles correctly.
+//
+// Run: bun tools/zap-tally-cache-test.js
+import puppeteer from 'puppeteer-core';
+import { generateMnemonic } from '@scure/bip39';
+import { wordlist } from '@scure/bip39/wordlists/english';
+import { buildHtml } from '../build.js';
+import { cacheKeyFor } from '../src/wallet.js';
+const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
+let ok = true;
+const check = (n, c, d = '') => { console.log(` ${c ? '✓' : '✗'} ${n}${d ? ' — ' + d : ''}`); if (!c) ok = false; };
+const html = await buildHtml({ minify: true, pwa: false });
+const server = Bun.serve({ port: 5236, fetch: () => new Response(html, { headers: { 'content-type': 'text/html' } }) });
+const browser = await puppeteer.launch({ executablePath: '/usr/bin/google-chrome', headless: 'new', args: ['--no-sandbox'] });
+const page = await browser.newPage();
+const click = (t) => page.evaluate((x) => { const e = [...document.querySelectorAll('button')].find((n) => n.textContent.trim().toLowerCase().includes(x)); if (e) { e.click(); return true; } return false; }, t);
+const waitText = async (x, ms = 25000) => { for (let i = 0; i < ms / 250; i++) { if ((await page.evaluate(() => document.body.innerText)).toLowerCase().includes(x)) return true; await sleep(250); } return false; };
+try {
+ await page.setViewport({ width: 390, height: 844 });
+ await page.goto('http://localhost:5236/', { waitUntil: 'domcontentloaded' });
+ await page.evaluate(() => localStorage.setItem('btc-wallet-network', 'regtest'));
+ await page.reload({ waitUntil: 'domcontentloaded' });
+ await sleep(400);
+ await click('get started'); await sleep(500);
+ await click('import existing'); await sleep(400);
+ await page.waitForSelector('textarea');
+ const mnemonic = generateMnemonic(wordlist);
+ await page.type('textarea', mnemonic);
+ await click('open wallet');
+ await waitText('receive', 20000);
+
+ // the feature state slot this wallet reads (seed-keyed, as loadFeatureState does)
+ const key = cacheKeyFor(mnemonic + '\n') + ':messages';
+
+ // the shape the chat paints from: remembered totals keyed by event id
+ const painted = await page.evaluate((k) => {
+ const s = JSON.parse(localStorage.getItem(k) || '{}');
+ s.zaps = { ['a'.repeat(64)]: { s: 2100, m: 1 }, ['b'.repeat(64)]: { s: 21 } };
+ localStorage.setItem(k, JSON.stringify(s));
+ return Object.keys(s.zaps).length;
+ }, key);
+ check('seeded two remembered tallies', painted === 2);
+
+ await page.reload({ waitUntil: 'domcontentloaded' });
+ await waitText('receive', 20000);
+ // open the chat — the screen that rewrites this state, and where a stale
+ // copy being saved back would quietly drop every remembered tally
+ await page.evaluate(() => { const b = [...document.querySelectorAll('button')].find((e) => /message/i.test(e.getAttribute('aria-label') || '')); if (b) b.click(); });
+ await sleep(2500);
+ const kept = await page.evaluate((k) => {
+ const s = JSON.parse(localStorage.getItem(k) || '{}');
+ return Object.keys(s.zaps || {}).length;
+ }, key);
+ check('they survive a refresh and a trip through chat', kept === 2, kept + ' kept');
+
+ // and they are readable as tallies, not re-fetched from scratch: the
+ // seeded totals are what a chip would paint before any relay answers
+ const readable = await page.evaluate((k) => {
+ const s = JSON.parse(localStorage.getItem(k) || '{}');
+ const v = s.zaps[Object.keys(s.zaps)[0]];
+ return v && typeof v.s === 'number' && v.s > 0;
+ }, key);
+ check('a remembered tally carries its sats', readable);
+} finally { await browser.close(); server.stop(true); }
+console.log(ok ? '\n✅ tallies survive a refresh' : '\n❌ failed');
+process.exit(ok ? 0 : 1);