eabe55b432
端到端验收实测(2026-10-10):文件型文档(docx)跨环境发布时,写入侧按设计保留了 目标库外来源 wiki 页(日志 in_scope=0 foreign=8 untouched=1 degraded=true), 但校验侧 VerifyWiki 仍要求"生产页 slug 集合与包内快照完全相等",必然误判,项级 报 release_verification_failed,批次随后被判失败单项失败。 - verifier.go: 归属判定与写入侧 replaceWiki 共用 packageWikiOwners; 校验改为"快照内页必须都存在 + 多出来的页只有在不属于本包时才放行", 保留原有牙齿:本包拥有却缺失/残留的页仍判失败;撤权仍禁止任何页引用被撤权文档 - wiki_channel_test.go: 用例改为 外来源多余页放行 / 本包拥有残留页拒绝 / 缺页拒绝 - 实测:同一批次重排后完成(done=1/1 item=verified 生产可用),回滚 rolled_back 这也是 wiki 归属门禁上线后第一次真实跨环境发布
204 lines
8.3 KiB
Go
204 lines
8.3 KiB
Go
package v071
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"os"
|
||
"reflect"
|
||
"testing"
|
||
|
||
"knowledge-governance/internal/platform/weknora"
|
||
releaseworkflow "knowledge-governance/internal/workflows/release"
|
||
)
|
||
|
||
// stubPGWikiExporter 在文档导出器之上模拟“库级 wiki 快照”能力。
|
||
type stubPGWikiExporter struct {
|
||
stubPGExporter
|
||
wikiTables []TableSnapshot
|
||
removing string
|
||
err error
|
||
}
|
||
|
||
func (s *stubPGWikiExporter) ExportWiki(_ context.Context, _ PostgresIdentity, removingKnowledgeID string) ([]TableSnapshot, error) {
|
||
s.removing = removingKnowledgeID
|
||
return s.wikiTables, s.err
|
||
}
|
||
|
||
func wikiTablesFor(t *testing.T, slugs ...string) []TableSnapshot {
|
||
t.Helper()
|
||
rows := make([]json.RawMessage, 0, len(slugs))
|
||
for _, slug := range slugs {
|
||
rows = append(rows, mustCanonicalRows(t, `{"id":"page-`+slug+`","tenant_id":10000,"knowledge_base_id":"kb-safe","slug":"`+slug+`","title":"`+slug+`","source_refs":["knowledge-safe|a.md"],"in_links":[],"out_links":[]}`)[0])
|
||
}
|
||
return []TableSnapshot{newTableSnapshot("wiki_folders", nil), newTableSnapshot("wiki_pages", rows)}
|
||
}
|
||
|
||
// wikiCarryingBundle 通过真实导出器产出一个带 wiki 快照的合法 bundle(manifest 与表摘要一致)。
|
||
func wikiCarryingBundle(t *testing.T, wikiExporter *stubPGWikiExporter, action string) (*DocumentBundle, releaseworkflow.PlanItem) {
|
||
t.Helper()
|
||
pg := PostgresSnapshot{Tables: []TableSnapshot{
|
||
newTableSnapshot("knowledges", mustCanonicalRows(t, `{"id":"knowledge-safe","title":"synthetic release marker"}`)),
|
||
newTableSnapshot("chunks", mustCanonicalRows(t, `{"id":"chunk-safe"}`)),
|
||
}}
|
||
search := SearchSnapshot{Documents: []SearchDocument{{
|
||
ID: "es-safe", Source: mustCanonicalRows(t, `{"tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"knowledge-safe","content":"synthetic release marker"}`)[0],
|
||
}}}
|
||
search.DocumentsSHA256, _ = searchDocumentsDigest(search.Documents)
|
||
search.MappingSHA256 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||
objects := ObjectSnapshot{SHA256: objectSnapshotDigest(nil)}
|
||
wikiExporter.stubPGExporter = stubPGExporter{snapshot: pg}
|
||
exporter, err := NewDocumentExporter(DocumentExporterConfig{
|
||
Contract: &stubSourceContract{contract: sourceContractFixture(search.MappingSHA256)},
|
||
Postgres: wikiExporter, Search: &stubSearchExporter{snapshot: search},
|
||
Objects: &stubObjectExporter{snapshot: objects}, TenantID: 10000, EmbeddingModel: "bge-m3", Now: fixedArchiveTime,
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
item := releasePlanForArchive()
|
||
item.Action = action
|
||
value, err := exporter.Export(context.Background(), item)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
return value.(*DocumentBundle), item
|
||
}
|
||
|
||
func TestDocumentExporterCarriesWikiSnapshotIntoBundle(t *testing.T) {
|
||
wikiExporter := &stubPGWikiExporter{wikiTables: wikiTablesFor(t, "entity/alpha", "index")}
|
||
bundle, _ := wikiCarryingBundle(t, wikiExporter, "upsert")
|
||
if wikiExporter.removing != "" {
|
||
t.Fatalf("publish must not retract: %q", wikiExporter.removing)
|
||
}
|
||
if bundle.Manifest.Tables == nil {
|
||
t.Fatal("missing manifest tables")
|
||
}
|
||
if got := bundle.Postgres.Table("wiki_pages").RowCount; got != 2 {
|
||
t.Fatalf("wiki_pages rows=%d", got)
|
||
}
|
||
if !hasWikiSnapshot(bundle.Postgres) {
|
||
t.Fatal("bundle must carry a wiki snapshot")
|
||
}
|
||
sealed := false
|
||
for _, table := range bundle.Manifest.Tables {
|
||
sealed = sealed || table.Name == "wiki_pages"
|
||
}
|
||
if !sealed {
|
||
t.Fatalf("manifest=%#v", bundle.Manifest.Tables)
|
||
}
|
||
}
|
||
|
||
func TestDocumentExporterRetractsWithdrawnDocumentFromWiki(t *testing.T) {
|
||
wikiExporter := &stubPGWikiExporter{wikiTables: wikiTablesFor(t, "entity/alpha")}
|
||
if _, _ = wikiCarryingBundle(t, wikiExporter, "delete"); wikiExporter.removing != "knowledge-safe" {
|
||
t.Fatalf("removing=%q", wikiExporter.removing)
|
||
}
|
||
}
|
||
|
||
func TestDocumentExporterSkipsWikiWhenKnowledgeBaseHasNone(t *testing.T) {
|
||
wikiExporter := &stubPGWikiExporter{}
|
||
bundle, _ := wikiCarryingBundle(t, wikiExporter, "upsert")
|
||
if hasWikiSnapshot(bundle.Postgres) {
|
||
t.Fatalf("unexpected wiki snapshot: %#v", bundle.Postgres.Tables)
|
||
}
|
||
}
|
||
|
||
func TestWeKnoraVerifierChecksWikiPageSet(t *testing.T) {
|
||
bundle, item := wikiCarryingBundle(t, &stubPGWikiExporter{wikiTables: wikiTablesFor(t, "entity/alpha", "index")}, "upsert")
|
||
|
||
client := &verificationClient{wikiPages: []weknora.WikiPageSummary{{Slug: "entity/alpha"}, {Slug: "index"}}, wikiTotal: 2}
|
||
verifier, _ := NewWeKnoraVerifier(client)
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
|
||
// 跨来源共库:目标库多出来的"外来源"页面(不引用本包知识)由写入侧按设计保留,
|
||
// 校验侧必须放行,否则 2026-10-10 那次跨环境发布会被误判(in_scope=0 / foreign=8)。
|
||
client.wikiPages = append(client.wikiPages, weknora.WikiPageSummary{Slug: "entity/foreign", SourceRefs: []string{"knowledge-other|b.md"}})
|
||
client.wikiTotal = 3
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err != nil {
|
||
t.Fatalf("foreign-owned production wiki page rejected: %v", err)
|
||
}
|
||
|
||
// 本包拥有(引用本包知识)却不在快照里的页面,仍然必须判失败。
|
||
client.wikiPages = append(client.wikiPages, weknora.WikiPageSummary{Slug: "entity/stale", SourceRefs: []string{"knowledge-safe|a.md"}})
|
||
client.wikiTotal = 4
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err == nil {
|
||
t.Fatal("package-owned stale production wiki page accepted")
|
||
}
|
||
|
||
// 无 source_refs 的页面无法判定归属,按"外来源"放行(写入侧同样不碰它)。
|
||
client.wikiPages = []weknora.WikiPageSummary{{Slug: "entity/alpha"}, {Slug: "index"}, {Slug: "entity/unrelated"}}
|
||
client.wikiTotal = 3
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err != nil {
|
||
t.Fatalf("unowned production wiki page rejected: %v", err)
|
||
}
|
||
|
||
client.wikiPages = []weknora.WikiPageSummary{{Slug: "entity/alpha"}}
|
||
client.wikiTotal = 1
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err == nil {
|
||
t.Fatal("missing production wiki page accepted")
|
||
}
|
||
}
|
||
|
||
func TestWeKnoraVerifierRejectsProductionWikiReferringToWithdrawnDocument(t *testing.T) {
|
||
bundle, item := wikiCarryingBundle(t, &stubPGWikiExporter{wikiTables: wikiTablesFor(t, "entity/shared")}, "delete")
|
||
item.KnowledgeID = "knowledge-safe"
|
||
client := &verificationClient{
|
||
wikiPages: []weknora.WikiPageSummary{{Slug: "entity/shared", SourceRefs: []string{"knowledge-other|b.md"}}},
|
||
wikiTotal: 1,
|
||
}
|
||
verifier, _ := NewWeKnoraVerifier(client)
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
client.wikiPages[0].SourceRefs = []string{"knowledge-other|b.md", "knowledge-safe|a.md"}
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err == nil {
|
||
t.Fatal("production wiki still citing the withdrawn document accepted")
|
||
}
|
||
}
|
||
|
||
func TestWeKnoraVerifierSkipsWikiForBundlesWithoutSnapshot(t *testing.T) {
|
||
bundle := testDocumentBundle(t)
|
||
client := &verificationClient{wikiErr: errUnexpectedWikiCall()}
|
||
verifier, _ := NewWeKnoraVerifier(client)
|
||
if err := verifier.VerifyWiki(context.Background(), releasePlanForArchive(), bundle); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
}
|
||
|
||
func errUnexpectedWikiCall() error { return os.ErrInvalid }
|
||
|
||
func TestDocumentTargetAppliesWikiSnapshotWhenWithdrawing(t *testing.T) {
|
||
root := t.TempDir()
|
||
_ = os.Chmod(root, 0o700)
|
||
archive, _ := NewBundleArchive(root, 16*1024*1024)
|
||
bundle, item := wikiCarryingBundle(t, &stubPGWikiExporter{wikiTables: wikiTablesFor(t, "entity/shared")}, "delete")
|
||
if err := archive.Save(bundle); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
calls := []string{}
|
||
target, err := NewDocumentTarget(DocumentTargetConfig{
|
||
Archive: archive, Contract: &stubContractInspector{contract: targetContractForBundle(bundle)},
|
||
Postgres: &stubPostgresTarget{calls: &calls}, Search: &stubSearchTarget{calls: &calls},
|
||
Objects: &stubObjectTarget{calls: &calls}, Verifier: &stubProductionVerifier{calls: &calls},
|
||
})
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
revision, err := target.Restore(context.Background(), item, "absent")
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if revision != "absent" {
|
||
t.Fatalf("revision=%q", revision)
|
||
}
|
||
if err := target.VerifyRestored(context.Background(), item, "absent"); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
want := []string{"postgres-disable", "search-disable", "postgres-replace-wiki", "verify-disabled", "verify-wiki:review-v1"}
|
||
if !reflect.DeepEqual(calls, want) {
|
||
t.Fatalf("calls=%v\nwant=%v", calls, want)
|
||
}
|
||
}
|