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

133 lines
4.4 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.
package main
import (
"context"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
)
// TestProgressCtxRoundTrip 进度回调挂进 ctx 后能取回、上报;没有回调时是空操作。
func TestProgressCtxRoundTrip(t *testing.T) {
if fn := progressFrom(context.Background()); fn != nil {
t.Fatalf("无回调的 ctx 不应取到进度函数")
}
if fn := progressFrom(nil); fn != nil {
t.Fatalf("nil ctx 不应取到进度函数")
}
// 空操作:没有回调时 reportProgress 不应 panic
reportProgress(context.Background(), 1, 2, "x")
reportProgress(nil, 1, 2, "x")
var gotDone, gotTotal float64
var gotMsg string
calls := 0
ctx := withProgress(context.Background(), func(done, total float64, msg string) {
calls++
gotDone, gotTotal, gotMsg = done, total, msg
})
fn := progressFrom(ctx)
if fn == nil {
t.Fatalf("挂了回调的 ctx 应能取到进度函数")
}
reportProgress(ctx, 3, 10, "处理中")
if calls != 1 {
t.Fatalf("应上报 1 次,实到 %d", calls)
}
if gotDone != 3 || gotTotal != 10 || gotMsg != "处理中" {
t.Fatalf("上报内容不对: done=%v total=%v msg=%q", gotDone, gotTotal, gotMsg)
}
}
// TestProgressTokenFromMeta 只认 tools/call 的 _meta.progressToken。
func TestProgressTokenFromMeta(t *testing.T) {
cases := []struct {
name string
req jsonrpcRequest
want bool
}{
{"tools/call 带 token", jsonrpcRequest{Method: "tools/call", Params: map[string]interface{}{
"_meta": map[string]interface{}{"progressToken": "abc-123"},
}}, true},
{"tools/call 无 _meta", jsonrpcRequest{Method: "tools/call", Params: map[string]interface{}{}}, false},
{"tools/call 有 _meta 但无 token", jsonrpcRequest{Method: "tools/call", Params: map[string]interface{}{
"_meta": map[string]interface{}{"other": 1},
}}, false},
{"token 为 null", jsonrpcRequest{Method: "tools/call", Params: map[string]interface{}{
"_meta": map[string]interface{}{"progressToken": nil},
}}, false},
{"非 tools/call 不认", jsonrpcRequest{Method: "tools/list", Params: map[string]interface{}{
"_meta": map[string]interface{}{"progressToken": "abc"},
}}, false},
}
for _, c := range cases {
tok, ok := mcpProgressToken(c.req)
if ok != c.want {
t.Errorf("%s: ok 应为 %v,实到 %v", c.name, c.want, ok)
}
if ok && tok == nil {
t.Errorf("%s: token 不应为 nil", c.name)
}
}
}
// TestProgressStreamerFrames 进度写出器产出的帧格式必须是 MCP 规范的
// notifications/progress(jsonrpc 2.0 + method + params.progressToken/progress)。
func TestProgressStreamerFrames(t *testing.T) {
w := httptest.NewRecorder()
p := newMCPProgressStreamer(w, "tok-1", 0)
p.report(1, 10, "第一步")
p.report(2, 10, "第二步")
body := w.Body.String()
if !strings.Contains(body, "event: message") {
t.Fatalf("应写出 SSE 帧:%q", body)
}
if !strings.Contains(body, `"method":"notifications/progress"`) {
t.Fatalf("应含 notifications/progress:%q", body)
}
if !strings.Contains(body, `"progressToken":"tok-1"`) {
t.Fatalf("应回带原 progressToken:%q", body)
}
if !strings.Contains(body, `"progress":1`) || !strings.Contains(body, `"total":10`) {
t.Fatalf("应含 progress/total:%q", body)
}
// 每帧都得是合法 JSON
for _, line := range strings.Split(body, "\n") {
if !strings.HasPrefix(line, "data: ") {
continue
}
var out map[string]interface{}
if err := json.Unmarshal([]byte(strings.TrimPrefix(line, "data: ")), &out); err != nil {
t.Fatalf("帧不是合法 JSON: %v (%q)", err, line)
}
if out["jsonrpc"] != "2.0" {
t.Fatalf("帧缺 jsonrpc: %q", line)
}
}
}
// TestProgressStreamerThrottle 节流:密集上报只写一次,pending 由 finish 补发。
// 目的是「上万实例的重算不会把 SSE 刷爆」,同时保证终态一定送达。
func TestProgressStreamerThrottle(t *testing.T) {
w := httptest.NewRecorder()
p := newMCPProgressStreamer(w, "tok-2", mcpProgressMinGap)
for i := 0; i < 50; i++ {
p.report(float64(i), 50, "处理")
}
body := w.Body.String()
if n := strings.Count(body, "notifications/progress"); n != 1 {
t.Fatalf("节流期内只应写出 1 帧,实到 %d:%q", n, body)
}
// 补发的应是最后一条(progress=49)
p.finish()
body = w.Body.String()
if n := strings.Count(body, "notifications/progress"); n != 2 {
t.Fatalf("finish 后应有 2 帧,实到 %d:%q", n, body)
}
if !strings.Contains(body, `"progress":49`) {
t.Fatalf("finish 应补发最后一条进度:%q", body)
}
}