c2402947a9
build / build (push) Successful in 1m26s
上游 task_dead_letters 没有解决标记,purge/DELETE 会销毁"失败过"的唯一证据。
治理侧新增解除记录(迁移 073):绑定实时行逐字节指纹、重放引用、源侧观测摘要与
操作人;行被改过、重放后又出现新死信、源侧未结清都不命中,封存照旧阻断。
指纹先压紧空白再摘要,避免 PG to_jsonb::text 与 json.Marshal 的空白差异。
试过:go test ./internal/{librarydeadletter,libraryagent,httpapi,releaseadapter/v071,
platform/postgres},make verify 15 项全绿。
160 lines
5.8 KiB
Go
160 lines
5.8 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-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")
|
|
}
|
|
}
|