154 lines
5.9 KiB
Go
154 lines
5.9 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
// Live read-only diagnostic, not a cross-system frozen version or release gate.
|
|
// Credentials are injected into this process only; never logged or persisted.
|
|
func TestLibrarySourceReadOnlyDiagnostic(t *testing.T) {
|
|
if os.Getenv("KG_LIBRARY_SOURCE_READONLY") != "1" {
|
|
t.Skip("requires explicit read-only test-source credentials")
|
|
}
|
|
var config struct {
|
|
DatabaseURL string
|
|
TenantID uint64
|
|
Scopes map[string]string
|
|
Search SearchExporterConfig
|
|
Object S3ObjectStoreConfig
|
|
}
|
|
if json.Unmarshal([]byte(os.Getenv("KG_LIBRARY_SOURCE_CONFIG")), &config) != nil || config.TenantID == 0 || len(config.Scopes) != 2 || config.Scopes["internal"] == "" || config.Scopes["external"] == "" {
|
|
t.Fatal("invalid read-only diagnostic configuration")
|
|
}
|
|
cfg, err := pgxpool.ParseConfig(config.DatabaseURL)
|
|
if err != nil {
|
|
t.Fatal("invalid diagnostic PG configuration")
|
|
}
|
|
cfg.ConnConfig.RuntimeParams["default_transaction_read_only"] = "on"
|
|
cfg.ConnConfig.RuntimeParams["statement_timeout"] = "30000"
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
|
defer cancel()
|
|
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatal("open diagnostic PG")
|
|
}
|
|
defer db.Close()
|
|
var readOnly string
|
|
if err = db.QueryRow(ctx, "SHOW default_transaction_read_only").Scan(&readOnly); err != nil || readOnly != "on" {
|
|
t.Fatal("read-only session not enforced")
|
|
}
|
|
if err = checkLibrarySourceSettlement(ctx, db); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
search, err := NewSearchExporter(config.Search)
|
|
if err != nil {
|
|
t.Fatal("invalid diagnostic search configuration")
|
|
}
|
|
objects, err := NewS3ObjectStore(config.Object)
|
|
if err != nil {
|
|
t.Fatal("invalid diagnostic object configuration")
|
|
}
|
|
for _, scope := range []string{"internal", "external"} {
|
|
t.Run(scope, func(t *testing.T) {
|
|
identity := PostgresIdentity{TenantID: config.TenantID, KnowledgeBaseID: config.Scopes[scope]}
|
|
pg, err := ExportLibrary(ctx, db, identity)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
es, err := search.ExportLibrary(ctx, SearchIdentity{TenantID: identity.TenantID, KnowledgeBaseID: identity.KnowledgeBaseID}, 100000)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = validateLibraryCandidate(identity, pg, es); err != nil {
|
|
missing, deleted, wrongParent := 0, 0, 0
|
|
reasons := map[string]int{}
|
|
chunks := map[string]libraryRow{}
|
|
for _, raw := range pg.Table("chunks").Rows {
|
|
var r libraryRow
|
|
if json.Unmarshal(raw, &r) != nil {
|
|
t.Fatal("invalid diagnostic chunk")
|
|
}
|
|
chunks[r.ID] = r
|
|
}
|
|
for _, doc := range es.Documents {
|
|
var r libraryRow
|
|
if json.Unmarshal(doc.Source, &r) != nil {
|
|
t.Fatal("invalid diagnostic index")
|
|
}
|
|
if c, ok := chunks[r.ChunkID]; ok {
|
|
if r.KnowledgeID != c.KnowledgeID {
|
|
wrongParent++
|
|
}
|
|
continue
|
|
}
|
|
var exists bool
|
|
if e := db.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM chunks WHERE id=$1 AND tenant_id=$2)`, r.ChunkID, identity.TenantID).Scan(&exists); e != nil {
|
|
t.Fatal("diagnostic relation query failed")
|
|
}
|
|
if exists {
|
|
deleted++
|
|
var reason string
|
|
if e := db.QueryRow(ctx, `SELECT CASE WHEN c.deleted_at IS NOT NULL THEN 'chunk_soft_deleted' WHEN k.id IS NULL THEN 'parent_missing' WHEN k.deleted_at IS NOT NULL THEN 'parent_soft_deleted' ELSE 'scope_mismatch' END FROM chunks c LEFT JOIN knowledges k ON k.id=c.knowledge_id AND k.tenant_id=c.tenant_id WHERE c.id=$1 AND c.tenant_id=$2`, r.ChunkID, identity.TenantID).Scan(&reason); e != nil {
|
|
t.Fatal("diagnostic exclusion query failed")
|
|
}
|
|
reasons[reason]++
|
|
} else {
|
|
missing++
|
|
}
|
|
}
|
|
t.Errorf("candidate rejected: %v; index_records=%d absent_chunk_records=%d existing_but_excluded_chunk_records=%d wrong_parent_records=%d", err, len(es.Documents), missing, deleted, wrongParent)
|
|
t.Logf("index exclusion reasons: %v", reasons)
|
|
probe, e := NewObjectExporter(objects, ObjectExporterLimits{MaxObjectBytes: 64 * 1024 * 1024, MaxTotalBytes: 256 * 1024 * 1024})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
assets, e := probe.Export(ctx, pg)
|
|
if e != nil {
|
|
t.Errorf("independent read-only resource probe: %v", e)
|
|
} else {
|
|
t.Logf("independent resource probe objects=%d bytes=%d", len(assets.Objects), assets.TotalBytes)
|
|
}
|
|
return
|
|
}
|
|
t.Logf("READONLY_DIAGNOSTIC frozen=false scope=%s documents=%d chunks=%d resources=%d wiki_pages=%d index_records=%d", scope, pg.Table("knowledges").RowCount, pg.Table("chunks").RowCount, pg.Table("resources").RowCount, pg.Table("wiki_pages").RowCount, len(es.Documents))
|
|
root := t.TempDir()
|
|
if err = os.Chmod(root, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
archive, err := librarysnapshot.Open(root, 64*1024*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer archive.Close()
|
|
// The model name is an explicit diagnostic placeholder, not deployment compatibility evidence.
|
|
compatibility := librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "diagnostic-unverified-model", EmbeddingDimension: 1024, IndexMappingSHA256: es.MappingSHA256}
|
|
id, err := ArchiveLibraryCandidate(ctx, archive, scope, identity, compatibility, pg, es, objects, time.Now().UTC())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
manifest, err := archive.Load(id)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var count, bytes int64
|
|
for _, part := range manifest.Parts {
|
|
if part.Kind == "resource" {
|
|
count++
|
|
bytes += part.Bytes
|
|
}
|
|
}
|
|
if count != int64(pg.Table("resources").RowCount) {
|
|
t.Fatal("resource archive coverage mismatch")
|
|
}
|
|
t.Logf("READONLY_DIAGNOSTIC frozen=false scope=%s archived_resources=%d resource_bytes=%d archive_verified=true compatibility_verified=false", scope, count, bytes)
|
|
})
|
|
}
|
|
}
|