test_LocalInferredRanking.js (1968B)
1 "use strict"; 2 3 // --- imports (adjust path if needed) --- 4 ChromeUtils.defineESModuleGetters(this, { 5 scoreItemInferred: 6 "resource://newtab/lib/InferredModel/GreedyContentRanker.mjs", 7 }); 8 9 // ---------- scoreItemInferred tests ---------- 10 11 add_task( 12 async function test_scoreItemInferred_combines_normalized_local_and_server() { 13 const inferredInterests = { 14 parenting: 0.6, // float 15 news_reader: 0.2, // float 16 clicks: 2, // integer → excluded from inferred_norm 17 }; 18 19 const weights = { 20 local: 0.6, // 60% 21 server: 0.4, // 40% 22 inferred_norm: 0.8, // 0.6 + 0.2 (floats only) 23 }; 24 25 const item = { 26 id: "a1", 27 section: "top_stories_section", 28 item_score: 0, 29 server_score: 0.5, 30 features: { s_parenting: 1, s_news_reader: 1, other: 1 }, 31 }; 32 33 const ret = await scoreItemInferred(item, inferredInterests, weights); 34 Assert.strictEqual(ret, item, "returns same object"); 35 36 // inferred_score = 0.6 + 0.2 = 0.8 37 // score = 0.60 * 0.8 / (0.8 + 1e-6) + 0.40 * 0.5 38 const expected = (0.6 * 0.8) / (0.8 + 1e-6) + 0.4 * 0.5; 39 40 Assert.greater(item.score, 0, "score is positive"); 41 Assert.less( 42 Math.abs(item.score - expected), 43 1e-6, 44 "score matches normalized formula with epsilon" 45 ); 46 Assert.equal(item.score, item.item_score, "score mirrors item_score"); 47 } 48 ); 49 50 add_task(async function test_scoreItemInferred_server_nullish_is_zero() { 51 const inferredInterests = { tech: 0.8 }; 52 const weights = { local: 1.0, server: 0.4, inferred_norm: 0.8 }; 53 const item = { 54 id: "a2", 55 section: "top_stories_section", 56 features: { s_tech: 1 }, 57 // merino_score is undefined → should default to 0 58 }; 59 60 await scoreItemInferred(item, inferredInterests, weights); 61 62 // inferred_score = 0.8; merino term = 0 63 const expected = (1.0 * 0.8) / (0.8 + 1e-6) + 0.4 * 0; 64 Assert.less(Math.abs(item.score - expected), 1e-6, "merino nullish → 0"); 65 });