//go:build integration package main // 跨版本结构 diff 的集成测试(真实 PG)。 // // 守护三件事: // 1. 每次 schema 变更都落一份结构快照——没有快照,「任意两版本」无从谈起; // 2. diff 认得出「机器事实」与「人工策展」的边界(人工改过的描述不能被重摄取冲掉, // diff 要能把它单独标出来); // 3. 认证位(certification 公理)标记的条目在 diff 里带 certified 标记。 import ( "context" "fmt" "strings" "testing" ) // latestSchemaVersion 取当前最新的 schema 版本号(变更流水是全局单调的)。 func latestSchemaVersion(t *testing.T, a *app) int64 { t.Helper() var v int64 if err := a.pool.QueryRow(context.Background(), `SELECT COALESCE(max(version),0) FROM schema_versions`).Scan(&v); err != nil { t.Fatalf("取最新 schema 版本失败: %v", err) } return v } // diffEntriesOf 取 diff 结果里某条事实的差异条目。 func diffEntriesOf(t *testing.T, body map[string]any, name string) []map[string]any { t.Helper() raw, _ := body["entries"].([]any) out := []map[string]any{} for _, e := range raw { m, _ := e.(map[string]any) if m != nil && m["name"] == name { out = append(out, m) } } return out } // fieldClassOf 取某个差异条目里某字段的分类。 func fieldClassOf(t *testing.T, entry map[string]any, field string) (string, bool) { t.Helper() fields, _ := entry["fields"].([]any) for _, f := range fields { m, _ := f.(map[string]any) if m != nil && m["field"] == field { cls, _ := m["class"].(string) return cls, true } } return "", false } // TestIntegration_SchemaSnapshotPerChange 每次 schema 变更都落一份快照。 func TestIntegration_SchemaSnapshotPerChange(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") key := uniqTypeKey("DiffSnap") if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{ "api_name": key, "display_name": "快照探针类型", }, h); code != 200 && code != 201 { t.Fatalf("建类型失败: %d %v", code, body) } ver := latestSchemaVersion(t, a) // 该版本必须有快照 var n int if err := a.pool.QueryRow(context.Background(), `SELECT count(*) FROM schema_snapshots WHERE version=$1`, ver).Scan(&n); err != nil { t.Fatalf("查快照失败: %v", err) } if n != 1 { t.Fatalf("版本 %d 应有 1 份快照,实际 %d 份", ver, n) } // 快照清单接口应能列出该版本 code, body := env.httpDo(t, "GET", "/api/v1/ontology/schema/snapshots", nil, h) if code != 200 { t.Fatalf("列快照失败: %d %v", code, body) } found := false items, _ := body["snapshots"].([]any) for _, it := range items { m, _ := it.(map[string]any) if m != nil && m["version"] == float64(ver) { found = true } } if !found { t.Errorf("快照清单里没有版本 %d: %v", ver, body) } } // TestIntegration_SchemaDiffDetectsAddAndCuratedChange diff 能认得出「新增类型」 // 与「人工改过的描述」,并把描述归到人工策展。 func TestIntegration_SchemaDiffDetectsAddAndCuratedChange(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") key := uniqTypeKey("DiffBasic") // v1:建类型 if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{ "api_name": key, "display_name": "差异探针类型", "description": "初始描述", }, h); code != 200 && code != 201 { t.Fatalf("建类型失败: %d %v", code, body) } v1 := latestSchemaVersion(t, a) // v2:改描述(人工策展)+ 改 title_property(机器事实) if code, body := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+key, map[string]any{ "description": "人工改过的描述", "title_property": "name", }, h); code != 200 { t.Fatalf("改类型失败: %d %v", code, body) } v2 := latestSchemaVersion(t, a) if v2 <= v1 { t.Fatalf("版本应单调递增: v1=%d v2=%d", v1, v2) } code, body := env.httpDo(t, "GET", fmt.Sprintf("/api/v1/ontology/schema/diff?from=%d&to=%d", v1, v2), nil, h) if code != 200 { t.Fatalf("diff 失败: %d %v", code, body) } entries := diffEntriesOf(t, body, key) if len(entries) != 1 { t.Fatalf("期望类型 %s 有 1 条差异,实际 %d 条: %v", key, len(entries), body) } e := entries[0] if e["change"] != "changed" { t.Errorf("change 期望 changed,实际 %v", e["change"]) } if cls, ok := fieldClassOf(t, e, "description"); !ok || cls != classCurated { t.Errorf("description 应归人工策展(%s),实际 %q(存在=%v)", classCurated, cls, ok) } if cls, ok := fieldClassOf(t, e, "title_property"); !ok || cls != classMachine { t.Errorf("title_property 应归机器事实(%s),实际 %q(存在=%v)", classMachine, cls, ok) } } // TestIntegration_SchemaDiffDetectsAddedType 新增的类型在 diff 里是 added。 func TestIntegration_SchemaDiffDetectsAddedType(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") key := uniqTypeKey("DiffAdded") key2 := uniqTypeKey("DiffAdded2") // v1:建第一个类型(该版本自带快照) if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{ "api_name": key, "display_name": "新增探针类型", }, h); code != 200 && code != 201 { t.Fatalf("建类型失败: %d %v", code, body) } v1 := latestSchemaVersion(t, a) // v2:再建一个类型 if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{ "api_name": key2, "display_name": "第二个新增探针类型", }, h); code != 200 && code != 201 { t.Fatalf("建第二个类型失败: %d %v", code, body) } v2 := latestSchemaVersion(t, a) code, body := env.httpDo(t, "GET", fmt.Sprintf("/api/v1/ontology/schema/diff?from=%d&to=%d", v1, v2), nil, h) if code != 200 { t.Fatalf("diff 失败: %d %v", code, body) } entries := diffEntriesOf(t, body, key2) if len(entries) != 1 { t.Fatalf("期望类型 %s 有 1 条差异,实际 %d 条: %v", key2, len(entries), body) } if entries[0]["change"] != "added" { t.Errorf("change 期望 added,实际 %v", entries[0]["change"]) } // 没动的类型不该出现在差异里 if got := diffEntriesOf(t, body, key); len(got) != 0 { t.Errorf("未改动的类型 %s 不该出现在差异里: %v", key, got) } } // TestIntegration_SchemaDiffMarksCertified 认证位标记的条目在 diff 里带 certified。 func TestIntegration_SchemaDiffMarksCertified(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") key := uniqTypeKey("DiffCert") if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{ "api_name": key, "display_name": "认证探针类型", "description": "认证前描述", "axioms": map[string]any{ "certification": map[string]any{ "certified": true, "certifiedBy": "manager", "certifiedAt": "2026-11-06T00:00:00Z", }, }, }, h); code != 200 && code != 201 { t.Fatalf("建类型失败: %d %v", code, body) } v1 := latestSchemaVersion(t, a) if code, body := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+key, map[string]any{ "description": "认证后描述", }, h); code != 200 { t.Fatalf("改类型失败: %d %v", code, body) } v2 := latestSchemaVersion(t, a) code, body := env.httpDo(t, "GET", fmt.Sprintf("/api/v1/ontology/schema/diff?from=%d&to=%d", v1, v2), nil, h) if code != 200 { t.Fatalf("diff 失败: %d %v", code, body) } entries := diffEntriesOf(t, body, key) if len(entries) != 1 { t.Fatalf("期望 1 条差异,实际 %d: %v", len(entries), body) } if certified, _ := entries[0]["certified"].(bool); !certified { t.Errorf("已认证条目的差异应带 certified=true: %v", entries[0]) } } // TestIntegration_SchemaDiffRejectsUnknownVersion 版本没有快照时如实报错,不静默返回空。 func TestIntegration_SchemaDiffRejectsUnknownVersion(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") code, body := env.httpDo(t, "GET", "/api/v1/ontology/schema/diff?from=999999999&to=live", nil, h) if code != 400 { t.Fatalf("无快照的版本应报 400,实际 %d: %v", code, body) } if msg, _ := body["error"].(string); msg == "" { t.Errorf("应给出错误说明: %v", body) } } // TestIntegration_CaptureSchemaSnapshot 手动锚点:把当前结构标成一份快照。 func TestIntegration_CaptureSchemaSnapshot(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") code, body := env.httpDo(t, "POST", "/api/v1/ontology/schema/snapshot", map[string]any{ "label": "重摄取前的锚点", }, h) if code != 200 { t.Fatalf("手动快照失败: %d %v", code, body) } if body["label"] != "重摄取前的锚点" { t.Errorf("label 期望原样回显,实际 %v", body["label"]) } ver := latestSchemaVersion(t, a) var label string if err := a.pool.QueryRow(context.Background(), `SELECT label FROM schema_snapshots WHERE version=$1`, ver).Scan(&label); err != nil { t.Fatalf("查快照失败: %v", err) } if label != "重摄取前的锚点" { t.Errorf("库里 label 期望「重摄取前的锚点」,实际 %q", label) } } // TestIntegration_SchemaDiffOverMCP MCP 侧也能拿到 diff(agent 常用路径)。 func TestIntegration_SchemaDiffOverMCP(t *testing.T) { env, a := setupTestEnv(t) h := authHeaderFor(t, a, "manager") key := uniqTypeKey("DiffMCP") if code, body := env.httpDo(t, "POST", "/api/v1/ontology/object-types", map[string]any{ "api_name": key, "display_name": "MCP 探针类型", }, h); code != 200 && code != 201 { t.Fatalf("建类型失败: %d %v", code, body) } ver := latestSchemaVersion(t, a) // 再改一次描述,产生第二个版本(两个版本都有快照) if code, body := env.httpDo(t, "PUT", "/api/v1/ontology/object-types/"+key, map[string]any{ "description": "MCP 探针的新描述", }, h); code != 200 { t.Fatalf("改类型失败: %d %v", code, body) } ver2 := latestSchemaVersion(t, a) text, isErr, rpcErr := mcpCallRaw(t, env, h, "diff_schema_versions", map[string]any{ "from": fmt.Sprintf("%d", ver), "to": fmt.Sprintf("%d", ver2), }) if rpcErr != "" { t.Fatalf("MCP diff 未被分发: %s", rpcErr) } if isErr { t.Fatalf("MCP diff 返回错误: %s", text) } if !strings.Contains(text, key) { t.Errorf("MCP diff 结果里应含类型名 %s: %s", key, text) } if !strings.Contains(text, classCurated) { t.Errorf("MCP diff 结果里应标出人工策展字段(%s): %s", classCurated, text) } }