Files
ssw-ontology/server/empty_reason_integration_test.go
T
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

197 lines
6.7 KiB
Go
Raw 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
import (
"fmt"
"strings"
"testing"
)
// TestIntegration_EmptyReasonCodes 空结果必须带原因,且原因分得清:
//
// no_data 类型在当前快照下真的没数据
// not_visible 有数据但当前主体看不到(权限问题,不是数据缺失)
// no_type 类型压根没注册
//
// 这三者的处置完全不同;混成一句「查不到」会让调用方(以及读它的 LLM)
// 把「不可见」当成「不存在」。
func TestIntegration_EmptyReasonCodes(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
// 空类型(注册了但一个实例都没有)
emptyType := env.typeKey + "_empty"
zoneProbeType(t, env, a, emptyType)
// 有实例的类型
dataType := env.typeKey + "_data"
pk, _ := zoneProbeInstance(t, env, a, dataType, "空原因探针", "深圳")
// 主体 A:没有任何 clearance → 一条都看不到
blindKey := newScopedPrincipal(t, a, "empty_probe_blind", nil)
blindH := map[string]string{"X-API-Key": blindKey}
// 主体 B:有 L0 clearance → 看得见(实例本身无标记)
seenKey := newScopedPrincipal(t, a, "empty_probe_seen", []string{"L0"})
seenH := map[string]string{"X-API-Key": seenKey}
reasonOf := func(t *testing.T, h map[string]string, typ string) map[string]any {
t.Helper()
code, body := env.httpDo(t, "GET", "/api/v1/connector/objects/"+typ, nil, h)
if code != 200 {
t.Fatalf("列实例失败: %d %v", code, body)
}
r, _ := body["empty_reason"].(map[string]any)
return r
}
// ---- no_data ----
r := reasonOf(t, blindH, emptyType)
if r == nil {
t.Fatal("空类型的列表回包应带 empty_reason")
}
if r["code"] != emptyCodeNoData {
t.Errorf("空类型的原因应是 %s,实到 %v", emptyCodeNoData, r["code"])
}
// ---- not_visible ----
r = reasonOf(t, blindH, dataType)
if r == nil {
t.Fatal("看不到实例时列表回包应带 empty_reason")
}
if r["code"] != emptyCodeNotVisible {
t.Errorf("看不到实例的原因应是 %s,实到 %v", emptyCodeNotVisible, r["code"])
}
// 不泄漏被挡住实例的主键(存在性泄漏)
if strings.Contains(fmt.Sprint(r), pk) {
t.Errorf("empty_reason 不应带实例主键:%v", r)
}
// ---- 有可见实例 → 不带 empty_reason ----
if r := reasonOf(t, seenH, dataType); r != nil {
t.Errorf("有可见实例时不应带 empty_reason,实到 %v", r)
}
// ---- no_type ----
r = reasonOf(t, seenH, "NoSuchType_"+env.typeKey)
if r == nil || r["code"] != emptyCodeNoType {
t.Errorf("未注册类型的原因应是 %s,实到 %v", emptyCodeNoType, r)
}
}
// TestIntegration_ExistenceProbe 三态存在性探针:
//
// exists 存在且该主体可见
// invisible 真实存在但该主体看不到(附 blocked_by)
// not_exists 确实没有
//
// 读路径对越权实例一律 404(不泄漏存在性),三态只走这条管理面探针。
func TestIntegration_ExistenceProbe(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
typ := env.typeKey + "_exist"
pk, _ := zoneProbeInstance(t, env, a, typ, "存在性探针", "广州")
probe := func(t *testing.T, qs string, h map[string]string) map[string]any {
t.Helper()
code, body := env.httpDo(t, "GET", "/api/v1/access/existence?"+qs, nil, h)
if code != 200 {
t.Fatalf("探针失败: %d %v", code, body)
}
return body
}
// ---- not_exists ----
b := probe(t, "type="+typ+"&pk=definitely-not-here", mh)
if b["state"] != existenceNotExists {
t.Errorf("不存在的实例应判 %s,实到 %v", existenceNotExists, b["state"])
}
// ---- exists(manager 视角)----
b = probe(t, "type="+typ+"&pk="+pk, mh)
if b["state"] != existenceExists {
t.Errorf("manager 应判 %s,实到 %v", existenceExists, b["state"])
}
if b["visible"] != true {
t.Errorf("exists 时 visible 应为 true,实到 %v", b["visible"])
}
// ---- invisible(受限主体视角)----
blindKey := newScopedPrincipal(t, a, "exist_probe_blind", nil)
b = probe(t, "type="+typ+"&pk="+pk+"&principal=exist_probe_blind", mh)
if b["state"] != existenceInvisible {
t.Errorf("受限主体应判 %s,实到 %v", existenceInvisible, b["state"])
}
if b["visible"] != false {
t.Errorf("invisible 时 visible 应为 false,实到 %v", b["visible"])
}
blocked, _ := b["blocked_by"].([]any)
if len(blocked) == 0 {
t.Errorf("invisible 应说明被哪一维挡住:%v", b)
}
if note, _ := b["note"].(string); !strings.Contains(note, "不存在") {
t.Errorf("invisible 的 note 应提示「不要说不存在」,实到 %q", note)
}
// ---- MCP:manager 可用 ----
text, isErr, rpcErr := mcpCallRaw(t, env, mh, "probe_existence",
map[string]any{"type": typ, "pk": pk})
if rpcErr != "" || isErr {
t.Fatalf("manager 调 probe_existence 失败: %s / %v / %s", text, isErr, rpcErr)
}
if !strings.Contains(text, existenceExists) {
t.Errorf("probe_existence 回包应含状态 %s:%s", existenceExists, text)
}
// ---- MCP:非 manager 被拒(工具被识别,但执行被拒)----
text, isErr, rpcErr = mcpCallRaw(t, env, map[string]string{"X-API-Key": blindKey},
"probe_existence", map[string]any{"type": typ, "pk": pk})
if rpcErr != "" {
t.Fatalf("非 manager 调用应被识别后拒绝,而不是 JSON-RPC 错误: %s", rpcErr)
}
if !isErr {
t.Errorf("非 manager 不应能调存在性探针:%s", text)
}
}
// TestIntegration_AnswerableClarification 可答性报告的结构化澄清输出:
//
// MCP 客户端往往一次调用,没有多轮澄清机会——
// 澄清必须表现为结构化返回(needs_clarification + items),而不是交互循环。
func TestIntegration_AnswerableClarification(t *testing.T) {
env, a := setupTestEnv(t)
seedConnectorForTest(t)
mh := authHeaderFor(t, a, "manager")
zoneProbeType(t, env, a, env.typeKey)
code, body := env.httpDo(t, "POST", "/api/v1/connector/answerable",
map[string]any{"question": "火星基地的氧气含量是多少"}, mh)
if code != 200 {
t.Fatalf("可答性评估失败: %d %v", code, body)
}
need, _ := body["needs_clarification"].(bool)
if !need {
t.Fatalf("问题含未落地实体,应判需要澄清:%v", body)
}
items, _ := body["items"].([]any)
if len(items) == 0 {
t.Fatalf("需要澄清时应给出 items:%v", body)
}
it, _ := items[0].(map[string]any)
if it == nil {
t.Fatalf("items 元素应是对象:%v", items)
}
if _, ok := it["term"].(string); !ok {
t.Errorf("澄清项应含 term:%v", it)
}
if q, _ := it["suggested_question"].(string); q == "" {
t.Errorf("澄清项应给出建议问法:%v", it)
}
// 报告带主体与快照维度(同一个问题在不同主体/快照下答案不同)
if body["principal"] != "manager" {
t.Errorf("报告应带 principal,实到 %v", body["principal"])
}
}