Files
ssw-ontology/server/classification_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

194 lines
6.1 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.
package main
import "testing"
// ---------- levelIndex / maxLevel ----------
func TestLevelIndex(t *testing.T) {
levels := []string{"L0", "L1", "L2", "L3"}
cases := []struct {
in string
want int
}{
{"L0", 0},
{"L1", 1},
{"L3", 3},
{"L9", -1},
{"", -1},
}
for _, c := range cases {
if got := levelIndex(levels, c.in); got != c.want {
t.Errorf("levelIndex(%q) = %d, want %d", c.in, got, c.want)
}
}
}
func TestMaxLevel(t *testing.T) {
levels := []string{"L0", "L1", "L2", "L3"}
// 两个已知:取较大下标
if got := maxLevel("L1", "L3", levels); got != "L3" {
t.Errorf("max(L1,L3) = %q, want L3", got)
}
if got := maxLevel("L3", "L1", levels); got != "L3" {
t.Errorf("max(L3,L1) = %q, want L3", got)
}
if got := maxLevel("L2", "L2", levels); got != "L2" {
t.Errorf("max(L2,L2) = %q, want L2", got)
}
// 一空一有:返回非空
if got := maxLevel("", "L2", levels); got != "L2" {
t.Errorf("max('',L2) = %q, want L2", got)
}
if got := maxLevel("L2", "", levels); got != "L2" {
t.Errorf("max(L2,'') = %q, want L2", got)
}
// 两个空
if got := maxLevel("", "", levels); got != "" {
t.Errorf("max('','') = %q, want ''", got)
}
// 一个未知一个已知:返回已知(已知优于未知,保证分级配置扩展时不静默失效)
if got := maxLevel("L9", "L2", levels); got != "L2" {
t.Errorf("max(L9,L2) = %q, want L2", got)
}
if got := maxLevel("L2", "L9", levels); got != "L2" {
t.Errorf("max(L2,L9) = %q, want L2", got)
}
// 两个未知:字典序兜底(确定性)
if got := maxLevel("ZZ", "AA", levels); got != "ZZ" {
t.Errorf("max(ZZ,AA) = %q, want ZZ (字典序)", got)
}
}
// ---------- materializeClassification ----------
func TestMaterializeClassification(t *testing.T) {
cfg := classConfig{Property: "data_level", Levels: []string{"L0", "L1", "L2", "L3"}, Default: "L0"}
// 属性值 → 直接采用
got := materializeClassification(cfg, map[string]interface{}{"data_level": "L2"}, "")
if got != "L2" {
t.Errorf("属性 L2 → %q, want L2", got)
}
// 属性空 → 默认
got = materializeClassification(cfg, map[string]interface{}{}, "")
if got != "L0" {
t.Errorf("空属性 → %q, want L0 (default)", got)
}
// KM 源文件 floor 抬升分级
got = materializeClassification(cfg, map[string]interface{}{"data_level": "L1"}, "L2")
if got != "L2" {
t.Errorf("属性 L1 + floor L2 → %q, want L2 (floor 抬升)", got)
}
// 属性分级更高 → 不会被 floor 拉低
got = materializeClassification(cfg, map[string]interface{}{"data_level": "L3"}, "L1")
if got != "L3" {
t.Errorf("属性 L3 + floor L1 → %q, want L3 (max)", got)
}
// 数字属性 → propString 转字面量;"2" 不在 cfg.Levels 里 → maxLevel 走"未知优于空"分支返回 "2"
// 真实业务应禁止数字级别的设计选择,所以测试只验证字面量化行为,不预测具体值。
got = materializeClassification(cfg, map[string]interface{}{"data_level": float64(2)}, "")
if got != "2" {
t.Errorf("属性 float(2) → %q, want %q (字面量化)", got, "2")
}
// 未配置分级 → 直接返回 floor
off := classConfig{}
got = materializeClassification(off, map[string]interface{}{"data_level": "L2"}, "L1")
if got != "L1" {
t.Errorf("未配置分级 + floor L1 → %q, want L1", got)
}
}
// ---------- principalMaxIndex / deniedLevelsFor ----------
func TestPrincipalMaxIndex(t *testing.T) {
levels := []string{"L0", "L1", "L2", "L3"}
// manager 永远全开
if got := principalMaxIndex(&Authz{Principal: "manager"}, levels); got != 3 {
t.Errorf("manager → %d, want 3", got)
}
// nil authz 当 manager
if got := principalMaxIndex(nil, levels); got != 3 {
t.Errorf("nil authz → %d, want 3", got)
}
// analyst L0+L2 → max index = 2
authz := &Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L2": true}}
if got := principalMaxIndex(authz, levels); got != 2 {
t.Errorf("analyst(L0,L2) → %d, want 2", got)
}
// 完全无交集 → -1(fail-closed:分级一旦启用就不静默失效)
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{"ZZ": true}}
if got := principalMaxIndex(authz, levels); got != -1 {
t.Errorf("analyst(ZZ) → %d, want -1", got)
}
// 空 clearance → -1
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{}}
if got := principalMaxIndex(authz, levels); got != -1 {
t.Errorf("analyst(empty) → %d, want -1", got)
}
}
func TestDeniedLevelsFor(t *testing.T) {
levels := []string{"L0", "L1", "L2", "L3"}
cfg := classConfig{Property: "data_level", Levels: levels, Default: "L0"}
// manager → 无屏蔽
if got := deniedLevelsFor(&Authz{Principal: "manager"}, cfg); got != nil {
t.Errorf("manager denied = %v, want nil", got)
}
// analyst L0+L1 → 屏蔽 L2+L3
authz := &Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L1": true}}
got := deniedLevelsFor(authz, cfg)
if len(got) != 2 || got[0] != "L2" || got[1] != "L3" {
t.Errorf("analyst(L0,L1) denied = %v, want [L2 L3]", got)
}
// 完全无交集 → 屏蔽全部级别,并额外屏蔽「未定义级别」(fail-closed)
// 语义见 scope.go:DenyAll 意味着该类型对该主体完全不可见,
// 连级别为空的存量实例也不放行(否则「先灌数据、后配引擎」的历史数据会整体泄露)。
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{"ZZ": true}}
got = deniedLevelsFor(authz, cfg)
if len(got) != 5 {
t.Errorf("analyst(ZZ) denied = %v, want 全屏蔽 + 未定义级", got)
}
for _, l := range []string{"L0", "L1", "L2", "L3", ""} {
found := false
for _, d := range got {
if d == l {
found = true
}
}
if !found {
t.Errorf("analyst(ZZ) denied = %v, 应含 %q", got, l)
}
}
// 未启用分级(无 property)→ 无屏蔽
off := classConfig{}
got = deniedLevelsFor(authz, off)
if got != nil {
t.Errorf("未配置分级 denied = %v, want nil", got)
}
// 主体覆盖全级别 → 无屏蔽
authz = &Authz{Principal: "analyst", Clearances: map[string]bool{"L0": true, "L1": true, "L2": true, "L3": true}}
if got := deniedLevelsFor(authz, cfg); got != nil {
t.Errorf("全覆盖 denied = %v, want nil", got)
}
}