@@ -99,6 +99,18 @@ const FLAG_BY_EMAIL = {
9999 ai : { exposed : true , description : 'Flag the lead by email.' } ,
100100} ;
101101
102+ /**
103+ * [#29173] The showcase's "Recalculate Selection" shape: the recalc target run
104+ * ONCE for a whole bulk selection (`execution: 'aggregate'`), with every
105+ * selected id in `params._selectedIds` and no `recordId` — and the same
106+ * `recordIdParam` a per-record run seeds from its row.
107+ */
108+ const RECALC_SELECTION = {
109+ name : 'recalc_selection' , label : 'Recalculate Selection' , objectName : OBJECT , type : 'api' ,
110+ execution : 'aggregate' , target : '/api/v1/apps/showcase/recalc' , recordIdParam : 'recordId' ,
111+ ai : { exposed : true , description : 'Recalculate the selected leads.' } ,
112+ } ;
113+
102114/** An api action whose `target` carries no record token. */
103115const PING_LOCAL = {
104116 name : 'ping_local' , label : 'Ping' , objectName : OBJECT , type : 'api' , method : 'POST' ,
@@ -112,6 +124,8 @@ function boot(opts: {
112124 /** What the target answers: status, JSON body, extra response headers. */
113125 answer ?: { status ?: number ; body ?: unknown ; text ?: string ; headers ?: Array < [ string , string ] > } ;
114126 noServer ?: boolean ;
127+ /** Actions declared on the object beside the shared roster above. */
128+ extraActions ?: unknown [ ] ;
115129} = { } ) {
116130 const disabled = new Set ( opts . disabled ?? [ ] ) ;
117131 const injected : Injected [ ] = [ ] ;
@@ -128,7 +142,7 @@ function boot(opts: {
128142 } ) ;
129143 const server = { getRawApp : ( ) => ( { request } ) } ;
130144 const executeAction = vi . fn ( async ( ) => ( { ran : 'script' } ) ) ;
131- const objectDef = { name : OBJECT , actions : [ BAN_LEAD , REMOTE_PING , DELETE_LEAD , RECALC_LEAD , APPROVE_LEAD , NOTE_OWNER , FLAG_BY_EMAIL , PING_LOCAL ] } ;
145+ const objectDef = { name : OBJECT , actions : [ BAN_LEAD , REMOTE_PING , DELETE_LEAD , RECALC_LEAD , APPROVE_LEAD , NOTE_OWNER , FLAG_BY_EMAIL , PING_LOCAL , ... ( opts . extraActions ?? [ ] ) ] } ;
132146 const ql : any = {
133147 executeAction,
134148 getSchema : ( name : string ) => ( name === OBJECT ? objectDef : undefined ) ,
@@ -1449,3 +1463,82 @@ describe('an api action whose target names its record, called with no record id,
14491463 expect ( ( rowless as any ) . path ) . toBe ( '/api/v1/approvals/requests/areq%201/approve' ) ;
14501464 } ) ;
14511465} ) ;
1466+
1467+ describe ( '[#29173] a record-less aggregate api action declaring `recordIdParam` is forwarded, and invents no record' , ( ) => {
1468+ const SELECTED = [ 'u_target' , 'lead 7' ] ;
1469+ const runViaMcp = async ( h : ReturnType < typeof boot > , name : string , input : any = { } ) => {
1470+ const deps : any = {
1471+ resolveService : async ( _ctx : unknown , service : string ) =>
1472+ service === 'metadata' ? h . metadata
1473+ : service === 'automation' ? null
1474+ : service === 'http.server' ? { getRawApp : ( ) => ( { request : h . request } ) }
1475+ : h . ql ,
1476+ getObjectQL : async ( ) => h . ql ,
1477+ } ;
1478+ return invokeBusinessAction ( deps , CTX ( ) as any , name , input , {
1479+ driver : h . ql ,
1480+ ec : { userId : 'u_agent' , tenantId : 'org_1' , systemPermissions : [ ] } ,
1481+ getMeta : ( ) => h . metadata ,
1482+ callData : async ( _a : string , p : any ) => ( { object : p ?. object , id : p ?. id , record : { id : p ?. id } } ) ,
1483+ } ) ;
1484+ } ;
1485+
1486+ it ( 'REST: the bulk bar\'s one aggregate request reaches the target with the selection, and no `recordId` key' , async ( ) => {
1487+ const { dispatcher, injected } = boot ( { extraActions : [ RECALC_SELECTION ] , answer : { body : { success : true , data : { selected : 2 , acted : 2 } } } } ) ;
1488+
1489+ const res = await dispatcher . handleActions (
1490+ `/${ OBJECT } /${ RECALC_SELECTION . name } ` , 'POST' , { params : { _selectedIds : SELECTED } } , CTX ( ) ,
1491+ ) ;
1492+
1493+ expect ( statusOf ( res ) , JSON . stringify ( res ?. response ?. body ) ) . toBe ( 200 ) ;
1494+ expect ( injected ) . toHaveLength ( 1 ) ;
1495+ expect ( injected [ 0 ] . url ) . toBe ( 'http://app.example.test/api/v1/apps/showcase/recalc' ) ;
1496+ const body = JSON . parse ( String ( injected [ 0 ] . init . body ) ) ;
1497+ expect ( body ) . toEqual ( { _selectedIds : SELECTED } ) ;
1498+ expect ( body ) . not . toHaveProperty ( 'recordId' ) ;
1499+ expect ( res ?. response ?. body ?. data ) . toEqual ( { selected : 2 , acted : 2 } ) ;
1500+ } ) ;
1501+
1502+ it ( 'MCP `run_action`: the same record-less run is forwarded, and no `recordId` key is invented' , async ( ) => {
1503+ const h = boot ( { extraActions : [ RECALC_SELECTION ] } ) ;
1504+
1505+ const result : any = await runViaMcp ( h , RECALC_SELECTION . name , { params : { _selectedIds : SELECTED } } ) ;
1506+
1507+ expect ( result . ok ) . toBe ( true ) ;
1508+ expect ( h . injected ) . toHaveLength ( 1 ) ;
1509+ const body = JSON . parse ( String ( h . injected [ 0 ] . init . body ) ) ;
1510+ expect ( body ) . toEqual ( { _selectedIds : SELECTED } ) ;
1511+ expect ( body ) . not . toHaveProperty ( 'recordId' ) ;
1512+ } ) ;
1513+
1514+ it ( 'REST, the per-record control: the same action run on one record still seeds `recordIdParam` from that row' , async ( ) => {
1515+ for ( const recordId of SELECTED ) {
1516+ const { dispatcher, injected } = boot ( { extraActions : [ RECALC_SELECTION ] } ) ;
1517+
1518+ const res = await dispatcher . handleActions ( `/${ OBJECT } /${ RECALC_SELECTION . name } ` , 'POST' , { recordId } , CTX ( ) ) ;
1519+
1520+ expect ( statusOf ( res ) , JSON . stringify ( res ?. response ?. body ) ) . toBe ( 200 ) ;
1521+ expect ( JSON . parse ( String ( injected [ 0 ] . init . body ) ) ) . toEqual ( { recordId } ) ;
1522+ }
1523+ } ) ;
1524+
1525+ it ( 'buildApiActionRequest: the empty subject record of a record-less dispatch is no row; a dispatch naming a record keeps both #8018 refusals' , ( ) => {
1526+ const ec = { userId : 'u1' , tenantId : 'org_9' } ;
1527+ for ( const record of [ { } , null , undefined ] ) {
1528+ const built = buildApiActionRequest ( RECALC_SELECTION , { record, params : { _selectedIds : SELECTED } , ec } ) ;
1529+ expect ( built , JSON . stringify ( record ) ) . toEqual ( { method : 'POST' , path : '/api/v1/apps/showcase/recalc' , body : { _selectedIds : SELECTED } } ) ;
1530+ }
1531+ // An empty id is no id, as the path half reads it.
1532+ expect ( ( buildApiActionRequest ( RECALC_SELECTION , { recordId : '' , record : { } , params : { } , ec } ) as any ) . body ) . toEqual ( { } ) ;
1533+
1534+ const byToken = { ...RECALC_SELECTION , recordIdParam : 'token' , recordIdField : 'token' } ;
1535+ const absent = buildApiActionRequest ( byToken , { recordId : 's1' , record : { id : 's1' } , params : { } , ec } ) ;
1536+ expect ( ( absent as any ) . status ) . toBe ( 400 ) ;
1537+ expect ( absent ) . not . toHaveProperty ( 'path' ) ;
1538+ expect ( ( absent as any ) . message ) . toContain ( '"token"' ) ;
1539+ const nulled = buildApiActionRequest ( byToken , { recordId : 's1' , record : { id : 's1' , token : null } , params : { } , ec } ) ;
1540+ expect ( ( nulled as any ) . status ) . toBe ( 400 ) ;
1541+ expect ( nulled ) . not . toHaveProperty ( 'path' ) ;
1542+ expect ( ( nulled as any ) . message ) . toContain ( '"token"' ) ;
1543+ } ) ;
1544+ } ) ;
0 commit comments