@@ -270,29 +270,27 @@ describe('getBlurbForResult', () => {
270270
271271 it ( 'finds the section with the most matches' , ( ) => {
272272 // Build content longer than BMAX (250) so the algorithm must choose a section.
273- // Place a cluster of 3 matches early and a lone match (IGNORE) far away .
274- // Positions array (sorted): [50=MATCH, 65=ME, 77=INSTEAD, 400= IGNORE]
275- // The algorithm should prefer the cluster (3 matches within BMAX) over the lone match.
276- const content = 'a' . repeat ( 50 ) + 'MATCH ' + 'c ' . repeat ( 10 ) + 'ME ' +
277- 'c' . repeat ( 10 ) + 'INSTEAD ' + 'b ' . repeat ( 316 ) + 'IGNORE ' + 'e' . repeat ( 100 ) ;
278- const matchPos = 50 ;
279- const mePos = 50 + 5 + 10 ; // 65
280- const insteadPos = 65 + 2 + 10 ; // 77
281- const ignorePos = 77 + 7 + 316 ; // 400
273+ // Place a lone match (IGNORE) FIRST, then a cluster of 3 matches later .
274+ // A greedy algorithm that picks the first match would incorrectly select IGNORE.
275+ // Positions array (sorted): [10=IGNORE, 320=MATCH, 335=ME, 347=INSTEAD]
276+ const content = 'a' . repeat ( 10 ) + 'IGNORE ' + 'b ' . repeat ( 304 ) + 'MATCH ' +
277+ 'c' . repeat ( 10 ) + 'ME ' + 'c ' . repeat ( 10 ) + 'INSTEAD ' + 'e' . repeat ( 100 ) ;
278+ const ignorePos = 10 ;
279+ const matchPos = 10 + 6 + 304 ; // 320
280+ const mePos = 320 + 5 + 10 ; // 335
281+ const insteadPos = 335 + 2 + 10 ; // 347
282282 const result = {
283283 matchData : { metadata : {
284284 kw : { content : { position : [
285- [ matchPos , 5 ] , [ mePos , 2 ] , [ insteadPos , 7 ] , [ ignorePos , 6 ]
285+ [ ignorePos , 6 ] , [ matchPos , 5 ] , [ mePos , 2 ] , [ insteadPos , 7 ]
286286 ] } } ,
287287 } }
288288 } ;
289- const positions = [ matchPos , mePos , insteadPos , ignorePos ] ;
289+ const positions = [ ignorePos , matchPos , mePos , insteadPos ] ;
290290 const item = { title : 'Test' , description : null , content : content } ;
291291 const blurb = getBlurbForResult ( result , item , positions ) ;
292- // Assert the negative first: a greedy implementation that includes everything
293- // would pass the positive checks below but fail here.
292+ // Should pick the cluster (MATCH, ME, INSTEAD) not the lone IGNORE
294293 assert . ok ( ! blurb . includes ( 'IGNORE' ) , 'Expected blurb to NOT contain IGNORE' ) ;
295- // Should pick the section with the most matches (MATCH, ME, INSTEAD)
296294 assert . ok ( blurb . includes ( 'MATCH' ) , 'Expected blurb to contain MATCH' ) ;
297295 assert . ok ( blurb . includes ( 'INSTEAD' ) , 'Expected blurb to contain INSTEAD' ) ;
298296 } ) ;
0 commit comments