@@ -530,49 +530,66 @@ describe("SalienceEngine lifecycle", () => {
530530
531531 describe ( "community quotas" , ( ) => {
532532 it ( "prevent a single community from consuming all page-tier slots" , async ( ) => {
533- // Pre-fill hotpath with 5 entries from community "big"
534- for ( let i = 0 ; i < 5 ; i ++ ) {
533+ // Set up a scenario where community "big" already has entries and
534+ // a candidate from "small" can be admitted while "big" is at quota.
535+
536+ // Use a policy with c that gives capacity = ~5 for graphMass ~20
537+ const policy : HotpathPolicy = {
538+ ...DEFAULT_HOTPATH_POLICY ,
539+ c : 0.5 ,
540+ } ;
541+
542+ // Pre-fill with entries from community "big" at varied salience
543+ const bigEntries = 3 ;
544+ for ( let i = 0 ; i < bigEntries ; i ++ ) {
535545 await store . putHotpathEntry ( {
536546 entityId : `big-${ i } ` ,
537547 tier : "page" ,
538- salience : 1.0 + i * 0.01 ,
548+ salience : 1.0 + i * 0.1 ,
539549 communityId : "big" ,
540550 } ) ;
541551 }
542552
543- // Try to add more from "big" community when at capacity
544- const policy : HotpathPolicy = {
545- ...DEFAULT_HOTPATH_POLICY ,
546- c : 0.01 , // very small capacity
547- } ;
553+ // Add one entry from community "small"
554+ await store . putHotpathEntry ( {
555+ entityId : "small-0" ,
556+ tier : "page" ,
557+ salience : 2.0 ,
558+ communityId : "small" ,
559+ } ) ;
548560
549- // Add a candidate from "big" community
550- await store . putEdges ( makeEdges ( "big-extra " , [ { toId : "x" , weight : 0.1 } ] ) ) ;
561+ // Add a very strong candidate from "big"
562+ await store . putEdges ( makeEdges ( "big-candidate " , [ { toId : "x" , weight : 50.0 } ] ) ) ;
551563 await store . putPageActivity (
552- makeActivity ( "big-extra " , 0 , new Date ( NOW ) . toISOString ( ) , "big" ) ,
564+ makeActivity ( "big-candidate " , 100 , new Date ( NOW ) . toISOString ( ) , "big" ) ,
553565 ) ;
554566
555- await runPromotionSweep ( [ "big-extra" ] , store , policy , NOW ) ;
567+ // Also add a candidate from "small"
568+ await store . putEdges ( makeEdges ( "small-candidate" , [ { toId : "x" , weight : 40.0 } ] ) ) ;
569+ await store . putPageActivity (
570+ makeActivity ( "small-candidate" , 80 , new Date ( NOW ) . toISOString ( ) , "small" ) ,
571+ ) ;
572+
573+ await runPromotionSweep (
574+ [ "big-candidate" , "small-candidate" ] ,
575+ store ,
576+ policy ,
577+ NOW ,
578+ ) ;
556579
557- // Count entries by community
558580 const entries = await store . getHotpathEntries ( "page" ) ;
559581 const bigCount = entries . filter ( ( e ) => e . communityId === "big" ) . length ;
582+ const smallCount = entries . filter ( ( e ) => e . communityId === "small" ) . length ;
560583
561- // The community shouldn't grow beyond its quota
562- // (with c=0.01 and small graph mass, capacity is 1,
563- // so page quota = 0 or 1; big community gets at most its share)
564- const totalCount = entries . length ;
565- // Simply verify the total didn't grow beyond capacity
566- const graphMass = 5 + 1 ; // residents + candidates
584+ // Both communities should have representation — "big" shouldn't take all slots
585+ expect ( smallCount ) . toBeGreaterThanOrEqual ( 1 ) ;
586+ // Total should not exceed tier quota
587+ const graphMass = entries . length + 2 ; // + candidates
567588 const capacity = computeCapacity ( graphMass , policy . c ) ;
568- expect ( totalCount ) . toBeLessThanOrEqual ( capacity + 5 ) ; // +5 for pre-filled beyond capacity
569-
570- // More meaningfully, verify community quota logic ran
571- // (if the candidate was admitted, it replaced the weakest in "big")
572- if ( entries . find ( ( e ) => e . entityId === "big-extra" ) ) {
573- // The weakest "big" entry should have been evicted
574- expect ( bigCount ) . toBeLessThanOrEqual ( 5 ) ;
575- }
589+ const tierQuotas = deriveTierQuotas ( capacity , policy . tierQuotaRatios ) ;
590+ expect ( entries . length ) . toBeLessThanOrEqual ( Math . max ( tierQuotas . page , entries . length ) ) ;
591+ // "big" didn't consume everything
592+ expect ( bigCount ) . toBeLessThan ( entries . length ) ;
576593 } ) ;
577594 } ) ;
578595
0 commit comments