@@ -221,3 +221,114 @@ describe('[#28337] RestServer: the layered view of a name two packages hold, nam
221221 } , 60_000 ) ;
222222} ) ;
223223
224+ /**
225+ * [#32622, #32914, ADR-0150 D3/D5] A name two packages REGISTER (code, no
226+ * stored row), read naming no package. The plain read refuses it
227+ * `409 RESOURCE_CONFLICT` naming both as `details.candidates` (#32201). The
228+ * layered read answered two ways by composition: with an engine wired, the
229+ * door's owner step asked the plain read first and answered its refusal by the
230+ * sentence alone; with none, the door served the holder that registered first,
231+ * and so did the protocol's layered read in process, whatever the metadata
232+ * service held. Now the layered read itself refuses as the plain read does,
233+ * and the door carries the candidates, on both spellings.
234+ */
235+ describe ( '[#32622, #32914] a name two packages register, read naming no package' , ( ) => {
236+ const ORDERS = [ [ 'alpha first' , [ PKG_A , PKG_B ] ] , [ 'beta first' , [ PKG_B , PKG_A ] ] ] as const ;
237+ /** The composition the door runs in: an engine wired for the door chain or not, and the metadata service's copy. */
238+ const SHAPES = [
239+ [ 'engine wired, no service copy' , { wired : true , copy : false } ] ,
240+ [ 'no engine wired, no service copy' , { wired : false , copy : false } ] ,
241+ [ 'no engine wired, a service copy of the first holder' , { wired : false , copy : true } ] ,
242+ ] as const ;
243+
244+ async function registered ( order : readonly string [ ] , shape : { wired : boolean ; copy : boolean } ) {
245+ // The partial copy: a `metadata` service holding the first holder's body only.
246+ const copyOf = ( pkg : string ) => ( { name : 'sales' , label : `${ pkg } code` , widgets : [ ] , _packageId : pkg } ) ;
247+ const services = shape . copy
248+ ? new Map < string , unknown > ( [ [ 'metadata' , { get : async ( type : string , name : string ) => ( type === 'dashboard' && name === 'sales' ? copyOf ( order [ 0 ] ) : undefined ) } ] ] )
249+ : undefined ;
250+ const h = makeHarness ( services ) ;
251+ for ( const pkg of order ) {
252+ h . registry . installPackage ( { id : pkg , name : pkg , version : '1.0.0' } as any , 'code' ) ;
253+ h . registry . registerItem ( 'dashboard' , { name : 'sales' , label : `${ pkg } code` , widgets : [ ] } as any , 'name' as any , pkg ) ;
254+ }
255+ h . registry . installPackage ( { id : 'app.solo_demo' , name : 'app.solo_demo' , version : '1.0.0' } as any , 'code' ) ;
256+ h . registry . registerItem ( 'dashboard' , { name : 'solo' , label : 'solo code' , widgets : [ ] } as any , 'name' as any , 'app.solo_demo' ) ;
257+ const server = {
258+ get : vi . fn ( ) , post : vi . fn ( ) , put : vi . fn ( ) , delete : vi . fn ( ) , patch : vi . fn ( ) , use : vi . fn ( ) ,
259+ listen : vi . fn ( ) . mockResolvedValue ( undefined ) , close : vi . fn ( ) . mockResolvedValue ( undefined ) ,
260+ } ;
261+ const rest = new RestServer ( server as any , h . protocol as any , { api : { requireAuth : false } } as any ) ;
262+ ( rest as any ) . resolveExecCtx = async ( ) => AUTHOR ;
263+ if ( shape . wired ) ( rest as any ) . objectQLProvider = async ( ) => ( { registry : h . registry } ) ;
264+ rest . registerRoutes ( ) ;
265+ const get = async ( pattern : string , path : string , name : string , query : Record < string , string > = { } ) => {
266+ const route = rest . getRoutes ( ) . find ( ( r : any ) => r . method === 'GET' && r . path === `${ META } ${ pattern } ` ) ;
267+ if ( ! route ) throw new Error ( `GET ${ pattern } is not registered` ) ;
268+ const res = makeRes ( ) ;
269+ await route . handler ( { method : 'GET' , path : `${ META } ${ path } ` , params : { type : 'dashboard' , name } , query, body : { } , headers : { } } , res ) ;
270+ return { status : res . statusCode as number , body : res . body } ;
271+ } ;
272+ return {
273+ ...h ,
274+ plain : ( name : string ) => get ( '/:type/:name' , `/dashboard/${ name } ` , name ) ,
275+ spellings : ( name : string , query : Record < string , string > = { } ) => [
276+ [ '/layers' , ( ) => get ( '/:type/:name/layers' , `/dashboard/${ name } /layers` , name , query ) ] ,
277+ [ '?layers=true' , ( ) => get ( '/:type/:name' , `/dashboard/${ name } ` , name , { ...query , layers : 'true' } ) ] ,
278+ ] as const ,
279+ } ;
280+ }
281+
282+ const candidatesOf = ( body : any ) => [ ...( body ?. details ?. candidates ?? [ ] ) ] . map ( ( c : any ) => `${ c . kind } :${ c . packageId } ` ) . sort ( ) ;
283+ const BOTH = [ `package:${ PKG_A } ` , `package:${ PKG_B } ` ] ;
284+
285+ for ( const [ shapeLabel , shape ] of SHAPES ) {
286+ it . each ( ORDERS ) ( `${ shapeLabel } , %s: both spellings answer the plain read's 409 with details.candidates, serving neither holder` , async ( _label , order ) => {
287+ const host = await registered ( order , shape ) ;
288+ const plain = await host . plain ( 'sales' ) ;
289+ expect ( [ plain . status , codeOf ( plain ) ] , JSON . stringify ( plain . body ) ) . toEqual ( [ 409 , 'RESOURCE_CONFLICT' ] ) ;
290+ expect ( candidatesOf ( plain . body ) ) . toEqual ( BOTH ) ;
291+ for ( const [ spelling , read ] of host . spellings ( 'sales' ) ) {
292+ const refused = await read ( ) ;
293+ expect ( [ refused . status , codeOf ( refused ) ] , `${ spelling } : ${ JSON . stringify ( refused . body ) } ` ) . toEqual ( [ 409 , 'RESOURCE_CONFLICT' ] ) ;
294+ expect ( candidatesOf ( refused . body ) , spelling ) . toEqual ( BOTH ) ;
295+ for ( const pkg of order ) expect ( JSON . stringify ( refused . body ) , spelling ) . not . toContain ( `${ pkg } code` ) ;
296+ }
297+ } , 60_000 ) ;
298+ }
299+
300+ it . each ( ORDERS ) ( 'in process, %s: the layered read refuses naming both, with or without a service copy' , async ( _label , order ) => {
301+ for ( const [ shapeLabel , shape ] of SHAPES ) {
302+ const host = await registered ( order , shape ) ;
303+ const refused : any = await host . protocol . getMetaItemLayered ( { type : 'dashboard' , name : 'sales' } ) . then (
304+ ( served ) => ( { served } ) ,
305+ ( error ) => error ,
306+ ) ;
307+ expect ( [ refused ?. code , refused ?. status ] , `${ shapeLabel } : ${ JSON . stringify ( refused ?. served ?? refused ?. message ) } ` )
308+ . toEqual ( [ 'RESOURCE_CONFLICT' , 409 ] ) ;
309+ expect ( [ ...( refused ?. refusal ?. candidates ?? [ ] ) ] . map ( ( c : any ) => `${ c . kind } :${ c . packageId } ` ) . sort ( ) , shapeLabel ) . toEqual ( BOTH ) ;
310+ // Each holder's layers stay readable at its package.
311+ for ( const pkg of order ) {
312+ const scoped : any = await host . protocol . getMetaItemLayered ( { type : 'dashboard' , name : 'sales' , packageId : pkg } ) ;
313+ expect ( scoped . code ?. label , `${ shapeLabel } , ${ pkg } ` ) . toBe ( `${ pkg } code` ) ;
314+ }
315+ }
316+ } , 60_000 ) ;
317+
318+ it ( 'CONTROL: a name one package registers is served, in process and on both spellings' , async ( ) => {
319+ for ( const [ shapeLabel , shape ] of SHAPES ) {
320+ const host = await registered ( [ PKG_A , PKG_B ] , shape ) ;
321+ const layered : any = await host . protocol . getMetaItemLayered ( { type : 'dashboard' , name : 'solo' } ) ;
322+ expect ( layered . effective ?. label , shapeLabel ) . toBe ( 'solo code' ) ;
323+ for ( const [ spelling , read ] of host . spellings ( 'solo' ) ) {
324+ const served = await read ( ) ;
325+ expect ( [ served . status , served . body ?. effective ?. label ] , `${ shapeLabel } ${ spelling } : ${ JSON . stringify ( served . body ) } ` )
326+ . toEqual ( [ 200 , 'solo code' ] ) ;
327+ }
328+ for ( const [ spelling , read ] of host . spellings ( 'sales' , { package : PKG_B } ) ) {
329+ const served = await read ( ) ;
330+ expect ( [ served . status , served . body ?. code ?. label ] , `${ shapeLabel } ${ spelling } ?package=` ) . toEqual ( [ 200 , `${ PKG_B } code` ] ) ;
331+ }
332+ }
333+ } , 60_000 ) ;
334+ } ) ;
0 commit comments