Files
knowledge-governance/internal/releaseadapter/v071/library_export_test.go
T
姚章浩 bf049cee1f
build / build (push) Successful in 1m12s
fix(snapshot): 暂停前后强制检查共享源全库任务
2026-10-28 01:05:44 +08:00

131 lines
4.1 KiB
Go

package v071
import (
"context"
"encoding/json"
"errors"
"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")
}
return &jsonRows{values: []json.RawMessage{}}, nil
}
type libraryFixtureRow struct {
d *libraryFixtureTx
sql string
}
func (r libraryFixtureRow) Scan(dest ...any) error {
switch {
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
}