Files
knowledge-governance/internal/releaseadapter/v071/wiki_scope_test.go
T
姚章浩 e5fa063d7d feat(release): wiki 通道按归属判定替换,共享知识库不再被整库清空
WeKnora 的 wiki 图谱与页面同表(wiki_pages),旧的库级 DELETE + 灌包在共享真实
知识库上会把其他文档的页面与 agent 手写页一起抹掉(2026-10-08 生产演练现场)。

- 新增 internal/releaseadapter/v071/wiki_scope.go:按 source_refs 判定归属
  (全部引用属于本包 = InScope;引用包外知识 = 外来页;无引用 = index/log/
  agent 页永不触碰),目标库存在外来页时降级为只替换包内页集,外来页上仅摘掉
  本包知识引用,包内库级页不写;包内会写入的页与外来页同 slug 时 fail-closed
  拒绝发布且不落任何改动。
- Replace 与 ReplaceWiki(撤权/回滚)共用同一套判定,不再整库清空。
- 作用域以一行日志落盘:release wiki replace scope in_scope=… degraded=…
- 测试:单测 7 条(纯函数判定)+ 真库集成 3 条(共享库保护 / slug 冲突拒绝 /
  镜像库整库替换不变),集成用例只用带随机后缀的临时知识库并自清理。
- 文档:P6 新增「wiki 通道的归属门禁」,P8 补验收点与镜像前提。
2026-10-08 19:06:36 +08:00

152 lines
6.2 KiB
Go

