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

211 lines
8.8 KiB
Go

package v071
import (
"context"
"encoding/json"
"os"
"strings"
"testing"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
// This fixture creates only synthetic content in an explicitly named disposable
// database. Never point this test at an application database.
func TestLibraryExportIsolatedPostgres(t *testing.T) {
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
if dsn == "" {
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
}
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") || os.Getenv("KG_LIBRARY_TEST_ISOLATED") != "1" {
t.Fatal("isolated database required")
}
ctx := context.Background()
db, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
t.Fatal(err)
}
defer db.Close()
if _, err = db.Exec(ctx, libraryTestSchema); err != nil {
t.Fatal(err)
}
scope := PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}
run := func() PostgresSnapshot {
t.Helper()
s, e := ExportLibrary(ctx, db, scope)
if e != nil {
t.Fatal(e)
}
return s
}
snap := run()
if snap.Table("knowledges").RowCount != 1 || snap.Table("wiki_pages").RowCount != 1 || snap.Table("resources").RowCount != 1 {
t.Fatal("incorrect scoped export")
}
if snap.Table("wiki_log_entries").RowCount != 1 || snap.Table("wiki_page_issues").RowCount != 1 {
t.Fatal("Wiki associated data lost")
}
for _, tab := range snap.Tables {
for _, raw := range tab.Rows {
if strings.Contains(string(raw), "other-scope") {
t.Fatalf("cross scope leak in %s", tab.Name)
}
}
}
t.Run("whole source settlement", func(t *testing.T) {
if err := checkLibrarySourceSettlement(ctx, db); err != nil {
t.Fatal(err)
}
for _, tc := range []struct{ name, change, restore string }{
{"other tenant document", `UPDATE knowledges SET parse_status='processing' WHERE id='other-scope'`, `UPDATE knowledges SET parse_status='completed' WHERE id='other-scope'`},
{"other library pending", `INSERT INTO task_pending_ops VALUES(20000,'wiki:ingest','knowledge_base','other-library')`, `DELETE FROM task_pending_ops`},
{"deleted scope pending", `INSERT INTO task_pending_ops VALUES(20000,'wiki:retract','knowledge','physically-deleted-doc')`, `DELETE FROM task_pending_ops`},
{"unknown scope pending", `INSERT INTO task_pending_ops VALUES(20000,'unknown:task','unknown','unknown')`, `DELETE FROM task_pending_ops`},
{"other tenant dead letter", `INSERT INTO task_dead_letters VALUES(20000,'document:process','knowledge_base','other-library')`, `DELETE FROM task_dead_letters`},
} {
t.Run(tc.name, func(t *testing.T) {
if _, err := db.Exec(ctx, tc.change); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if _, err := db.Exec(ctx, tc.restore); err != nil {
t.Error(err)
}
})
if _, err := ExportLibrary(ctx, db, scope); err != nil {
t.Fatalf("selected library unexpectedly busy: %v", err)
}
if err := checkLibrarySourceSettlement(ctx, db); err == nil {
t.Fatal("source maintenance allowed while another scope has unfinished work")
}
})
}
if err := checkLibrarySourceSettlement(ctx, db); err != nil {
t.Fatalf("settled source did not recover: %v", err)
}
})
t.Run("MVCC concurrent update", func(t *testing.T) {
wrapper := libraryHookDB{pool: db, hook: func() {
if _, err := db.Exec(ctx, `UPDATE wiki_pages SET content='new-content' WHERE id='wiki-safe'`); err != nil {
t.Fatal(err)
}
}}
old, err := ExportLibrary(ctx, wrapper, scope)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(old.Table("wiki_pages").Rows[0]), "old-content") {
t.Fatal("mixed PostgreSQL versions during export")
}
fresh := run()
if !strings.Contains(string(fresh.Table("wiki_pages").Rows[0]), "new-content") {
t.Fatal("concurrent writer did not actually commit")
}
})
t.Run("deleted knowledge tasks", func(t *testing.T) {
for _, test := range []struct{ table, task, scope, id string }{
{"task_pending_ops", "wiki:ingest", "knowledge_base", "kb-safe"},
{"task_pending_ops", "wiki:finalize", "knowledge_base", "kb-safe"},
{"task_dead_letters", "wiki:ingest", "knowledge", "deleted-doc"},
} {
_, err := db.Exec(ctx, `INSERT INTO `+test.table+`(tenant_id,task_type,scope,scope_id) VALUES(10000,$1,$2,$3)`, test.task, test.scope, test.id)
if err != nil {
t.Fatal(err)
}
if _, err = ExportLibrary(ctx, db, scope); err == nil {
t.Fatalf("ignored %s %s", test.table, test.task)
}
if _, err = db.Exec(ctx, `DELETE FROM `+test.table); err != nil {
t.Fatal(err)
}
}
})
t.Run("dangling resource blocks", func(t *testing.T) {
_, err := db.Exec(ctx, `UPDATE wiki_pages SET content='resource://abcdefghijklmnopqrstuv' WHERE id='wiki-safe'`)
if err != nil {
t.Fatal(err)
}
if _, err = ExportLibrary(ctx, db, scope); err == nil {
t.Fatal("accepted missing Wiki resource dependency")
}
if _, err = db.Exec(ctx, `UPDATE wiki_pages SET content='new-content' WHERE id='wiki-safe'`); err != nil {
t.Fatal(err)
}
})
t.Run("empty library has explicit empty tables", func(t *testing.T) {
s, err := ExportLibrary(ctx, db, PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-empty"})
if err != nil {
t.Fatal(err)
}
if len(s.Tables) != 10 {
t.Fatal("empty library snapshot incomplete")
}
for _, tab := range s.Tables {
if tab.RowCount != 0 {
t.Fatal("empty library leaked content")
}
}
})
// The exporter may not mutate content: only fixture updates above are visible.
var count int
if err := db.QueryRow(ctx, `SELECT count(*) FROM sessions`).Scan(&count); err != nil || count != 1 {
t.Fatal("nonknowledge data changed")
}
encoded, err := json.Marshal(snap)
if err != nil || strings.Contains(string(encoded), "private-session-marker") {
t.Fatal("nonknowledge data exported")
}
}
type libraryHookDB struct {
pool *pgxpool.Pool
hook func()
}
func (d libraryHookDB) BeginTx(ctx context.Context, o pgx.TxOptions) (pgx.Tx, error) {
tx, err := d.pool.BeginTx(ctx, o)
return &libraryHookTx{Tx: tx, hook: d.hook}, err
}
type libraryHookTx struct {
pgx.Tx
hook func()
fired bool
}
func (d *libraryHookTx) Query(ctx context.Context, q string, args ...any) (pgx.Rows, error) {
if !d.fired {
d.fired = true
d.hook()
}
return d.Tx.Query(ctx, q, args...)
}
const libraryTestSchema = `
CREATE TABLE schema_migrations(version int,dirty bool); INSERT INTO schema_migrations VALUES(74,false);
CREATE TABLE knowledge_bases(id text,tenant_id bigint,indexing_strategy jsonb,deleted_at timestamptz);
INSERT INTO knowledge_bases VALUES('kb-safe',10000,'{}',NULL),('kb-empty',10000,'{}',NULL);
CREATE TABLE knowledges(id text PRIMARY KEY,tenant_id bigint,knowledge_base_id text,parse_status text,pending_subtasks_count int,file_path text,deleted_at timestamptz);
INSERT INTO knowledges VALUES('doc-safe',10000,'kb-safe','completed',0,'resource://ABCDEFGHIJKLMNOPQRSTUV',NULL),('other-scope',20000,'kb-safe','completed',0,'',NULL),('deleted-doc',10000,'kb-safe','completed',0,'',now());
CREATE TABLE chunks(id text,tenant_id bigint,knowledge_base_id text,knowledge_id text,content text,deleted_at timestamptz);
INSERT INTO chunks VALUES('chunk-safe',10000,'kb-safe','doc-safe','chunk',NULL),('other-scope',20000,'kb-safe','other-scope','secret',NULL);
CREATE TABLE knowledge_tags(id text,tenant_id bigint,knowledge_base_id text,deleted_at timestamptz);
CREATE TABLE knowledge_tag_relations(knowledge_id text,tag_id text);
CREATE TABLE resources(id text,handle text,tenant_id bigint,physical_path text,deleted_at timestamptz);
INSERT INTO resources VALUES('res-safe','ABCDEFGHIJKLMNOPQRSTUV',10000,'source.txt',NULL),('other-scope','abcdefghijklmnopqrstuv',20000,'other-scope',NULL);
CREATE TABLE resource_bindings(id text,tenant_id bigint,resource_id text,owner_type text,owner_id text);
INSERT INTO resource_bindings VALUES('binding-safe',10000,'res-safe','knowledge','doc-safe');
CREATE TABLE wiki_pages(id text,tenant_id bigint,knowledge_base_id text,slug text,content text,deleted_at timestamptz);
INSERT INTO wiki_pages VALUES('wiki-safe',10000,'kb-safe','index','old-content',NULL),('other-scope',20000,'kb-safe','secret','other-scope',NULL);
CREATE TABLE wiki_folders(id text,tenant_id bigint,knowledge_base_id text,deleted_at timestamptz);
CREATE TABLE wiki_page_issues(id text,tenant_id bigint,knowledge_base_id text,deleted_at timestamptz);
INSERT INTO wiki_page_issues VALUES('issue-safe',10000,'kb-safe',NULL);
CREATE TABLE wiki_log_entries(id bigint,tenant_id bigint,knowledge_base_id text);
INSERT INTO wiki_log_entries VALUES(1,10000,'kb-safe');
CREATE TABLE task_pending_ops(tenant_id bigint,task_type text,scope text,scope_id text);
CREATE TABLE task_dead_letters(tenant_id bigint,task_type text,scope text,scope_id text);
CREATE TABLE sessions(marker text); INSERT INTO sessions VALUES('private-session-marker');
`