@@ -15,6 +15,7 @@ import {
1515 type ChangeOwnerEngine ,
1616} from './change-record-owner.js' ;
1717import { USER_WORK_TRANSFER_ROW_CAP , UserWorkTransferRefusal } from './user-assigned-work.js' ;
18+ import { recordNotFoundError } from '../utils/record-not-found.js' ;
1819
1920type Row = Record < string , any > ;
2021
@@ -50,7 +51,14 @@ const CALLER = { userId: 'u_manager', tenantId: 'org_1' };
5051 * without, answering the stored row; refused before any write, as
5152 * `ERR_READONLY_FIELD_REJECTED`, under `strictReadonlyWrites`.
5253 */
53- function fakeEngine ( opts : { refuse ?: ( id : string ) => unknown ; hiddenUsers ?: string [ ] ; locked ?: ( row : Row ) => string [ ] ; disabledPackageObjects ?: string [ ] } = { } ) {
54+ function fakeEngine ( opts : {
55+ refuse ?: ( id : string ) => unknown ;
56+ hiddenUsers ?: string [ ] ;
57+ locked ?: ( row : Row ) => string [ ] ;
58+ disabledPackageObjects ?: string [ ] ;
59+ /** Rows the write moves out of the caller's row scope: the update lands, and its read-back is `null`, as the engine answers one. */
60+ leavesScope ?: ( object : string , id : string ) => boolean ;
61+ } = { } ) {
5462 /** Per update call, in order: whether it passed `strictReadonlyWrites: true`. */
5563 const strictWrites : boolean [ ] = [ ] ;
5664 const tables : Record < string , Row [ ] > = {
@@ -99,14 +107,17 @@ function fakeEngine(opts: { refuse?: (id: string) => unknown; hiddenUsers?: stri
99107 calls . push ( { op : 'update' , object, data, where : o ?. where , context : o ?. context } ) ;
100108 strictWrites . push ( o ?. strictReadonlyWrites === true ) ;
101109 const row = ( tables [ object ] ?? [ ] ) . find ( ( r ) => r . id === o ?. where ?. id ) ;
102- if ( ! row ) return null ;
110+ // The engine's not-found gate: a by-id update reads the row first and
111+ // throws before the write when the caller cannot reach it (`engine-update.ts`).
112+ if ( ! row ) throw recordNotFoundError ( object , String ( o ?. where ?. id ) , o ?. context as Parameters < typeof recordNotFoundError > [ 2 ] ) ;
103113 const refusal = opts . refuse ?.( row . id ) ;
104114 if ( refusal ) throw refusal ;
105115 const lockedKeys = ( opts . locked ?.( row ) ?? [ ] ) . filter ( ( k ) => Object . prototype . hasOwnProperty . call ( data , k ) ) ;
106116 if ( lockedKeys . length > 0 && o ?. strictReadonlyWrites === true ) throw readonlyRejected ( object , lockedKeys ) ;
107117 const kept = Object . fromEntries ( Object . entries ( data ) . filter ( ( [ k ] ) => ! lockedKeys . includes ( k ) ) ) ;
108118 Object . assign ( row , kept ) ;
109- return row ;
119+ // A write that took the row out of the caller's scope reads back nothing, though it landed.
120+ return opts . leavesScope ?.( object , row . id ) === true ? null : row ;
110121 } ,
111122 } ;
112123 return engine ;
@@ -282,6 +293,50 @@ describe('[#35402] changeRecordsOwner — the bulk form', () => {
282293 } ) ;
283294} ) ;
284295
296+ describe ( 'a hand-away whose read-back is out of the caller\'s scope moved: the engine\'s null read-back is not "not found"' , ( ) => {
297+ // Every row of `lead` reads back `null` once written: the move took it out of the caller's scope.
298+ const scoped = ( ) => fakeEngine ( { leavesScope : ( object ) => object === 'lead' } ) ;
299+
300+ it ( 'single: answers the move and tells the new owner' , async ( ) => {
301+ const engine = scoped ( ) ;
302+ const messaging = { emit : vi . fn ( async ( ) => SENT ) } ;
303+ const result = await changeRecordOwner ( engine , 'lead' , 'l1' , { ownerId : 'u_next' } , CALLER , { notify : true , messaging } ) ;
304+ expect ( result ) . toEqual ( { object : 'lead' , id : 'l1' , ownerId : 'u_next' , notified : true } ) ;
305+ expect ( engine . tables . lead [ 0 ] ! . owner_id ) . toBe ( 'u_next' ) ;
306+ expect ( messaging . emit ) . toHaveBeenCalledTimes ( 1 ) ;
307+ } ) ;
308+
309+ it ( 'bulk: the row counts under updated, none under failed, and the notice names it' , async ( ) => {
310+ const engine = scoped ( ) ;
311+ const messaging = { emit : vi . fn ( async ( ) => SENT ) } ;
312+ const report = await changeRecordsOwner ( engine , 'lead' , [ 'l1' ] , { ownerId : 'u_next' } , CALLER , { notify : true , messaging } ) ;
313+ expect ( report ) . toMatchObject ( { updated : 1 , failed : 0 , failures : [ ] , notified : true } ) ;
314+ expect ( ( messaging . emit . mock . calls [ 0 ] as any [ ] ) [ 0 ] . payload . recordIds ) . toEqual ( [ 'l1' ] ) ;
315+ } ) ;
316+
317+ it ( 'a queue target whose read-back is null moved too' , async ( ) => {
318+ const result = await changeRecordOwner ( scoped ( ) , 'lead' , 'l1' , { queue : 'triage' } , CALLER ) ;
319+ expect ( result ) . toEqual ( { object : 'lead' , id : 'l1' , ownerId : null , queue : 'triage' , notified : false } ) ;
320+ } ) ;
321+
322+ it ( 'a cascade still runs from a moved record, and a child whose read-back is null counts as moved' , async ( ) => {
323+ const engine = fakeEngine ( { leavesScope : ( ) => true } ) ;
324+ const result = await changeRecordOwner ( engine , 'lead' , 'l1' , { ownerId : 'u_next' } , CALLER , {
325+ cascade : [ { object : 'lead_task' , field : 'lead' } ] ,
326+ } ) ;
327+ expect ( result . cascade ) . toEqual ( [ { object : 'lead_task' , field : 'lead' , updated : 2 , failed : 0 , failures : [ ] , remaining : 0 } ] ) ;
328+ expect ( engine . tables . lead_task . map ( ( r ) => [ r . id , r . owner_id ] ) ) . toEqual ( [ [ 't1' , 'u_next' ] , [ 't2' , 'u_b' ] , [ 't3' , 'u_next' ] , [ 't4' , 'u_b' ] ] ) ;
329+ } ) ;
330+
331+ it ( 'a row that is not there is the engine\'s own 404 RECORD_NOT_FOUND, thrown before the write: single refused, bulk lists it' , async ( ) => {
332+ const single = await refusalOf ( changeRecordOwner ( scoped ( ) , 'lead' , 'l9' , { ownerId : 'u_next' } , CALLER ) ) ;
333+ expect ( { code : single . code , status : single . status } ) . toEqual ( { code : 'RECORD_NOT_FOUND' , status : 404 } ) ;
334+ const report = await changeRecordsOwner ( scoped ( ) , 'lead' , [ 'l1' , 'l9' ] , { ownerId : 'u_next' } , CALLER ) ;
335+ expect ( report ) . toMatchObject ( { updated : 1 , failed : 1 } ) ;
336+ expect ( report . failures . map ( ( f ) => [ f . id , f . code ] ) ) . toEqual ( [ [ 'l9' , 'RECORD_NOT_FOUND' ] ] ) ;
337+ } ) ;
338+ } ) ;
339+
285340describe ( 'a readonlyWhen lock that holds: the answer is what was saved, not what was asked' , ( ) => {
286341 const closed = ( engine : ReturnType < typeof fakeEngine > , ids : string [ ] ) => {
287342 for ( const r of engine . tables . lead ) if ( ids . includes ( r . id ) ) r . status = 'closed' ;
0 commit comments