77 createStartAccountContext ,
88} from "openclaw/plugin-sdk/channel-test-helpers" ;
99import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts" ;
10+ import { createDeferred } from "openclaw/plugin-sdk/extension-shared" ;
1011import {
1112 createPluginStateKeyedStoreForTests ,
1213 resetPluginStateStoreForTests ,
@@ -15,6 +16,7 @@ import { afterEach, beforeEach, expect, it, vi } from "vitest";
1516import { startBuzzBus , type BuzzBus } from "./buzz-bus.js" ;
1617import { createBuzzRelayFixture } from "./buzz-relay.test-harness.js" ;
1718import { startBuzzGatewayAccount } from "./gateway.js" ;
19+ import { handleBuzzInbound } from "./inbound.js" ;
1820import { setBuzzRuntime } from "./runtime.js" ;
1921import { resolveBuzzAccount } from "./types.js" ;
2022
@@ -152,6 +154,88 @@ it("keeps removal denied through stale snapshots and accepts a confirmed rejoin"
152154 }
153155} ) ;
154156
157+ it ( "finishes an admitted room turn after its sender is removed" , async ( ) => {
158+ const fixture = await createBuzzRelayFixture ( ) ;
159+ const runtime = createPluginRuntimeMock ( ) ;
160+ runtime . state . openKeyedStore = ( options ) => createPluginStateKeyedStoreForTests ( "buzz" , options ) ;
161+ setBuzzRuntime ( runtime ) ;
162+ const dispatched = createDeferred < void > ( ) ;
163+ const continueTurn = createDeferred < void > ( ) ;
164+ const completed = createDeferred < void > ( ) ;
165+ const cfg = {
166+ channels : {
167+ buzz : {
168+ relayUrl : fixture . relayUrl ,
169+ privateKey : fixture . botPrivateKey ,
170+ groupPolicy : "open" ,
171+ groups : { [ fixture . roomId ] : { requireMention : false } } ,
172+ } ,
173+ } ,
174+ } satisfies OpenClawConfig ;
175+ const account = resolveBuzzAccount ( { cfg } ) ;
176+ vi . mocked ( runtime . channel . inbound . dispatch ) . mockImplementation ( async ( params ) => {
177+ dispatched . resolve ( ) ;
178+ await continueTurn . promise ;
179+ await params . delivery . deliver ( { text : "admitted room reply" } , { kind : "final" } ) ;
180+ return {
181+ admission : { kind : "dispatch" } ,
182+ dispatched : true ,
183+ ctxPayload : params . ctxPayload ,
184+ routeSessionKey : params . route . sessionKey ,
185+ dispatchResult : { queuedFinal : true , counts : { tool : 0 , block : 0 , final : 1 } } ,
186+ } ;
187+ } ) ;
188+ let bus : BuzzBus | undefined ;
189+ try {
190+ bus = await startBuzzBus ( {
191+ accountId : account . accountId ,
192+ relayUrl : fixture . relayUrl ,
193+ privateKey : fixture . botPrivateKey ,
194+ channelIds : [ fixture . roomId ] ,
195+ onMessage : async ( message , activeBus , signal , assertCurrent ) => {
196+ await handleBuzzInbound ( { account, cfg, bus : activeBus , message, signal, assertCurrent } ) ;
197+ completed . resolve ( ) ;
198+ } ,
199+ onMessageError : completed . reject ,
200+ } ) ;
201+ const message = fixture . sendMessage ( "accepted before removal" ) ;
202+ await dispatched . promise ;
203+ const initial = fixture . events . find ( ( event ) => event . kind === 39002 ) ! ;
204+ fixture . broadcast (
205+ fixture . signRelay ( {
206+ kind : 39002 ,
207+ created_at : initial . created_at + 1 ,
208+ content : "" ,
209+ tags : initial . tags . filter ( ( tag ) => tag [ 0 ] !== "p" || tag [ 1 ] !== fixture . senderPublicKey ) ,
210+ } ) ,
211+ ) ;
212+ await vi . waitFor ( ( ) =>
213+ expect (
214+ bus ?. directory
215+ . listGroupMembers ( { groupId : fixture . roomId } )
216+ . some ( ( member ) => member . id === fixture . senderPublicKey ) ,
217+ ) . toBe ( false ) ,
218+ ) ;
219+ continueTurn . resolve ( ) ;
220+ await completed . promise ;
221+ const replies = fixture . events . filter (
222+ ( event ) => event . pubkey === fixture . botPublicKey && event . kind === 9 ,
223+ ) ;
224+ expect ( replies ) . toHaveLength ( 1 ) ;
225+ expect ( replies [ 0 ] ) . toMatchObject ( {
226+ content : "admitted room reply" ,
227+ tags : expect . arrayContaining ( [
228+ [ "h" , fixture . roomId ] ,
229+ [ "e" , message . id , "" , "reply" ] ,
230+ ] ) ,
231+ } ) ;
232+ } finally {
233+ continueTurn . resolve ( ) ;
234+ await bus ?. close ( ) ;
235+ await fixture . close ( ) ;
236+ }
237+ } ) ;
238+
155239it ( "revokes the active bot immediately on a signed role downgrade" , async ( ) => {
156240 const fixture = await createBuzzRelayFixture ( ) ;
157241 const fatal : Error [ ] = [ ] ;
@@ -186,6 +270,9 @@ it("revokes the active bot immediately on a signed role downgrade", async () =>
186270 await vi . waitFor ( ( ) => expect ( fatal ) . toHaveLength ( 1 ) ) ;
187271 expect ( fatal [ 0 ] ?. message ) . toContain ( "no longer has the Bot role" ) ;
188272 expect ( turnSignal ?. aborted ) . toBe ( true ) ;
273+ await expect (
274+ bus . sendText ( { channelId : fixture . roomId , text : "retired bot reply" } ) ,
275+ ) . rejects . toThrow ( "no longer has the Bot role" ) ;
189276 } finally {
190277 await bus ?. close ( ) ;
191278 await fixture . close ( ) ;
0 commit comments