1111// depth) got the contact slice's rows filtered at the field zoo's `own` depth —
1212// 1 row beside `total: 26` — whenever the contact slice was not the last one.
1313//
14- // The pin: a non-admin gets min(top, total) rows for every slice whatever its
15- // position. CONTROLS: the same slices one at a time, and the seeded admin
16- // (whose reads take no depth at all).
14+ // The pin: each related slice equals its own read alone, whatever its
15+ // position — a non-admin gets min(top, total) rows for every slice, and on a
16+ // page whose slices sit on a `private` object (`showcase_private_note`) beside
17+ // an `org`-depth one (`showcase_contact`) each slice's ids and total are the
18+ // ones it answers alone, in both orders. CONTROLS: the same slices one at a
19+ // time, and the seeded admin (whose reads take no depth at all).
1720
1821import { describe , it , expect , beforeAll , afterAll } from 'vitest' ;
1922import { type VerifyStack } from '@objectstack/verify' ;
@@ -22,6 +25,9 @@ import { bootShowcase } from './showcase-boot.js';
2225const SYS = { context : { isSystem : true } } as const ;
2326const CONTACT = { object : 'showcase_contact' , field : 'account' , top : 3 } as const ;
2427const ZOO = { object : 'showcase_field_zoo' , field : 'f_lookup' , top : 3 } as const ;
28+ // Parent = a plain member (bob); both children point back at `sys_user` by `owner_id`.
29+ const NOTE = { object : 'showcase_private_note' , field : 'owner_id' , top : 50 } as const ;
30+ const CONTACT_BY_OWNER = { object : 'showcase_contact' , field : 'owner_id' , top : 3 } as const ;
2531
2632type Slice = { object : string ; field : string ; total ?: number ; records ?: unknown [ ] ; error ?: unknown } ;
2733
@@ -31,16 +37,19 @@ describe('#33051 record page related slices — each slice reads at its own obje
3137 let adminTok = '' ;
3238 let managerTok = '' ;
3339 let northwindId = '' ;
40+ let bobId = '' ;
3441
35- const page = async ( token : string , related : ReadonlyArray < typeof CONTACT | typeof ZOO > ) : Promise < Slice [ ] > => {
36- const res = await stack . apiAs ( token , 'POST' , `/data/showcase_account/${ northwindId } /page` , { related } ) ;
42+ type Entry = { object : string ; field : string ; top : number } ;
43+ const pageOf = async ( token : string , object : string , id : string , related : ReadonlyArray < Entry > ) : Promise < Slice [ ] > => {
44+ const res = await stack . apiAs ( token , 'POST' , `/data/${ object } /${ id } /page` , { related } ) ;
3745 const text = await res . text ( ) ;
3846 expect ( res . status , text ) . toBe ( 200 ) ;
3947 const json = JSON . parse ( text ) ;
4048 return ( json . data ?? json ) . related as Slice [ ] ;
4149 } ;
42- const sliceOf = ( slices : Slice [ ] , object : string ) : Slice => {
43- const slice = slices . find ( ( s ) => s . object === object ) ;
50+ const page = ( token : string , related : ReadonlyArray < Entry > ) => pageOf ( token , 'showcase_account' , northwindId , related ) ;
51+ const sliceOf = ( slices : Slice [ ] , object : string , field ?: string ) : Slice => {
52+ const slice = slices . find ( ( s ) => s . object === object && ( field === undefined || s . field === field ) ) ;
4453 expect ( slice , `a ${ object } slice` ) . toBeTruthy ( ) ;
4554 expect ( slice ! . error , JSON . stringify ( slice ! . error ) ) . toBeUndefined ( ) ;
4655 return slice ! ;
@@ -70,6 +79,24 @@ describe('#33051 record page related slices — each slice reads at its own obje
7079 expect ( membership , `a sys_member row for ${ email } ` ) . toBeTruthy ( ) ;
7180 await ql . insert ( 'sys_user_position' , { user_id : user . id , position : 'showcase_manager' , organization_id : membership . organization_id } , SYS ) ;
7281 expect ( ( await stack . contextFor ( managerTok ) ) . positions ?? [ ] ) . toContain ( 'showcase_manager' ) ;
82+
83+ // bob: a plain member who owns two private notes and two contacts.
84+ const bobEmail = 'page-slices-bob@verify.test' ;
85+ const bobTok = await stack . signUp ( bobEmail , 'Rel!Pass12345' , 'Page slices bob' ) ;
86+ const [ bob ] = await ql . find ( 'sys_user' , { where : { email : bobEmail } , limit : 1 } , SYS ) ;
87+ bobId = String ( bob . id ) ;
88+ for ( const title of [ 'bob note 1' , 'bob note 2' ] ) {
89+ const res = await stack . apiAs ( bobTok , 'POST' , '/data/showcase_private_note' , { title } ) ;
90+ expect ( res . status , await res . clone ( ) . text ( ) ) . toBeLessThan ( 300 ) ;
91+ }
92+ let bobMember : any ;
93+ for ( let i = 0 ; i < 40 && ! bobMember ; i ++ ) {
94+ [ bobMember ] = await ql . find ( 'sys_member' , { where : { user_id : bob . id } , limit : 1 } , SYS ) ;
95+ if ( ! bobMember ) await new Promise ( ( r ) => setTimeout ( r , 250 ) ) ;
96+ }
97+ for ( const n of [ 1 , 2 ] ) {
98+ await ql . insert ( 'showcase_contact' , { name : `Bob Contact ${ n } ` , email : `bob.c${ n } @verify.test` , owner_id : bobId , organization_id : bobMember ?. organization_id } , SYS ) ;
99+ }
73100 } , 180_000 ) ;
74101
75102 afterAll ( async ( ) => {
@@ -104,11 +131,32 @@ describe('#33051 record page related slices — each slice reads at its own obje
104131 const zoo = sliceOf ( slices , ZOO . object ) ;
105132 expectFilled ( contact , CONTACT . top ) ;
106133 expectFilled ( zoo , ZOO . top ) ;
107- // Never wider either: a sibling's depth must not widen a slice .
134+ // And each slice equals its own read alone .
108135 expect ( contact . total ) . toBe ( alone . contact . total ) ;
109136 expect ( zoo . total ) . toBe ( alone . zoo . total ) ;
110137 expect ( ( zoo . records as any [ ] ) . map ( ( r ) => r . id ) . sort ( ) ) . toEqual ( ( alone . zoo . records as any [ ] ) . map ( ( r ) => r . id ) . sort ( ) ) ;
111138 }
112139 }
113140 } ) ;
141+
142+ it ( 'each related slice equals its own read alone, whatever its position (a private-object slice beside an org-depth one)' , async ( ) => {
143+ const ids = ( slice : Slice ) => ( slice . records ?? [ ] ) . map ( ( r : any ) => String ( r . id ) ) . sort ( ) ;
144+ const aloneNote = sliceOf ( await pageOf ( managerTok , 'sys_user' , bobId , [ NOTE ] ) , NOTE . object , NOTE . field ) ;
145+ const aloneContact = sliceOf ( await pageOf ( managerTok , 'sys_user' , bobId , [ CONTACT_BY_OWNER ] ) , CONTACT_BY_OWNER . object , CONTACT_BY_OWNER . field ) ;
146+ expect ( aloneNote . total , 'the manager reads none of bob\'s private notes alone' ) . toBe ( 0 ) ;
147+ expect ( aloneNote . records ?? [ ] ) . toHaveLength ( 0 ) ;
148+ expect ( aloneContact . total , 'bob owns two contacts' ) . toBe ( 2 ) ;
149+ for ( const related of [ [ NOTE , CONTACT_BY_OWNER ] , [ CONTACT_BY_OWNER , NOTE ] ] as const ) {
150+ for ( let round = 0 ; round < 3 ; round ++ ) {
151+ const slices = await pageOf ( managerTok , 'sys_user' , bobId , related ) ;
152+ const note = sliceOf ( slices , NOTE . object , NOTE . field ) ;
153+ const contact = sliceOf ( slices , CONTACT_BY_OWNER . object , CONTACT_BY_OWNER . field ) ;
154+ const seen = `${ related [ 0 ] . object } first: note total=${ note . total } rows=${ JSON . stringify ( ( note . records ?? [ ] ) . map ( ( r : any ) => r . title ) ) } ` ;
155+ expect ( ids ( note ) , seen ) . toEqual ( ids ( aloneNote ) ) ;
156+ expect ( note . total , seen ) . toBe ( aloneNote . total ) ;
157+ expect ( ids ( contact ) ) . toEqual ( ids ( aloneContact ) ) ;
158+ expect ( contact . total ) . toBe ( aloneContact . total ) ;
159+ }
160+ }
161+ } ) ;
114162} ) ;
0 commit comments