@@ -233,6 +233,11 @@ async function refusalOf(run: () => Promise<unknown>): Promise<unknown> {
233233
234234/** ADR-0137 D2: the rule has no verdict over a header that is not there. */
235235const UNEVALUABLE = { status : 400 , code : 'VALIDATION_FAILED' , fields : [ { field : '_record' , code : 'rule_violation' } ] } ;
236+ /**
237+ * [#29029] A header the write itself names is judged by the reference check
238+ * before the record's rules: the answer is the master-detail field's.
239+ */
240+ const NOT_FOUND = { status : 400 , code : 'VALIDATION_FAILED' , fields : [ { field : 'invoice' , code : 'reference_not_found' } ] } ;
236241
237242/** The rule's own verdict: it read the header's stored state and refused (the bulk path adds the row id). */
238243function expectRuleVerdict ( answer : Answer ) : void {
@@ -251,6 +256,8 @@ describe('[#22892] a predicate rule reading its master through the master-detail
251256
252257 type Door = 'insert' | 'batch insert' | 'update by id' | 'repoint by id' | 'bulk update' ;
253258 const DOORS : Door [ ] = [ 'insert' , 'batch insert' , 'update by id' , 'repoint by id' , 'bulk update' ] ;
259+ /** [#29029] The doors whose write names the header; the other two carry a stored one. */
260+ const namesHeader = ( door : Door ) => door === 'insert' || door === 'batch insert' || door === 'repoint by id' ;
254261
255262 /** One write through `door` that lands the line under `header`. */
256263 const write = ( door : Door , header : string , context : Record < string , unknown > = ALICE ) => {
@@ -275,8 +282,12 @@ describe('[#22892] a predicate rule reading its master through the master-detail
275282 const before = [ ...storeFor ( LINE ) . values ( ) ] ;
276283 const nowhere = await refusalOf ( ( ) => write ( door , NOWHERE ) ) ;
277284 expect ( nowhere ) . toBeInstanceOf ( ValidationError ) ;
278- expect ( answerOf ( nowhere , NOWHERE ) ) . toMatchObject ( UNEVALUABLE ) ;
279- expect ( answerOf ( nowhere , NOWHERE ) . message ) . toContain ( 'the related record was not found' ) ;
285+ if ( namesHeader ( door ) ) {
286+ expect ( answerOf ( nowhere , NOWHERE ) ) . toMatchObject ( NOT_FOUND ) ;
287+ } else {
288+ expect ( answerOf ( nowhere , NOWHERE ) ) . toMatchObject ( UNEVALUABLE ) ;
289+ expect ( answerOf ( nowhere , NOWHERE ) . message ) . toContain ( 'the related record was not found' ) ;
290+ }
280291 for ( const header of UNREADABLE ) {
281292 expect ( answerOf ( await refusalOf ( ( ) => write ( door , header ) ) , header ) ) . toEqual ( answerOf ( nowhere , NOWHERE ) ) ;
282293 }
@@ -289,9 +300,13 @@ describe('[#22892] a predicate rule reading its master through the master-detail
289300
290301 it . each ( DOORS ) ( '%s — a refused read of the header object answers as a header that exists nowhere' , async ( door ) => {
291302 const nowhere = answerOf ( await refusalOf ( ( ) => write ( door , NOWHERE , CAROL ) ) , NOWHERE ) ;
292- expect ( nowhere ) . toMatchObject ( UNEVALUABLE ) ;
303+ expect ( nowhere ) . toMatchObject ( namesHeader ( door ) ? NOT_FOUND : UNEVALUABLE ) ;
293304 // Alice's headers exist; Carol's read door refuses the object, whatever they hold.
294- for ( const header of [ 'h_mine_open' , 'h_mine_locked' ] ) {
305+ // [#29029] The batch's first row and the repoint's stored line are
306+ // `h_mine_open` itself, the door's own fixture: those doors take the
307+ // locked header only.
308+ const headers = door === 'batch insert' || door === 'repoint by id' ? [ 'h_mine_locked' ] : [ 'h_mine_open' , 'h_mine_locked' ] ;
309+ for ( const header of headers ) {
295310 expect ( answerOf ( await refusalOf ( ( ) => write ( door , header , CAROL ) ) , header ) ) . toEqual ( nowhere ) ;
296311 }
297312 } ) ;
@@ -302,7 +317,7 @@ describe('[#22892] a predicate rule reading its master through the master-detail
302317 return ( out . results ?. [ 0 ] ?. errors ?? [ ] ) . map ( ( e : any ) => ( { field : e . field , code : e . code , message : String ( e . message ) . split ( header ) . join ( 'ID' ) } ) ) ;
303318 } ;
304319 const nowhere = await preview ( NOWHERE ) ;
305- expect ( nowhere ) . toEqual ( [ expect . objectContaining ( { field : '_record ' , code : 'rule_violation ' } ) ] ) ;
320+ expect ( nowhere ) . toEqual ( [ expect . objectContaining ( { field : 'invoice ' , code : 'reference_not_found ' } ) ] ) ;
306321 for ( const header of UNREADABLE ) expect ( await preview ( header ) ) . toEqual ( nowhere ) ;
307322 // CONTROL: a readable header is judged on its stored state.
308323 expect ( await preview ( 'h_mine_locked' ) ) . toEqual ( [ { field : '_record' , code : 'rule_violation' , message : RULE_MESSAGE } ] ) ;
@@ -320,7 +335,7 @@ describe('[#22892] a predicate rule reading its master through the master-detail
320335 expect ( out . map ( ( o ) => o . ok ) ) . toEqual ( [ false , false , false , false , true ] ) ;
321336 const errors = out . map ( ( o ) => ( o . ok ? undefined : o . error ) ) ;
322337 const nowhere = answerOf ( errors [ 2 ] , NOWHERE ) ;
323- expect ( nowhere ) . toMatchObject ( UNEVALUABLE ) ;
338+ expect ( nowhere ) . toMatchObject ( NOT_FOUND ) ;
324339 expect ( answerOf ( errors [ 0 ] , 'h_other_open' ) ) . toEqual ( nowhere ) ;
325340 expect ( answerOf ( errors [ 1 ] , 'h_other_locked' ) ) . toEqual ( nowhere ) ;
326341 expectRuleVerdict ( answerOf ( errors [ 3 ] , 'h_mine_locked' ) ) ;
@@ -329,8 +344,10 @@ describe('[#22892] a predicate rule reading its master through the master-detail
329344
330345 it ( 'the mechanism: the caller\'s read door first, and no elevated read of a header it did not return' , async ( ) => {
331346 await refusalOf ( ( ) => engine . insert ( LINE , { invoice : 'h_other_locked' , quantity : 1 } , { context : ALICE } as any ) ) ;
347+ // The rule's binding read and the reference check's [#29029], each through the read door.
332348 expect ( headerReads ) . toEqual ( [
333349 { operation : 'find' , elevated : false , where : { id : 'h_other_locked' } } ,
350+ { operation : 'find' , elevated : false , where : { id : 'h_other_locked' } } ,
334351 ] ) ;
335352 headerReads . length = 0 ;
336353 await engine . insert ( LINE , { invoice : 'h_mine_open' , quantity : 1 } , { context : ALICE } as any ) ;
0 commit comments