package v071
import (
"encoding/json"
"testing"
)
func wikiPageRefJSON(t *testing.T, fields map[string]any) json.RawMessage {
t.Helper()
encoded, err := json.Marshal(fields)
if err != nil {
t.Fatalf("encode wiki page row: %v", err)
}
return encoded
}
func contains(values []string, want string) bool {
for _, value := range values {
if value == want {
return true
}
}
return false
}
// 镜像库对原件:目标库每一页的来源都在包内 → 允许整库替换(旧行为保持不变)。
func TestClassifyTargetWikiPagesAllowsLibraryReplaceOnFaithfulMirror(t *testing.T) {
owners := map[string]bool{"knowledge-a": true, "knowledge-b": true}
pages := []targetWikiRef{
{ID: "p1", Slug: "summary/a", SourceRefs: []string{"knowledge-a|A.docx"}},
{ID: "p2", Slug: "entity/shared", SourceRefs: []string{"knowledge-a|A.docx", "knowledge-b|B.docx"}},
{ID: "p3", Slug: "index", SourceRefs: nil, ChunkRefs: nil},
}
scope, inScope, foreign := classifyTargetWikiPages(pages, owners)
if scope.Degraded || scope.Foreign != 0 {
t.Fatalf("mirror must not degrade: %+v", scope)
}
if scope.InScope != 2 || scope.Untouched != 1 {
t.Fatalf("scope=%+v", scope)
}
if !contains(inScope, "summary/a") || !contains(inScope, "entity/shared") {
t.Fatalf("in-scope slugs=%v", inScope)
}
if len(foreign) != 0 {
t.Fatalf("foreign=%v", foreign)
}
}
// 共享真实库:目标库存在别的文档的页 → 降级,只换本包页集,其余绝不触碰。
func TestClassifyTargetWikiPagesDegradesOnSharedKnowledgeBase(t *testing.T) {
owners := map[string]bool{"knowledge-ours": true}
pages := []targetWikiRef{
{ID: "p1", Slug: "summary/ours", SourceRefs: []string{"knowledge-ours|Ours.docx"}},
{ID: "p2", Slug: "entity/shared", SourceRefs: []string{"knowledge-ours|Ours.docx", "knowledge-theirs|Theirs.xlsx"}},
{ID: "p3", Slug: "concept/agent-made", SourceRefs: nil, ChunkRefs: nil},
{ID: "p4", Slug: "summary/theirs", SourceRefs: []string{"knowledge-theirs|Theirs.xlsx"}},
{ID: "p5", Slug: "orphan/chunk-only", SourceRefs: nil, ChunkRefs: []string{"chunk-1"}},
}
scope, inScope, foreign := classifyTargetWikiPages(pages, owners)
if !scope.Degraded {
t.Fatalf("shared knowledge base must degrade: %+v", scope)
}
if scope.InScope != 1 || scope.Foreign != 3 || scope.Untouched != 1 {
t.Fatalf("scope=%+v", scope)
}
if scope.ForeignReferencingIn != 1 || len(foreign) != 1 || foreign[0].Slug != "entity/shared" {
t.Fatalf("foreign referencing in-scope must be reported for ref stripping: %+v %v", scope, foreign)
}
if len(inScope) != 1 || inScope[0] != "summary/ours" {
t.Fatalf("in-scope slugs=%v", inScope)
}
}
// 结构异常(引用带坏 id / 非数组)的页按外来页处理:不删、不改。
func TestClassifyTargetWikiPagesTreatsMalformedRowsAsForeign(t *testing.T) {
owners := map[string]bool{"knowledge-ours": true}
pages := []targetWikiRef{
{ID: "p1", Slug: "summary/ours", SourceRefs: []string{"knowledge-ours|Ours.docx"}},
{ID: "p2", Slug: "broken/ref", SourceRefs: []string{"not a valid id|Doc"}},
}
scope, inScope, _ := classifyTargetWikiPages(pages, owners)
if scope.InScope != 1 || scope.Foreign != 1 || !scope.Degraded {
t.Fatalf("scope=%+v", scope)
}
if len(inScope) != 1 {
t.Fatalf("in-scope slugs=%v", inScope)
}
}
func TestPackageWikiOwnersUnionsKnowledgesAndPageReferences(t *testing.T) {
snapshot := PostgresSnapshot{Tables: []TableSnapshot{
newTableSnapshot("knowledges", []json.RawMessage{wikiPageRefJSON(t, map[string]any{"id": "knowledge-ours"})}),
newTableSnapshot("wiki_pages", []json.RawMessage{
wikiPageRefJSON(t, map[string]any{"id": "p1", "slug": "summary/ours", "source_refs": []string{"knowledge-ours|Ours.docx", "knowledge-other|Other.pdf"}}),
wikiPageRefJSON(t, map[string]any{"id": "p2", "slug": "index", "source_refs": []string{}}),
}),
}}
owners, err := packageWikiOwners(snapshot)
if err != nil {
t.Fatal(err)
}
if !owners["knowledge-ours"] || !owners["knowledge-other"] {
t.Fatalf("owners=%v", owners)
}
empty := PostgresSnapshot{Tables: []TableSnapshot{
newTableSnapshot("knowledges", []json.RawMessage{wikiPageRefJSON(t, map[string]any{"id": "knowledge-ours"})}),
// 只有空 wiki 快照时集合仍来自 knowledges 行。
newTableSnapshot("wiki_pages", []json.RawMessage{wikiPageRefJSON(t, map[string]any{"id": "p1", "slug": "index", "source_refs": []string{}})}),
}}
if _, err := packageWikiOwners(empty); err != nil {
t.Fatalf("knowledge rows alone must define ownership: %v", err)
}
if _, err := packageWikiOwners(PostgresSnapshot{}); err == nil {
t.Fatal("empty package must be rejected")
}
}
func TestReferenceOwnerParsesKnowledgeID(t *testing.T) {
if got := referenceOwner("knowledge-abc|手册.pdf"); got != "knowledge-abc" {
t.Fatalf("owner=%q", got)
}
if got := referenceOwner("knowledge-abc"); got != "knowledge-abc" {
t.Fatalf("owner=%q", got)
}
if got := referenceOwner("not a valid id|Doc"); got != "" {
t.Fatalf("malformed reference must not yield an owner: %q", got)
}
}
// 包内页 slug 已被外来页占用 → 必须拒绝发布。
func TestWikiSlugCollisionDetectsForeignSlug(t *testing.T) {
pages := []targetWikiRef{
{ID: "p1", Slug: "summary/ours", SourceRefs: []string{"knowledge-ours|Ours.docx"}},
{ID: "p2", Slug: "entity/contested", SourceRefs: []string{"knowledge-theirs|Theirs.xlsx"}},
}
bundle := map[string]bool{"summary/ours": true, "entity/contested": true}
if slug, collided := wikiSlugCollision(pages, []string{"summary/ours"}, bundle); !collided || slug != "entity/contested" {
t.Fatalf("collision=%q %t", slug, collided)
}
if _, collided := wikiSlugCollision(pages, []string{"summary/ours", "entity/contested"}, bundle); collided {
t.Fatal("in-scope slugs must not count as collisions")
}
// 包内的库级页(index/log)在降级路径上不写,因此目标库的同名页不算占用。
library := []targetWikiRef{{ID: "p9", Slug: "index", SourceRefs: nil, ChunkRefs: nil}}
if slug, collided := wikiSlugCollision(library, nil, map[string]bool{}); collided {
t.Fatalf("library pages must not collide: %q", slug)
}
if slug, collided := wikiSlugCollision(library, nil, map[string]bool{"index": true}); !collided || slug != "index" {
t.Fatalf("ref-bearing package page must collide: %q %t", slug, collided)
}
}