Skip to content

Commit b429d70

Browse files
committed
fix(tree): refresh the Archived section when a Bot is retired
archive_bot hides a retired Bot from the default list, so the sidebar's Archived section is the only way back to its chat. That section's field-change subscription covered archived/parent/treeRole/removed but not retired, while the structure entry already reacts to it and docs/subsystems/tree.md states both consumers refresh on it. A retired Bot now appears in the Archived section on the change itself rather than on the next 10 s roster poll. The archived-section test harness takes the react stub it renders with, so the new test can run the section's effects and fire the change. Docs-not-needed: the behaviour is already stated in docs/subsystems/tree.md (both consumers refresh on `retired`); this commit makes client.js match it. Fixes #2450
1 parent 16d1645 commit b429d70

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
A Bot retired with `archive_bot` now appears in the sidebar's Archived section at once, instead of up to one 10 s roster poll later ([#2450](https://github.com/ThinkFlowLab/FlyBot/issues/2450)).

‎plugins/tree/client.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ window.__ModuleLoader__.load({
533533
const applyBots = useCallback(list => setBots(archivedOf(list)), [])
534534
useEffect(() => {
535535
const offs = [feed.subscribe(applyBots)]
536-
try { offs.push(flybot.fields?.on?.('change', c => { if (c && (c.key === 'archived' || c.key === 'parent' || c.key === 'treeRole' || c.removed)) feed.refresh() })) } catch { /* no change feed */ }
536+
try { offs.push(flybot.fields?.on?.('change', c => { if (c && (c.key === 'archived' || c.key === 'parent' || c.key === 'treeRole' || c.key === 'retired' || c.removed)) feed.refresh() })) } catch { /* no change feed */ }
537537
try { if (typeof flybot.on === 'function') offs.push(flybot.on('bot.removed', () => feed.refresh())) } catch { /* no removal event */ }
538538
return () => { for (const off of offs) { try { off?.() } catch { /* already gone */ } } }
539539
}, [applyBots, feed])

‎plugins/tree/test/archived-section.test.js‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { readFileSync } from 'node:fs'
77
import { test } from 'node:test'
88
import { findAll, reactStub, renderToString } from './render.js'
99

10-
const loadClient = () => {
10+
const loadClient = (react = reactStub) => {
1111
let mod
12-
globalThis.window = { __ModuleLoader__: { load: ({ factory }) => { mod = factory(() => reactStub) } } }
12+
globalThis.window = { __ModuleLoader__: { load: ({ factory }) => { mod = factory(() => react) } } }
1313
new Function(readFileSync(new URL('../client.js', import.meta.url), 'utf8'))()
1414
return mod
1515
}
@@ -138,3 +138,32 @@ test('#810: the section is registered on the sidebar slot, and archived Bots sta
138138
mod.apply({ flybot: { ...flybot, slots: { register: (name, entry) => { if (name === 'flybot.sidebar.section') off.push(entry.id); return () => {} } } } }, { archivedSection: false })
139139
assert.ok(!off.includes('flybot-tree-archived'), 'config.archivedSection: false turns the section off')
140140
})
141+
142+
// #2450: `archive_bot` hides the retired Bot from the default list, so this section is the way back
143+
// to its chat. Its field-change subscription must refresh on `retired` itself, like the structure
144+
// entry does (docs/subsystems/tree.md says both consumers refresh on `retired`), not one 10 s poll later.
145+
test('#2450: a retired Bot reaches the Archived section on the change, not the next poll', () => {
146+
const effects = []
147+
const react = {
148+
createElement: (type, props, ...children) => ({ type, props: props ?? {}, children: children.flat(Infinity) }),
149+
useState: init => [typeof init === 'function' ? init() : init, () => {}],
150+
useEffect: fn => effects.push(fn),
151+
useCallback: fn => fn,
152+
}
153+
const mod = loadClient(react)
154+
const calls = []
155+
const feed = { subscribe: () => () => {}, refresh: () => calls.push('refresh'), get: () => [] }
156+
const flybot = { fields: { on: (event, fn) => { if (event === 'change') calls.push(fn); return () => {} } } }
157+
mod.makeArchivedSection(flybot, feed)({})
158+
for (const fn of effects) fn()
159+
const onChange = calls.find(v => typeof v === 'function')
160+
assert.equal(typeof onChange, 'function', 'the section subscribes to field changes')
161+
162+
calls.length = 0
163+
onChange({ key: 'parent' })
164+
assert.equal(calls.filter(v => v === 'refresh').length, 1, 'control: parent still refreshes')
165+
166+
calls.length = 0
167+
onChange({ key: 'retired', value: true })
168+
assert.equal(calls.filter(v => v === 'refresh').length, 1, 'a retired change refreshes the archived list')
169+
})

0 commit comments

Comments
 (0)