@@ -15,6 +15,8 @@ import {
1515 nearestName ,
1616 listNames ,
1717 packagesOf ,
18+ objectExtensionsByTarget ,
19+ objectsAsServed ,
1820 RELATIONSHIP_FIELD_TYPES ,
1921} from './object-graph.js' ;
2022import { walkFilterFieldKeys , type FilterFieldKey } from './filter-walk.js' ;
@@ -345,6 +347,93 @@ describe('object-graph — packagesOf (#20206, ruling A on #15293 `5634034754`)'
345347 } ) ;
346348} ) ;
347349
350+ /**
351+ * [#30303] The shared extension fold on junk input, beside the
352+ * `indexObjectGraph` and `packagesOf` cases above. Neither export refuses
353+ * anything: a malformed `packages` is {@link packagesOf}'s refusal, met by a
354+ * caller that reads the bodies through it (`validateSecurityPosture` does,
355+ * before either fold runs). Each shape below reads as "nothing to fold" for
356+ * the part that is junk, and never throws.
357+ */
358+ describe ( '[#30303] object-graph — objectExtensionsByTarget / objectsAsServed on junk input' , ( ) => {
359+ const OBJ = { name : 'crm_contract' , fields : { contract_no : { type : 'text' } } } ;
360+ const EXT = { extend : 'crm_contract' , fields : { note : { type : 'text' } } } ;
361+ const served = ( stack : unknown ) =>
362+ objectsAsServed ( stack ) . objects . map ( ( o ) => [ o . name , ( o . fields as Array < { name : string } > | Record < string , unknown > ) ] ) ;
363+
364+ it ( 'a null or non-record stack: no extensions, no objects' , ( ) => {
365+ for ( const stack of [ null , undefined , 'junk' , 42 , [ ] ] ) {
366+ expect ( objectExtensionsByTarget ( stack ) . size , String ( stack ) ) . toBe ( 0 ) ;
367+ expect ( objectsAsServed ( stack ) , String ( stack ) ) . toEqual ( { objects : [ ] , tied : new Map ( ) } ) ;
368+ }
369+ } ) ;
370+
371+ it ( 'packages: [null] and other non-record entries or bodies contribute nothing; a record body beside them still does' , ( ) => {
372+ expect ( objectExtensionsByTarget ( { packages : [ null ] } ) . size ) . toBe ( 0 ) ;
373+ const stack = {
374+ objects : [ OBJ ] ,
375+ packages : [ null , 'junk' , 42 , [ ] , { manifest : null } , { manifest : 'junk' } , { manifest : { objectExtensions : [ EXT ] } } ] ,
376+ } ;
377+ expect ( [ ...objectExtensionsByTarget ( stack ) . entries ( ) ] . map ( ( [ t , list ] ) => [ t , list . map ( ( e ) => [ e . carrier , e . path ] ) ] ) ) . toEqual ( [
378+ [ 'crm_contract' , [ [ 6 , 'packages[6].manifest.objectExtensions[0]' ] ] ] ,
379+ ] ) ;
380+ expect ( objectsAsServed ( stack ) . objects [ 0 ] . fields ) . toEqual ( [
381+ { name : 'contract_no' , type : 'text' } ,
382+ { name : 'note' , type : 'text' } ,
383+ ] ) ;
384+ } ) ;
385+
386+ it ( 'a non-array objectExtensions folds nothing' , ( ) => {
387+ for ( const objectExtensions of [ null , 'junk' , 42 , { crm_contract : EXT } ] ) {
388+ const stack = { objects : [ OBJ ] , objectExtensions } ;
389+ expect ( objectExtensionsByTarget ( stack ) . size , JSON . stringify ( objectExtensions ) ) . toBe ( 0 ) ;
390+ expect ( objectsAsServed ( stack ) . objects , JSON . stringify ( objectExtensions ) ) . toEqual ( [ OBJ ] ) ;
391+ }
392+ } ) ;
393+
394+ it ( 'a non-record entry, or one whose extend is not a string, is skipped; the record entry beside it is read' , ( ) => {
395+ const stack = {
396+ objects : [ OBJ ] ,
397+ objectExtensions : [ null , 'junk' , 42 , [ ] , { extend : 7 , fields : { x : { type : 'text' } } } , { fields : { y : { } } } , EXT ] ,
398+ } ;
399+ expect ( [ ...objectExtensionsByTarget ( stack ) . keys ( ) ] ) . toEqual ( [ 'crm_contract' ] ) ;
400+ expect ( objectExtensionsByTarget ( stack ) . get ( 'crm_contract' ) ! . map ( ( e ) => e . path ) ) . toEqual ( [ 'objectExtensions[6]' ] ) ;
401+ expect ( served ( stack ) ) . toEqual ( [ [ 'crm_contract' , [ { name : 'contract_no' , type : 'text' } , { name : 'note' , type : 'text' } ] ] ] ) ;
402+ } ) ;
403+
404+ it ( 'junk fields add nothing: a non-collection, non-record members, a member with no name' , ( ) => {
405+ for ( const fields of [ null , 'junk' , 42 , [ null , 'x' , 3 , { type : 'text' } , { name : '' , type : 'text' } ] ] ) {
406+ const stack = { objects : [ OBJ ] , objectExtensions : [ { extend : 'crm_contract' , fields } ] } ;
407+ expect ( ( ) => objectsAsServed ( stack ) , JSON . stringify ( fields ) ) . not . toThrow ( ) ;
408+ expect ( objectsAsServed ( stack ) . objects [ 0 ] . fields , JSON . stringify ( fields ) ) . toEqual ( [ { name : 'contract_no' , type : 'text' } ] ) ;
409+ }
410+ // The array form is read by name, as the map form is.
411+ const arrayForm = { objects : [ OBJ ] , objectExtensions : [ { extend : 'crm_contract' , fields : [ { name : 'note' , type : 'text' } ] } ] } ;
412+ expect ( served ( arrayForm ) ) . toEqual ( [ [ 'crm_contract' , [ { name : 'contract_no' , type : 'text' } , { name : 'note' , type : 'text' } ] ] ] ) ;
413+ } ) ;
414+
415+ // `ObjectExtensionSchema.priority` is a number, so a parsed stack never
416+ // carries this shape; the fold reads a non-number as absent (the default 200,
417+ // as `SchemaRegistry.registerObjectExtension` does) rather than guessing.
418+ it ( 'a string priority reads as the default 200 and never throws' , ( ) => {
419+ const stack = {
420+ objects : [ OBJ ] ,
421+ packages : [
422+ { manifest : { objectExtensions : [ { extend : 'crm_contract' , priority : '100' , fields : { note : { type : 'text' , label : 'A' } } } ] } } ,
423+ { manifest : { objectExtensions : [ { extend : 'crm_contract' , priority : 150 , fields : { note : { type : 'text' , label : 'B' } } } ] } } ,
424+ ] ,
425+ } ;
426+ expect ( objectExtensionsByTarget ( stack ) . get ( 'crm_contract' ) ! . map ( ( e ) => [ e . carrier , e . priority ] ) ) . toEqual ( [
427+ [ 1 , 150 ] ,
428+ [ 0 , 200 ] ,
429+ ] ) ;
430+ // So the string-priority entry folds LAST and its definition wins, untied.
431+ const { objects, tied } = objectsAsServed ( stack ) ;
432+ expect ( ( objects [ 0 ] . fields as Array < Record < string , unknown > > ) . find ( ( f ) => f . name === 'note' ) ?. label ) . toBe ( 'A' ) ;
433+ expect ( tied . size ) . toBe ( 0 ) ;
434+ } ) ;
435+ } ) ;
436+
348437/**
349438 * [#19289] A `{ type: 'user' }` field with no `reference` is TRAVERSABLE — the
350439 * third defect found by the implicit-target census, and the widest-reaching of
0 commit comments