Show each bot's Bot Chat in Canopy and harden the session tree (0.2.0)

- Bot Chat as the first row under each bot, read from the roster
  projection (profiles.list include_sessions) and opened in the bots
  workspace like the built-in Bots pane; never created by Canopy
- Sort conversations by last activity, cap the stored-cursor scan at
  100 pages, typed CanopyError for user-facing messages
- jsxs for static children (no React key warnings), useRef for the lock
- package.json with npm test, .gitignore, tests and browser fixture

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Mikei386andClaude Opus 5.5 committed 2026-11-20 19:32:27 +01:00
1 parent a8b2459dbc
commit e0a147e2e8
6 files changed
+225 -66

No files matched your search

+69 -3
View File
@@ -3,11 +3,11 @@ import assert from 'node:assert/strict'
import {registerHooks} from 'node:module'
const hooks = registerHooks({resolve(specifier, context, next) {
if (specifier === '@hermes/plugin-sdk') return {url: 'data:text/javascript,export const host={},useValue=()=>{},useQuery=()=>{},useQueryClient=()=>{}', shortCircuit:true}
if (specifier === 'react') return {url: 'data:text/javascript,export const useEffect=()=>{},useState=()=>{},useId=()=>{}', shortCircuit:true}
if (specifier === 'react/jsx-runtime') return {url: 'data:text/javascript,export const jsx=(type,props)=>({type,props})', shortCircuit:true}
if (specifier === 'react') return {url: 'data:text/javascript,export const useEffect=()=>{},useState=()=>{},useId=()=>{},useRef=()=>{}', shortCircuit:true}
if (specifier === 'react/jsx-runtime') return {url: 'data:text/javascript,export const jsx=(type,props)=>({type,props}),jsxs=(type,props)=>({type,props})', shortCircuit:true}
return next(specifier, context)
}})
const {default: plugin, routesFor, loadBot, openExisting, avatarSource} = await import('../plugin.js')
const {default: plugin, routesFor, loadBot, openExisting, openBotChat, botChatOf, avatarSource, CanopyError} = await import('../plugin.js')
hooks.deregister()
const route = {connectionId:'g', profile:'work',targetProfile:'work',mode:'remote'}
const state = name => ({get:()=>name})
@@ -104,3 +104,69 @@ test('avatar only accepts raster data or falls back to initials', ()=>{
assert.equal(avatarSource({found:true,data:'data:image/svg+xml;base64,aGk='}),null)
assert.equal(avatarSource({found:false}),null)
})
test('sorts by last activity first, then by title', async () => {
const res=await loadBot({requestProfile:async (_,__,p)=>p.scope==='live'?live([]):page([
row('alt',{title:'Alpha',last_active_at:100}),row('neu',{title:'Zulu',last_active_at:200}),row('ohne',{title:'Beta'})])},route)
assert.deepEqual(res.sessions.map(r=>r.id),['neu','alt','ohne'])
})
test('an endless chain of fresh cursors is cut off', async () => {
let n=0
await assert.rejects(loadBot({requestProfile:async (_,__,p)=>p.scope==='live'?live([]):page([], `c${++n}`)},route),
err=>err instanceof CanopyError && /Zu viele Seiten/.test(err.message))
assert.equal(n,100)
})
test('user-facing errors are typed, transport errors are not', async () => {
await assert.rejects(routesFor({profileRoutes:async()=>[]},'g'),CanopyError)
await assert.rejects(loadBot({requestProfile:async()=>{throw Error('offline')}},route),
err=>!(err instanceof CanopyError))
})
test('reads the Bot Chat summary from the roster projection, preferring the compression tip', () => {
assert.deepEqual(botChatOf({id:'reg',resolved_id:'tip',preview:' hi ',last_active:50,live_message_count:0}),
{id:'reg',openId:'tip',aliases:['reg','tip'],preview:'hi',lastActive:50,expectHistory:false})
assert.equal(botChatOf({id:'reg'}).openId,'reg')
assert.equal(botChatOf({id:'reg'}).expectHistory,true)
assert.equal(botChatOf(null),null)
assert.equal(botChatOf({id:''}),null)
})
const botChatHost = (canonical, extra={}) => {
const calls={open:[],rpc:[],focus:[]}
return {calls, host:{state:{connectionId:state('g'),gateway:state('open')},profileRoutes:async()=>[route],
requestProfile:async (owner,method,p)=>{
calls.rpc.push([method,p])
assert.equal(method,'profiles.list')
assert.equal(p.include_sessions,true)
return {profiles:[{name:'work',canonical_session:canonical}]}
},
focusOpenWorkspaceSession:(...args)=>calls.focus.push(args),
openSession:async (...args)=>calls.open.push(args), ...extra}}
}
test('opens the Bot Chat in the bots workspace like the built-in Bots pane', async () => {
const {host,calls}=botChatHost({id:'reg',resolved_id:'tip',message_count:3})
await openBotChat(host,'g',route,route)
const [id,o]=calls.open[0]
assert.equal(id,'tip')
assert.equal(o.route,route)
assert.equal(o.workspaceMode,'bots')
assert.equal(o.workspaceOwnerKey,'bot:g::work')
assert.equal(o.tabTitle,'Bot Chat')
assert.equal(o.forceResume,true)
assert.equal(o.expectHistory,true)
const [key,isStale,only]=calls.focus[0]
assert.equal(key,'bot:g::work'); assert.deepEqual(only,[])
assert.equal(isStale({storedSessionId:'old-segment',workspaceTabTitle:'Bot Chat'}),true)
assert.equal(isStale({storedSessionId:'tip',workspaceTabTitle:'Bot Chat'}),false)
assert.equal(isStale({storedSessionId:'x',workspaceTabTitle:'Other'}),false)
})
test('a missing Bot Chat is reported and never created', async () => {
const {host,calls}=botChatHost(null)
await assert.rejects(openBotChat(host,'g',route,route),err=>err instanceof CanopyError && /noch keinen Bot Chat/.test(err.message))
assert.equal(calls.open.length,0)
assert.ok(calls.rpc.every(([method])=>method==='profiles.list'))
})
test('Bot Chat open refuses a disconnected gateway or a vanished route', async () => {
const offline=botChatHost({id:'reg'},{state:{connectionId:state('g'),gateway:state('closed')}})
await assert.rejects(openBotChat(offline.host,'g',route,route),/nicht verbunden/)
const moved=botChatHost({id:'reg'},{profileRoutes:async()=>[{...route,profile:'other',targetProfile:'other'}]})
await assert.rejects(openBotChat(moved.host,'g',route,route),/Profilroute/)
assert.equal(offline.calls.open.length+moved.calls.open.length,0)
})