|
| 1 | +// #3359: the paused-chat card (#404) reads `social/paused` every 5 s through `pollWhileVisible`, and |
| 2 | +// its rejection branch was an empty function — so a read that failed printed nothing, which is drawn |
| 3 | +// exactly like "this chat is not paused": the card disappeared when it had been there, and a card read |
| 4 | +// before a failure stayed on screen saying "Send your message again" from an answer that no longer |
| 5 | +// succeeds. The failure is the card's answer while it lasts (#3323/#3325 fixed the same gap for the |
| 6 | +// friends list), and the 5 s poll is the retry — still quiet, still no buttons (owner 2026-10-19). |
| 7 | +// |
| 8 | +// The harness is the one `poll-visibility.test.js` uses: the client is a classic script, react is the |
| 9 | +// tiny hook runner the other client tests use, and `setInterval` / `visibilitychange` are recorded |
| 10 | +// instead of waited on (no jsdom). `rerender()` reuses the hook table and does not re-run effects. |
| 11 | +import assert from 'node:assert/strict' |
| 12 | +import { readFileSync } from 'node:fs' |
| 13 | +import { test } from 'node:test' |
| 14 | + |
| 15 | +const live = { inst: null, idx: 0 } |
| 16 | +const reactStub = { |
| 17 | + createElement: (type, props, ...children) => ({ type, props: props ?? {}, children: children.flat(Infinity) }), |
| 18 | + Fragment: 'F', |
| 19 | + useState(init) { |
| 20 | + const inst = live.inst |
| 21 | + const i = live.idx++ |
| 22 | + if (!(i in inst.hooks)) inst.hooks[i] = { value: typeof init === 'function' ? init() : init } |
| 23 | + const slot = inst.hooks[i] |
| 24 | + return [slot.value, next => { slot.value = typeof next === 'function' ? next(slot.value) : next; inst.dirty = true }] |
| 25 | + }, |
| 26 | + useEffect(fn, deps) { |
| 27 | + live.inst.effects.push([live.idx++, fn, deps]) |
| 28 | + }, |
| 29 | + useRef(value) { |
| 30 | + const inst = live.inst |
| 31 | + const i = live.idx++ |
| 32 | + if (!(i in inst.hooks)) inst.hooks[i] = { current: value } |
| 33 | + return inst.hooks[i] |
| 34 | + }, |
| 35 | + useMemo(fn) { live.idx++; return fn() }, |
| 36 | + useCallback(fn) { live.idx++; return fn }, |
| 37 | +} |
| 38 | + |
| 39 | +// The browser bundle registers its factory under its package name; load it the way the other client |
| 40 | +// tests do and hand the factory the hook stub above. |
| 41 | +function loadApp() { |
| 42 | + const modules = new Map() |
| 43 | + const window = { __ModuleLoader__: { load: entry => modules.set(entry.id, entry.factory) } } |
| 44 | + new Function('window', readFileSync(new URL('../client.js', import.meta.url), 'utf8'))(window) |
| 45 | + return modules.get('@flybot/social')(name => (name === 'react' ? reactStub : {})) |
| 46 | +} |
| 47 | + |
| 48 | +// A document whose `hidden` can change and whose `visibilitychange` listeners the test fires, and a |
| 49 | +// timer table the test advances — both installed as globals for the duration of a case. |
| 50 | +function makeDocument({ hidden = false } = {}) { |
| 51 | + const listeners = new Map() |
| 52 | + return { |
| 53 | + hidden, |
| 54 | + getElementById: () => null, |
| 55 | + createElement: () => ({}), |
| 56 | + head: { appendChild() {} }, |
| 57 | + addEventListener(type, fn) { |
| 58 | + const set = listeners.get(type) ?? new Set() |
| 59 | + set.add(fn) |
| 60 | + listeners.set(type, set) |
| 61 | + }, |
| 62 | + removeEventListener(type, fn) { listeners.get(type)?.delete(fn) }, |
| 63 | + listening: type => (listeners.get(type) ?? new Set()).size, |
| 64 | + emit(type) { for (const fn of [...(listeners.get(type) ?? [])]) fn() }, |
| 65 | + } |
| 66 | +} |
| 67 | +function makeTimers() { |
| 68 | + let next = 0 |
| 69 | + const timers = new Map() |
| 70 | + return { |
| 71 | + setInterval(fn, ms) { const id = `t${++next}`; timers.set(id, { fn, ms }); return id }, |
| 72 | + clearInterval(id) { timers.delete(id) }, |
| 73 | + periods: () => [...timers.values()].map(timer => timer.ms), |
| 74 | + tick() { for (const timer of [...timers.values()]) timer.fn() }, |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// One case: a fresh document, timer table, answer queue and module instance. Each `call` takes the next |
| 79 | +// queued answer; an `Error` in the queue is a rejection. `settle()` lets the promise chain land before |
| 80 | +// the next render, so a case never reads a half-updated card. |
| 81 | +function scene({ hidden = false, answers = [] } = {}) { |
| 82 | + const doc = makeDocument({ hidden }) |
| 83 | + const timers = makeTimers() |
| 84 | + const calls = [] |
| 85 | + const app = loadApp() |
| 86 | + const saved = { |
| 87 | + document: globalThis.document, |
| 88 | + setInterval: globalThis.setInterval, |
| 89 | + clearInterval: globalThis.clearInterval, |
| 90 | + flybot: globalThis.__flybot__, |
| 91 | + } |
| 92 | + let queued = [...answers] |
| 93 | + globalThis.document = doc |
| 94 | + globalThis.setInterval = timers.setInterval |
| 95 | + globalThis.clearInterval = timers.clearInterval |
| 96 | + globalThis.__flybot__ = { |
| 97 | + call: (endpoint, payload) => { |
| 98 | + calls.push([endpoint, payload]) |
| 99 | + const answer = queued.length > 0 ? queued.shift() : {} |
| 100 | + return answer instanceof Error ? Promise.reject(answer) : Promise.resolve(answer) |
| 101 | + }, |
| 102 | + canRetry: () => true, |
| 103 | + } |
| 104 | + return { |
| 105 | + doc, timers, calls, app, |
| 106 | + restore() { |
| 107 | + globalThis.document = saved.document |
| 108 | + globalThis.setInterval = saved.setInterval |
| 109 | + globalThis.clearInterval = saved.clearInterval |
| 110 | + globalThis.__flybot__ = saved.flybot |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | +const settle = () => new Promise(resolve => setTimeout(resolve, 0)) |
| 115 | +const unmount = cleanups => { for (const off of cleanups.reverse()) off() } |
| 116 | + |
| 117 | +// React would run the first-mount effects after drawing; here that is the call site's own immediate |
| 118 | +// read plus its timer, which is exactly what the cases below observe. |
| 119 | +function mount(app, name, props) { |
| 120 | + live.inst = { hooks: {}, effects: [], dirty: false } |
| 121 | + live.idx = 0 |
| 122 | + const out = app[name](props) |
| 123 | + const cleanups = [] |
| 124 | + for (const [, fn] of live.inst.effects) { |
| 125 | + const off = fn() |
| 126 | + if (typeof off === 'function') cleanups.push(off) |
| 127 | + } |
| 128 | + return { out, cleanups } |
| 129 | +} |
| 130 | +// A state update asks React for a new render; this reuses the hook table and runs no effect again. |
| 131 | +function rerender(app, name, props) { |
| 132 | + live.idx = 0 |
| 133 | + return app[name](props) |
| 134 | +} |
| 135 | +// Every element in a drawn tree, depth first, for counting what a state shows. |
| 136 | +function nodes(node, out = []) { |
| 137 | + if (node == null || node === false || node === true) return out |
| 138 | + if (typeof node === 'string' || typeof node === 'number') return out |
| 139 | + if (Array.isArray(node)) { for (const child of node) nodes(child, out); return out } |
| 140 | + if (typeof node !== 'object') return out |
| 141 | + out.push(node) |
| 142 | + nodes(node.children ?? [], out) |
| 143 | + return out |
| 144 | +} |
| 145 | +const byMark = (tree, mark) => nodes(tree).filter(node => node?.props?.['data-flybot'] === mark) |
| 146 | +const textOf = node => (Array.isArray(node?.children) ? node.children : [node?.children]).join('') |
| 147 | + |
| 148 | +const friendBot = { id: 'f1', name: 'Pal', remote: { handle: '@pal.home' } } |
| 149 | +const pausedAnswer = { paused: { reason: 'loop detected' } } |
| 150 | +const pausedChat = app => mount(app, 'PausedChat', { bot: friendBot }) |
| 151 | + |
| 152 | +test('a paused-chat read that fails writes one quiet error row', async () => { |
| 153 | + const s = scene({ answers: [new Error('offline')] }) |
| 154 | + try { |
| 155 | + const { cleanups, out } = pausedChat(s.app) |
| 156 | + await settle() |
| 157 | + const tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 158 | + assert.equal(byMark(tree, 'social-paused-error').length, 1, 'the failed read is the card’s answer') |
| 159 | + unmount(cleanups) |
| 160 | + } finally { s.restore() } |
| 161 | +}) |
| 162 | + |
| 163 | +test('a NETWORK failure names the reason the users can act on', async () => { |
| 164 | + const s = scene({ answers: [Object.assign(new Error('offline'), { code: 'NETWORK' })] }) |
| 165 | + try { |
| 166 | + const { cleanups, out } = pausedChat(s.app) |
| 167 | + await settle() |
| 168 | + const tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 169 | + assert.equal(textOf(byMark(tree, 'social-paused-error')[0]), 'Could not reach FlyBot. Check that it is still running.') |
| 170 | + unmount(cleanups) |
| 171 | + } finally { s.restore() } |
| 172 | +}) |
| 173 | + |
| 174 | +test('a card read before the failure is dropped when the next read fails', async () => { |
| 175 | + const s = scene({ answers: [pausedAnswer, new Error('offline')] }) |
| 176 | + try { |
| 177 | + const { cleanups, out } = pausedChat(s.app) |
| 178 | + await settle() |
| 179 | + let tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 180 | + assert.equal(byMark(tree, 'chat-paused').length, 1, 'the first answer draws the card') |
| 181 | + s.timers.tick() |
| 182 | + await settle() |
| 183 | + tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 184 | + assert.equal(byMark(tree, 'chat-paused').length, 0, 'a stale card is not an answer') |
| 185 | + assert.equal(byMark(tree, 'social-paused-error').length, 1, 'the failure replaces it') |
| 186 | + unmount(cleanups) |
| 187 | + } finally { s.restore() } |
| 188 | +}) |
| 189 | + |
| 190 | +test('the next read that succeeds clears the error and draws the card again', async () => { |
| 191 | + const s = scene({ answers: [new Error('offline'), pausedAnswer] }) |
| 192 | + try { |
| 193 | + const { cleanups, out } = pausedChat(s.app) |
| 194 | + await settle() |
| 195 | + let tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 196 | + assert.equal(byMark(tree, 'social-paused-error').length, 1) |
| 197 | + s.timers.tick() |
| 198 | + await settle() |
| 199 | + tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 200 | + assert.equal(byMark(tree, 'chat-paused').length, 1, 'a later success draws the card') |
| 201 | + assert.equal(byMark(tree, 'social-paused-error').length, 0, 'and drops the error') |
| 202 | + unmount(cleanups) |
| 203 | + } finally { s.restore() } |
| 204 | +}) |
| 205 | + |
| 206 | +test('the failed read draws no button, input or form', async () => { |
| 207 | + const s = scene({ answers: [new Error('offline')] }) |
| 208 | + try { |
| 209 | + const { cleanups, out } = pausedChat(s.app) |
| 210 | + await settle() |
| 211 | + const tree = rerender(s.app, 'PausedChat', { bot: friendBot }, out) |
| 212 | + const interactive = nodes(tree).filter(node => node?.type === 'button' || node?.type === 'input' || node?.type === 'form') |
| 213 | + assert.equal(interactive.length, 0, 'the quiet notice is all this state draws (owner 2026-10-19)') |
| 214 | + unmount(cleanups) |
| 215 | + } finally { s.restore() } |
| 216 | +}) |
0 commit comments