|
|
@@ -0,0 +1,286 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.common.constant.DimensionCodeConstants;
|
|
|
+import com.etotem.cfc.controller.assessment.AssessmentResultController;
|
|
|
+import com.etotem.cfc.dto.AssessmentHistoryItemDTO;
|
|
|
+import com.etotem.cfc.dto.AssessmentResultDTO;
|
|
|
+import com.etotem.cfc.entity.DanAssessmentResult;
|
|
|
+import com.etotem.cfc.mapper.DanAssessmentResultMapper;
|
|
|
+import com.etotem.cfc.service.CognitiveService;
|
|
|
+import org.apache.ibatis.builder.MapperBuilderAssistant;
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Modifier;
|
|
|
+import java.lang.reflect.ParameterizedType;
|
|
|
+import java.lang.reflect.Type;
|
|
|
+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 java.util.stream.Collectors;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.junit.jupiter.api.Assertions.fail;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测:能力键 {@code assessment.result.view}(业务域 D,SC-P-39 家长查看测评结果)——
|
|
|
+ * <b>测评历史 {@code /api/assessment/history} 与量纲口径契约</b>(补充 {@code AssessmentResultViewContractTest} 未覆盖面)。
|
|
|
+ *
|
|
|
+ * <p><b>溯源</b>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-39「查看测评结果(DAN 报告)」——
|
|
|
+ * 流程:结果页 → 八类报告展示;边界:<b>结果未录入不展示;量纲标注</b>。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.10 / 2911 行登记
|
|
|
+ * {@code POST /api/assessment/history} → {@code List<AssessmentHistoryItemDTO>}(assessmentDate/overallScore,
|
|
|
+ * status=completed 按日期倒序);历史与最新结果同属「家长查看测评结果」链路,量纲口径必须一致。</li>
|
|
|
+ * <li>量纲口径:{@code docs/superpowers/specs/2026-11-05-dimension-scoring-convention.md} §1——
|
|
|
+ * 所有维度得分统一落在 <b>0–100</b> 闭区间(0=最弱,100=最强),共享常量
|
|
|
+ * {@link DimensionCodeConstants#SCORE_MIN}=0 / {@link DimensionCodeConstants#SCORE_MAX}=100。</li>
|
|
|
+ * <li>与 {@code teacher.assessment.record}(S-cfc-184)区分:家长看结果 vs 规划师记录测评。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>被校验的契约边界</b>
|
|
|
+ * <ul>
|
|
|
+ * <li><b>边界一(已落地,作对照)</b>:历史为空(无 completed 记录)→ 返回 {@code Result.success(空列表)},
|
|
|
+ * 而非错误;结果未录入不展示同适用于历史列表。</li>
|
|
|
+ * <li><b>边界二(缺失,预期红)</b>:历史条目 {@link AssessmentHistoryItemDTO} 须携带<b>量纲标注</b>
|
|
|
+ * (0–100 闭区间 + 中文标签),与最新结果页同口径渲染;当前仅回裸 {@code overallScore},
|
|
|
+ * 历史列表无量纲元信息。</li>
|
|
|
+ * <li><b>边界三(缺失,预期红)</b>:历史链路同样拒绝透传越界分数——单条结果 {@code overallScore} 超出
|
|
|
+ * 0–100 时须拒显/过滤/拦截;当前 {@code getHistory} 原样透传。</li>
|
|
|
+ * <li><b>边界四(缺失,预期红)</b>:最新结果的量纲拦截须覆盖<b>八类全部维度</b>,而非仅个别抽查维度;
|
|
|
+ * 任一维度(如 {@code processingSpeedScore=120})越界即整体拒显。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito + 反射注入 + MyBatis-Plus 纯注解级 {@link TableInfoHelper} 初始化
|
|
|
+ * (无 Spring 上下文 / 无 DB / 无外部服务),沿用已合入主链同批 RED 契约测试风格。
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:边界二 / 三 / 四相关断言在实现补齐前均应失败(红);GREEN 阶段由实现方在
|
|
|
+ * {@code AssessmentHistoryItemDTO} 增加「量纲标注(标签 + 0–100 范围)」字段、在 {@code getHistory}
|
|
|
+ * 增加越界拒显后自动转绿。边界一(历史为空返回空列表)当前已落地,作对照用例。
|
|
|
+ */
|
|
|
+class AssessmentResultViewHistoryContractTest {
|
|
|
+
|
|
|
+ /** SC-P-39 八类报告的英文维度字段名(camelCase),量纲拦截须覆盖全量而非抽查 */
|
|
|
+ private static final String[] EIGHT_DIMENSION_FIELDS = {
|
|
|
+ "overallScore", "attentionScore", "focusScore", "memoryScore",
|
|
|
+ "logicScore", "perceptionScore", "spatialScore", "processingSpeedScore"
|
|
|
+ };
|
|
|
+
|
|
|
+ private AssessmentResultController controller;
|
|
|
+ private CognitiveService cognitiveService;
|
|
|
+ private DanAssessmentResultMapper danAssessmentResultMapper;
|
|
|
+
|
|
|
+ @BeforeEach
|
|
|
+ void setUp() throws Exception {
|
|
|
+ // 纯注解级初始化 MyBatis-Plus lambda 列名缓存(无 DB),使 getHistory 内部
|
|
|
+ // new LambdaQueryWrapper<DanAssessmentResult>().eq(DanAssessmentResult::getMemberId, ...) 可解析列名
|
|
|
+ TableInfoHelper.initTableInfo(
|
|
|
+ new MapperBuilderAssistant(new MybatisConfiguration(), ""), DanAssessmentResult.class);
|
|
|
+
|
|
|
+ controller = new AssessmentResultController();
|
|
|
+ // 反射注入全部非 static / 非 final 字段为 mock,隔离 DB / 外部服务
|
|
|
+ injectAll(controller);
|
|
|
+ cognitiveService = (CognitiveService) readField(controller, "cognitiveService");
|
|
|
+ danAssessmentResultMapper = (DanAssessmentResultMapper) readField(controller, "danAssessmentResultMapper");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界一(对照,已落地):历史为空不展示 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-39 边界(对照,当前已落地):历史为空时应返回 success(空列表) 而非错误")
|
|
|
+ void history_noResultsRecorded_shouldReturnSuccessEmptyList() {
|
|
|
+ when(danAssessmentResultMapper.selectList(any(Wrapper.class))).thenReturn(Collections.emptyList());
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("memberId", 55L);
|
|
|
+
|
|
|
+ Result<List<AssessmentHistoryItemDTO>> resp = controller.getHistory(body, null);
|
|
|
+
|
|
|
+ assertNotNull(resp, "Result 包装不应为 null");
|
|
|
+ assertEquals(200, resp.getCode(), "历史为空 → 应为成功码 200(而非错误码)");
|
|
|
+ assertNotNull(resp.getData(), "历史为空 → data 应为空列表(前端据空列表渲染空态)");
|
|
|
+ assertTrue(resp.getData().isEmpty(), "历史为空 → data 应为空列表");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 结构锚点(已落地,作对照):/history 契约载体 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("结构锚点:getHistory 方法 + POST /history 端点载体应存在,返回 Result<List<AssessmentHistoryItemDTO>>")
|
|
|
+ void historyContractCarrier_shouldExist() throws Exception {
|
|
|
+ assertNotNull(controller, "[assessment.result.view] AssessmentResultController 实例应可创建");
|
|
|
+
|
|
|
+ java.lang.reflect.Method m = AssessmentResultController.class.getMethod("getHistory", Map.class, Long.class);
|
|
|
+ assertNotNull(m, "[assessment.result.view] getHistory(Map, Long) 方法应存在(端点 /history 宿主)");
|
|
|
+
|
|
|
+ PostMapping pm = m.getAnnotation(PostMapping.class);
|
|
|
+ assertNotNull(pm, "[assessment.result.view] getHistory 应标注 @PostMapping");
|
|
|
+ assertTrue(Arrays.asList(pm.value()).contains("/history"),
|
|
|
+ "[assessment.result.view] getHistory 应映射 POST /api/assessment/history");
|
|
|
+
|
|
|
+ // 返回类型契约:Result<List<AssessmentHistoryItemDTO>>
|
|
|
+ Type rt = m.getGenericReturnType();
|
|
|
+ assertTrue(rt instanceof ParameterizedType, "[assessment.result.view] 返回类型应为 Result<...>");
|
|
|
+ ParameterizedType resultType = (ParameterizedType) rt;
|
|
|
+ assertEquals(Result.class, resultType.getRawType(), "[assessment.result.view] 返回包装应为 Result");
|
|
|
+ Type listType = resultType.getActualTypeArguments()[0];
|
|
|
+ assertTrue(listType instanceof ParameterizedType && List.class.equals(((ParameterizedType) listType).getRawType()),
|
|
|
+ "[assessment.result.view] Result 泛型应为 List<AssessmentHistoryItemDTO>");
|
|
|
+ assertEquals(AssessmentHistoryItemDTO.class, ((ParameterizedType) listType).getActualTypeArguments()[0],
|
|
|
+ "[assessment.result.view] 历史条目 DTO 应为 AssessmentHistoryItemDTO");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界二(缺失,预期红):历史条目须携带 0–100 量纲标注 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-39 边界:历史条目 AssessmentHistoryItemDTO 应暴露量纲标注(0–100)供历史列表直接渲染")
|
|
|
+ void historyDto_shouldExposeScaleAnnotationMatchingSharedConvention() {
|
|
|
+ List<String> fieldNames = dtoFieldNames(AssessmentHistoryItemDTO.class);
|
|
|
+
|
|
|
+ boolean hasScaleMin = fieldNames.contains("scaleMin");
|
|
|
+ boolean hasScaleMax = fieldNames.contains("scaleMax");
|
|
|
+ boolean hasScale = fieldNames.contains("scale");
|
|
|
+ boolean hasOverallScale = fieldNames.contains("overallScale");
|
|
|
+
|
|
|
+ assertTrue(hasScaleMin && hasScaleMax || hasScale || hasOverallScale,
|
|
|
+ "[assessment.result.view] AssessmentHistoryItemDTO(测评历史条目)缺少量纲标注字段:"
|
|
|
+ + "期望 scaleMin+scaleMax 或 scale/overallScale(0–100 范围),供历史列表与结果页同量纲渲染。"
|
|
|
+ + "SC-P-39 边界「量纲标注」未覆盖历史条目(当前仅裸 overallScore)。");
|
|
|
+
|
|
|
+ // 若实现已带上下界字段,其值必须锚定共享常量 SCORE_MIN=0 / SCORE_MAX=100(dimension-scoring-convention §1)
|
|
|
+ if (hasScaleMin && hasScaleMax) {
|
|
|
+ assertScaleBounds(AssessmentHistoryItemDTO.class, "scaleMin", "scaleMax");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-39 边界:历史条目应暴露综合得分中文标签(overallLabel)供历史列表直接展示")
|
|
|
+ void historyDto_shouldExposeOverallChineseLabel() {
|
|
|
+ List<String> fieldNames = dtoFieldNames(AssessmentHistoryItemDTO.class);
|
|
|
+
|
|
|
+ assertTrue(fieldNames.contains("overallLabel"),
|
|
|
+ "[assessment.result.view] AssessmentHistoryItemDTO 缺少综合得分中文标签字段(overallLabel):"
|
|
|
+ + "历史列表无法按八类报告口径直接渲染中文档位标题。SC-P-39「八类报告展示」的标签标注未覆盖历史条目。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界三(缺失,预期红):历史链路拒绝透传越界分数 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-39 边界:历史中越界分数(150 > 100)应被拒显/过滤,不得原样透传")
|
|
|
+ void history_outOfRangeOverallScore_shouldNotPassThrough() {
|
|
|
+ DanAssessmentResult stored = new DanAssessmentResult();
|
|
|
+ stored.setMemberId(55L);
|
|
|
+ stored.setChildName("小李");
|
|
|
+ stored.setOverallScore(150); // 超出 0–100 量纲
|
|
|
+ stored.setAssessmentDate(new Date());
|
|
|
+ when(danAssessmentResultMapper.selectList(any(Wrapper.class))).thenReturn(Collections.singletonList(stored));
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("memberId", 55L);
|
|
|
+
|
|
|
+ Result<List<AssessmentHistoryItemDTO>> resp = controller.getHistory(body, null);
|
|
|
+
|
|
|
+ boolean passesRaw = resp.getCode() == 200 && resp.getData() != null
|
|
|
+ && resp.getData().stream()
|
|
|
+ .anyMatch(i -> i.getOverallScore() != null && i.getOverallScore() == 150);
|
|
|
+ assertTrue(!passesRaw,
|
|
|
+ "[assessment.result.view] 历史链路越界分数(overallScore=150)应被拒显/过滤/拦截,"
|
|
|
+ + "当前 getHistory 原样透传脏数据。SC-P-39 量纲口径(0–100 闭区间)对历史链路同样生效。");
|
|
|
+ if (resp.getCode() != 200) {
|
|
|
+ assertNull(resp.getData(), "拒显时 data 不应携带脏数据");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 边界四(缺失,预期红):最新结果量纲拦截须覆盖八类全部维度 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-39 边界:八类报告任意维度越界(如 processingSpeedScore=120)都应整体拒显,而非仅抽查维度")
|
|
|
+ void latestResult_outOfRangeOnAnyOfEightDimensions_shouldRejectDisplay() {
|
|
|
+ // 构造一条仅「加工速度」维度越界的标准结果(整体/注意力等均在量纲内的正常值,仅偏离既有抽查维度)
|
|
|
+ DanAssessmentResult result = new DanAssessmentResult();
|
|
|
+ result.setMemberId(55L);
|
|
|
+ result.setChildName("小李");
|
|
|
+ result.setOverallScore(80);
|
|
|
+ result.setAttentionScore(80);
|
|
|
+ result.setFocusScore(75);
|
|
|
+ result.setMemoryScore(85);
|
|
|
+ result.setLogicScore(90);
|
|
|
+ result.setPerceptionScore(70);
|
|
|
+ result.setSpatialScore(88);
|
|
|
+ result.setProcessingSpeedScore(120); // 加工速度越界(120 > 100)
|
|
|
+ when(cognitiveService.getChildLatestResult(any(Long.class))).thenReturn(result);
|
|
|
+
|
|
|
+ Map<String, Object> body = new HashMap<>();
|
|
|
+ body.put("memberId", 55L);
|
|
|
+
|
|
|
+ Result<AssessmentResultDTO> resp = controller.getLatestResult(body, null);
|
|
|
+
|
|
|
+ boolean rejected = resp.getCode() != 200;
|
|
|
+ assertTrue(rejected,
|
|
|
+ "[assessment.result.view] 八类报告任一维度越界(processingSpeedScore=120)都应整体拒显(返回非 200),"
|
|
|
+ + "当前实现仅对个别抽查维度生效/未做全量拦截(code=" + resp.getCode() + ")。"
|
|
|
+ + "SC-P-39 量纲口径(0–100 闭区间)须覆盖八类全部维度字段:" + Arrays.toString(EIGHT_DIMENSION_FIELDS));
|
|
|
+ if (rejected) {
|
|
|
+ assertNull(resp.getData(), "拒显时 data 不应携带脏数据");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 辅助构造 / 反射 ====================
|
|
|
+
|
|
|
+ private List<String> dtoFieldNames(Class<?> dtoClass) {
|
|
|
+ return Arrays.stream(dtoClass.getDeclaredFields())
|
|
|
+ .map(Field::getName)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 断言 DTO 的量纲上下界字段值 == 共享常量 SCORE_MIN / SCORE_MAX(0–100 闭区间) */
|
|
|
+ private static void assertScaleBounds(Class<?> dtoClass, String minField, String maxField) {
|
|
|
+ try {
|
|
|
+ Field fMin = dtoClass.getDeclaredField(minField);
|
|
|
+ Field fMax = dtoClass.getDeclaredField(maxField);
|
|
|
+ fMin.setAccessible(true);
|
|
|
+ fMax.setAccessible(true);
|
|
|
+ Object vMin = fMin.get(dtoClass.newInstance());
|
|
|
+ Object vMax = fMax.get(dtoClass.newInstance());
|
|
|
+ assertEquals(DimensionCodeConstants.SCORE_MIN, ((Number) vMin).intValue(),
|
|
|
+ "[assessment.result.view] 量纲下界应锚定 DimensionCodeConstants.SCORE_MIN(0)");
|
|
|
+ assertEquals(DimensionCodeConstants.SCORE_MAX, ((Number) vMax).intValue(),
|
|
|
+ "[assessment.result.view] 量纲上界应锚定 DimensionCodeConstants.SCORE_MAX(100)");
|
|
|
+ } catch (ReflectiveOperationException e) {
|
|
|
+ fail("[assessment.result.view] 读取量纲标注字段失败: " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 反射注入全部非 static / 非 final 字段为对应类型 mock */
|
|
|
+ private static void injectAll(Object target) throws Exception {
|
|
|
+ for (Field f : target.getClass().getDeclaredFields()) {
|
|
|
+ if (Modifier.isStatic(f.getModifiers()) || Modifier.isFinal(f.getModifiers())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ f.setAccessible(true);
|
|
|
+ f.set(target, mock(f.getType()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Object readField(Object target, String name) throws Exception {
|
|
|
+ Field f = target.getClass().getDeclaredField(name);
|
|
|
+ f.setAccessible(true);
|
|
|
+ return f.get(target);
|
|
|
+ }
|
|
|
+}
|