|
|
@@ -0,0 +1,278 @@
|
|
|
+package com.etotem.cfc.unit;
|
|
|
+
|
|
|
+import org.junit.jupiter.api.DisplayName;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+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.fail;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 契约级单测(TDD-RED,预期失败/红):能力键 {@code plan.ai.generate} 的边界/异常流补强。
|
|
|
+ *
|
|
|
+ * <p><b>与同批已合入测试的关系</b>:{@code PlanAiGenerateContractTest}(8 例)已覆盖 SC-P-40/41
|
|
|
+ * 主路径(生成 overview+三板块、会员门禁、生成超时 Fallback、重生成不丢其他板块、端点结构);
|
|
|
+ * 本文件只补其未覆盖的 TC-P-41-E 两条 AC:</p>
|
|
|
+ * <ol>
|
|
|
+ * <li>「重生成板块 A 时断网 → Fallback 保留其他板块不丢」;</li>
|
|
|
+ * <li>「连续点同一板块重生成 → 幂等,仅 1 次重生成(内容不漂移)」。</li>
|
|
|
+ * </ol>
|
|
|
+ *
|
|
|
+ * <p><b>契约溯源</b>:{@code tests/user-scenarios/应用场景总览.md} SC-P-41(逐板块重新生成/确认,
|
|
|
+ * 重生成不丢其他板块);{@code tests/test-cases/parent.md} TC-P-41-E;
|
|
|
+ * {@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};业务域 D。</p>
|
|
|
+ *
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:{@code src/main} 中 {@code AiGateway} 无
|
|
|
+ * {@code generateGrowthPlan(Map, boolean)} / {@code regenerateGrowthPlanSection(Map, boolean)}
|
|
|
+ * 入口,本测试以反射锚定契约宿主并断言方法缺失(红),GREEN 阶段实现后自动转绿。</p>
|
|
|
+ *
|
|
|
+ * <p><b>测试边界</b>:纯反射 + JUnit 5,无 Spring 上下文 / 无 DB / 无真实外部服务调用,
|
|
|
+ * 与同批 RED 契约测试(TeamProfitHookContractTest / TeamStarMultiplierContractTest /
|
|
|
+ * TeamProfitPipelineContractTest / FamilyJoinValidationRulesContractTest)风格一致。</p>
|
|
|
+ */
|
|
|
+class PlanAiGenerateEdgeContractTest {
|
|
|
+
|
|
|
+ /** 契约宿主:AI 统一入口(LangGraph 网关 + Fallback)。 */
|
|
|
+ private static final String GATEWAY_CLASS = "com.etotem.cfc.service.AiGateway";
|
|
|
+
|
|
|
+ /** 契约方法(SC-P-40/41,GREEN 按此签名落地)。 */
|
|
|
+ private static final String METHOD_GENERATE = "generateGrowthPlan";
|
|
|
+ private static final String METHOD_REGENERATE = "regenerateGrowthPlanSection";
|
|
|
+
|
|
|
+ /** 三板块(TC-P-13/TC-P-40:营养补充建议 / 饮食建议 / 运动计划)。 */
|
|
|
+ private static final Collection<String> SECTION_KEYS = Arrays.asList("nutrition", "diet", "exercise");
|
|
|
+
|
|
|
+ // ================================================================
|
|
|
+ // TC-P-41-E-1:重生成时断网 → Fallback 且保留其他板块
|
|
|
+ // ================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-41/TC-P-41-E:重生成 nutrition 时 LangGraph 断网 → Fallback 保留 diet/exercise")
|
|
|
+ void regenerationOfflineFallback_preservesOtherSections() {
|
|
|
+ Object host = gatewayHost();
|
|
|
+
|
|
|
+ Map<String, Object> baseline = asMap(call(host, METHOD_GENERATE, baseInputs(), Boolean.TRUE),
|
|
|
+ "基线生成返回值");
|
|
|
+ assertThreeSections(baseline, "基线生成");
|
|
|
+
|
|
|
+ // 模拟上游不可用:不可达地址 + 短超时 + 熔断打开,任何真实调用都会快速失败
|
|
|
+ setField(host, "baseUrl", "http://127.0.0.1:1");
|
|
|
+ setField(host, "timeoutMs", 50);
|
|
|
+ setField(host, "readTimeoutMs", 50);
|
|
|
+ invokeNoArg(host, "init");
|
|
|
+ setField(host, "circuitOpen", Boolean.TRUE);
|
|
|
+ setField(host, "lastFailureTime", System.currentTimeMillis());
|
|
|
+
|
|
|
+ Map<String, Object> inputs = new LinkedHashMap<String, Object>(baseInputs());
|
|
|
+ inputs.put("sectionKey", "nutrition");
|
|
|
+ inputs.put("content", "【离线新营养建议】");
|
|
|
+ Map<String, Object> result = asMap(
|
|
|
+ callExpectNoThrow(host, METHOD_REGENERATE, inputs, Boolean.TRUE),
|
|
|
+ METHOD_REGENERATE + " 断网返回值");
|
|
|
+
|
|
|
+ assertNotNull(result,
|
|
|
+ "RED[plan.ai.generate] 重生成时 LangGraph 断网必须返回 Fallback,不得返回 null 导致 500");
|
|
|
+ assertEquals(Boolean.TRUE, result.get("fallback_used"),
|
|
|
+ "RED[plan.ai.generate] 重生成断网结果应标记 fallback_used=true");
|
|
|
+ assertThreeSections(result, "断网重生成");
|
|
|
+ for (String key : Arrays.asList("diet", "exercise")) {
|
|
|
+ assertEquals(sectionContent(baseline, key), sectionContent(result, key),
|
|
|
+ "RED[plan.ai.generate] 重生成断网 Fallback 不得丢失/改写 " + key + " 板块(TC-P-41-E)");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ================================================================
|
|
|
+ // TC-P-41-E-2:连续点击同一板块重生成幂等
|
|
|
+ // ================================================================
|
|
|
+
|
|
|
+ @Test
|
|
|
+ @DisplayName("SC-P-41/TC-P-41-E:连续点同一板块重生成应幂等(内容不漂移,其他板块不变)")
|
|
|
+ void consecutiveRegeneration_isIdempotent() {
|
|
|
+ Object host = gatewayHost();
|
|
|
+
|
|
|
+ Map<String, Object> baseline = asMap(call(host, METHOD_GENERATE, baseInputs(), Boolean.TRUE),
|
|
|
+ "基线生成返回值");
|
|
|
+ assertThreeSections(baseline, "基线生成");
|
|
|
+
|
|
|
+ Map<String, Object> inputs = new LinkedHashMap<String, Object>(baseInputs());
|
|
|
+ inputs.put("sectionKey", "nutrition");
|
|
|
+ inputs.put("content", "【幂等营养建议】每日补充维生素D3 400IU");
|
|
|
+
|
|
|
+ Map<String, Object> first = asMap(call(host, METHOD_REGENERATE, inputs, Boolean.TRUE),
|
|
|
+ "首次重生成返回值");
|
|
|
+ Map<String, Object> second = asMap(call(host, METHOD_REGENERATE, inputs, Boolean.TRUE),
|
|
|
+ "再次重生成返回值");
|
|
|
+
|
|
|
+ assertThreeSections(first, "首次重生成");
|
|
|
+ assertThreeSections(second, "再次重生成");
|
|
|
+ assertEquals(sectionContent(first, "nutrition"), sectionContent(second, "nutrition"),
|
|
|
+ "RED[plan.ai.generate] 连续点同一板块重生成应幂等,内容不应二次漂移(TC-P-41-E)");
|
|
|
+ for (String key : Arrays.asList("diet", "exercise")) {
|
|
|
+ assertEquals(sectionContent(baseline, key), sectionContent(second, key),
|
|
|
+ "RED[plan.ai.generate] 连续重生成不得改写未点击的 " + key + " 板块");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ================================================================
|
|
|
+ // 反射与断言辅助(与 PlanAiGenerateContractTest 同风格)
|
|
|
+ // ================================================================
|
|
|
+
|
|
|
+ private Map<String, Object> baseInputs() {
|
|
|
+ Map<String, Object> inputs = new LinkedHashMap<String, Object>();
|
|
|
+ inputs.put("memberId", 501L); // 选孩子(不能登录用户/家庭成员)
|
|
|
+ inputs.put("goal", "提升学习自驱力"); // 选目标
|
|
|
+ inputs.put("reportId", 9001L);
|
|
|
+ return inputs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 断言结果含 overview + 恰好 nutrition/diet/exercise 三个非空板块。 */
|
|
|
+ private void assertThreeSections(Map<String, Object> plan, String scenario) {
|
|
|
+ Object sectionsObj = plan.get("sections");
|
|
|
+ assertNotNull(sectionsObj,
|
|
|
+ "RED[plan.ai.generate] " + scenario + "结果应含 sections 集合(overview + 三板块)");
|
|
|
+ org.junit.jupiter.api.Assertions.assertTrue(sectionsObj instanceof Collection,
|
|
|
+ "RED[plan.ai.generate] " + scenario + "的 sections 应为集合");
|
|
|
+ Collection<?> sections = (Collection<?>) sectionsObj;
|
|
|
+ assertEquals(3, sections.size(), "RED[plan.ai.generate] " + scenario + "应恰好生成三个板块");
|
|
|
+ for (String key : SECTION_KEYS) {
|
|
|
+ assertFalse(sectionContent(plan, key).trim().isEmpty(), "板块 " + key + " 内容不应为空");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 从 sections 集合中按 key 取板块 content(兼容 Map 结构)。 */
|
|
|
+ private String sectionContent(Map<String, Object> plan, String key) {
|
|
|
+ Collection<?> sections = (Collection<?>) plan.get("sections");
|
|
|
+ for (Object item : sections) {
|
|
|
+ Map<String, Object> section = asMap(item, "section");
|
|
|
+ if (key.equals(String.valueOf(section.get("key")))) {
|
|
|
+ Object content = section.get("content");
|
|
|
+ assertNotNull(content, "板块 " + key + " 应含 content");
|
|
|
+ return String.valueOf(content);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fail("RED[plan.ai.generate] 缺少板块 " + key + "(期望 " + SECTION_KEYS + ")");
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object gatewayHost() {
|
|
|
+ Class<?> clazz = loadClass(GATEWAY_CLASS);
|
|
|
+ assertNotNull(clazz, "RED[plan.ai.generate] 契约宿主 AiGateway 不存在(实现未落地)");
|
|
|
+ try {
|
|
|
+ Object host = clazz.getDeclaredConstructor().newInstance();
|
|
|
+ invokeNoArg(host, "initFastTemplate");
|
|
|
+ invokeNoArg(host, "init");
|
|
|
+ return host;
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("RED[plan.ai.generate] AiGateway 实例化失败:" + e.getMessage());
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Class<?> loadClass(String name) {
|
|
|
+ try {
|
|
|
+ return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private Method findMethod(String name, int paramCount) {
|
|
|
+ Class<?> clazz = loadClass(GATEWAY_CLASS);
|
|
|
+ if (clazz == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (Method m : clazz.getMethods()) {
|
|
|
+ if (m.getName().equals(name) && m.getParameterCount() == paramCount) {
|
|
|
+ return m;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Object call(Object host, String method, Object... args) {
|
|
|
+ Method target = findMethod(method, args.length);
|
|
|
+ assertNotNull(target,
|
|
|
+ "RED[plan.ai.generate] 契约方法 AiGateway#" + method + "(" + args.length
|
|
|
+ + " 参数) 未实现(实现未落地)");
|
|
|
+ try {
|
|
|
+ return target.invoke(host, args);
|
|
|
+ } catch (Exception e) {
|
|
|
+ Throwable cause = e.getCause() != null ? e.getCause() : e;
|
|
|
+ fail("RED[plan.ai.generate] 契约方法 " + method + " 调用失败:" + cause);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 调用时期望不抛异常(Fallback 契约:上游不可用不得 500)。 */
|
|
|
+ private Object callExpectNoThrow(Object host, String method, Object... args) {
|
|
|
+ Method target = findMethod(method, args.length);
|
|
|
+ assertNotNull(target,
|
|
|
+ "RED[plan.ai.generate] 契约方法 AiGateway#" + method + "(" + args.length
|
|
|
+ + " 参数) 未实现(实现未落地)");
|
|
|
+ try {
|
|
|
+ return target.invoke(host, args);
|
|
|
+ } catch (InvocationTargetException e) {
|
|
|
+ fail("RED[plan.ai.generate] LangGraph 超时/熔断时 " + method
|
|
|
+ + " 必须返回 Fallback,不得抛异常(禁止 500):" + e.getCause());
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ } catch (Exception e) {
|
|
|
+ fail("RED[plan.ai.generate] 契约方法 " + method + " 调用失败:" + e.getMessage());
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressWarnings("unchecked")
|
|
|
+ private Map<String, Object> asMap(Object value, String what) {
|
|
|
+ if (value instanceof Map) {
|
|
|
+ return (Map<String, Object>) value;
|
|
|
+ }
|
|
|
+ fail("RED[plan.ai.generate] " + what + " 应为 Map,实际 " + value);
|
|
|
+ throw new IllegalStateException("unreachable");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void invokeNoArg(Object target, String method) {
|
|
|
+ try {
|
|
|
+ Method m = target.getClass().getMethod(method);
|
|
|
+ m.invoke(target);
|
|
|
+ } catch (Exception ignore) {
|
|
|
+ // 初始化方法为可选:契约探测不依赖其存在
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void setField(Object target, String name, Object value) {
|
|
|
+ Class<?> clazz = target.getClass();
|
|
|
+ while (clazz != null) {
|
|
|
+ try {
|
|
|
+ Field f = clazz.getDeclaredField(name);
|
|
|
+ f.setAccessible(true);
|
|
|
+ // final AtomicLong/AtomicInteger 不可整体替换,改其内部值(熔断状态字段)
|
|
|
+ Object current = f.get(target);
|
|
|
+ if (current instanceof java.util.concurrent.atomic.AtomicLong && value instanceof Number) {
|
|
|
+ ((java.util.concurrent.atomic.AtomicLong) current).set(((Number) value).longValue());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (current instanceof java.util.concurrent.atomic.AtomicInteger && value instanceof Number) {
|
|
|
+ ((java.util.concurrent.atomic.AtomicInteger) current).set(((Number) value).intValue());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ f.set(target, value);
|
|
|
+ return;
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ clazz = clazz.getSuperclass();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|