@@ -402,11 +402,30 @@ async function makeCommitEngine() {
402402 if ( String ( ctx . input . name ) . startsWith ( 'veto' ) ) throw new Error ( 'refused by the gate' ) ;
403403 } ,
404404 } as unknown as Hook ;
405- const bind = ( withGate : boolean ) => bindHooksToEngine ( engine , withGate ? [ asyncNote , gate ] : [ asyncNote ] , { packageId : 'app:t' , logger : logger as any } ) ;
405+ // [#33760 review] A `'user'` pin on a trigger that carries no user: the
406+ // handler's `ctx.api` is the unscoped api (`UnscopedHookApi`), which has no
407+ // engine and no detach of its own. It records the api it got, and touches no
408+ // data (that api refuses every data door).
409+ const apiKinds : string [ ] = [ ] ;
410+ const userPin = {
411+ name : 'user_pin' , object : 'rec' , events : [ 'afterInsert' ] , async : true , runAs : 'user' , onError : 'log' ,
412+ handler : ( ctx : any ) => {
413+ const name = String ( ctx . input . name ) ;
414+ events . push ( `user-pin started:${ name } ` ) ;
415+ apiKinds . push ( ctx . api ?. constructor ?. name ) ;
416+ runOf ( name ) ;
417+ settle [ name ] ! ( 0 ) ;
418+ } ,
419+ } as unknown as Hook ;
420+ const bind = ( withGate : boolean , opts : { userPin ?: boolean } = { } ) => bindHooksToEngine (
421+ engine ,
422+ opts . userPin ? [ userPin ] : withGate ? [ asyncNote , gate ] : [ asyncNote ] ,
423+ { packageId : 'app:t' , logger : logger as any } ,
424+ ) ;
406425 const api = new ScopedContext ( { isSystem : true } as any , engine as any ) ;
407426 const droppedRun = ( ) => new Promise < string > ( ( r ) => { dropped = r ; } ) ;
408427 const notes = ( ) => driver . rowsOf ( 'note' ) . map ( ( r : any ) => r . name ) ;
409- return { engine, driver, api, events, drops, bind, runOf, droppedRun, notes } ;
428+ return { engine, driver, api, events, drops, bind, runOf, droppedRun, notes, apiKinds } ;
410429}
411430
412431describe ( '#33760 — an async after-hook fired inside a unit runs on its commit, never on its rollback' , ( ) => {
@@ -531,6 +550,31 @@ describe('#33760 — an async after-hook fired inside a unit runs on its commit,
531550 expect ( notes ( ) ) . toEqual ( [ ] ) ;
532551 } ) ;
533552
553+ it ( 'runAs \'user\' on a user-less system write in engine.transaction(): the unscoped api still waits for the commit' , async ( ) => {
554+ const { engine, events, bind, runOf, apiKinds } = await makeCommitEngine ( ) ;
555+ bind ( false , { userPin : true } ) ;
556+ await engine . transaction ( async ( trx : any ) => {
557+ await engine . insert ( 'rec' , { name : 'r1' } , { context : { ...trx , isSystem : true } } ) ;
558+ events . push ( 'callback returns' ) ;
559+ } ) ;
560+ await runOf ( 'r1' ) ;
561+ expect ( events ) . toEqual ( [ 'callback returns' , 'commit' , 'user-pin started:r1' ] ) ;
562+ expect ( apiKinds ) . toEqual ( [ 'UnscopedHookApi' ] ) ;
563+ } ) ;
564+
565+ it ( 'runAs \'user\' on a user-less system write in engine.transaction() that rolls back: the hook never starts' , async ( ) => {
566+ const { engine, events, drops, bind, droppedRun } = await makeCommitEngine ( ) ;
567+ bind ( false , { userPin : true } ) ;
568+ const drop = droppedRun ( ) ;
569+ await expect ( engine . transaction ( async ( trx : any ) => {
570+ await engine . insert ( 'rec' , { name : 'r1' } , { context : { ...trx , isSystem : true } } ) ;
571+ throw new Error ( 'the caller rolls back' ) ;
572+ } ) ) . rejects . toThrow ( 'the caller rolls back' ) ;
573+ expect ( await drop ) . toBe ( 'user_pin' ) ;
574+ expect ( events ) . toEqual ( [ 'rollback' ] ) ;
575+ expect ( drops ) . toEqual ( [ 'user_pin' ] ) ;
576+ } ) ;
577+
534578 it ( 'CONTROL outside any unit: the hook starts at dispatch, before the write returns, unchanged' , async ( ) => {
535579 const { driver, api, events, bind, runOf } = await makeCommitEngine ( ) ;
536580 bind ( false ) ;
0 commit comments