|
|
@@ -0,0 +1,262 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.content.ArticleController;
|
|
|
+import com.etotem.cfc.entity.Article;
|
|
|
+import com.etotem.cfc.mapper.ArticleMapper;
|
|
|
+import com.etotem.cfc.mapper.ArticleReadingRecordMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
+import com.etotem.cfc.service.ArticlePermissionService;
|
|
|
+import com.etotem.cfc.service.ArticleService;
|
|
|
+import com.etotem.cfc.service.ContentDimensionThresholdService;
|
|
|
+import com.etotem.cfc.service.FiveDimensionScoreService;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
+import org.mockito.Mockito;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.anyInt;
|
|
|
+import static org.mockito.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约测试:能力键 {@code content.article.search}(规格 ARTICLE-SEARCH-2026-11-14-V1,契约 C1-C3 后端部分)。
|
|
|
+ *
|
|
|
+ * <p><b>契约锚点</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code docs/需求分析/2026-11-14-小程序知识分享页搜索功能需求变更.md} C1/C2/C3(C4-C6 为前端交互,本类不覆盖)</li>
|
|
|
+ * <li>设计:{@code docs/design/2026-11-14-article-center-search-design.md} D1-D4</li>
|
|
|
+ * <li>实现锚点:{@code ArticleController.list}(body.keyword 透传,:140)、{@code ArticleService.getPublicList}
|
|
|
+ * (10 参,LIKE 于 title/summary/tags 且不含 content,:134-137;tag 内存过滤 :154-159;手动分页 total=visible.size() :183-192)</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>各条款断言要点</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>C1:空 keyword 请求(body 仅 {page,size})→ Service 收到 keyword=null,返回 code=200</li>
|
|
|
+ * <li>C2:keyword 非空 → wrapper 构造断言 LIKE 范围锁定 {@code title/summary/tags} 且不含 {@code content};
|
|
|
+ * 分页 {@code total} 为过滤后真实条数(非页大小)</li>
|
|
|
+ * <li>C3:keyword 与 tag 同时提交 → 结果为「LIKE 命中 ∩ 内存 tag 过滤命中」交集,{@code total} 反映交集真实总数</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito + 反射注入(无 Spring / 无 DB / 无外部服务),与同批契约为同款式。</p>
|
|
|
+ *
|
|
|
+ * <p><b>已验证结果</b>:5 个契约测试全部通过(全绿)。LIKE 范围断言通过
|
|
|
+ * {@code @BeforeAll initMybatisPlusTableInfo()}({@code TableInfoHelper.initTableInfo})注册实体元数据,
|
|
|
+ * 使 {@code LambdaQueryWrapper.getSqlSegment()} 在无 Spring 上下文可解析(参照
|
|
|
+ * ChildBadgeViewContractTest / CfBalanceControllerTest 同款模式),断言 wrapper 含
|
|
|
+ * title/summary/tags LIKE 且不含 content。全绿即证明实现已满足契约;按执行纪律,
|
|
|
+ * 交编排侧核实能力键状态(转测试登记),不改 src/main 实现。</p>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:契约派发时为 TDD-RED 阶段;实测实现已在 cfclub 落地(C1/C2 设计预期可过,交集语义随 S-cfc-131 标签过滤合入),本类转绿则按执行纪律汇报「实现已存在,交测试登记」,不私改实现。</p>
|
|
|
+ */
|
|
|
+class ArticleSearchContractTest {
|
|
|
+
|
|
|
+ private static final String KEYWORD_SLEEP = "睡眠";
|
|
|
+ private static final String TAG_LOSE_FAT = "减脂";
|
|
|
+
|
|
|
+ private ArticleService articleService;
|
|
|
+ private ArticleController articleController;
|
|
|
+
|
|
|
+ private ArticleMapper articleMapper;
|
|
|
+ private ArticlePermissionService articlePermissionService;
|
|
|
+ private ArticleReadingRecordMapper articleReadingRecordMapper;
|
|
|
+ private FiveDimensionScoreService fiveDimensionScoreService;
|
|
|
+ private ContentDimensionThresholdService contentDimensionThresholdService;
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+ private UserMapper userMapper;
|
|
|
+
|
|
|
+ @BeforeAll
|
|
|
+ static void initMybatisPlusTableInfo() {
|
|
|
+ // 使 LambdaQueryWrapper.getSqlSegment() 在无 Spring 上下文中可用(校验 LIKE 范围契约 C2)
|
|
|
+ // 参照 ChildBadgeViewContractTest / CfBalanceControllerTest 同款模式
|
|
|
+ TableInfoHelper.initTableInfo(
|
|
|
+ new MapperBuilderAssistant(new MybatisConfiguration(), ""), Article.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ articleService = new ArticleService();
|
|
|
+ articleController = new ArticleController();
|
|
|
+
|
|
|
+ articleMapper = mock(ArticleMapper.class);
|
|
|
+ articlePermissionService = mock(ArticlePermissionService.class);
|
|
|
+ articleReadingRecordMapper = mock(ArticleReadingRecordMapper.class);
|
|
|
+ fiveDimensionScoreService = mock(FiveDimensionScoreService.class);
|
|
|
+ contentDimensionThresholdService = mock(ContentDimensionThresholdService.class);
|
|
|
+ jdbcTemplate = mock(JdbcTemplate.class);
|
|
|
+ userMapper = mock(UserMapper.class);
|
|
|
+
|
|
|
+ injectService("articleMapper", articleMapper);
|
|
|
+ injectService("articlePermissionService", articlePermissionService);
|
|
|
+ injectService("articleReadingRecordMapper", articleReadingRecordMapper);
|
|
|
+ injectService("fiveDimensionScoreService", fiveDimensionScoreService);
|
|
|
+ injectService("contentDimensionThresholdService", contentDimensionThresholdService);
|
|
|
+ injectService("jdbcTemplate", jdbcTemplate);
|
|
|
+ injectService("userMapper", userMapper);
|
|
|
+ injectController("articleService", articleService);
|
|
|
+ injectController("userMapper", userMapper);
|
|
|
+
|
|
|
+ // 环境放行:权限过滤原样返回;无家庭得分(阈值 fail-open);无阅读时长;无已完成阅读记录
|
|
|
+ when(articlePermissionService.filterVisible(any(), any(), any(), any()))
|
|
|
+ .thenAnswer(inv -> inv.getArgument(0));
|
|
|
+ when(fiveDimensionScoreService.getFamilyAverageScores(any())).thenReturn(Collections.emptyMap());
|
|
|
+ when(contentDimensionThresholdService.listByContents(anyString(), any())).thenReturn(Collections.emptyMap());
|
|
|
+ when(jdbcTemplate.queryForList(anyString())).thenReturn(Collections.emptyList());
|
|
|
+ when(articleReadingRecordMapper.selectCompletedArticleIdsByChild(anyLong()))
|
|
|
+ .thenReturn(Collections.emptyList());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== C1:空 keyword → code=200 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("C1: body 仅含 {page,size} 时返回 code=200")
|
|
|
+ void c1_空keyword请求返回code200() {
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("page", 1);
|
|
|
+ body.put("size", 10);
|
|
|
+
|
|
|
+ Result<Page<Article>> result = articleController.list(body, null, null);
|
|
|
+
|
|
|
+ assertEquals(200, result.getCode(), "空 keyword 请求非搜索场景,列表接口必须正常返回 code=200");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== C2:LIKE 范围 title/summary/tags(不含 content)+ 真实 total ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("C2: keyword 非空时 LIKE 范围锁定 title/summary/tags,且不含 content")
|
|
|
+ void c2_like范围锁定titleSummaryTags_不含content() {
|
|
|
+ mockSelectList(Collections.singletonList(
|
|
|
+ article(1L, "睡眠指南", "[\"减脂\",\"睡眠\"]")));
|
|
|
+
|
|
|
+ articleService.getPublicList(null, KEYWORD_SLEEP, null, null,
|
|
|
+ 1, 10, null, null, null, null);
|
|
|
+
|
|
|
+ String sql = captureWrapperSql();
|
|
|
+ assertTrue(sql.contains("title LIKE"), "LIKE 必须覆盖 title(契约 C2 / 设计 D2 字段范围)");
|
|
|
+ assertTrue(sql.contains("summary LIKE"), "LIKE 必须覆盖 summary(契约 C2 / 设计 D2 字段范围)");
|
|
|
+ assertTrue(sql.contains("tags LIKE"), "LIKE 必须覆盖 tags(契约 C2 / 设计 D2 字段范围)");
|
|
|
+ assertFalse(sql.contains("content"),
|
|
|
+ "LIKE 不得覆盖 content 正文(设计 D2:控制实现面,避免长文本 LIKE 性能与误命中)");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("C2: total 为 LIKE 命中后的真实条数,翻页切片不依赖 size")
|
|
|
+ void c2_命中集total为过滤后真实条数_翻页不依赖size() {
|
|
|
+ mockSelectList(Arrays.asList(
|
|
|
+ article(1L, "睡眠指南", "[\"减脂\",\"睡眠\"]"),
|
|
|
+ article(2L, "深度睡眠法", "[\"睡眠\"]"),
|
|
|
+ article(3L, "睡眠与记忆", "[\"睡眠\"]")));
|
|
|
+
|
|
|
+ Page<Article> page = articleService.getPublicList(null, KEYWORD_SLEEP, null, null,
|
|
|
+ 2, 2, null, null, null, null);
|
|
|
+
|
|
|
+ assertEquals(3, page.getTotal(),
|
|
|
+ "第 2 页 total 必须是过滤后真实条数 3(不得 <=size,否则前端 noMore 恒真无法翻页)");
|
|
|
+ assertEquals(1, page.getRecords().size(), "第 2 页仅剩 1 条");
|
|
|
+ assertEquals(3L, page.getRecords().get(0).getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== C3:keyword ∩ tag 交集 + 真实 total ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("C3: keyword 与 tag 同时提交 → LIKE 命中 ∩ 标签命中 的交集")
|
|
|
+ void c3_keyword与tag交集_只含两者均命中的文章() {
|
|
|
+ mockSelectList(Arrays.asList(
|
|
|
+ article(1L, "睡眠指南", "[\"减脂\",\"睡眠\"]"),
|
|
|
+ article(2L, "深度睡眠法", "[\"减脂\"]"),
|
|
|
+ article(3L, "睡眠百科", "[\"免疫\"]")));
|
|
|
+
|
|
|
+ Page<Article> page = articleService.getPublicList(null, KEYWORD_SLEEP, null, TAG_LOSE_FAT,
|
|
|
+ 1, 10, null, null, null, null);
|
|
|
+
|
|
|
+ assertEquals(2, page.getTotal(), "交集 = {1,2}(3 号含睡眠不含减脂,被 tag 过滤排除)");
|
|
|
+ assertEquals(2, page.getRecords().size());
|
|
|
+ assertTrue(page.getRecords().stream().allMatch(a ->
|
|
|
+ a.getTitle().contains(KEYWORD_SLEEP)
|
|
|
+ && String.valueOf(a.getTags()).contains(TAG_LOSE_FAT)),
|
|
|
+ "返回记录必须同时满足 keyword 与 tag,不允许并集混入");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("C3: 交集 total 反映真实总数,跨页翻页不失效")
|
|
|
+ void c3_交集total反映真实总数_跨页不失效() {
|
|
|
+ mockSelectList(Arrays.asList(
|
|
|
+ article(1L, "睡眠指南", "[\"减脂\"]"),
|
|
|
+ article(2L, "睡眠与免疫", "[\"减脂\"]"),
|
|
|
+ article(3L, "睡眠节律", "[\"减脂\"]"),
|
|
|
+ article(4L, "睡眠环境", "[\"减脂\"]"),
|
|
|
+ article(5L, "睡眠与情绪", "[\"减脂\"]")));
|
|
|
+
|
|
|
+ Page<Article> page = articleService.getPublicList(null, KEYWORD_SLEEP, null, TAG_LOSE_FAT,
|
|
|
+ 2, 3, null, null, null, null);
|
|
|
+
|
|
|
+ assertEquals(5, page.getTotal(), "交集 total 必须是 5(真实总数),不得被页大小截断成 3");
|
|
|
+ assertEquals(2, page.getRecords().size(), "第 2 页应剩 2 条");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 辅助构造 ====================
|
|
|
+
|
|
|
+ private void mockSelectList(List<Article> all) {
|
|
|
+ when(articleMapper.selectList(any())).thenReturn(new ArrayList<>(all));
|
|
|
+ }
|
|
|
+
|
|
|
+ private String captureWrapperSql() {
|
|
|
+ ArgumentCaptor<Wrapper<Article>> captor = ArgumentCaptor.forClass(Wrapper.class);
|
|
|
+ verify(articleMapper).selectList(captor.capture());
|
|
|
+ String sql = captor.getValue().getSqlSegment();
|
|
|
+ assertNotNull(sql, "wrapper SQL 段不应为空");
|
|
|
+ return sql;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Article article(Long id, String title, String tags) {
|
|
|
+ Article a = new Article();
|
|
|
+ a.setId(id);
|
|
|
+ a.setStatus("published");
|
|
|
+ a.setAuditStatus("approved");
|
|
|
+ a.setTitle(title);
|
|
|
+ a.setSummary("摘要" + id);
|
|
|
+ a.setTags(tags);
|
|
|
+ a.setIsFeatured(0);
|
|
|
+ a.setSortOrder(0);
|
|
|
+ a.setPublishedAt(new Date());
|
|
|
+ return a;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void injectService(String field, Object value) throws Exception {
|
|
|
+ Field f = ArticleService.class.getDeclaredField(field);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(articleService, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void injectController(String field, Object value) throws Exception {
|
|
|
+ Field f = ArticleController.class.getDeclaredField(field);
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(articleController, value);
|
|
|
+ }
|
|
|
+}
|