8febe12d7d
真实本机 PG/ES/MinIO:v071 目录镜像 10 模式、结构引用/游标 15 模式、 8 表×2 外部范围身份矩阵、13 模式自然键(含软删除与合法同 parent 正例)、 备份期间三注入、收尾故障与授权撤销两个真实治理 PG 用例。 需隔离服务的用例在 test-scope.json 显式声明:普通 CI 跳过不算通过。
171 lines
6.3 KiB
Go
171 lines
6.3 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"knowledge-governance/internal/librarydeadletter"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
func TestLibraryExportIncludesEmptyAndCompleteWiki(t *testing.T) {
|
|
db := newLibraryFixture()
|
|
snapshot, err := ExportLibrary(context.Background(), db, PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(snapshot.Tables) != 10 {
|
|
t.Fatalf("table count=%d, want all ten content tables including empty ones", len(snapshot.Tables))
|
|
}
|
|
for _, name := range []string{"knowledges", "chunks", "resources", "resource_bindings", "knowledge_tags", "knowledge_tag_relations", "wiki_pages", "wiki_folders", "wiki_page_issues", "wiki_log_entries"} {
|
|
if !tablePresent(snapshot, name) {
|
|
t.Fatalf("missing table %s", name)
|
|
}
|
|
}
|
|
if db.options.IsoLevel != pgx.RepeatableRead || db.options.AccessMode != pgx.ReadOnly {
|
|
t.Fatalf("unsafe snapshot options: %+v", db.options)
|
|
}
|
|
if !db.tx.committed {
|
|
t.Fatal("snapshot transaction not committed")
|
|
}
|
|
for _, q := range db.tx.queries {
|
|
for _, forbidden := range []string{"from users", "from sessions", "from api_keys", "from messages"} {
|
|
if strings.Contains(strings.ToLower(q), forbidden) {
|
|
t.Fatalf("nonknowledge export: %s", forbidden)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLibraryExportBlocksUnsettledWorkIncludingDeletedDocumentRetracts(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
setup func(*libraryFixtureTx)
|
|
}{
|
|
{"document failed or parsing", func(d *libraryFixtureTx) { d.unsettled = 1 }},
|
|
{"deleted document retract or finalizer", func(d *libraryFixtureTx) { d.pending = 1 }},
|
|
{"dead letter not semantic success", func(d *libraryFixtureTx) { d.failed = 1 }},
|
|
{"unsupported graph data", func(d *libraryFixtureTx) { d.graph = true }},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
db := newLibraryFixture()
|
|
test.setup(db.tx)
|
|
_, err := ExportLibrary(context.Background(), db, PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"})
|
|
if err == nil {
|
|
t.Fatal("unsettled library was exported")
|
|
}
|
|
if db.tx.committed {
|
|
t.Fatal("blocked snapshot committed")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLibraryExportDoesNotReturnPartialSnapshot(t *testing.T) {
|
|
db := newLibraryFixture()
|
|
db.tx.queryError = true
|
|
got, err := ExportLibrary(context.Background(), db, PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"})
|
|
if err == nil || len(got.Tables) != 0 || db.tx.committed {
|
|
t.Fatal("partial snapshot escaped")
|
|
}
|
|
}
|
|
|
|
type libraryFixture struct {
|
|
options pgx.TxOptions
|
|
tx *libraryFixtureTx
|
|
}
|
|
type libraryFixtureTx struct {
|
|
pgx.Tx
|
|
queries []string
|
|
committed bool
|
|
sourcePending int64
|
|
pending, failed, unsettled int64
|
|
graph bool
|
|
queryError bool
|
|
}
|
|
|
|
func newLibraryFixture() *libraryFixture { return &libraryFixture{tx: &libraryFixtureTx{}} }
|
|
func (d *libraryFixture) BeginTx(_ context.Context, o pgx.TxOptions) (pgx.Tx, error) {
|
|
d.options = o
|
|
return d.tx, nil
|
|
}
|
|
func (d *libraryFixtureTx) Commit(context.Context) error { d.committed = true; return nil }
|
|
func (d *libraryFixtureTx) Rollback(context.Context) error { return nil }
|
|
func (d *libraryFixtureTx) QueryRow(_ context.Context, q string, _ ...any) pgx.Row {
|
|
d.queries = append(d.queries, q)
|
|
return libraryFixtureRow{d: d, sql: q}
|
|
}
|
|
func (d *libraryFixtureTx) Query(_ context.Context, q string, _ ...any) (pgx.Rows, error) {
|
|
d.queries = append(d.queries, q)
|
|
if d.queryError {
|
|
return nil, errors.New("fixture failure")
|
|
}
|
|
// 死信行按真实形状返回:解除判定必须逐字节比对指纹,空结果会让"存在即阻断"失效。
|
|
if strings.Contains(q, "to_jsonb(d)") && d.failed > 0 {
|
|
return &jsonRows{values: []json.RawMessage{json.RawMessage(fixtureDeadLetter)}}, nil
|
|
}
|
|
return &jsonRows{values: []json.RawMessage{}}, nil
|
|
}
|
|
|
|
// fixtureDeadLetter 与 PostgreSQL to_jsonb(task_dead_letters) 的键序/形状一致。
|
|
const fixtureDeadLetter = `{"id": 1, "payload": {"knowledge_id": "deleted-doc"}, "scope": "knowledge", "scope_id": "deleted-doc", "failed_at": "2026-10-28T10:00:00+00:00", "last_error": "llm timeout", "task_type": "wiki:ingest", "tenant_id": 10000, "fail_count": 3, "related_id": "deleted-doc"}`
|
|
|
|
type libraryFixtureRow struct {
|
|
d *libraryFixtureTx
|
|
sql string
|
|
}
|
|
|
|
func (r libraryFixtureRow) Scan(dest ...any) error {
|
|
switch {
|
|
case strings.Contains(r.sql, "library-relational-closure"), strings.Contains(r.sql, "library-resource-owner-closure"):
|
|
*dest[0].(*bool) = false
|
|
case strings.Contains(r.sql, "jsonb_agg(to_jsonb(q))"):
|
|
*dest[0].(*[]byte) = []byte(`[]`)
|
|
if strings.Contains(r.sql, "relrowsecurity") {
|
|
rows := []map[string]any{}
|
|
for _, table := range productionLibraryTables {
|
|
rows = append(rows, map[string]any{"table_name": table, "kind": "r"})
|
|
}
|
|
*dest[0].(*[]byte), _ = json.Marshal(rows)
|
|
}
|
|
case strings.Contains(r.sql, "library-source-settlement"):
|
|
*dest[0].(*int64) = r.d.unsettled
|
|
*dest[1].(*int64) = r.d.pending + r.d.sourcePending
|
|
*dest[2].(*int64) = r.d.failed
|
|
case strings.Contains(r.sql, "schema_migrations"):
|
|
*dest[0].(*int) = 74
|
|
*dest[1].(*bool) = false
|
|
case strings.Contains(r.sql, "indexing_strategy"):
|
|
*dest[0].(*bool) = r.d.graph
|
|
default:
|
|
*dest[0].(*int64) = r.d.unsettled
|
|
*dest[1].(*int64) = r.d.pending
|
|
*dest[2].(*int64) = r.d.failed
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 解除只能按精确指纹生效:同一条死信换个字节(或没有解除)都必须继续阻断。
|
|
func TestLibraryExportReleasesOnlyExactDeadLetterFingerprint(t *testing.T) {
|
|
db := newLibraryFixture()
|
|
db.tx.failed = 1
|
|
identity := PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}
|
|
if _, err := ExportLibrary(context.Background(), db, identity); err == nil {
|
|
t.Fatal("unreleased dead letter did not block the export")
|
|
}
|
|
if _, err := ExportLibraryWithReleases(context.Background(), db, identity,
|
|
[]string{"sha256:0000000000000000000000000000000000000000000000000000000000000000"}); err == nil {
|
|
t.Fatal("a stale fingerprint released the dead letter")
|
|
}
|
|
released := librarydeadletter.Fingerprint([]byte(fixtureDeadLetter))
|
|
if _, err := ExportLibraryWithReleases(context.Background(), db, identity, []string{released}); err != nil {
|
|
t.Fatalf("exact released fingerprint still blocked: %v", err)
|
|
}
|
|
if !db.tx.committed {
|
|
t.Fatal("released export did not commit its snapshot")
|
|
}
|
|
}
|