1616// - the copy GRANTS: a holder of it resolves its object grants;
1717// - the source is read from the catalog, not from a `sys_permission_set` row:
1818// the source's rows are removed first, and the clone still lands;
19- // - a caller without `manage_metadata` is refused 403 and nothing is saved.
19+ // - a caller without `manage_metadata` is refused 403 and nothing is saved;
20+ // - [ADR-0146 D3/D6] "new similar": a copy of a PACKAGED set with no target
21+ // lands in the source package's paired `<X>.custom`, created by N-13's
22+ // ensure door on the first copy and reused on the next — the stored row
23+ // bound to the pair, the item itself naming no package — and is granted
24+ // through a position AND a direct assignment by its bare name; a given
25+ // target is judged (writable and in closure lands, read-only / unknown /
26+ // out-of-closure are refused with no row); an environment-authored source
27+ // still copies environment-wide.
2028
2129import { describe , it , expect , beforeAll , afterAll } from 'vitest' ;
2230import { resolveUserAuthzGrants , securityCatalogReaderOf } from '@objectstack/core' ;
@@ -34,6 +42,12 @@ const DEFAULT_COPY = 'my_member_default';
3442const MEMBER_EMAIL = 'clone.member@example.com' ;
3543const HOLDER_EMAIL = 'clone.holder@example.com' ;
3644const COPY_POSITION = 'clone_door_position' ;
45+ /** The showcase's manifest id, and its paired customization package (ADR-0146 D3). */
46+ const SHOWCASE = 'com.example.showcase' ;
47+ const SHOWCASE_PAIR = `${ SHOWCASE } .custom` ;
48+ /** A Studio-created writable package that declares the showcase, and one that declares nothing. */
49+ const IN_CLOSURE = 'com.acme.clone-in-closure' ;
50+ const OUT_OF_CLOSURE = 'com.acme.clone-out-of-closure' ;
3751
3852interface Engine {
3953 find ( object : string , options ?: unknown ) : Promise < Array < Record < string , unknown > > > ;
@@ -82,17 +96,35 @@ describe('the permission-set clone door, on a booted showcase', () => {
8296 // stage 8 retired the `sys_permission_set` row object.
8397 expect ( ql . getObject ?.( 'sys_permission_set' ) ) . toBeFalsy ( ) ;
8498
99+ // Anti-vacuity for the landing: the source is the showcase's, and no
100+ // pair exists before the first copy.
101+ expect ( source ?. packageId ) . toBe ( SHOWCASE ) ;
102+ expect ( ( await stack . apiAs ( token , 'GET' , `/packages/${ SHOWCASE_PAIR } ` ) ) . status ) . toBe ( 404 ) ;
103+
85104 const res = await clone ( token , DELEGATE_SET , { name : DELEGATE_COPY , label : 'My Field Ops Delegate' } ) ;
86105 const text = await res . clone ( ) . text ( ) ;
87106 expect ( res . status , text ) . toBe ( 200 ) ;
88- const data = JSON . parse ( text ) . data as { permissionSet : Record < string , unknown > ; notice : string } ;
107+ const data = JSON . parse ( text ) . data as { permissionSet : Record < string , unknown > ; package : string | null ; notice : string } ;
89108 expect ( data . permissionSet . name ) . toBe ( DELEGATE_COPY ) ;
90109 expect ( data . permissionSet ) . not . toHaveProperty ( 'adminScope' ) ;
110+ expect ( data . permissionSet ) . not . toHaveProperty ( 'packageId' ) ;
91111 expect ( data . notice ) . toContain ( 'adminScope' ) ;
112+ // [ADR-0146 D3/D6] The copy landed in the showcase's pair, which the
113+ // ensure door created for it.
114+ expect ( data . package ) . toBe ( SHOWCASE_PAIR ) ;
115+ expect ( data . notice ) . toContain ( `'${ SHOWCASE_PAIR } '` ) ;
116+ const pair = await stack . apiAs ( token , 'GET' , `/packages/${ SHOWCASE_PAIR } ` ) ;
117+ expect ( pair . status , await pair . clone ( ) . text ( ) ) . toBe ( 200 ) ;
118+
119+ // The stored row is bound to the pair (its `package_id`), and the item
120+ // names no package of its own.
121+ const rows = await ql . find ( 'sys_metadata' , { where : { type : 'permission' , name : DELEGATE_COPY } , context : SYSTEM_CTX } ) ;
122+ expect ( rows . map ( ( r ) => r . package_id ) ) . toEqual ( [ SHOWCASE_PAIR ] ) ;
123+ expect ( JSON . parse ( String ( rows [ 0 ] . metadata ) ) ) . not . toHaveProperty ( 'packageId' ) ;
92124
93125 const copy = await reader ! . resolve ( 'permission' , DELEGATE_COPY ) ;
94126 expect ( copy ?. name , 'the copy does not resolve through the catalog' ) . toBe ( DELEGATE_COPY ) ;
95- expect ( copy ?. packageId ) . toBeUndefined ( ) ;
127+ expect ( copy ?. packageId ) . toBe ( SHOWCASE_PAIR ) ;
96128 expect ( copy ?. definition ) . not . toHaveProperty ( 'adminScope' ) ;
97129 expect ( copy ?. definition . label ) . toBe ( 'My Field Ops Delegate' ) ;
98130 // The facets came across whole.
@@ -106,9 +138,16 @@ describe('the permission-set clone door, on a booted showcase', () => {
106138
107139 const res = await clone ( token , DEFAULT_SET , { name : DEFAULT_COPY , label : 'My Member Default' } ) ;
108140 expect ( res . status , await res . clone ( ) . text ( ) ) . toBe ( 200 ) ;
141+ // The second copy REUSES the pair the first created: one pair, the same id.
142+ expect ( ( ( await res . json ( ) ) as any ) . data . package ) . toBe ( SHOWCASE_PAIR ) ;
143+ const listed = ( ( await ( await stack . apiAs ( token , 'GET' , '/packages' ) ) . json ( ) ) as any ) . data ;
144+ const pairs = ( Array . isArray ( listed ) ? listed : listed ?. packages ?? [ ] )
145+ . filter ( ( p : any ) => ( p ?. manifest ?. id ?? p ?. id ) === SHOWCASE_PAIR ) ;
146+ expect ( pairs ) . toHaveLength ( 1 ) ;
109147
110148 const copy = await reader . resolve ( 'permission' , DEFAULT_COPY ) ;
111149 expect ( copy ?. name ) . toBe ( DEFAULT_COPY ) ;
150+ expect ( copy ?. packageId ) . toBe ( SHOWCASE_PAIR ) ;
112151 expect ( copy ?. definition . isDefault === undefined || copy ?. definition . isDefault === false ) . toBe ( true ) ;
113152 expect ( copy ?. definition . objects ) . toEqual ( source ! . definition . objects ) ;
114153
@@ -131,6 +170,73 @@ describe('the permission-set clone door, on a booted showcase', () => {
131170 expect ( grants . permissions ) . toContain ( DEFAULT_COPY ) ;
132171 } ) ;
133172
173+ it ( 'a copy in the pair is granted through a direct assignment too, by its bare name' , async ( ) => {
174+ const email = 'clone.direct@example.com' ;
175+ await stack . signUp ( email ) ;
176+ const [ user ] = await ql . find ( 'sys_user' , { where : { email } , limit : 1 , context : SYSTEM_CTX } ) ;
177+ expect ( user ?. id , `no sys_user row for ${ email } ` ) . toBeTruthy ( ) ;
178+ const tenantId = await sessionOrgOf ( stack ) ;
179+ // Anti-vacuity: the user holds no copy before the assignment.
180+ const before = await resolveUserAuthzGrants ( ql as never , String ( user . id ) , { tenantId } ) ;
181+ expect ( before . permissions ) . not . toContain ( DELEGATE_COPY ) ;
182+ await ql . insert (
183+ 'sys_user_permission_set' ,
184+ { id : 'ups_clone_door_direct' , user_id : String ( user . id ) , permission_set : DELEGATE_COPY } ,
185+ { context : SYSTEM_CTX } ,
186+ ) ;
187+ const after = await resolveUserAuthzGrants ( ql as never , String ( user . id ) , { tenantId } ) ;
188+ expect ( after . permissions ) . toContain ( DELEGATE_COPY ) ;
189+ } ) ;
190+
191+ it ( 'a given writable target inside the closure lands there; read-only, unknown and out-of-closure targets are refused with no row' , async ( ) => {
192+ const reader = securityCatalogReaderOf ( ql ) ! ;
193+ for ( const [ id , namespace , dependencies ] of [
194+ [ IN_CLOSURE , 'clone_in' , { [ SHOWCASE ] : '*' } ] ,
195+ [ OUT_OF_CLOSURE , 'clone_out' , { } ] ,
196+ ] as const ) {
197+ const created = await stack . apiAs ( token , 'POST' , '/packages' , {
198+ manifest : { id, name : id , version : '0.1.0' , type : 'app' , namespace, dependencies } ,
199+ } ) ;
200+ expect ( created . status , await created . clone ( ) . text ( ) ) . toBe ( 201 ) ;
201+ }
202+
203+ const landed = await clone ( token , DEFAULT_SET , { name : 'in_closure_copy' , label : 'In Closure' , package : IN_CLOSURE } ) ;
204+ expect ( landed . status , await landed . clone ( ) . text ( ) ) . toBe ( 200 ) ;
205+ expect ( ( ( await landed . json ( ) ) as any ) . data . package ) . toBe ( IN_CLOSURE ) ;
206+ expect ( ( await reader . resolve ( 'permission' , 'in_closure_copy' ) ) ?. packageId ) . toBe ( IN_CLOSURE ) ;
207+
208+ const refused : Array < [ string , string , number , string ] > = [
209+ [ 'read_only_copy' , SHOWCASE , 422 , 'WRITABLE_PACKAGE_REQUIRED' ] ,
210+ [ 'unknown_copy' , 'com.acme.no_such_package' , 422 , 'WRITABLE_PACKAGE_REQUIRED' ] ,
211+ [ 'out_closure_copy' , OUT_OF_CLOSURE , 409 , 'RESOURCE_CONFLICT' ] ,
212+ ] ;
213+ for ( const [ name , target , status , code ] of refused ) {
214+ const res = await clone ( token , DEFAULT_SET , { name, label : 'X' , package : target } ) ;
215+ const text = await res . clone ( ) . text ( ) ;
216+ expect ( res . status , `${ target } : ${ text } ` ) . toBe ( status ) ;
217+ expect ( ( ( await res . json ( ) ) as any ) . error ?. code , target ) . toBe ( code ) ;
218+ expect ( await reader . resolve ( 'permission' , name ) , target ) . toBeUndefined ( ) ;
219+ const rows = await ql . find ( 'sys_metadata' , { where : { type : 'permission' , name } , context : SYSTEM_CTX } ) ;
220+ expect ( rows , target ) . toEqual ( [ ] ) ;
221+ }
222+ } ) ;
223+
224+ it ( 'CONTROL — an environment-authored source still copies environment-wide, with no package' , async ( ) => {
225+ const saved = await stack . apiAs ( token , 'PUT' , '/meta/permission/env_authored_source' , {
226+ name : 'env_authored_source' , label : 'Env Authored' , objects : { showcase_task : { allowRead : true } } ,
227+ } ) ;
228+ expect ( saved . status , await saved . clone ( ) . text ( ) ) . toBe ( 200 ) ;
229+ const source = await securityCatalogReaderOf ( ql ) ! . resolve ( 'permission' , 'env_authored_source' ) ;
230+ expect ( source ?. packageId , 'the control source is packaged' ) . toBeUndefined ( ) ;
231+
232+ const res = await clone ( token , 'env_authored_source' , { name : 'env_authored_copy' , label : 'Env Authored Copy' } ) ;
233+ expect ( res . status , await res . clone ( ) . text ( ) ) . toBe ( 200 ) ;
234+ expect ( ( ( await res . json ( ) ) as any ) . data . package ) . toBeNull ( ) ;
235+ expect ( ( await securityCatalogReaderOf ( ql ) ! . resolve ( 'permission' , 'env_authored_copy' ) ) ?. packageId ) . toBeUndefined ( ) ;
236+ const rows = await ql . find ( 'sys_metadata' , { where : { type : 'permission' , name : 'env_authored_copy' } , context : SYSTEM_CTX } ) ;
237+ expect ( rows . map ( ( r ) => r . package_id ?? null ) ) . toEqual ( [ null ] ) ;
238+ } ) ;
239+
134240 it ( 'a taken name is 409, an unknown source 404, a bad name 400' , async ( ) => {
135241 const taken = await clone ( token , DEFAULT_SET , { name : DELEGATE_SET , label : 'X' } ) ;
136242 expect ( taken . status , await taken . clone ( ) . text ( ) ) . toBe ( 409 ) ;
0 commit comments