@@ -8,8 +8,14 @@ import { createEmptyPluginRegistry } from "../../plugins/registry-empty.js";
88import {
99 getActivePluginRegistry ,
1010 resetPluginRuntimeStateForTest ,
11+ requireActivePluginRegistry ,
1112 setActivePluginRegistry ,
1213} from "../../plugins/runtime.js" ;
14+ import { withPluginRuntimeRegistryScope } from "../../plugins/runtime/gateway-request-scope.js" ;
15+ import {
16+ createChannelTestPluginBase ,
17+ createTestRegistry ,
18+ } from "../../test-utils/channel-plugins.js" ;
1319
1420const loaderMocks = vi . hoisted ( ( ) => ( {
1521 loadPluginRegistryHandle : vi . fn ( ) ,
@@ -26,8 +32,11 @@ vi.mock("../../plugins/loader.js", () => ({
2632
2733const { bootstrapOutboundChannelPlugin, resetOutboundChannelBootstrapStateForTests } =
2834 await import ( "./channel-bootstrap.runtime.js" ) ;
29- const { resolveChannelOutboundDirectiveOptions, resolveOutboundDurableFinalDeliverySupport } =
30- await import ( "./deliver-channel.js" ) ;
35+ const {
36+ createChannelHandler,
37+ resolveChannelOutboundDirectiveOptions,
38+ resolveOutboundDurableFinalDeliverySupport,
39+ } = await import ( "./deliver-channel.js" ) ;
3140const { resolveChannelTargetForDelivery, resolveOutboundSessionRouteForDelivery } =
3241 await import ( "../../cron/isolated-agent/delivery-target.runtime.js" ) ;
3342
@@ -307,6 +316,52 @@ describe("bootstrapOutboundChannelPlugin", () => {
307316 expect ( loaderMocks . loadPluginRegistryHandle ) . not . toHaveBeenCalled ( ) ;
308317 } ) ;
309318
319+ it . each ( [ false , true ] ) (
320+ "activates the scoped setup shell without borrowing a same-id root sender (cached=%s)" ,
321+ ( cached ) => {
322+ const base = createChannelTestPluginBase ( { id : "discord" } ) ;
323+ const root = {
324+ ...base ,
325+ ...( ! cached ? { outbound : { deliveryMode : "direct" as const , sendText : vi . fn ( ) } } : { } ) ,
326+ } ;
327+ const activated = {
328+ ...base ,
329+ outbound : { deliveryMode : "direct" as const , sendText : vi . fn ( ) } ,
330+ } ;
331+ setActivePluginRegistry (
332+ createTestRegistry ( [ { pluginId : "root" , source : "root" , plugin : root } ] ) ,
333+ ) ;
334+ const setupRegistry = createTestRegistry ( [
335+ { pluginId : "selected" , source : "setup" , plugin : base } ,
336+ ] ) ;
337+ const runtimeRegistry = createTestRegistry ( [
338+ { pluginId : "selected" , source : "runtime" , plugin : activated } ,
339+ ] ) ;
340+ if ( cached ) {
341+ const prior = createTestRegistry ( [
342+ { pluginId : "root" , source : "prior" , plugin : activated } ,
343+ ] ) ;
344+ loaderMocks . loadPluginRegistryHandle . mockReturnValue ( prior ) ;
345+ expect ( bootstrapOutboundChannelPlugin ( { channel : "discord" , cfg : discordConfig } ) ) . toBe (
346+ prior ,
347+ ) ;
348+ loaderMocks . loadPluginRegistryHandle . mockClear ( ) ;
349+ }
350+ loaderMocks . loadPluginRegistryHandle . mockReturnValue ( runtimeRegistry ) ;
351+
352+ expect (
353+ withPluginRuntimeRegistryScope ( setupRegistry , ( ) =>
354+ bootstrapOutboundChannelPlugin ( {
355+ channel : "discord" ,
356+ cfg : discordConfig ,
357+ } ) ,
358+ ) ,
359+ ) . toBe ( runtimeRegistry ) ;
360+ expect ( loaderMocks . loadPluginRegistryHandle ) . toHaveBeenCalledOnce ( ) ;
361+ expect ( getActivePluginRegistry ( ) ?. channels [ 0 ] ?. plugin ) . toBe ( root ) ;
362+ } ,
363+ ) ;
364+
310365 it ( "returns a scoped handle without replacing the process root" , ( ) => {
311366 installDiscordSetupShell ( ) ;
312367 const root = getActivePluginRegistry ( ) ;
@@ -361,6 +416,74 @@ describe("bootstrapOutboundChannelPlugin", () => {
361416 ) . resolves . toEqual ( { ok : true , automaticUnknownSendReconciliation : false } ) ;
362417 } ) ;
363418
419+ it . each ( [ "outbound" , "message" ] as const ) (
420+ "retains the scoped %s transport and its durable capabilities through delivery" ,
421+ async ( transport ) => {
422+ const base = createChannelTestPluginBase ( { id : "discord" } ) ;
423+ const rootSend = vi . fn ( async ( ) => ( { channel : "discord" , messageId : "root" } ) ) ;
424+ const sendRegistries : ReturnType < typeof requireActivePluginRegistry > [ ] = [ ] ;
425+ const scopedSend = vi . fn ( async ( ) => {
426+ sendRegistries . push ( requireActivePluginRegistry ( ) ) ;
427+ return { channel : "discord" , messageId : "scoped" } ;
428+ } ) ;
429+ const message = ( send : typeof rootSend , id : string ) => ( {
430+ send : {
431+ text : async ( ) => {
432+ await send ( ) ;
433+ return {
434+ receipt : {
435+ primaryPlatformMessageId : id ,
436+ platformMessageIds : [ id ] ,
437+ sentAt : 1 ,
438+ parts : [ { platformMessageId : id , kind : "text" as const , index : 0 } ] ,
439+ } ,
440+ } ;
441+ } ,
442+ } ,
443+ } ) ;
444+ const root = {
445+ ...base ,
446+ outbound : { deliveryMode : "direct" as const , sendText : rootSend } ,
447+ message : {
448+ ...message ( rootSend , "root" ) ,
449+ durableFinal : { capabilities : { text : true , silent : true } } ,
450+ } ,
451+ } ;
452+ const scoped = {
453+ ...base ,
454+ ...( transport === "outbound"
455+ ? { outbound : { deliveryMode : "direct" as const , sendText : scopedSend } }
456+ : { message : message ( scopedSend , "scoped" ) } ) ,
457+ } ;
458+ setActivePluginRegistry (
459+ createTestRegistry ( [ { pluginId : "root" , source : "root" , plugin : root } ] ) ,
460+ ) ;
461+ const registry = createTestRegistry ( [
462+ { pluginId : "scoped" , source : "scoped" , plugin : scoped } ,
463+ ] ) ;
464+
465+ const handler = await withPluginRuntimeRegistryScope ( registry , async ( ) => {
466+ await expect (
467+ resolveOutboundDurableFinalDeliverySupport ( {
468+ cfg : discordConfig ,
469+ channel : "discord" ,
470+ requirements : { silent : true } ,
471+ } ) ,
472+ ) . resolves . toEqual ( { ok : false , reason : "capability_mismatch" , capability : "silent" } ) ;
473+ return await createChannelHandler ( {
474+ cfg : discordConfig ,
475+ channel : "discord" ,
476+ to : "recipient" ,
477+ } ) ;
478+ } ) ;
479+ await expect ( handler . sendText ( "hello" ) ) . resolves . toMatchObject ( { messageId : "scoped" } ) ;
480+ expect ( sendRegistries ) . toEqual ( [ registry ] ) ;
481+ expect ( scopedSend ) . toHaveBeenCalledOnce ( ) ;
482+ expect ( rootSend ) . not . toHaveBeenCalled ( ) ;
483+ expect ( loaderMocks . loadPluginRegistryHandle ) . not . toHaveBeenCalled ( ) ;
484+ } ,
485+ ) ;
486+
364487 it ( "does not retry an unusable handle in the same generation" , ( ) => {
365488 installDiscordSetupShell ( ) ;
366489 loaderMocks . loadPluginRegistryHandle . mockReturnValue ( createEmptyPluginRegistry ( ) ) ;
0 commit comments