44
55import { describe , it , expect , vi } from 'vitest' ;
66import { assertEngineUpdateDispatch } from '@objectstack/metadata-core' ;
7- import { OWNER_FIELD_DEF } from '@objectstack/spec/data' ;
7+ import { OWNER_FIELD_DEF , QUEUE_FIELD_DEF } from '@objectstack/spec/data' ;
88import {
99 CHANGE_OWNER_DISABLED ,
1010 CHANGE_OWNER_NOTIFICATION_TOPIC ,
@@ -19,8 +19,6 @@ import { recordNotFoundError } from '../utils/record-not-found.js';
1919
2020type Row = Record < string , any > ;
2121
22- const QUEUE_FIELD_DEF = { type : 'text' , system : true } ;
23-
2422// Registered shapes, as `applySystemFields` leaves them: the owner column and
2523// the queue column ride together on an owner-bearing object (ADR-0142 D3).
2624const lead = { name : 'lead' , label : 'Lead' , sharingModel : 'private' , fields : { name : { type : 'text' } , owner_id : OWNER_FIELD_DEF , queue : QUEUE_FIELD_DEF } } ;
@@ -31,6 +29,19 @@ const orgOwned = { name: 'setting_row', ownership: 'org', fields: { name: { type
3129const managed = { name : 'auth_session' , managedBy : 'better-auth' , fields : { user_id : { type : 'lookup' , reference : 'sys_user' } } } ;
3230const platform = { name : 'sys_team' , fields : { name : { type : 'text' } } } ;
3331const noQueue = { name : 'memo' , fields : { owner_id : OWNER_FIELD_DEF } } ;
32+ // [#36262] An external (ADR-0015) object with a provable owner: the injection
33+ // still registers `queue` beside `owner_id`, with no column behind it.
34+ const extAccount = {
35+ name : 'ext_account' , label : 'Account' , sharingModel : 'private' , external : { remoteName : 'accounts' } ,
36+ fields : { name : { type : 'text' } , owner_id : { type : 'lookup' , reference : 'sys_user' , label : 'Account owner' } , queue : QUEUE_FIELD_DEF } ,
37+ } ;
38+ // [#36262] The registered list shape of `fields`, the platform's queue column among them.
39+ const listed = {
40+ name : 'listed_lead' , sharingModel : 'private' ,
41+ fields : [ { name : 'name' , type : 'text' } , { name : 'owner_id' , ...OWNER_FIELD_DEF } , { name : 'queue' , ...QUEUE_FIELD_DEF } ] ,
42+ } ;
43+ // [#36262] A field the author named `queue`: theirs, not the ownership queue.
44+ const authoredQueue = { name : 'ticket' , sharingModel : 'private' , fields : { owner_id : OWNER_FIELD_DEF , queue : { type : 'text' , label : 'Support queue' } } } ;
3445// [#35315 phase 2] The cascade's children of `lead`: one declared cascade, one
3546// plain lookup, and — registered raw, past the parse that refuses it — a
3647// cascade on a controlled_by_parent detail, which is never a candidate.
@@ -39,7 +50,7 @@ const leadCall = { name: 'lead_call', sharingModel: 'private', fields: { lead: {
3950const leadLine = { name : 'lead_line' , sharingModel : 'controlled_by_parent' , fields : { lead : { type : 'master_detail' , reference : 'lead' , transferBehavior : 'cascade' } , owner_id : OWNER_FIELD_DEF } } ;
4051
4152const SCHEMAS : Record < string , unknown > = Object . fromEntries (
42- [ lead , pinned , line , lineOptedIn , orgOwned , managed , platform , noQueue , leadTask , leadCall , leadLine ] . map ( ( o ) => [ o . name , o ] ) ,
53+ [ lead , pinned , line , lineOptedIn , orgOwned , managed , platform , noQueue , extAccount , listed , authoredQueue , leadTask , leadCall , leadLine ] . map ( ( o ) => [ o . name , o ] ) ,
4354) ;
4455
4556const CALLER = { userId : 'u_manager' , tenantId : 'org_1' } ;
@@ -66,6 +77,9 @@ function fakeEngine(opts: {
6677 lead : [ { id : 'l1' , owner_id : 'u_a' } , { id : 'l2' , owner_id : 'u_b' } , { id : 'l3' , owner_id : 'u_a' } ] ,
6778 contract_line : [ { id : 'c1' , owner_id : 'u_a' } ] ,
6879 memo : [ { id : 'm1' , owner_id : 'u_a' } ] ,
80+ ext_account : [ { id : 'x1' , owner_id : 'u_a' } ] ,
81+ listed_lead : [ { id : 'd1' , owner_id : 'u_a' } ] ,
82+ ticket : [ { id : 'k1' , owner_id : 'u_a' } ] ,
6983 lead_task : [
7084 { id : 't1' , lead : 'l1' , owner_id : 'u_a' } ,
7185 { id : 't2' , lead : 'l1' , owner_id : 'u_b' } ,
@@ -199,7 +213,7 @@ describe('[#35402] changeRecordOwner — one record', () => {
199213
200214 it ( 'a queue target on an object with no queue column is refused before any write' , async ( ) => {
201215 const engine = fakeEngine ( ) ;
202- const err = await refusalOf ( changeRecordOwner ( engine , 'memo ' , 'm1 ' , { queue : 'triage' } , CALLER ) ) ;
216+ const err = await refusalOf ( changeRecordOwner ( engine , 'ext_account ' , 'x1 ' , { queue : 'triage' } , CALLER ) ) ;
203217 expect ( err ) . toBeInstanceOf ( UserWorkTransferRefusal ) ;
204218 expect ( { code : err . code , status : err . status } ) . toEqual ( { code : 'VALIDATION_ERROR' , status : 400 } ) ;
205219 expect ( engine . calls . some ( ( c ) => c . op === 'update' ) ) . toBe ( false ) ;
@@ -245,6 +259,47 @@ describe('[#35402] changeRecordOwner — one record', () => {
245259 } ) ;
246260} ) ;
247261
262+ describe ( '[#36262] a queue target asks for the queue column with storage behind it, as the engine does' , ( ) => {
263+ it ( 'an external object with an owner, whose injected queue has no column, is refused 400 before any write: single and bulk' , async ( ) => {
264+ const engine = fakeEngine ( ) ;
265+ const single = await refusalOf ( changeRecordOwner ( engine , 'ext_account' , 'x1' , { queue : 'triage' } , CALLER ) ) ;
266+ expect ( { code : single . code , status : single . status } ) . toEqual ( { code : 'VALIDATION_ERROR' , status : 400 } ) ;
267+ expect ( single . message ) . toMatch ( / ^ ' e x t _ a c c o u n t ' h a s n o o w n e r s h i p q u e u e c o l u m n / ) ;
268+ const bulk = await refusalOf ( changeRecordsOwner ( engine , 'ext_account' , [ 'x1' ] , { queue : 'triage' } , CALLER ) ) ;
269+ expect ( { code : bulk . code , status : bulk . status } ) . toEqual ( { code : 'VALIDATION_ERROR' , status : 400 } ) ;
270+ expect ( engine . calls . some ( ( c ) => c . op === 'update' ) ) . toBe ( false ) ;
271+ expect ( engine . tables . ext_account [ 0 ] ) . toEqual ( { id : 'x1' , owner_id : 'u_a' } ) ;
272+ } ) ;
273+
274+ it ( 'the external object still takes a person target: only the queue column is missing' , async ( ) => {
275+ const result = await changeRecordOwner ( fakeEngine ( ) , 'ext_account' , 'x1' , { ownerId : 'u_next' } , CALLER ) ;
276+ expect ( result ) . toEqual ( { object : 'ext_account' , id : 'x1' , ownerId : 'u_next' , notified : false } ) ;
277+ } ) ;
278+
279+ it ( 'list-shaped fields with the platform\'s queue column are admitted, single and bulk' , async ( ) => {
280+ const engine = fakeEngine ( ) ;
281+ const result = await changeRecordOwner ( engine , 'listed_lead' , 'd1' , { queue : 'triage' } , CALLER ) ;
282+ expect ( result ) . toEqual ( { object : 'listed_lead' , id : 'd1' , ownerId : null , queue : 'triage' , notified : false } ) ;
283+ const report = await changeRecordsOwner ( engine , 'listed_lead' , [ 'd1' ] , { queue : 'triage' } , CALLER ) ;
284+ expect ( report ) . toMatchObject ( { updated : 1 , failed : 0 } ) ;
285+ } ) ;
286+
287+ it ( 'control: a local object with the provisioned queue column is admitted, whether its document lists the column or the injection adds it' , async ( ) => {
288+ for ( const [ object , id ] of [ [ 'lead' , 'l1' ] , [ 'memo' , 'm1' ] ] as const ) {
289+ const engine = fakeEngine ( ) ;
290+ const result = await changeRecordOwner ( engine , object , id , { queue : 'triage' } , CALLER ) ;
291+ expect ( result , object ) . toEqual ( { object, id, ownerId : null , queue : 'triage' , notified : false } ) ;
292+ }
293+ } ) ;
294+
295+ it ( 'a field the author named queue is not the ownership queue: refused 400 before any write' , async ( ) => {
296+ const engine = fakeEngine ( ) ;
297+ const err = await refusalOf ( changeRecordOwner ( engine , 'ticket' , 'k1' , { queue : 'triage' } , CALLER ) ) ;
298+ expect ( { code : err . code , status : err . status } ) . toEqual ( { code : 'VALIDATION_ERROR' , status : 400 } ) ;
299+ expect ( engine . calls . some ( ( c ) => c . op === 'update' ) ) . toBe ( false ) ;
300+ } ) ;
301+ } ) ;
302+
248303describe ( '[#35402] changeRecordsOwner — the bulk form' , ( ) => {
249304 it ( 'a mixed batch: one moves, one refused by the owner guard, one out of reach — every failure listed with its id' , async ( ) => {
250305 const engine = fakeEngine ( { refuse : ( id ) => ( id === 'l2' ? transferDenied ( ) : undefined ) } ) ;
0 commit comments