_SEARCH,列= listVisible 业务列")
void composesMaintenanceCrud() {
RecipePageComposer.Spec spec = new RecipePageComposer.Spec(
"EIP4AI_CUSTOMER", "客户", PageBlueprint.RECIPE_MAINTENANCE_CRUD,
"客户维护", "NAME", 20,
List.of(
new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null),
new RecipePageComposer.AttrSpec("LEVEL", "等级", "integer", false, null, null)));
ObjectNode page = composer.compose(spec);
assertEquals("page", page.path("type").asText());
assertEquals("客户维护", page.path("title").asText());
ObjectNode crud = (ObjectNode) page.path("body");
assertEquals("/api/flow/EIP4AI_CUSTOMER_SEARCH", crud.path("api").asText());
ArrayNode columns = (ArrayNode) crud.path("columns");
assertEquals(2, columns.size());
assertEquals("NAME", columns.get(0).path("name").asText());
assertEquals("LEVEL", columns.get(1).path("name").asText());
}
@Test
@DisplayName("2. compose tree-crud → 注入 PARENTID 字段且列表不显示该列")
void composesTreeCrudWithParent() {
RecipePageComposer.Spec spec = new RecipePageComposer.Spec(
"EIP4AI_CATEGORY", "分类", PageBlueprint.RECIPE_TREE_CRUD,
"分类维护", "NAME", 20,
List.of(
new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null),
new RecipePageComposer.AttrSpec("PARENTID", "上级", "string", false, null, null)));
ObjectNode page = composer.compose(spec);
ObjectNode crud = (ObjectNode) page.path("body");
assertEquals("eip4aiCategoryTreeCrud", crud.path("name").asText());
// 业务列里不含 PARENTID(compiler.treeColumns 跳过 PARENTID),但保留「操作」列。
ArrayNode columns = (ArrayNode) crud.path("columns");
boolean hasParentColumn = false;
for (int i = 0; i < columns.size(); i++) {
if ("PARENTID".equals(columns.get(i).path("name").asText())) {
hasParentColumn = true;
}
}
assertTrue(!hasParentColumn);
}
@Test
@DisplayName("3. displayField 省略时回退首个 NAME/required 列")
void derivesDisplayFieldWhenBlank() {
RecipePageComposer.Spec spec = new RecipePageComposer.Spec(
"EIP4AI_ITEM", "条目", PageBlueprint.RECIPE_MAINTENANCE_CRUD,
"条目维护", null, 20,
List.of(
new RecipePageComposer.AttrSpec("CODE", "编码", "string", false, null, null),
new RecipePageComposer.AttrSpec("NAME", "名称", "string", true, null, null)));
ObjectNode page = composer.compose(spec);
// displayField 回退到 NAME,filter 兜底用它(此处只需 compose 成功不抛)。
assertEquals("page", page.path("type").asText());
}
}
```
**Step 6.2 — 运行测试,预期 FAIL(编译失败)**
```
JAVA_HOME=/opt/homebrew/opt/openjdk@21 mvn -pl platform-metadata -am test -Dtest=RecipePageComposerTest
```
预期 **FAIL**,输出含编译错误,例如:
`cannot find symbol: class RecipePageComposer` / `package ... does not exist`。
**Step 6.3 — 写最小实现 `RecipePageComposer`**
写入 `RecipePageComposer.java`(完整文件):
```java
package com.eip4.platform.metadata.schema.blueprint;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
/**
* 共享蓝图装配器:实体属性 → 受控 {@link PageBlueprint} → {@link PageBlueprintCompiler#compile}。
*
* 人工单步端点(DesignFeatureController)与未来 AI 路径共用同一装配口径(DRY),UI/UE 规则集中在
* recipe/compiler 而非散落在调用方。本类不写库、不校验白名单(严格 typed 闸在端点层),只负责装配+编译。
*
*
装配口径镜像 {@code AiFeatureDraftGenerator.pageBlueprint}:tree-crud 的 PARENTID 列 listVisible=false;
* displayField 缺省回退首个 NAME/required 列。引用字段(type=reference + referenceTable/displayColumn)
* 透传到 {@link PageBlueprintField},由 compiler 渲染显名列(见 applyColumnRender)。
*/
public final class RecipePageComposer {
private final PageBlueprintCompiler compiler;
public RecipePageComposer(ObjectMapper objectMapper) {
this.compiler = new PageBlueprintCompiler(objectMapper);
}
public ObjectNode compose(Spec spec) {
if (spec == null) {
throw new IllegalArgumentException("compose spec 不能为空");
}
PageBlueprint blueprint = toBlueprint(spec);
return compiler.compile(blueprint);
}
PageBlueprint toBlueprint(Spec spec) {
List fields = new ArrayList<>();
for (AttrSpec attr : spec.attributes()) {
boolean treeParentField = PageBlueprint.RECIPE_TREE_CRUD.equals(spec.recipe())
&& "PARENTID".equals(attr.column());
fields.add(new PageBlueprintField(
attr.column(), attr.display(), attr.type(),
attr.required(), !treeParentField, true,
attr.referenceTable(), attr.displayColumn()));
}
String displayField = firstNonBlank(spec.displayField(), deriveDisplayField(spec.attributes()));
int pageSize = spec.pageSize() < 1 ? 20 : spec.pageSize();
// templateId 由端点层服务端生成;compose 阶段用占位(compiler.validate 只要求非空)。
return new PageBlueprint(
PageBlueprint.VERSION, spec.recipe(),
"compose-placeholder", spec.title(), spec.tableName(), spec.entityName(),
spec.tableName() + "_ID", displayField, fields,
true, true, true, pageSize);
}
private static String deriveDisplayField(List attrs) {
for (AttrSpec a : attrs) {
if ("NAME".equals(a.column()) || a.required()) {
return a.column();
}
}
return attrs.isEmpty() ? "NAME" : attrs.get(0).column();
}
private static String firstNonBlank(String preferred, String fallback) {
if (preferred != null && !preferred.isBlank()) {
return preferred.trim();
}
return fallback;
}
/** 装配输入:等价于 GET /api/meta/entity/{table}/attributes 的读取结果 + recipe/title/displayField/pageSize。 */
public record Spec(
String tableName,
String entityName,
String recipe,
String title,
String displayField,
int pageSize,
List attributes) {
public Spec {
attributes = attributes == null ? List.of() : List.copyOf(attributes);
}
}
/** 单个实体属性。referenceTable/displayColumn 仅引用列非空(type=reference)。 */
public record AttrSpec(
String column,
String display,
String type,
boolean required,
String referenceTable,
String displayColumn) {
}
}
```
> 注意:`PageBlueprintField` 当前是 6-arg;上面用了 8-arg。这会在 Task 6 阶段编译失败——故 Task 6 与 Task 8 的字段扩展互相依赖。**为让本任务独立绿,先在本步顺手加 `PageBlueprintField` 的 8-arg + 6-arg 兼容构造**(Task 8 复用同一记录、不再改签名,只扩 compiler 渲染)。把 `PageBlueprintField.java` 替换为:
```java
package com.eip4.platform.metadata.schema.blueprint;
public record PageBlueprintField(
String name,
String label,
String type,
boolean required,
boolean listVisible,
boolean formVisible,
String referenceTable,
String displayColumn) {
/** 6-arg 兼容构造:非引用字段(referenceTable/displayColumn 置空)。保持既有调用方/测试零改。 */
public PageBlueprintField(String name, String label, String type,
boolean required, boolean listVisible, boolean formVisible) {
this(name, label, type, required, listVisible, formVisible, null, null);
}
}
```
**Step 6.3b — 运行既有蓝图测试,确认 6-arg 兼容未破坏存量**
```
JAVA_HOME=/opt/homebrew/opt/openjdk@21 mvn -pl platform-metadata -am test -Dtest=PageBlueprintCompilerTest,PageSchemaWriterTest
```
预期 **PASS**(既有 `PageBlueprintCompilerTest` 用 6-arg 构造,应仍编译通过、全绿)。
**Step 6.4 — 运行 Task 6 测试,预期 PASS**
```
JAVA_HOME=/opt/homebrew/opt/openjdk@21 mvn -pl platform-metadata -am test -Dtest=RecipePageComposerTest
```
预期 **PASS**(3 个用例全绿)。
**Step 6.5 — 提交**
```
git add eip4-platform/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposer.java \
eip4-platform/platform-metadata/src/main/java/com/eip4/platform/metadata/schema/blueprint/PageBlueprintField.java \
eip4-platform/platform-metadata/src/test/java/com/eip4/platform/metadata/schema/blueprint/RecipePageComposerTest.java
git commit -m "feat(design): RecipePageComposer 共享蓝图装配器 + PageBlueprintField 引用字段扩展(向后兼容)
Co-Authored-By: Claude Opus 4.8 (1M context) "
```
---
### Task 7 · 新建 `DesignFeatureController`:`POST /api/design/recipe/compile`(建物 2 主端点)
管理员单步端点:读实体属性 → `RecipePageComposer.compose` → 服务端生成 `templateId` → 严格 typed 校验(不通过 400)→ `PageSchemaWriter.upsertStandard`。
**Files:**
- Create `/Users/zhengwr/workspace/eip3/eip4-platform/platform-metadata/src/main/java/com/eip4/platform/metadata/web/DesignFeatureController.java`
- Create `/Users/zhengwr/workspace/eip3/eip4-platform/platform-metadata/src/main/java/com/eip4/platform/metadata/web/EntityAttributeReader.java`(薄接口,Section 1 的属性读取逻辑可实现它)
- Test (create) `/Users/zhengwr/workspace/eip3/eip4-platform/platform-metadata/src/test/java/com/eip4/platform/metadata/web/DesignFeatureControllerTest.java`
---
**Step 7.1 — 写失败测试(compile 端点返回新 templateId 并经 writer 落标准层一次)**
> 用「可注入协作者」的包级构造让单测脱库:注入一个 stub `EntityAttributeReader`、真 `RecipePageComposer`、mock `PageSchemaWriter`。admin 门禁在单测层旁路(用包级 `boolean skipAdminCheck` 测试构造,生产构造恒开门禁)。
写入 `DesignFeatureControllerTest.java`:
```java
package com.eip4.platform.metadata.web;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.ResponseEntity;
import com.eip4.platform.metadata.schema.PageSchemaWriter;
import com.eip4.platform.metadata.web.EntityAttributeReader.EntityAttributes;
import com.eip4.platform.metadata.web.EntityAttributeReader.EntityAttribute;
class DesignFeatureControllerTest {
private DesignFeatureController controller(PageSchemaWriter writer, EntityAttributeReader reader) {
// 包级测试构造:跳过 admin 门禁(生产构造恒走 requireAdmin)。
return new DesignFeatureController(writer, reader, true);
}
@Test
@DisplayName("1. compile 返回服务端生成的 design- 前缀 templateId,并经 writer 落标准层一次")
void compileGeneratesTemplateIdAndWritesOnce() {
PageSchemaWriter writer = mock(PageSchemaWriter.class);
when(writer.upsertStandard(anyString(), anyString(), anyString(), anyString()))
.thenReturn("etag-deadbeef");
EntityAttributeReader reader = mock(EntityAttributeReader.class);
when(reader.read("EIP4AI_CUSTOMER")).thenReturn(new EntityAttributes(
"EIP4AI_CUSTOMER", "NAME", List.of(
new EntityAttribute("NAME", "名称", "string", true, null),
new EntityAttribute("LEVEL", "等级", "integer", false, null))));
var body = new DesignFeatureController.CompileRequest();
body.tableName = "EIP4AI_CUSTOMER";
body.recipe = "maintenance-crud-v1";
body.title = "客户维护";
body.pageSize = 20;
ResponseEntity