Files
ssw-ontology/server/schema_diff_integration_test.go
adminandClaude Code d14440a3ed feat(server): v1.8.0 本体导入 + 分区 + 行级归属 + 认证位 + 结构 diff + 分页 + 进度回报
一次收口 2026-11-06 的九项能力(CHANGELOG 1.5.4 → 1.8.0):

- 自检探针(readiness.go):check_readiness / check_answerable 两个只读探针,把
  「本体里没有」「我没权限看」「模型没抽全」三种空结果原因拆开
- 读路径语义钉死(scope.go):标记与分级的可见范围统一由 Scope 产出,实例分级
  为空时继承类型默认级(fail-closed),列表与详情口径一致
- 行级可改性轴(object_owner.go):归属只收窄不宽于 grants,所有对象写路径统一
  走 canWriteObject,归属查询失败即拒绝
- 公理第 6 类 certification:认证位保护人工策展内容,AI 重摄取不再覆盖已认证条目
- 跨版本结构 diff(schema_diff.go):schema_snapshots 全量快照 + diff 端点,机器
  事实与人工策展分组呈现
- 分区维度 zone:分区绑类型、命中任一即可见,读路径一律带 zone 条件
- 统一分页信封(pagination.go):列表回包统一 count/total/limit/offset/has_more/
  truncated,total 与 items 同源
- MCP 长任务进度(mcp_progress.go):客户端带 progressToken 才走 SSE 进度帧
- 本体 schema 导入(ontology_import.go):Turtle 导入 + 四道 fail-fast 闸门 +
  整事务 + (source, external_id) 幂等 + IRI 往返不变式

MCP 工具清单并入单一注册表(mcp_registry.go),对外声明与实际可调不再各说各话;
工具数 42 → 54(只读 27 / 写入 27),注册表与分发 case 的双向漂移由 AST 单测拦截。

CHANGELOG 编号整理:新「认证位 + 行级归属」条目由 1.6.0 改为 1.5.6(1.6.x 留给
分区与结构 diff),2026-09-20 的 GB/T 规划稿改标「规划稿·未发布」——该能力已并入
v1.5.1,避免与正式发布的版本号重复。

Co-Authored-By: Claude Code <[email protected]>
2026-11-06 13:51:48 +08:00

303 lines
10 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//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)
}
}