背景:v1.2.0 加了 object_types.classification_property/_levels/_default 三列,
但只暴露了 GET /classification/config,没有配对的 PUT/POST。
后果:
- 前端编辑对象类型分级保存按钮看似无反应(POST 到不存在的路由)
- 587 个打了 data_level 标签的实例是「裸属性」,分级引擎不接管
- 23 个类型 enabled 全是 false
改动:
- 新增 REST PUT /api/v1/ontology/object-types/:name/classification
(handler: setObjectTypeClassification)
- 校验:levels 非空有序、default ∈ levels、property 必须是已存在属性
- 写入后调 invalidateClassCache() 让下一次 GET config 立刻反映
- 「关闭分级」入口:property=空 允许 levels=[] / default=''
- 新增 MCP 工具 set_object_type_classification(manager 必填)
- 24 个测试全绿(11 纯函数 + 13 集成)
203 lines
6.7 KiB
Go
203 lines
6.7 KiB
Go
//go:build integration
|
||
|
||
package main
|
||
|
||
import (
|
||
"strconv"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// TestIntegration_SetObjectTypeClassification 验证「设置对象类型分级配置」端点
|
||
// 端到端能力:写入 → invalidateClassCache → GET config 立刻反映 → 不存在的 property 校验。
|
||
//
|
||
// 真实场景背景(v1.3.0 之前):
|
||
// - GET /classification/config 已存在很久(v1.2.0),但 PUT 端点缺失
|
||
// - 前端编辑「对象类型分级」保存按钮看似无反应(POST 到不存在的路由 → 404)
|
||
// - 后果:587 个打了 data_level 标签的实例是「裸属性」无法被分级引擎接管
|
||
func TestIntegration_SetObjectTypeClassification(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
// 用一个已有「真属性行」的类型,避免 setupTestEnv 新建的测试类型 properties 行为不同
|
||
// DataDemand 在本工作区有 64 个属性行(详见 docs/db/0910 schema)。
|
||
const fixedType = "DataDemand"
|
||
t.Cleanup(func() {
|
||
// 测试结束清理 DataDemand 的分级配置(避免污染其他测试)
|
||
env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "",
|
||
"classification_levels": []string{},
|
||
"classification_default": "",
|
||
}, h)
|
||
})
|
||
|
||
// 1) PUT 设置分级属性(DataDemand 有 approval_status 字段)
|
||
code, body := env.httpDo(t, "PUT",
|
||
"/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "approval_status",
|
||
"classification_levels": []string{"1", "3-replaced"},
|
||
"classification_default": "1",
|
||
}, h)
|
||
if code != 200 {
|
||
t.Fatalf("PUT 应 200, got %d body=%v", code, body)
|
||
}
|
||
if body["enabled"] != true {
|
||
t.Errorf("enabled 应 true, got %v", body["enabled"])
|
||
}
|
||
|
||
// 2) GET config 立刻反映
|
||
_, getConfig := env.httpDo(t, "GET", "/api/v1/ontology/classification/config", nil, h)
|
||
var found map[string]any
|
||
for _, it := range getConfig["items"].([]any) {
|
||
m := it.(map[string]any)
|
||
if m["api_name"] == fixedType {
|
||
found = m
|
||
}
|
||
}
|
||
if found == nil {
|
||
t.Fatalf("GET config 应包含 %s, got %v", fixedType, getConfig)
|
||
}
|
||
if found["enabled"] != true || found["classification_property"] != "approval_status" {
|
||
t.Errorf("config 不匹配 PUT, got %+v", found)
|
||
}
|
||
|
||
// 3) 校验:levels 空但 property 已设 → 400(property=""+levels=[] 是关闭分级入口,合法)
|
||
code, _ = env.httpDo(t, "PUT",
|
||
"/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "approval_status",
|
||
"classification_levels": []string{},
|
||
}, h)
|
||
if code != 400 {
|
||
t.Errorf("property 已设但 levels=空 应 400, got %d", code)
|
||
}
|
||
|
||
// 4) 校验:default 不在 levels → 400
|
||
code, _ = env.httpDo(t, "PUT",
|
||
"/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "approval_status",
|
||
"classification_levels": []string{"1", "3-replaced"},
|
||
"classification_default": "GAMMA-OUT-OF-RANGE",
|
||
}, h)
|
||
if code != 400 {
|
||
t.Errorf("default 不在 levels 应 400, got %d", code)
|
||
}
|
||
|
||
// 5) 校验:property 指向不存在的属性 → 400
|
||
code, _ = env.httpDo(t, "PUT",
|
||
"/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "this_property_does_not_exist",
|
||
"classification_levels": []string{"x"},
|
||
}, h)
|
||
if code != 400 {
|
||
t.Errorf("property 不存在应 400, got %d", code)
|
||
}
|
||
|
||
// 6) PUT 关闭该类型分级(property=空)
|
||
code, body = env.httpDo(t, "PUT",
|
||
"/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "",
|
||
"classification_levels": []string{},
|
||
"classification_default": "",
|
||
}, h)
|
||
if code != 200 {
|
||
t.Fatalf("关闭分级应 200, got %d", code)
|
||
}
|
||
if body["enabled"] != false {
|
||
t.Errorf("property=空 时 enabled 应 false, got %v", body["enabled"])
|
||
}
|
||
|
||
// 7) GET config 立刻反映关闭
|
||
_, getConfig2 := env.httpDo(t, "GET", "/api/v1/ontology/classification/config", nil, h)
|
||
var found2 map[string]any
|
||
for _, it := range getConfig2["items"].([]any) {
|
||
m := it.(map[string]any)
|
||
if m["api_name"] == fixedType {
|
||
found2 = m
|
||
}
|
||
}
|
||
if found2["enabled"] != false {
|
||
t.Errorf("关闭后 GET 应 enabled=false, got %+v", found2)
|
||
}
|
||
}
|
||
|
||
// TestIntegration_SetClassificationThroughMCP 验证 MCP 工具 set_object_type_classification
|
||
// 是否正确代理到刚补的 REST PUT。需要 manager 主体。
|
||
func TestIntegration_SetClassificationThroughMCP(t *testing.T) {
|
||
env, _ := setupTestEnv(t)
|
||
h := map[string]string{"X-Principal": "manager"}
|
||
|
||
const fixedType = "DataDemand"
|
||
t.Cleanup(func() {
|
||
env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+fixedType+"/classification",
|
||
map[string]any{
|
||
"classification_property": "",
|
||
"classification_levels": []string{},
|
||
"classification_default": "",
|
||
}, h)
|
||
})
|
||
|
||
// 用 MCP 调用 set_object_type_classification
|
||
code, body := env.httpDo(t, "POST", "/api/v1/mcp",
|
||
map[string]any{
|
||
"jsonrpc": "2.0", "id": "1", "method": "tools/call",
|
||
"params": map[string]any{
|
||
"name": "set_object_type_classification",
|
||
"arguments": map[string]any{
|
||
"api_name": fixedType,
|
||
"classification_property": "approval_status",
|
||
"classification_levels": []string{"1", "3-replaced"},
|
||
"classification_default": "1",
|
||
},
|
||
},
|
||
}, h)
|
||
if code != 200 {
|
||
t.Fatalf("MCP 调用应 200, got %d body=%v", code, body)
|
||
}
|
||
|
||
// 响应 result.content[0].text 是 JSON 字符串
|
||
result, _ := body["result"].(map[string]any)
|
||
if result == nil {
|
||
t.Fatalf("MCP 响应缺 result, got %v", body)
|
||
}
|
||
content, _ := result["content"].([]any)
|
||
if len(content) == 0 {
|
||
t.Fatalf("MCP 响应缺 content, got %v", result)
|
||
}
|
||
first, _ := content[0].(map[string]any)
|
||
text, _ := first["text"].(string)
|
||
if !strings.Contains(text, `"enabled":true`) {
|
||
t.Errorf("MCP 响应应 enabled:true, got %s", text)
|
||
}
|
||
|
||
// MCP 缺 levels 应报错(参数错误,levels 不能为空)
|
||
code, body = env.httpDo(t, "POST", "/api/v1/mcp",
|
||
map[string]any{
|
||
"jsonrpc": "2.0", "id": "2", "method": "tools/call",
|
||
"params": map[string]any{
|
||
"name": "set_object_type_classification",
|
||
"arguments": map[string]any{
|
||
"api_name": fixedType,
|
||
},
|
||
},
|
||
}, h)
|
||
if code != 200 {
|
||
t.Fatalf("MCP 缺 levels 应 200 含错误信息, got %d", code)
|
||
}
|
||
r2, _ := body["result"].(map[string]any)
|
||
c2, _ := r2["content"].([]any)
|
||
if len(c2) == 0 {
|
||
t.Fatalf("MCP 响应缺 content")
|
||
}
|
||
t2, _ := c2[0].(map[string]any)["text"].(string)
|
||
if !strings.Contains(t2, "classification_levels") {
|
||
t.Errorf("MCP 缺 levels 应报错, got %s", t2)
|
||
}
|
||
|
||
_ = strconv.Itoa // 占位避免 unused
|
||
} |