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) } }