|
|
@@ -0,0 +1,299 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.controller.ai.AIChatController;
|
|
|
+import com.etotem.cfc.controller.growth.GrowthPlanController;
|
|
|
+import com.etotem.cfc.dto.AiGateResult;
|
|
|
+import com.etotem.cfc.entity.GrowthPlan;
|
|
|
+import com.etotem.cfc.enums.AiGateStatus;
|
|
|
+import com.etotem.cfc.service.AiGateway;
|
|
|
+import com.etotem.cfc.service.MembershipService;
|
|
|
+import com.etotem.cfc.service.PortraitService;
|
|
|
+import com.etotem.cfc.service.UserService;
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+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.lang.reflect.Parameter;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
|
+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.ArgumentMatchers.anyLong;
|
|
|
+import static org.mockito.ArgumentMatchers.anyString;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(补充):AI 方案生成的<b>路由行为、身份门禁与结构化持久化</b>(能力键
|
|
|
+ * {@code plan.ai.generate},任务 S-cfc-279,业务域 D,TDD-RED 预期红)。
|
|
|
+ *
|
|
|
+ * <p><b>与主契约测试的分工</b>:主链已合入 {@code PlanAiGenerateContractTest}(S-cfc-279,
|
|
|
+ * commit 78a270bf7),以反射探测 {@code AiGateway.generateGrowthPlan} /
|
|
|
+ * {@code regenerateGrowthPlanSection} 的生成/重生成/付费墙/Fallback 行为。本类<b>不重复</b>这些断言,
|
|
|
+ * 而是补充三类主测试未覆盖的契约面:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li>§4.9 载体路由 {@code AIChatController.sendHealthCoachMessage} 在 LangGraph 返回 null
|
|
|
+ * (超时/熔断)时须给出非空 Fallback 兜底文案(TC-P-40-E),不能只回空串;</li>
|
|
|
+ * <li>{@code AIChatController} 的生成/重生成端点须以 {@code @RequestAttribute("userId"/"familyId")}
|
|
|
+ * 暴露身份与家庭输入,承载付费墙与横向越权守卫(TC-P-40-P / TC-P-41-P);</li>
|
|
|
+ * <li>{@code GrowthPlan} 实体须有 {@code overview} / {@code sections} 字段,使 overview+三板块
|
|
|
+ * 能以结构化形式落地,而非混在整段 {@code planContent} 文本中(SC-P-40/41)。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
+ * <ul>
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-40 SC-P-41;
|
|
|
+ * {@code tests/test-cases/parent.md} TC-P-40 / TC-P-41 及 -E / -P 派生用例。</li>
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.9 {@code POST /api/ai/health-coach/send}
|
|
|
+ * (LangGraph 路由)、§4.6 {@code POST /api/growth/plan/create};
|
|
|
+ * 模块:{@code AIChatController} / {@code AiGateway} / {@code GrowthPlanController}。</li>
|
|
|
+ * </ul>
|
|
|
+ *
|
|
|
+ * <p><b>RED 预期</b>:9 条契约断言用例在 GREEN 补齐前失败(红);2 条载体基线 + 1 条既有付费墙用例
|
|
|
+ * 预期绿,用于证明 Mockito 夹具本身可用、失败确由契约缺失造成而非测试脚手架错误。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试范围</b>:纯 Mockito / 反射,无 Spring / 无 DB / 无外部服务;与同批已合入主链的
|
|
|
+ * {@code TeamProfitHookContractTest} / {@code FamilyJoinValidationRulesContractTest} /
|
|
|
+ * {@code AiCoachReportAnalyzeContractTest} 风格一致。期望值均为手工推导的字面量/契约键,
|
|
|
+ * 不复用被测代码计算。</p>
|
|
|
+ */
|
|
|
+class PlanAiGenerateRouteContractTest extends AssessmentPlanContractTestBase {
|
|
|
+
|
|
|
+ private static final long USER_ID = 1L;
|
|
|
+ private static final long MEMBER_ID = 10L;
|
|
|
+ private static final int CODE_MEMBER_ONLY = 46003;
|
|
|
+ private static final String PATH_GENERATE = "/plan/generate";
|
|
|
+ private static final String PATH_REGENERATE_SECTION = "/plan/regenerate-section";
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 载体基线(§4.9 / §4.6):预期绿——证明夹具可用,红失败确由契约缺失造成
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("载体基线-A:AIChatController 已提供 POST /api/ai/health-coach/send(§4.9 LangGraph 路由)")
|
|
|
+ void carrier_healthCoachSendExists() {
|
|
|
+ Method m = findMethod(AIChatController.class, "sendHealthCoachMessage");
|
|
|
+ assertNotNull(m, "[plan.ai.generate] §4.9 载体 AIChatController.sendHealthCoachMessage 不存在。");
|
|
|
+ assertTrue(hasPostMapping(m, "/health-coach/send"),
|
|
|
+ "[plan.ai.generate] §4.9 载体缺 POST /health-coach/send 映射。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("载体基线-B:GrowthPlanController 已提供 POST /api/growth/plan/create(§4.6)")
|
|
|
+ void carrier_growthPlanCreateExists() {
|
|
|
+ Method m = findMethod(GrowthPlanController.class, "createPlan");
|
|
|
+ assertNotNull(m, "[plan.ai.generate] §4.6 载体 GrowthPlanController.createPlan 不存在。");
|
|
|
+ assertTrue(hasPostMapping(m, "/create"),
|
|
|
+ "[plan.ai.generate] §4.6 载体缺 POST /create 映射。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 正常流(SC-P-40):选孩子+目标 → LangGraph(AiGateway) → overview+三板块
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-1:AiGateway 应提供儿童成长规划 AI 生成入口 generateGrowthPlan —— 当前未落地")
|
|
|
+ void normal_aiGateway_hasGrowthPlanGenerationEntry() {
|
|
|
+ Method m = findMethod(AiGateway.class, "generateGrowthPlan");
|
|
|
+ assertNotNull(m,
|
|
|
+ "[plan.ai.generate] SC-P-40 契约要求 AiGateway 提供 generateGrowthPlan(...) 入口,"
|
|
|
+ + "经 LangGraph 生成 overview+三板块(区别于健康计划红线 generateHealthPlan)。"
|
|
|
+ + "实现现状:AiGateway 只有 generateHealthPlan,儿童成长规划生成入口未落地。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-2:AIChatController 应提供 POST /plan/generate AI 方案生成端点 —— 当前未落地")
|
|
|
+ void normal_aiChatController_hasGeneratePlanEndpoint() {
|
|
|
+ Method m = findMethod(AIChatController.class, "generatePlan");
|
|
|
+ assertNotNull(m,
|
|
|
+ "[plan.ai.generate] SC-P-40 契约要求 AIChatController 暴露 AI 方案生成端点"
|
|
|
+ + "(方法 generatePlan,POST " + PATH_GENERATE + "),承接「选孩子+目标」请求。"
|
|
|
+ + "实现现状:该类只有 /chat/send、/health-coach/send 等对话端点,无方案生成端点。");
|
|
|
+ assertTrue(hasPostMapping(m, PATH_GENERATE),
|
|
|
+ "[plan.ai.generate] generatePlan 应标注 @PostMapping(\"" + PATH_GENERATE + "\")(§4.9 AI 域)。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-3:GrowthPlan 实体应含 overview 字段(生成结果总览)—— 当前未落地")
|
|
|
+ void normal_growthPlanEntity_hasOverview() {
|
|
|
+ Field f = findField(GrowthPlan.class, "overview");
|
|
|
+ assertNotNull(f,
|
|
|
+ "[plan.ai.generate] SC-P-40 契约要求生成 overview(总体概述)并随方案持久化。"
|
|
|
+ + "实现现状:GrowthPlan 仅有整段 planContent,无 overview 字段。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-4:GrowthPlan 实体应含 sections 字段(营养/饮食/运动三板块)—— 当前未落地")
|
|
|
+ void normal_growthPlanEntity_hasSections() {
|
|
|
+ Field f = findField(GrowthPlan.class, "sections");
|
|
|
+ assertNotNull(f,
|
|
|
+ "[plan.ai.generate] SC-P-40/41 契约要求生成 nutrition/diet/exercise 三板块并支持板块级"
|
|
|
+ + "重生成/确认(TC-P-41)。实现现状:GrowthPlan 无 sections 字段,"
|
|
|
+ + "板块内容只混在 planContent 文本中,无法逐板块迭代。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("正常流-5:AIChatController 应提供 POST /plan/regenerate-section 分板块重生成端点 —— 当前未落地")
|
|
|
+ void normal_aiChatController_hasRegenerateSectionEndpoint() {
|
|
|
+ Method m = findMethod(AIChatController.class, "regeneratePlanSection");
|
|
|
+ assertNotNull(m,
|
|
|
+ "[plan.ai.generate] SC-P-41 契约要求逐板块重新生成/确认(TC-P-41),"
|
|
|
+ + "AIChatController 应暴露 regeneratePlanSection(POST "
|
|
|
+ + PATH_REGENERATE_SECTION + ")。实现现状:无该端点。");
|
|
|
+ assertTrue(hasPostMapping(m, PATH_REGENERATE_SECTION),
|
|
|
+ "[plan.ai.generate] regeneratePlanSection 应标注 @PostMapping(\""
|
|
|
+ + PATH_REGENERATE_SECTION + "\")。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 异常流(TC-P-40-E / TC-P-41-E)
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-1(载体基线):未购会员请求 AI 能力 → 46003 提示开通会员,不白生成")
|
|
|
+ void exceptional_paywallPromptOnCarrier() throws Exception {
|
|
|
+ AIChatController c = newCoachController(false);
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("query", "为成员生成成长方案");
|
|
|
+ params.put("memberId", MEMBER_ID);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = c.sendHealthCoachMessage(USER_ID, params);
|
|
|
+
|
|
|
+ assertEquals(CODE_MEMBER_ONLY, r.getCode(),
|
|
|
+ "[plan.ai.generate] TC-P-40-E 契约要求未购会员请求 AI 生成被付费墙拦截(46003)。");
|
|
|
+ assertNotNull(r.getMessage(), "付费墙拦截应给出提示文案。");
|
|
|
+ assertTrue(r.getMessage().contains("会员"),
|
|
|
+ "[plan.ai.generate] 未购会员提示应包含「会员」字样,实际=" + r.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-2:AI 超时(AiGateway 返回 null)应返回 Fallback 兜底提示,不 500、不空白 —— 当前未落地")
|
|
|
+ void exceptional_timeoutFallback_notBlank() throws Exception {
|
|
|
+ AIChatController c = newCoachController(true);
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
+ params.put("query", "请为成员 " + MEMBER_ID + " 生成提升专注力的成长方案");
|
|
|
+ params.put("memberId", MEMBER_ID);
|
|
|
+
|
|
|
+ Result<Map<String, Object>> r = c.sendHealthCoachMessage(USER_ID, params);
|
|
|
+
|
|
|
+ assertEquals(200, r.getCode(),
|
|
|
+ "[plan.ai.generate] SC-P-40 契约要求 AI 超时 Fallback 不 500,实际 code=" + r.getCode());
|
|
|
+ assertNotNull(r.getData(), "Fallback 响应 data 不应为 null。");
|
|
|
+ Object answerObj = r.getData().get("answer");
|
|
|
+ String answer = answerObj != null ? String.valueOf(answerObj) : "";
|
|
|
+ assertTrue(!answer.trim().isEmpty(),
|
|
|
+ "[plan.ai.generate] TC-P-40-E 契约要求:AI 超时/AiGateway 返回 null 时,"
|
|
|
+ + "健康教练 LangGraph 路由应返回 Fallback 兜底方案+提示(非空 answer),"
|
|
|
+ + "不返回空白。实现现状 answer=\"\"。");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("异常流-3:重生成板块 A 失败应保留其他板块(不丢数据)—— 当前无板块级端点可验证")
|
|
|
+ void exceptional_regenerateFailure_preservesOtherSections() throws Exception {
|
|
|
+ Method m = findMethod(AIChatController.class, "regeneratePlanSection");
|
|
|
+ if (m == null) {
|
|
|
+ fail("[plan.ai.generate] TC-P-41-E 契约要求:重生成板块 A 断网/Fallback 时保留其他板块内容;"
|
|
|
+ + "实现现状无 regeneratePlanSection 端点,无法执行板块级重生成,"
|
|
|
+ + "更无法保证「重生成不丢其他板块」。");
|
|
|
+ }
|
|
|
+ // GREEN 阶段:用 planJson fixture(三板块各含独立标记)调用端点并断言其余两板块原样保留。
|
|
|
+ assertTrue(hasRequestAttribute(m, "familyId") || hasRequestAttribute(m, "userId"),
|
|
|
+ "[plan.ai.generate] 板块重生成端点应暴露身份/家庭请求属性以校验归属,防止越权重生成他人方案。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 权限流(TC-P-40-P / TC-P-41-P)
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("权限流:AI 方案生成端点应暴露 userId/familyId 请求属性以承载付费墙与归属校验 —— 当前未落地")
|
|
|
+ void permission_generatePlan_hasIdentityAndFamilyInputs() {
|
|
|
+ Method m = findMethod(AIChatController.class, "generatePlan");
|
|
|
+ if (m == null) {
|
|
|
+ fail("[plan.ai.generate] TC-P-40-P/TC-P-41-P 契约要求:非归属数据访问统一 403/401;"
|
|
|
+ + "实现现状无 generatePlan 端点,无从校验 userId/familyId 归属。");
|
|
|
+ }
|
|
|
+ assertTrue(hasRequestAttribute(m, "userId"),
|
|
|
+ "[plan.ai.generate] generatePlan 应暴露 @RequestAttribute(\"userId\") 以执行会员付费墙。");
|
|
|
+ assertTrue(hasRequestAttribute(m, "familyId"),
|
|
|
+ "[plan.ai.generate] generatePlan 应暴露 @RequestAttribute(\"familyId\") 以执行家庭归属校验。");
|
|
|
+ }
|
|
|
+
|
|
|
+ // ==================================================================
|
|
|
+ // 辅助构造(纯 Mockito / 反射,无 Spring / 无 DB / 无外部服务)
|
|
|
+ // ==================================================================
|
|
|
+
|
|
|
+ /** 构造健康教练路由 controller:可控门禁 + AiGateway.chat 返回 null 模拟超时。 */
|
|
|
+ private AIChatController newCoachController(boolean canUse) throws Exception {
|
|
|
+ AIChatController c = new AIChatController();
|
|
|
+
|
|
|
+ MembershipService membershipSvc = mock(MembershipService.class);
|
|
|
+ AiGateResult gate = new AiGateResult();
|
|
|
+ gate.setEnabled(true);
|
|
|
+ gate.setStatus(canUse ? AiGateStatus.ALLOW : AiGateStatus.UPGRADE_REQUIRED);
|
|
|
+ gate.setCanUse(canUse);
|
|
|
+ when(membershipSvc.checkAiFeatureGate(anyLong())).thenReturn(gate);
|
|
|
+ inject(c, "membershipService", membershipSvc);
|
|
|
+
|
|
|
+ AiGateway gateway = mock(AiGateway.class);
|
|
|
+ when(gateway.chat(anyString(), anyLong(), any(), any(Map.class))).thenReturn(null);
|
|
|
+ inject(c, "aiGateway", gateway);
|
|
|
+
|
|
|
+ UserService userSvc = mock(UserService.class);
|
|
|
+ when(userSvc.getUserInfo(anyLong())).thenReturn(null);
|
|
|
+ inject(c, "userService", userSvc);
|
|
|
+
|
|
|
+ PortraitService portraitSvc = mock(PortraitService.class);
|
|
|
+ when(portraitSvc.buildPortraitPrompt(anyLong(), any())).thenReturn(null);
|
|
|
+ inject(c, "portraitService", portraitSvc);
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Method findMethod(Class<?> clazz, String name) {
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
+ if (m.getName().equals(name)) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Field findField(Class<?> clazz, String name) {
|
|
|
+ Class<?> cur = clazz;
|
|
|
+ while (cur != null) {
|
|
|
+ for (Field f : cur.getDeclaredFields()) {
|
|
|
+ if (f.getName().equals(name)) {
|
|
|
+ return f;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ cur = cur.getSuperclass();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean hasPostMapping(Method m, String path) {
|
|
|
+ PostMapping ann = m.getAnnotation(PostMapping.class);
|
|
|
+ return ann != null && Arrays.asList(ann.value()).contains(path);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean hasRequestAttribute(Method m, String attr) {
|
|
|
+ for (Parameter p : m.getParameters()) {
|
|
|
+ for (Annotation a : p.getAnnotations()) {
|
|
|
+ if (a instanceof RequestAttribute && attr.equals(((RequestAttribute) a).value())) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|