|
|
@@ -0,0 +1,255 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.controller.tianpan.TianpanController;
|
|
|
+import com.etotem.cfc.dto.TianpanDashboardVO;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestAttribute;
|
|
|
+
|
|
|
+import java.lang.annotation.Annotation;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED):能力键 {@code ai.coach.familyBoard} / 任务号 S-cfc-249 ——
|
|
|
+ * 「家庭成长看板(教练聚合视图)」<b>既有 API 面</b>契约缺口。
|
|
|
+ *
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-B-10——目标「一屏聚合全家五维/任务/报告/方案」,
|
|
|
+ * 流程「打开 → 聚合展示 → 下钻」,边界「仅本家庭;缺失降级」(模块:家庭天盘/仪表板)。</li>
|
|
|
+ * <li>需求(AC):{@code tests/test-cases/butler.md} TC-B-10 / TC-B-10-E / TC-B-10-P——
|
|
|
+ * 四类聚合卡片齐全 + 每张卡片下钻 + 数据缺失降级空态;跨家庭/孩子端 403。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.10(测评)/§4.14(先天画像)五维来源;
|
|
|
+ * 派单指定载体 = 家庭天盘 {@code TianpanController /api/tianpan/*}
|
|
|
+ * (dashboard / member/{memberId} / related-items,全家五维/任务/报告/方案聚合下钻),
|
|
|
+ * 与 {@code teacher.family.overview} 同源(任务总览/进行中方案/报告对比/天盘聚合)。</li>
|
|
|
+ * <li>派单:{@code docs/tdd/orchestration/RED-pending-dispatch-2026-11-14.md} S-cfc-249 /
|
|
|
+ * {@code docs/tdd/orchestration/sc-key-map.md} SC-B-10 = {@code ai.coach.familyBoard}(RED)。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>与已合入主链的 {@code CoachFamilyBoardContractTest} 互补</b>:后者锚定「新建宿主服务 + 新 Controller」
|
|
|
+ * 路径({@code CoachFamilyBoardService} / {@code CoachFamilyBoardController}),本类则锚定派单明确指定的
|
|
|
+ * <b>既有天盘面</b>——防止「另起炉灶」导致 {@code /api/tianpan/*} 与 {@code teacher.family.overview}
|
|
|
+ * 同源数据无法承载看板契约。两批均为纯 JUnit + 反射,无 Spring 上下文 / 无 DB / 无外部服务。</p>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:{@link TianpanController} 现有 7 个 POST 端点,均无「家庭看板聚合/下钻」入口;
|
|
|
+ * {@link TianpanDashboardVO} 仅含 familyName/members/relationships/familyWuxing/relatedItems/dailyFortune/
|
|
|
+ * annualEnergies,<b>不含</b>五维聚合/任务/报告/方案/预警卡片,也不含降级默认值字段。
|
|
|
+ * 因此下列断言全部失败(红),失败原文以 {@code [S-cfc-249/ai.coach.familyBoard]} 开头。</p>
|
|
|
+ *
|
|
|
+ * <p><b>GREEN 指引</b>:在天盘面(或契约端点 {@code /api/ai/coach/family-board})补齐
|
|
|
+ * 聚合入口 + 下钻入口 + 响应载体四类卡片字段 + 降级默认值,并保持 {@code familyId} 家庭隔离后自动转绿。</p>
|
|
|
+ */
|
|
|
+class AiCoachFamilyBoardSurfaceContractTest {
|
|
|
+
|
|
|
+ /** SC-B-10 / TC-B-10 四类聚合卡片 + 预警项。 */
|
|
|
+ private static final Set<String> BOARD_CARD_FIELDS = new HashSet<>(Arrays.asList(
|
|
|
+ "fiveDimension", "reports", "tasks", "plans", "warnings"));
|
|
|
+
|
|
|
+ /** TC-B-10-E 缺失降级默认值:整页空态/局部降级/引导文案。 */
|
|
|
+ private static final Set<String> DEGRADATION_FIELDS = new HashSet<>(Arrays.asList(
|
|
|
+ "degraded", "emptyState", "emptyGuidance"));
|
|
|
+
|
|
|
+ /** 聚合入口候选方法名(既有天盘面 / 新契约端点)。 */
|
|
|
+ private static final Set<String> AGGREGATE_METHOD_NAMES = new HashSet<>(Arrays.asList(
|
|
|
+ "familyBoard", "aggregateBoard", "board", "aggregate", "familyBoardAggregate"));
|
|
|
+
|
|
|
+ /** 聚合入口候选 @PostMapping 路径。 */
|
|
|
+ private static final Set<String> AGGREGATE_PATHS = new HashSet<>(Arrays.asList(
|
|
|
+ "/family-board", "/board", "/aggregate", "/family-board/aggregate"));
|
|
|
+
|
|
|
+ /** 下钻入口候选方法名 / 路径。 */
|
|
|
+ private static final Set<String> DRILL_DOWN_METHOD_NAMES = new HashSet<>(Arrays.asList(
|
|
|
+ "drillDown", "familyBoardDetail", "boardDetail", "detail"));
|
|
|
+
|
|
|
+ private static final Set<String> DRILL_DOWN_PATHS = new HashSet<>(Arrays.asList(
|
|
|
+ "/family-board/detail", "/board/detail", "/drill-down", "/family-board/drill-down"));
|
|
|
+
|
|
|
+ /** 家庭隔离服务方法候选(既有宿主 / 新宿主)。 */
|
|
|
+ private static final Set<String> FAMILY_ACCESS_METHODS = new HashSet<>(Arrays.asList(
|
|
|
+ "hasFamilyAccess", "aggregateBoardForFamily", "canViewWholeFamilyBoard"));
|
|
|
+
|
|
|
+ // ==================== 正常流:聚合入口 + 四类卡片 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:看板响应载体应含五维/报告/任务/方案/预警五类聚合字段")
|
|
|
+ void boardCarrier_shouldCarryFourAggregationCards() {
|
|
|
+ List<Class<?>> carriers = boardCarrierClasses();
|
|
|
+ for (Class<?> carrier : carriers) {
|
|
|
+ Set<String> fields = declaredFieldNames(carrier);
|
|
|
+ if (fields.containsAll(BOARD_CARD_FIELDS)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(false,
|
|
|
+ "[S-cfc-249/ai.coach.familyBoard] 看板响应载体缺少聚合卡片字段 "
|
|
|
+ + BOARD_CARD_FIELDS + ":SC-B-10 要求一屏聚合全家五维/任务/报告/方案(+预警),"
|
|
|
+ + "当前 TianpanDashboardVO 仅返回成员/关系/五行/运势/年度能量,聚合卡片未落地(实现未落地)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:看板一屏聚合应为 @PostMapping 端点(禁止 GET)")
|
|
|
+ void boardAggregate_shouldBePostEndpoint() {
|
|
|
+ Method endpoint = findBoardEndpoint(AGGREGATE_METHOD_NAMES, AGGREGATE_PATHS);
|
|
|
+ assertNotNull(endpoint,
|
|
|
+ "[S-cfc-249/ai.coach.familyBoard] 家庭看板聚合 POST 端点未落地:"
|
|
|
+ + "应在 TianpanController /api/tianpan/*(或 /api/ai/coach/family-board)提供"
|
|
|
+ + "「打开→聚合展示」入口(@PostMapping)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:卡片下钻应为 @PostMapping 端点")
|
|
|
+ void boardDrillDown_shouldBePostEndpoint() {
|
|
|
+ Method endpoint = findBoardEndpoint(DRILL_DOWN_METHOD_NAMES, DRILL_DOWN_PATHS);
|
|
|
+ assertNotNull(endpoint,
|
|
|
+ "[S-cfc-249/ai.coach.familyBoard] 卡片下钻 POST 端点未落地:"
|
|
|
+ + "SC-B-10 流程「打开→聚合展示→下钻」要求每张卡片可下钻到详情(@PostMapping)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 异常流:缺失降级默认值 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-4:看板响应载体应暴露缺失降级默认值(degraded/emptyState/emptyGuidance)")
|
|
|
+ void boardCarrier_shouldExposeDegradationDefaults() {
|
|
|
+ List<Class<?>> carriers = boardCarrierClasses();
|
|
|
+ for (Class<?> carrier : carriers) {
|
|
|
+ Set<String> fields = declaredFieldNames(carrier);
|
|
|
+ if (fields.containsAll(DEGRADATION_FIELDS)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ assertTrue(false,
|
|
|
+ "[S-cfc-249/ai.coach.familyBoard] 看板响应载体缺少缺失降级默认值字段 "
|
|
|
+ + DEGRADATION_FIELDS + ":TC-B-10-E 要求某成员报告缺失/能量超时时局部降级、"
|
|
|
+ + "全家无数据时整页空态并给出引导,当前默认字段未实现(实现未落地)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 权限流:家庭隔离 ====================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流-5:看板应做家庭隔离(familyId 请求属性或服务级 hasFamilyAccess)")
|
|
|
+ void board_shouldEnforceFamilyIsolation() {
|
|
|
+ Method endpoint = findBoardEndpoint(AGGREGATE_METHOD_NAMES, AGGREGATE_PATHS);
|
|
|
+ if (endpoint != null && hasRequestAttribute(endpoint, "familyId")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (hasFamilyAccessServiceMethod()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ assertTrue(false,
|
|
|
+ "[S-cfc-249/ai.coach.familyBoard] 看板未落地家庭隔离契约:"
|
|
|
+ + "聚合端点应接收 @RequestAttribute(\"familyId\"),或宿主服务应提供 "
|
|
|
+ + FAMILY_ACCESS_METHODS + " 之一(仅本家庭;跨家庭 403)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================== 反射辅助(宽松适配既有天盘面 / 新契约端点) ====================
|
|
|
+
|
|
|
+ /** 可加载的看板响应载体:既有天盘 VO 必在,新 VO 若已落地一并纳入。 */
|
|
|
+ private List<Class<?>> boardCarrierClasses() {
|
|
|
+ List<Class<?>> classes = new ArrayList<>();
|
|
|
+ classes.add(TianpanDashboardVO.class);
|
|
|
+ addIfPresent(classes, "com.etotem.cfc.dto.FamilyBoardVO");
|
|
|
+ addIfPresent(classes, "com.etotem.cfc.dto.CoachFamilyBoardVO");
|
|
|
+ addIfPresent(classes, "com.etotem.cfc.dto.FamilyBoardAggregateVO");
|
|
|
+ return classes;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addIfPresent(List<Class<?>> classes, String name) {
|
|
|
+ Class<?> clazz = loadClass(name);
|
|
|
+ if (clazz != null) {
|
|
|
+ classes.add(clazz);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 在既有天盘 Controller 与新契约 Controller 中按方法名 / 路径查找 POST 端点。 */
|
|
|
+ private Method findBoardEndpoint(Set<String> methodNames, Set<String> paths) {
|
|
|
+ List<Class<?>> controllers = new ArrayList<>();
|
|
|
+ controllers.add(TianpanController.class);
|
|
|
+ addIfPresent(controllers, "com.etotem.cfc.controller.ai.CoachFamilyBoardController");
|
|
|
+ for (Class<?> controller : controllers) {
|
|
|
+ for (Method m : controller.getDeclaredMethods()) {
|
|
|
+ if (!m.isAnnotationPresent(PostMapping.class) || m.isAnnotationPresent(GetMapping.class)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (methodNames.contains(m.getName()) || hasAnyPostPath(m, paths)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasAnyPostPath(Method m, Set<String> paths) {
|
|
|
+ PostMapping pm = m.getAnnotation(PostMapping.class);
|
|
|
+ if (pm == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (String path : pm.value()) {
|
|
|
+ if (paths.contains(path)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String path : pm.path()) {
|
|
|
+ if (paths.contains(path)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasRequestAttribute(Method m, String value) {
|
|
|
+ for (Annotation[] paramAnnotations : m.getParameterAnnotations()) {
|
|
|
+ for (Annotation a : paramAnnotations) {
|
|
|
+ if (a instanceof RequestAttribute && value.equals(((RequestAttribute) a).value())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasFamilyAccessServiceMethod() {
|
|
|
+ List<String> hosts = Arrays.asList(
|
|
|
+ "com.etotem.cfc.service.CoachFamilyBoardService",
|
|
|
+ "com.etotem.cfc.service.FamilyBoardService");
|
|
|
+ for (String host : hosts) {
|
|
|
+ Class<?> clazz = loadClass(host);
|
|
|
+ if (clazz == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ for (Method m : clazz.getMethods()) {
|
|
|
+ if (FAMILY_ACCESS_METHODS.contains(m.getName())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Set<String> declaredFieldNames(Class<?> clazz) {
|
|
|
+ Set<String> names = new HashSet<>();
|
|
|
+ for (Field f : clazz.getDeclaredFields()) {
|
|
|
+ names.add(f.getName());
|
|
|
+ }
|
|
|
+ return names;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|