3636 * - CONTROL a body that catches the refusal and throws its own error speaks
3737 * for itself, and leaves the runner as a `SandboxError` exactly as before;
3838 * - CONTROL a body that rewrites the refusal's message before re-throwing it
39- * is a speaker too.
39+ * is a speaker too;
40+ * - an ACTION body that lets the same refusal leave now hands back the
41+ * engine's error too, and the action door's answer does not move: `500
42+ * INTERNAL_ERROR`, the delete's own sentence, `success: false`.
4043 *
4144 * The rig is `action-delete-cascade-summary-rollback.test.ts`'s: a real
4245 * `ObjectQL` over `SqlDriver` on better-sqlite3, which rolls back for real, and
4851import { describe , it , expect , beforeAll , afterAll , vi } from 'vitest' ;
4952import { ObjectQL , bindHooksToEngine } from '@objectstack/objectql' ;
5053import { SqlDriver } from '@objectstack/driver-sql' ;
51- import { hookBodyRunnerFactory } from './sandbox/body-runner.js' ;
54+ import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol' ;
55+ import { HttpDispatcher } from './http-dispatcher.js' ;
56+ import { actionBodyRunnerFactory , hookBodyRunnerFactory } from './sandbox/body-runner.js' ;
5257import { QuickJSScriptRunner , SandboxError } from './sandbox/quickjs-runner.js' ;
5358import { installDefaultOrganization } from './default-organization.testkit.js' ;
5459
@@ -62,6 +67,9 @@ const FINDER = 'hfinder';
6267const text = ( name : string ) => ( { name, type : 'text' as const , label : name } ) ;
6368const id = { name : 'id' , type : 'text' as const , primaryKey : true , label : 'ID' } ;
6469
70+ /** Deletes the account; its cascade is stopped by the first line's refused roll-up. */
71+ const PURGE = `await ctx.api.object('${ ACCOUNT } ').delete({ where: { id: 'a1' } });` ;
72+
6573const OBJECTS = [
6674 { name : ACCOUNT , label : 'Account' , fields : { id, name : text ( 'name' ) } } ,
6775 {
@@ -80,12 +88,13 @@ const OBJECTS = [
8088 bucket : { name : 'bucket' , type : 'lookup' as const , reference : BUCKET , label : 'Bucket' } ,
8189 } ,
8290 } ,
83- { name : CASE , label : 'Case' , fields : { id, name : text ( 'name' ) , mode : text ( 'mode' ) } } ,
91+ {
92+ name : CASE , label : 'Case' , fields : { id, name : text ( 'name' ) , mode : text ( 'mode' ) } ,
93+ actions : [ { name : 'purge' , label : 'Purge' , type : 'script' , body : { language : 'js' , capabilities : [ 'api.read' , 'api.write' ] , source : `${ PURGE } return { purged: true };` } } ] ,
94+ } ,
8495 { name : FINDER , label : 'Finder' , fields : { id, name : text ( 'name' ) } } ,
8596] ;
8697
87- /** Deletes the account; its cascade is stopped by the first line's refused roll-up. */
88- const PURGE = `await ctx.api.object('${ ACCOUNT } ').delete({ where: { id: 'a1' } });` ;
8998const WRITE_CAPS = [ 'api.read' , 'api.write' ] ;
9099
91100/** A sandboxed hook on `hcase`, active only for records whose `mode` names it. */
@@ -103,6 +112,7 @@ const HOOKS = [
103112] ;
104113
105114let engine : ObjectQL ;
115+ let dispatcher : HttpDispatcher ;
106116let errorLog : ReturnType < typeof vi . spyOn > ;
107117
108118beforeAll ( async ( ) => {
@@ -123,6 +133,14 @@ beforeAll(async () => {
123133 }
124134 engine . setDefaultBodyRunner ( hookBodyRunnerFactory ( new QuickJSScriptRunner ( { hookTimeoutMs : 10_000 } ) , { ql : engine , appId : 'hcase_app' } ) ) ;
125135 bindHooksToEngine ( engine , HOOKS as any , { packageId : TEST_PACKAGE_ID } ) ;
136+ const purge = ( OBJECTS . find ( ( o ) => o . name === CASE ) as any ) . actions [ 0 ] ;
137+ ( engine as any ) . registerAction ( CASE , 'purge' , actionBodyRunnerFactory ( new QuickJSScriptRunner ( ) , { ql : engine , appId : 'hcase_app' } ) ( { ...purge , object : CASE } ) ) ;
138+ const protocol = new ObjectStackProtocolImplementation ( engine as any ) ;
139+ const services : Record < string , unknown > = { objectql : engine , data : engine , protocol } ;
140+ const get = ( n : string ) => services [ n ] ?? null ;
141+ const kernel : any = { context : { getService : get } , getService : get , getServiceAsync : async ( n : string ) => get ( n ) } ;
142+ dispatcher = new HttpDispatcher ( kernel ) ;
143+ ( dispatcher as any ) . timedResolveExecutionContext = async ( ) => ( { userId : 'u_member' , isSystem : false } ) ;
126144} , 60_000 ) ;
127145
128146afterAll ( async ( ) => {
@@ -191,3 +209,17 @@ describe('[#29527] CONTROL a body that speaks for itself keeps the relay it had'
191209 await expectNothingPurged ( ) ;
192210 } ) ;
193211} ) ;
212+
213+ describe ( '[#29527] the action door\'s answer does not move for an action body that lets the refusal leave' , ( ) => {
214+ it ( '500 INTERNAL_ERROR, the delete\'s own sentence, success false, nothing deleted' , async ( ) => {
215+ const path = `/actions/${ CASE } /purge` ;
216+ const request = new Request ( `http://localhost/api/v1${ path } ` , { method : 'POST' , headers : { 'content-type' : 'application/json' } , body : '{}' } ) ;
217+ const res : any = await dispatcher . dispatch ( 'POST' , path , { } , { } , { request } as any , '/api/v1' ) ;
218+ const response = res . response as { status : number ; body : any } ;
219+ expect ( response . status , JSON . stringify ( response . body ) ) . toBe ( 500 ) ;
220+ expect ( response . body ?. success , JSON . stringify ( response . body ) ) . toBe ( false ) ;
221+ expect ( response . body ?. error ?. code , JSON . stringify ( response . body ) ) . toBe ( 'INTERNAL_ERROR' ) ;
222+ expect ( response . body ?. error ?. message ) . toContain ( `The delete of "${ ACCOUNT } " did not complete` ) ;
223+ await expectNothingPurged ( ) ;
224+ } ) ;
225+ } ) ;
0 commit comments