|
|
@@ -0,0 +1,67 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.dto.RecommendItemVO;
|
|
|
+import com.etotem.cfc.service.K8RecommendationBoostService;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * TDD 契约:还原水文章优先加分 + 读完降权(spec §6.2)。
|
|
|
+ */
|
|
|
+class K8ArticleRecommendationTest {
|
|
|
+
|
|
|
+ private final K8RecommendationBoostService svc = new K8RecommendationBoostService();
|
|
|
+
|
|
|
+ private RecommendItemVO item(Long id, String title, int score) {
|
|
|
+ RecommendItemVO v = new RecommendItemVO();
|
|
|
+ v.setId(id); v.setTitle(title); v.setRecommendScore(score); return v;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final Set<Long> NO_COMPLETED = Collections.emptySet();
|
|
|
+ private static final List<String> KEYWORDS = Arrays.asList("还原水", "电解水", "Enagic", "富氢水", "水机");
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void keyword_match_gets_boost() {
|
|
|
+ List<RecommendItemVO> items = new ArrayList<>(Arrays.asList(
|
|
|
+ item(1L, "还原水与甲亢", 10), // 命中
|
|
|
+ item(2L, "肠道菌群与免疫", 10))); // 不命中
|
|
|
+ List<RecommendItemVO> r = svc.applyArticleRules(items, KEYWORDS, 50, -100, NO_COMPLETED);
|
|
|
+ assertEquals(60, r.get(0).getRecommendScore(), "命中关键词+50");
|
|
|
+ assertEquals(10, r.get(1).getRecommendScore(), "不命中不变");
|
|
|
+ assertTrue(r.get(0).getRecommendReason().contains("还原水") || r.get(0).getRecommendReason() != null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void family_completed_demoted_to_min_directly() {
|
|
|
+ // 文章1 家人读完 → 直接赋 minScore(-100),即便原分 90 或 -50
|
|
|
+ List<RecommendItemVO> items = new ArrayList<>(Arrays.asList(
|
|
|
+ item(1L, "还原水与甲亢", 90),
|
|
|
+ item(2L, "Enagic 强还原水", 40),
|
|
|
+ item(3L, "普通文章", 5)));
|
|
|
+ Set<Long> completed = new HashSet<>(Arrays.asList(1L, 3L));
|
|
|
+ List<RecommendItemVO> r = svc.applyArticleRules(items, KEYWORDS, 50, -100, completed);
|
|
|
+ Map<Long,Integer> byId = new HashMap<>();
|
|
|
+ r.forEach(v -> byId.put(v.getId(), v.getRecommendScore()));
|
|
|
+ assertEquals(-100, byId.get(1L), "原分90→直接赋-100");
|
|
|
+ assertEquals(-100, byId.get(3L), "原分5(非关键词)读完→也赋-100");
|
|
|
+ assertEquals(90, byId.get(2L), "Enagic命中+50=90,未读完保持");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void completed_even_if_original_below_min_still_min() {
|
|
|
+ List<RecommendItemVO> items = new ArrayList<>(Arrays.asList(item(1L, "还原水", -200)));
|
|
|
+ Set<Long> completed = Collections.singleton(1L);
|
|
|
+ List<RecommendItemVO> r = svc.applyArticleRules(items, KEYWORDS, 50, -100, completed);
|
|
|
+ assertEquals(-100, r.get(0).getRecommendScore(), "原分-200(<min)读完→赋-100(直接赋值,非Math.min)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void not_mutate_input() {
|
|
|
+ List<RecommendItemVO> items = new ArrayList<>(Arrays.asList(item(1L, "还原水", 10)));
|
|
|
+ svc.applyArticleRules(items, KEYWORDS, 50, -100, NO_COMPLETED);
|
|
|
+ assertEquals(10, items.get(0).getRecommendScore(), "入参不应被修改");
|
|
|
+ }
|
|
|
+}
|