@@ -21,6 +21,7 @@ import { describe, it, expect, vi } from 'vitest';
2121import '@objectstack/metadata' ;
2222
2323import { HttpDispatcher } from '../http-dispatcher.js' ;
24+ import { SchemaRegistry } from '@objectstack/objectql' ;
2425
2526const CRM = 'com.crm.pkg' ;
2627const HR = 'com.hr.pkg' ;
@@ -492,3 +493,117 @@ describe('/automation/connectors/:ns/:name/connection — a connector instance b
492493 expect ( automation . getConnectorConnection ) . not . toHaveBeenCalled ( ) ;
493494 } ) ;
494495} ) ;
496+
497+ /**
498+ * [ADR-0150 D3, #33386; seat rulings 6884403301 and Q2 6888807772] A name two
499+ * packages hold, one of them disabled, through the real dispatcher over a real
500+ * `SchemaRegistry` (its `disablePackage`, its served filter), in both
501+ * registration orders. The doors judge the SERVED holders: the bare name is the
502+ * one served holder's slot or instance; an address naming the disabled holder
503+ * is refused naming the served holder only; `_env` and a third namespace name
504+ * the served holder only; with both disabled, nothing is reached.
505+ */
506+ describe ( '[#33386] the by-name doors judge the served holders of a name two packages hold' , ( ) => {
507+ const OPS = 'com.ops.pkg' ;
508+ const ORDERS = [ [ CRM , HR ] , [ HR , CRM ] ] ;
509+
510+ function registryOf ( order : readonly string [ ] , disabled : readonly string [ ] ) {
511+ const registry = new SchemaRegistry ( ) ;
512+ registry . logLevel = 'silent' ;
513+ for ( const [ id , namespace ] of [ [ CRM , 'crm' ] , [ HR , 'hr' ] , [ OPS , 'ops' ] ] ) {
514+ registry . installPackage ( { id, name : id , version : '1.0.0' , type : 'app' , namespace } as never , 'code' ) ;
515+ }
516+ for ( const holder of order ) {
517+ registry . registerItem ( 'flow' , { name : 'convert' , _packageId : holder } , 'name' as never , holder ) ;
518+ registry . registerItem ( 'connector' , { name : 'mail' , _packageId : holder } , 'name' as never , holder ) ;
519+ }
520+ for ( const id of disabled ) registry . disablePackage ( id ) ;
521+ return registry ;
522+ }
523+
524+ function dispatcherOver ( registry : SchemaRegistry ) {
525+ const slotOf = ( pkg : string | null , name : string ) => ( name === 'convert' && pkg !== null && [ CRM , HR ] . includes ( pkg ) ? `${ pkg } /convert` : undefined ) ;
526+ const automation = {
527+ handlerReady : true ,
528+ getFlow : vi . fn ( async ( ) => null ) ,
529+ execute : vi . fn ( async ( name : string ) => ( { success : false , error : `Flow '${ name } ' not found` } ) ) ,
530+ toggleFlow : vi . fn ( async ( ) => { } ) ,
531+ listRunsPage : vi . fn ( async ( ) => ( { runs : [ ] as unknown [ ] , hasMore : false } ) ) ,
532+ getConnectorConnection : vi . fn ( async ( request : { connector : string } ) => ( { connector : request . connector , status : 'not_connected' } ) ) ,
533+ getActionDescriptors : vi . fn ( ( ) => [ ] ) ,
534+ getFlowRuntimeStates : vi . fn ( ( ) => [ ] ) ,
535+ flowSlotKey : vi . fn ( slotOf ) ,
536+ getFlowSlot : vi . fn ( ( pkg : string | null , name : string ) => ( slotOf ( pkg , name ) ? { name, label : `${ pkg } flow` } : null ) ) ,
537+ executeSlot : vi . fn ( async ( pkg : string | null ) => ( { success : true , output : { ran : pkg } } ) ) ,
538+ toggleFlowSlot : vi . fn ( async ( ) => { } ) ,
539+ } ;
540+ const services : Record < string , unknown > = { automation, objectql : { registry } } ;
541+ const resolve = ( name : string ) => services [ name ] ;
542+ const kernel : any = { getService : resolve , getServiceAsync : async ( name : string ) => resolve ( name ) , context : { getService : resolve } } ;
543+ const dispatcher = new HttpDispatcher ( kernel ) ;
544+ const call = async ( path : string , method : string ) => {
545+ const answered = await dispatcher . handleAutomation ( path , method , { } , { request : { } , executionContext : ADMIN } as any , { } ) ;
546+ return { status : answered . response ?. status ?? 200 , body : answered . response ?. body as any } ;
547+ } ;
548+ return { call, automation } ;
549+ }
550+
551+ const packagesOf = ( a : { body ?: any } ) => ( candidatesOf ( a ) ?? [ ] ) . map ( ( c : { packageId ?: string } ) => c . packageId ) ;
552+
553+ describe . each ( ORDERS . map ( ( order ) => [ order . join ( ' then ' ) , order ] as const ) ) ( 'registered %s' , ( _o , order ) => {
554+ it ( 'CONTROL — nothing disabled: the bare doors refuse naming both; each address is its own slot or instance' , async ( ) => {
555+ const { call, automation } = dispatcherOver ( registryOf ( order , [ ] ) ) ;
556+ const bare = await call ( '/convert/trigger' , 'POST' ) ;
557+ expect ( [ bare . status , packagesOf ( bare ) ] ) . toEqual ( [ 409 , [ ...order ] ] ) ;
558+ const mail = await call ( '/connectors/mail/connection' , 'GET' ) ;
559+ expect ( [ mail . status , packagesOf ( mail ) ] ) . toEqual ( [ 409 , [ ...order ] ] ) ;
560+ expect ( ( await call ( '/crm/convert/trigger' , 'POST' ) ) . status ) . toBe ( 200 ) ;
561+ expect ( automation . executeSlot . mock . calls . map ( ( c ) => c [ 0 ] ) ) . toEqual ( [ CRM ] ) ;
562+ expect ( ( await call ( '/connectors/hr/mail/connection' , 'GET' ) ) . status ) . toBe ( 200 ) ;
563+ expect ( automation . getConnectorConnection . mock . calls . at ( - 1 ) ?. [ 0 ] ) . toMatchObject ( { connector : 'mail' , package : HR } ) ;
564+ } ) ;
565+
566+ it ( 'CRM disabled — flow: the bare door runs HR\'s slot (FROM 409 naming both); CRM\'s address is refused naming HR (FROM CRM\'s slot ran); HR\'s address is HR\'s slot' , async ( ) => {
567+ const { call, automation } = dispatcherOver ( registryOf ( order , [ CRM ] ) ) ;
568+ expect ( ( await call ( '/convert/trigger' , 'POST' ) ) . status ) . toBe ( 200 ) ;
569+ expect ( automation . executeSlot . mock . calls . map ( ( c ) => c [ 0 ] ) ) . toEqual ( [ HR ] ) ;
570+ const off = await call ( '/crm/convert/trigger' , 'POST' ) ;
571+ expect ( [ off . status , codeOf ( off ) , packagesOf ( off ) ] ) . toEqual ( [ 409 , 'RESOURCE_CONFLICT' , [ HR ] ] ) ;
572+ expect ( ( await call ( '/hr/convert/trigger' , 'POST' ) ) . status ) . toBe ( 200 ) ;
573+ for ( const path of [ '/_env/convert/trigger' , '/ops/convert/trigger' ] ) {
574+ const other = await call ( path , 'POST' ) ;
575+ expect ( [ path , other . status , packagesOf ( other ) ] ) . toEqual ( [ path , 409 , [ HR ] ] ) ;
576+ }
577+ expect ( automation . executeSlot . mock . calls . map ( ( c ) => c [ 0 ] ) ) . toEqual ( [ HR , HR ] ) ;
578+ expect ( automation . execute ) . not . toHaveBeenCalled ( ) ;
579+ } ) ;
580+
581+ it ( 'CRM disabled — connector (Q2 rows): the address naming CRM is refused naming HR (FROM 404 from the service); HR\'s address and the bare name connect HR\'s instance (bare FROM 409 naming both); `_env` and a third namespace name HR only (FROM naming both)' , async ( ) => {
582+ const { call, automation } = dispatcherOver ( registryOf ( order , [ CRM ] ) ) ;
583+ const off = await call ( '/connectors/crm/mail/connection' , 'GET' ) ;
584+ expect ( [ off . status , codeOf ( off ) , packagesOf ( off ) ] ) . toEqual ( [ 409 , 'RESOURCE_CONFLICT' , [ HR ] ] ) ;
585+ expect ( automation . getConnectorConnection ) . not . toHaveBeenCalled ( ) ;
586+ for ( const path of [ '/connectors/hr/mail/connection' , '/connectors/mail/connection' ] ) {
587+ expect ( ( await call ( path , 'GET' ) ) . status , path ) . toBe ( 200 ) ;
588+ expect ( automation . getConnectorConnection . mock . calls . at ( - 1 ) ?. [ 0 ] , path ) . toMatchObject ( { connector : 'mail' , package : HR } ) ;
589+ }
590+ for ( const path of [ '/connectors/_env/mail/connection' , '/connectors/ops/mail/connection' ] ) {
591+ const other = await call ( path , 'GET' ) ;
592+ expect ( [ path , other . status , packagesOf ( other ) ] ) . toEqual ( [ path , 409 , [ HR ] ] ) ;
593+ }
594+ expect ( automation . getConnectorConnection ) . toHaveBeenCalledTimes ( 2 ) ;
595+ } ) ;
596+
597+ it ( 'both disabled: no address reaches a flow or an instance (404); the bare door and `_env` reach no slot, and the service answers them with no package' , async ( ) => {
598+ const { call, automation } = dispatcherOver ( registryOf ( order , [ CRM , HR ] ) ) ;
599+ for ( const path of [ '/crm/convert/trigger' , '/hr/convert/trigger' ] ) expect ( ( await call ( path , 'POST' ) ) . status , path ) . toBe ( 404 ) ;
600+ for ( const path of [ '/connectors/crm/mail/connection' , '/connectors/hr/mail/connection' ] ) expect ( ( await call ( path , 'GET' ) ) . status , path ) . toBe ( 404 ) ;
601+ expect ( ( await call ( '/convert/trigger' , 'POST' ) ) . status ) . toBe ( 404 ) ;
602+ expect ( automation . executeSlot ) . not . toHaveBeenCalled ( ) ;
603+ for ( const path of [ '/connectors/mail/connection' , '/connectors/_env/mail/connection' ] ) {
604+ await call ( path , 'GET' ) ;
605+ expect ( automation . getConnectorConnection . mock . calls . at ( - 1 ) ?. [ 0 ] , path ) . not . toHaveProperty ( 'package' ) ;
606+ }
607+ } ) ;
608+ } ) ;
609+ } ) ;
0 commit comments