From 338304df01506e32cbe9baee842b63e16fe6cc02 Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:22:25 +0800 Subject: [PATCH 1/2] test: a failed Auto-send settings read is drawn as the setting being off (#3367) --- .../test/friend-auto-read-error.test.js | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 plugins/social/test/friend-auto-read-error.test.js diff --git a/plugins/social/test/friend-auto-read-error.test.js b/plugins/social/test/friend-auto-read-error.test.js new file mode 100644 index 000000000..b5c514c2d --- /dev/null +++ b/plugins/social/test/friend-auto-read-error.test.js @@ -0,0 +1,208 @@ +// A friend Bot's settings card reads the pair's Auto-send setting once, on mount, through +// `social/settings`. Its rejection branch was an empty function, so a read that failed left the +// checkbox at its initial `false` — drawn exactly like a setting that is off — while the switch +// stayed live; the person then reads "my Bots' replies to Eve go out without asking" as the state +// they chose. The failure is the card's answer while it lasts (#3323/#3325/#3359 fixed the same gap +// for the friends list, the paused card and the approval cards): one quiet status row, and the switch +// is not offered until the value is known, because a value nobody read is not a value to flip. A +// remount (reopen the card) retries; no button, no new copy. +// +// The harness is the one `poll-visibility.test.js` uses: the client is a classic script, react is the +// tiny hook runner the other client tests use, and `setInterval` / `visibilitychange` are recorded +// instead of waited on (no jsdom). `rerender()` reuses the hook table and does not re-run effects. +import assert from 'node:assert/strict' +import { readFileSync } from 'node:fs' +import { test } from 'node:test' + +const live = { inst: null, idx: 0 } +const reactStub = { + createElement: (type, props, ...children) => ({ type, props: props ?? {}, children: children.flat(Infinity) }), + Fragment: 'F', + useState(init) { + const inst = live.inst + const i = live.idx++ + if (!(i in inst.hooks)) inst.hooks[i] = { value: typeof init === 'function' ? init() : init } + const slot = inst.hooks[i] + return [slot.value, next => { slot.value = typeof next === 'function' ? next(slot.value) : next; inst.dirty = true }] + }, + useEffect(fn, deps) { + live.inst.effects.push([live.idx++, fn, deps]) + }, + useRef(value) { + const inst = live.inst + const i = live.idx++ + if (!(i in inst.hooks)) inst.hooks[i] = { current: value } + return inst.hooks[i] + }, + useMemo(fn) { live.idx++; return fn() }, + useCallback(fn) { live.idx++; return fn }, +} + +// The browser bundle registers its factory under its package name; load it the way the other client +// tests do and hand the factory the hook stub above. +function loadApp() { + const modules = new Map() + const window = { __ModuleLoader__: { load: entry => modules.set(entry.id, entry.factory) } } + new Function('window', readFileSync(new URL('../client.js', import.meta.url), 'utf8'))(window) + return modules.get('@flybot/social')(name => (name === 'react' ? reactStub : {})) +} + +// A document whose `hidden` can change and whose `visibilitychange` listeners the test fires, and a +// timer table the test advances — both installed as globals for the duration of a case. +function makeDocument({ hidden = false } = {}) { + const listeners = new Map() + return { + hidden, + getElementById: () => null, + createElement: () => ({}), + head: { appendChild() {} }, + addEventListener(type, fn) { + const set = listeners.get(type) ?? new Set() + set.add(fn) + listeners.set(type, set) + }, + removeEventListener(type, fn) { listeners.get(type)?.delete(fn) }, + listening: type => (listeners.get(type) ?? new Set()).size, + emit(type) { for (const fn of [...(listeners.get(type) ?? [])]) fn() }, + } +} +function makeTimers() { + let next = 0 + const timers = new Map() + return { + setInterval(fn, ms) { const id = `t${++next}`; timers.set(id, { fn, ms }); return id }, + clearInterval(id) { timers.delete(id) }, + periods: () => [...timers.values()].map(timer => timer.ms), + tick() { for (const timer of [...timers.values()]) timer.fn() }, + } +} + +// One case: a fresh document, timer table, answer queue and module instance. Each `call` takes the next +// queued answer; an `Error` in the queue is a rejection. `settle()` lets the promise chain land before +// the next render, so a case never reads a half-updated card. +function scene({ hidden = false, answers = [] } = {}) { + const doc = makeDocument({ hidden }) + const timers = makeTimers() + const calls = [] + const app = loadApp() + const saved = { + document: globalThis.document, + setInterval: globalThis.setInterval, + clearInterval: globalThis.clearInterval, + flybot: globalThis.__flybot__, + } + let queued = [...answers] + globalThis.document = doc + globalThis.setInterval = timers.setInterval + globalThis.clearInterval = timers.clearInterval + globalThis.__flybot__ = { + call: (endpoint, payload) => { + calls.push([endpoint, payload]) + const answer = queued.length > 0 ? queued.shift() : {} + return answer instanceof Error ? Promise.reject(answer) : Promise.resolve(answer) + }, + canRetry: () => true, + } + return { + doc, timers, calls, app, + restore() { + globalThis.document = saved.document + globalThis.setInterval = saved.setInterval + globalThis.clearInterval = saved.clearInterval + globalThis.__flybot__ = saved.flybot + }, + } +} +const settle = () => new Promise(resolve => setTimeout(resolve, 0)) +const unmount = cleanups => { for (const off of cleanups.reverse()) off() } + +// React would run the first-mount effects after drawing; here that is the call site's own immediate +// read plus its timer, which is exactly what the cases below observe. +function mount(app, name, props) { + live.inst = { hooks: {}, effects: [], dirty: false } + live.idx = 0 + const out = app[name](props) + const cleanups = [] + for (const [, fn] of live.inst.effects) { + const off = fn() + if (typeof off === 'function') cleanups.push(off) + } + return { out, cleanups } +} +// A state update asks React for a new render; this reuses the hook table and runs no effect again. +function rerender(app, name, props) { + live.idx = 0 + return app[name](props) +} +// Every element in a drawn tree, depth first, for counting what a state shows. +function nodes(node, out = []) { + if (node == null || node === false || node === true) return out + if (typeof node === 'string' || typeof node === 'number') return out + if (Array.isArray(node)) { for (const child of node) nodes(child, out); return out } + if (typeof node !== 'object') return out + out.push(node) + nodes(node.children ?? [], out) + return out +} +const byMark = (tree, mark) => nodes(tree).filter(node => node?.props?.['data-flybot'] === mark) +const textOf = node => (Array.isArray(node?.children) ? node.children : [node?.children]).join('') + +const friendBot = { id: 'f1', name: 'Eve', remote: { home: '@eve.home', handle: '@eve.bot', homeName: 'Eve' } } +const settingsCard = app => mount(app, 'FriendChatSettings', { bot: friendBot }) +const switchOf = tree => nodes(byMark(tree, 'friend-auto-send')[0] ?? {}).find(node => node?.type === 'input') + +test('a settings read that fails is drawn as a failure, not as a setting', async () => { + const s = scene({ answers: [new Error('offline')] }) + try { + const { cleanups, out } = settingsCard(s.app) + await settle() + const tree = rerender(s.app, 'FriendChatSettings', { bot: friendBot }, out) + assert.equal(byMark(tree, 'friend-auto-read-error').length, 1, 'the failed read is the card’s answer') + unmount(cleanups) + } finally { s.restore() } +}) + +test('a NETWORK failure names the reason the users can act on', async () => { + const s = scene({ answers: [Object.assign(new Error('offline'), { code: 'NETWORK' })] }) + try { + const { cleanups, out } = settingsCard(s.app) + await settle() + const tree = rerender(s.app, 'FriendChatSettings', { bot: friendBot }, out) + assert.equal(textOf(byMark(tree, 'friend-auto-read-error')[0]), 'Could not reach FlyBot. Check that it is still running.') + unmount(cleanups) + } finally { s.restore() } +}) + +test('a setting nobody read is not offered as a switch to flip', async () => { + const s = scene({ answers: [new Error('offline')] }) + try { + const { cleanups, out } = settingsCard(s.app) + await settle() + const tree = rerender(s.app, 'FriendChatSettings', { bot: friendBot }, out) + assert.equal(switchOf(tree).props.disabled, true, 'an unknown setting is not a checkbox') + unmount(cleanups) + } finally { s.restore() } +}) + +test('a read that says the setting is on still draws a checked switch', async () => { + const s = scene({ answers: [{ autoAccept: { '@eve.home': true } }] }) + try { + const { cleanups, out } = settingsCard(s.app) + await settle() + const tree = rerender(s.app, 'FriendChatSettings', { bot: friendBot }, out) + assert.equal(switchOf(tree).props.checked, true, 'on is drawn as on') + assert.equal(byMark(tree, 'friend-auto-read-error').length, 0, 'a read that answered draws no failure') + unmount(cleanups) + } finally { s.restore() } +}) + +test('a read that says the setting is off still draws an unchecked switch', async () => { + const s = scene({ answers: [{ autoAccept: { '@eve.home': false } }] }) + try { + const { cleanups, out } = settingsCard(s.app) + await settle() + const tree = rerender(s.app, 'FriendChatSettings', { bot: friendBot }, out) + assert.equal(switchOf(tree).props.checked, false, 'off is drawn as off') + unmount(cleanups) + } finally { s.restore() } +}) From 16fcc4c8ed0eb6b676c656fcc8af717378f824ef Mon Sep 17 00:00:00 2001 From: Day Dreamer <87640074+HugoXK@users.noreply.github.com> Date: Tue, 24 Nov 2026 17:28:51 +0800 Subject: [PATCH 2/2] social: a failed Auto-send settings read is drawn as an error row (#3367) `FriendChatSettings` read `social/settings` with an empty rejection branch, so a failed read left the Auto-send checkbox at its initial `false` and the switch live: the card drew "my Bots' replies to this friend go out without asking" as the state the person chose. The value is now `null` until a read answers, a failure draws one quiet `friend-auto-read-error` status row through the existing `errorText()`, and the checkbox is not offered while the value is unknown. A read that answers draws on and off exactly as before, a remount retries, and there is no new copy or button. Docs-not-needed: the card's copy, its box and its hint are unchanged; the new row shows the existing `errorText()` sentence and the issue scopes the diff to `FriendChatSettings` plus the new test file. --- changelog.d/3367.fixed.md | 1 + plugins/social/client.js | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 changelog.d/3367.fixed.md diff --git a/changelog.d/3367.fixed.md b/changelog.d/3367.fixed.md new file mode 100644 index 000000000..759ebbfae --- /dev/null +++ b/changelog.d/3367.fixed.md @@ -0,0 +1 @@ +A friend Bot's settings card no longer draws a failed Auto-send read as the setting being off: until a read answers, one quiet line says the read failed and the switch is not offered, so "my Bots ask first" can no longer read as a state the person chose ([#3367](https://github.com/ThinkFlowLab/FlyBot/issues/3367)). diff --git a/plugins/social/client.js b/plugins/social/client.js index 89592f6af..bc33266e9 100644 --- a/plugins/social/client.js +++ b/plugins/social/client.js @@ -3759,14 +3759,25 @@ window.__ModuleLoader__.load({ const remote = bot?.remote const home = remote?.home const handle = remote?.handle - const [auto, setAuto] = useState(false) + // #3366: `null` is "nobody read it yet". A read that failed used to leave `false` here, which is + // drawn exactly like a setting that is off — and the switch stayed live, so the person could read + // "my Bots ask first" as a state they chose. The failure is the card's answer while it lasts, and + // the value stays unknown until a read answers (#3323/#3325/#3359 for the same shape elsewhere). + const [auto, setAuto] = useState(null) const [saving, setSaving] = useState(false) const [busy, setBusy] = useState(false) const [asking, setAsking] = useState(false) const [error, setError] = useState('') + const [readError, setReadError] = useState('') useEffect(() => { if (!home) return undefined - call('social/settings', {}).then(value => setAuto(value?.autoAccept?.[home] === true), () => {}) + let live = true + call('social/settings', {}).then(value => { + if (!live) return + setAuto(value?.autoAccept?.[home] === true) + setReadError('') + }, e => { if (live) setReadError(errorText(e)) }) + return () => { live = false } }, [home]) if (!handle) return null const toggleAuto = async (on) => { @@ -3813,9 +3824,10 @@ window.__ModuleLoader__.load({ // The box holds the setting only; the destructive link sits apart, below it (#274). h('div', { className: 'fs-card fs-friend-auto', 'data-flybot': 'friend-auto-box' }, h('label', { className: 'fs-auto', title: tr(AUTO_SEND_NOTE), 'data-flybot': 'friend-auto-send' }, - h('input', { type: 'checkbox', checked: auto === true, disabled: saving || !home, onChange: e => toggleAuto(e.target.checked) }), + h('input', { type: 'checkbox', checked: auto === true, disabled: saving || !home || auto === null, onChange: e => toggleAuto(e.target.checked) }), tr("Auto-send my Bots' replies to {who}", { who })), - h('div', { className: 'fs-hint', 'data-flybot': 'friend-auto-hint' }, tr("Your Bots' replies to {who} go out without asking you first. What you type is not affected.", { who }))), + h('div', { className: 'fs-hint', 'data-flybot': 'friend-auto-hint' }, tr("Your Bots' replies to {who} go out without asking you first. What you type is not affected.", { who })), + readError ? h('div', { className: 'fs-status', 'data-tone': 'error', role: 'status', 'data-flybot': 'friend-auto-read-error' }, readError) : null), h('div', { className: 'fs-friend-remove', 'data-flybot': 'friend-remove-row' }, asking ? h('span', { className: 'fs-leave-ask', 'data-flybot': 'remove-friend-bot-confirm' }, h('span', { className: 'fs-hint' }, tr('Remove {bot} from your list? {note}', { bot: botWithOwner(bot.name, named), note: keepLine })),