@@ -17,11 +17,16 @@ import {
1717 * Mock config source for testing.
1818 */
1919class MockSource implements ConfigSource {
20+ public priority ?: number ;
21+
2022 constructor (
2123 public name : string ,
2224 private config : NormalizedConfig | undefined ,
2325 private location ?: string ,
24- ) { }
26+ priority ?: number ,
27+ ) {
28+ this . priority = priority ;
29+ }
2530
2631 load ( _options : ResolveConfigOptions ) : ConfigLoadResult | undefined {
2732 if ( this . config === undefined ) {
@@ -353,4 +358,86 @@ describe('config/resolver', () => {
353358 expect ( config . hostname ) . to . equal ( 'test.demandware.net' ) ;
354359 } ) ;
355360 } ) ;
361+
362+ describe ( 'priority-based sorting' , ( ) => {
363+ it ( 'sorts sources by priority (lower number = higher priority)' , ( ) => {
364+ // Sources added in wrong order, but should be sorted by priority
365+ const lowPriority = new MockSource ( 'low' , { clientId : 'low-client' } , undefined , 100 ) ;
366+ const highPriority = new MockSource ( 'high' , { clientId : 'high-client' } , undefined , - 10 ) ;
367+ const defaultPriority = new MockSource ( 'default' , { clientId : 'default-client' } , undefined , 0 ) ;
368+
369+ // Pass sources in "wrong" order - they should get sorted
370+ const resolver = new ConfigResolver ( [ lowPriority , defaultPriority , highPriority ] ) ;
371+ const { config} = resolver . resolve ( ) ;
372+
373+ // High priority source (-10) wins
374+ expect ( config . clientId ) . to . equal ( 'high-client' ) ;
375+ } ) ;
376+
377+ it ( 'treats undefined priority as 0' , ( ) => {
378+ const withPriority = new MockSource ( 'with' , { clientId : 'with-priority' } , undefined , 10 ) ;
379+ const noPriority = new MockSource ( 'no' , { clientId : 'no-priority' } , undefined , undefined ) ;
380+
381+ // No priority (=0) should win over priority 10
382+ const resolver = new ConfigResolver ( [ withPriority , noPriority ] ) ;
383+ const { config} = resolver . resolve ( ) ;
384+
385+ expect ( config . clientId ) . to . equal ( 'no-priority' ) ;
386+ } ) ;
387+
388+ it ( 'maintains insertion order for same priority' , ( ) => {
389+ const first = new MockSource ( 'first' , { clientId : 'first-client' } , undefined , 0 ) ;
390+ const second = new MockSource ( 'second' , { clientId : 'second-client' } , undefined , 0 ) ;
391+
392+ const resolver = new ConfigResolver ( [ first , second ] ) ;
393+ const { config} = resolver . resolve ( ) ;
394+
395+ // First source should win since both have same priority
396+ expect ( config . clientId ) . to . equal ( 'first-client' ) ;
397+ } ) ;
398+
399+ it ( 'negative priorities come before 0' , ( ) => {
400+ const before = new MockSource ( 'before' , { hostname : 'before.com' } , undefined , - 1 ) ;
401+ const builtin = new MockSource ( 'builtin' , { hostname : 'builtin.com' } , undefined , 0 ) ;
402+
403+ const resolver = new ConfigResolver ( [ builtin , before ] ) ;
404+ const { config} = resolver . resolve ( ) ;
405+
406+ // -1 priority should win
407+ expect ( config . hostname ) . to . equal ( 'before.com' ) ;
408+ } ) ;
409+
410+ it ( 'high priorities (1000) come last' , ( ) => {
411+ const packageJson = new MockSource ( 'package' , { shortCode : 'package-code' } , undefined , 1000 ) ;
412+ const dwJson = new MockSource ( 'dwjson' , { shortCode : 'dw-code' } , undefined , 0 ) ;
413+
414+ const resolver = new ConfigResolver ( [ packageJson , dwJson ] ) ;
415+ const { config} = resolver . resolve ( ) ;
416+
417+ // 0 priority should win over 1000
418+ expect ( config . shortCode ) . to . equal ( 'dw-code' ) ;
419+ } ) ;
420+
421+ it ( 'plugin priorities work with before/after pattern' , ( ) => {
422+ // Simulating: plugin 'before' (-1), builtin (0), plugin 'after' (10)
423+ const pluginBefore = new MockSource ( 'plugin-before' , { clientId : 'before-client' } , undefined , - 1 ) ;
424+ const builtin = new MockSource ( 'builtin' , { clientId : 'builtin-client' , hostname : 'builtin.com' } , undefined , 0 ) ;
425+ const pluginAfter = new MockSource (
426+ 'plugin-after' ,
427+ { clientId : 'after-client' , mrtProject : 'after-project' } ,
428+ undefined ,
429+ 10 ,
430+ ) ;
431+
432+ const resolver = new ConfigResolver ( [ pluginAfter , builtin , pluginBefore ] ) ;
433+ const { config} = resolver . resolve ( ) ;
434+
435+ // 'before' plugin wins for clientId
436+ expect ( config . clientId ) . to . equal ( 'before-client' ) ;
437+ // builtin provides hostname (not in before plugin)
438+ expect ( config . hostname ) . to . equal ( 'builtin.com' ) ;
439+ // 'after' plugin provides mrtProject (not in others)
440+ expect ( config . mrtProject ) . to . equal ( 'after-project' ) ;
441+ } ) ;
442+ } ) ;
356443} ) ;
0 commit comments