|
@@ -0,0 +1,391 @@
|
|
|
|
|
+package com.etotem.cfc.unit;
|
|
|
|
|
+
|
|
|
|
|
+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.InvocationTargetException;
|
|
|
|
|
+import java.lang.reflect.Method;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Collection;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
|
+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.junit.jupiter.api.Assertions.fail;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 契约级单测:能力键 {@code plan.ai.generate}(任务 S-cfc-279,TDD-RED 阶段,预期失败/红)。
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>契约溯源</b></p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>需求:{@code tests/user-scenarios/应用场景总览.md} SC-P-40(生成 AI 方案:选孩子+目标 →
|
|
|
|
|
+ * LangGraph({@code AiGateway} 转发) → 生成 overview + 三板块;未购会员提示购买;AI 超时
|
|
|
|
|
+ * {@code AiGateway} Fallback 不 500)与 SC-P-41(方案分板块重生成:逐板块重新生成/确认,
|
|
|
|
|
+ * 重生成不丢其他板块);业务域 D。</li>
|
|
|
|
|
+ * <li>需求明细:{@code tests/test-cases/parent.md} TC-P-13 / TC-P-40 / TC-P-41 / TC-P-40-E /
|
|
|
|
|
+ * TC-P-41-E:三板块为 nutrition(营养补充建议) / diet(饮食建议) / exercise(运动计划),
|
|
|
|
|
+ * overview + 三板块;未购会员返回『请先开通会员』拦截;LangGraph 超时 Fallback 兜底而非 500;
|
|
|
|
|
+ * 重生成某板块时断网保留其他板块,连续点击幂等。</li>
|
|
|
|
|
+ * <li>设计:{@code docs/superpowers/api/API_REFERENCE.md} §4.9 AI 对话
|
|
|
|
|
+ * {@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>边界声明</b>:本键为「儿童成长规划」AI 方案生成({@code docs/tdd/orchestration/sc-key-map.md}
|
|
|
|
|
+ * SC-P-40/41 → {@code plan.ai.generate}),与 {@code plan.parent.generate}( HealthPlanController
|
|
|
|
|
+ * {@code /api/health/plan/generate} + {@code /regenerate-section},P1-1 健康计划红线)是不同能力键。
|
|
|
|
|
+ * 因此本测试只探测 {@code AIChatController} / {@code GrowthPlanController} / {@code AiGateway},
|
|
|
|
|
+ * <b>不</b>把 HealthPlanController 的既有实现当作本键的落地。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>实现现状(RED 依据)</b>:{@code src/main} 中
|
|
|
|
|
+ * {@link #GATEWAY_CLASS} 无 AI 成长方案生成/重生成入口,{@code AIChatController} 与
|
|
|
|
|
+ * {@code GrowthPlanController} 无对应 {@code @PostMapping}。本测试通过反射锚定契约宿主,
|
|
|
|
|
+ * 以「类/方法/端点缺失 → 断言失败(红)」暴露真实缺口,且能编译、能运行、失败原文清晰。</p>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>GREEN 升级指引(实现方按此落地)</b>:</p>
|
|
|
|
|
+ * <ul>
|
|
|
|
|
+ * <li>{@code AiGateway} 新增公开方法
|
|
|
|
|
+ * {@code Map<String,Object> generateGrowthPlan(Map<String,Object> inputs, boolean memberPaid)}:
|
|
|
|
|
+ * inputs 含 memberId(Long)/goal(String)(可选 reportId);
|
|
|
|
|
+ * {@code memberPaid=false} → 返回 {@code {needPurchase=true, message:"请先开通会员…"}} 且不调 LangGraph;
|
|
|
|
|
+ * 正常 → 返回 {@code {overview:String, sections:[{key,title,content} ×3], fallback_used:false}};
|
|
|
|
|
+ * LangGraph 超时/熔断 → 返回非空 Fallback({@code fallback_used=true} + overview + 3 板块),
|
|
|
|
|
+ * 禁止抛异常/500。</li>
|
|
|
|
|
+ * <li>{@code AiGateway} 新增公开方法
|
|
|
|
|
+ * {@code Map<String,Object> regenerateGrowthPlanSection(Map<String,Object> inputs, boolean memberPaid)}:
|
|
|
|
|
+ * inputs 含 sectionKey(nutrition|diet|exercise)/content/原方案板块;
|
|
|
|
|
+ * 仅重生成 sectionKey 对应板块,其余两板块内容原样保留;超时同样走 Fallback。</li>
|
|
|
|
|
+ * <li>{@code AIChatController} 或 {@code GrowthPlanController} 新增 {@code @PostMapping}
|
|
|
|
|
+ * 提供 AI 方案生成(路径含 generate)与分板块重生成(路径含 regenerate)端点。</li>
|
|
|
|
|
+ * </ul>
|
|
|
|
|
+ *
|
|
|
|
|
+ * <p><b>测试范围</b>:纯反射 + JUnit 5,无 Spring 上下文 / 无 DB / 无真实外部服务调用,
|
|
|
|
|
+ * 与已合入主链的同批 RED 契约测试(TeamProfitHookContractTest / TeamStarMultiplierContractTest /
|
|
|
|
|
+ * TeamProfitPipelineContractTest / FamilyJoinValidationRulesContractTest)风格一致。</p>
|
|
|
|
|
+ */
|
|
|
|
|
+class PlanAiGenerateContractTest {
|
|
|
|
|
+
|
|
|
|
|
+ /** 契约宿主: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";
|
|
|
|
|
+
|
|
|
|
|
+ /** 设计文档指定模块:AI 对话入口 + 成长方案入口(不含 HealthPlanController,见边界声明)。 */
|
|
|
|
|
+ private static final String[] PLAN_MODULES = {
|
|
|
|
|
+ "com.etotem.cfc.controller.ai.AIChatController",
|
|
|
|
|
+ "com.etotem.cfc.controller.growth.GrowthPlanController"
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ /** 三板块(TC-P-13:营养补充建议 / 饮食建议 / 运动计划)。 */
|
|
|
|
|
+ private static final List<String> SECTION_KEYS = Arrays.asList("nutrition", "diet", "exercise");
|
|
|
|
|
+
|
|
|
|
|
+ // ================================================================
|
|
|
|
|
+ // SC-P-40:一键生成 AI 方案
|
|
|
|
|
+ // ================================================================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-40-结构:AiGateway 应提供 AI 成长方案生成入口 generateGrowthPlan")
|
|
|
|
|
+ void generationEntry_onAiGateway_exists() {
|
|
|
|
|
+ Object host = gatewayHost();
|
|
|
|
|
+ assertNotNull(findMethod(METHOD_GENERATE, 2),
|
|
|
|
|
+ "RED[plan.ai.generate] AiGateway#" + METHOD_GENERATE
|
|
|
|
|
+ + "(Map inputs, boolean memberPaid) 未实现:SC-P-40 要求选孩子+目标后经 AiGateway 调 LangGraph 生成方案。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-40-正常流:已购会员生成 overview + nutrition/diet/exercise 三板块")
|
|
|
|
|
+ void normalMember_generatesOverviewAndThreeSections() {
|
|
|
|
|
+ Object host = gatewayHost();
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = asMap(call(host, METHOD_GENERATE, baseInputs(), Boolean.TRUE),
|
|
|
|
|
+ METHOD_GENERATE + " 返回值");
|
|
|
|
|
+
|
|
|
|
|
+ Object overview = result.get("overview");
|
|
|
|
|
+ assertNotNull(overview, "RED[plan.ai.generate] 生成结果应含 overview(方案总览)");
|
|
|
|
|
+ assertFalse(String.valueOf(overview).trim().isEmpty(), "overview 不应为空");
|
|
|
|
|
+
|
|
|
|
|
+ assertThreeSections(result, "生成");
|
|
|
|
|
+ assertEquals(Boolean.FALSE, result.get("fallback_used"),
|
|
|
|
|
+ "LangGraph 正常返回时 fallback_used 应为 false");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-40-会员门禁:未购会员提示开通会员且不生成方案")
|
|
|
|
|
+ void nonMember_promptPurchase_noGeneration() {
|
|
|
|
|
+ Object host = gatewayHost();
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> result = asMap(call(host, METHOD_GENERATE, baseInputs(), Boolean.FALSE),
|
|
|
|
|
+ METHOD_GENERATE + " 返回值");
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(Boolean.TRUE, result.get("needPurchase"),
|
|
|
|
|
+ "RED[plan.ai.generate] 未购会员必须返回 needPurchase=true,提示购买而非白生成(TC-P-13/TC-P-40-E)");
|
|
|
|
|
+ String message = String.valueOf(result.get("message"));
|
|
|
|
|
+ assertTrue(message.contains("会员"), "未购会员提示语应包含『会员』,实际:" + message);
|
|
|
|
|
+ Object sections = result.get("sections");
|
|
|
|
|
+ assertTrue(sections == null || (sections instanceof Collection && ((Collection<?>) sections).isEmpty()),
|
|
|
|
|
+ "未购会员不应返回已生成的三板块内容");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-40-异常流:LangGraph 超时/熔断返回 Fallback,不抛异常(不 500)")
|
|
|
|
|
+ void langGraphUnavailable_returnsFallback_not500() {
|
|
|
|
|
+ Object host = gatewayHost();
|
|
|
|
|
+ // 模拟上游不可用:指向不可达地址 + 打开熔断器,任何真实调用都会快速失败
|
|
|
|
|
+ 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> result = asMap(callExpectNoThrow(host, METHOD_GENERATE, baseInputs(), Boolean.TRUE),
|
|
|
|
|
+ METHOD_GENERATE + " 返回值");
|
|
|
|
|
+
|
|
|
|
|
+ assertNotNull(result, "RED[plan.ai.generate] LangGraph 不可用时必须返回 Fallback 结果,不得返回 null 导致 500");
|
|
|
|
|
+ assertEquals(Boolean.TRUE, result.get("fallback_used"),
|
|
|
|
|
+ "RED[plan.ai.generate] Fallback 结果应标记 fallback_used=true");
|
|
|
|
|
+ assertFalse(String.valueOf(result.get("overview")).trim().isEmpty(),
|
|
|
|
|
+ "Fallback 也应给出非空 overview 兜底文案");
|
|
|
|
|
+ assertThreeSections(result, "Fallback");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-40-结构:AI/成长方案模块应提供生成端点(@PostMapping 含 generate)")
|
|
|
|
|
+ void generationEndpoint_onPlanModules_exists() {
|
|
|
|
|
+ assertControllerPostMapping("generate", "regenerate", "SC-P-40 AI 方案生成");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ================================================================
|
|
|
|
|
+ // SC-P-41:分板块重生成(不丢其他板块)
|
|
|
|
|
+ // ================================================================
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-41-结构:AiGateway 应提供分板块重生成入口 regenerateGrowthPlanSection")
|
|
|
|
|
+ void regenerationEntry_onAiGateway_exists() {
|
|
|
|
|
+ Object host = gatewayHost();
|
|
|
|
|
+ assertNotNull(findMethod(METHOD_REGENERATE, 2),
|
|
|
|
|
+ "RED[plan.ai.generate] AiGateway#" + METHOD_REGENERATE
|
|
|
|
|
+ + "(Map inputs, boolean memberPaid) 未实现:SC-P-41 要求逐板块重新生成。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-41-正常流:只重生成 nutrition 板块,diet/exercise 原样保留")
|
|
|
|
|
+ void regenerateOneSection_preservesOtherTwo() {
|
|
|
|
|
+ Object host = gatewayHost();
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> baseline = asMap(call(host, METHOD_GENERATE, baseInputs(), Boolean.TRUE),
|
|
|
|
|
+ METHOD_GENERATE + " 返回值");
|
|
|
|
|
+ assertThreeSections(baseline, "基线生成");
|
|
|
|
|
+
|
|
|
|
|
+ Map<String, Object> inputs = new HashMap<String, Object>(baseInputs());
|
|
|
|
|
+ inputs.put("sectionKey", "nutrition");
|
|
|
|
|
+ inputs.put("content", "【新营养建议】每日补充维生素D3 400IU");
|
|
|
|
|
+ Map<String, Object> regenerated = asMap(call(host, METHOD_REGENERATE, inputs, Boolean.TRUE),
|
|
|
|
|
+ METHOD_REGENERATE + " 返回值");
|
|
|
|
|
+
|
|
|
|
|
+ assertThreeSections(regenerated, "重生成结果");
|
|
|
|
|
+ assertEquals("【新营养建议】每日补充维生素D3 400IU", sectionContent(regenerated, "nutrition"),
|
|
|
|
|
+ "RED[plan.ai.generate] 重生成 nutrition 后该板块内容应更新");
|
|
|
|
|
+ for (String key : Arrays.asList("diet", "exercise")) {
|
|
|
|
|
+ assertEquals(sectionContent(baseline, key), sectionContent(regenerated, key),
|
|
|
|
|
+ "RED[plan.ai.generate] 重生成 nutrition 不得丢失/改写 " + key + " 板块(SC-P-41)");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ @DisplayName("SC-P-41-结构:AI/成长方案模块应提供分板块重生成端点(@PostMapping 含 regenerate)")
|
|
|
|
|
+ void regenerationEndpoint_onPlanModules_exists() {
|
|
|
|
|
+ assertControllerPostMapping("regenerate", null, "SC-P-41 分板块重生成");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // ================================================================
|
|
|
|
|
+ // 反射与断言辅助
|
|
|
|
|
+ // ================================================================
|
|
|
|
|
+
|
|
|
|
|
+ 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");
|
|
|
|
|
+ assertTrue(sectionsObj instanceof Collection,
|
|
|
|
|
+ "RED[plan.ai.generate] " + scenario + "结果应含 sections 集合(overview + 三板块)");
|
|
|
|
|
+ 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");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 探测设计文档指定的两个模块中存在含指定路径片段的 @PostMapping。 */
|
|
|
|
|
+ private void assertControllerPostMapping(String fragment, String excludeFragment, String scenario) {
|
|
|
|
|
+ for (String module : PLAN_MODULES) {
|
|
|
|
|
+ Class<?> clazz = loadClass(module);
|
|
|
|
|
+ assertNotNull(clazz, "RED[plan.ai.generate] 模块类不存在:" + module);
|
|
|
|
|
+ for (Method m : clazz.getDeclaredMethods()) {
|
|
|
|
|
+ PostMapping mapping = m.getAnnotation(PostMapping.class);
|
|
|
|
|
+ if (mapping == null) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ for (String rawPath : mapping.value()) {
|
|
|
|
|
+ String path = normalizePath(rawPath);
|
|
|
|
|
+ if (excludeFragment != null && path.contains(excludeFragment)) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (path.contains(fragment)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ fail("RED[plan.ai.generate] " + scenario
|
|
|
|
|
+ + " 缺少 POST 端点:应在 AIChatController/GrowthPlanController 提供路径含 '"
|
|
|
|
|
+ + fragment + "' 的 @PostMapping(实现未落地)。");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /** 归一化路径:/ai-regenerate-section → airegeneratesection,便于包含匹配。 */
|
|
|
|
|
+ private String normalizePath(String rawPath) {
|
|
|
|
|
+ return rawPath.toLowerCase().replace("-", "").replace("_", "").replace("/", "");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|