ea6e56b304
发布通道原来只有三条:PostgreSQL 关系行、Elasticsearch 分片、OSS 对象。 WeKnora 的 wiki 图谱是 wiki_pages 一张纯 PG 表(in_links/out_links 存 slug), 所以它不在任何通道里——撤权后生产 wiki 会一直留着已撤权文档的页面与引用。 本次新增第 5 条通道:库级 wiki 快照,挂在现有文档发布包里,检索与 wiki 同一个 release 单元(同一次执行、同一次校验、同一次回滚): - 导出:ExportWiki 按 knowledge_base_id 全量读 wiki_pages/wiki_folders; 收敛门禁(库内无解析中/合成中文档)不满足直接失败并给中文原因; 未启用 wiki 且无 wiki 行的库不参与同步(生产 wiki 保持原样)。 - 撤权:item.Action == "delete" 时在导出端剥离该文档的 wiki 贡献 (source_refs/chunk_refs 去条目、无引用页面整页不带、摘死链、清 parent_slug、 无页面引用的空文件夹及其祖先一并去掉),使“源端仍在只停止发布”和 “源端删除”两种情况收敛到同一结果。 - 导入:库级 replace-by-kb(同一事务内 DELETE by kb 后灌入快照,文件夹先于页面), 新增 ReplaceWiki 供撤权路径使用(文档行只 disable,wiki 整体替换)。 - 校验:VerifyWiki 用生产 WeKnora wiki API 比对页面 slug 集合,撤权时额外断言 生产 wiki 不再引用被撤权文档。 驱动与验收:新增 cmd/release-channel-e2e(复用发布 worker 真实代码路径, 支持 publish/withdraw/rollback),scripts/wiki_e2e_test5.sh、 scripts/wiki_e2e_newkb.sh 两套端到端脚本;证据见 docs/WIKI_CO_TRANSPORT_ACCEPTANCE.md。
188 lines
7.3 KiB
Go
188 lines
7.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)
|
||
}
|
||
|
||
client.wikiPages = append(client.wikiPages, weknora.WikiPageSummary{Slug: "entity/stale"})
|
||
client.wikiTotal = 3
|
||
if err := verifier.VerifyWiki(context.Background(), item, bundle); err == nil {
|
||
t.Fatal("stale production wiki page accepted")
|
||
}
|
||
|
||
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)
|
||
}
|
||
}
|