Files
knowledge-governance/internal/releaseadapter/v071/data_admission_fixture_r10_test.go
T
姚章浩 8febe12d7d test(library): 隔离数据契约与 r11 身份自然键矩阵
真实本机 PG/ES/MinIO:v071 目录镜像 10 模式、结构引用/游标 15 模式、
8 表×2 外部范围身份矩阵、13 模式自然键(含软删除与合法同 parent 正例)、
备份期间三注入、收尾故障与授权撤销两个真实治理 PG 用例。

需隔离服务的用例在 test-scope.json 显式声明:普通 CI 跳过不算通过。
2026-11-01 11:35:09 +08:00

83 lines
4.4 KiB
Go

package v071
import (
"context"
"net/http/httptest"
"os"
"testing"
"github.com/jackc/pgx/v5/pgxpool"
"knowledge-governance/internal/appconfig"
"knowledge-governance/internal/objectgateway"
postgresstore "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/platform/postgres/pgtest"
)
// Exported only into the test binary, so the external-package test exercises
// librarystack.OpenProductionStack without an import cycle or duplicated fixture.
type R10AdmissionFixture struct {
Context context.Context
Config appconfig.Config
Snapshot string
Exec func(string)
State func() map[string]any
Unchanged func(map[string]any)
Save func(string, any)
}
func OpenR10AdmissionFixture(t *testing.T) R10AdmissionFixture {
t.Helper()
fx := openWriteSetFixtureWith(t, writeSetFixtureOptions{reset: true, embeddingDimension: 1024})
seedTargetData(t, fx)
r10Exec(t, fx, `ALTER TABLE knowledge_bases ADD COLUMN embedding_model_id text; ALTER TABLE knowledge_bases ADD COLUMN vector_store_id text;
CREATE TABLE models(id text PRIMARY KEY,name text,parameters jsonb,deleted_at timestamptz);
CREATE TABLE vector_stores(id text PRIMARY KEY,engine_type text,index_config jsonb,deleted_at timestamptz);
INSERT INTO models VALUES('r10-model','text-embedding-v4','{"embedding_parameters":{"dimension":1024}}',NULL);
INSERT INTO vector_stores VALUES('r10-vector','elasticsearch_v8','{}',NULL);
UPDATE knowledge_bases SET embedding_model_id='r10-model',vector_store_id='r10-vector'`)
if _, e := fx.pool.Exec(fx.ctx, `UPDATE vector_stores SET index_config=jsonb_build_object('index_name',$1::text)`, fx.index); e != nil {
t.Fatal(e)
}
dsn := pgtest.IsolatedDatabaseURL(t, os.Getenv("KG_LIBRARY_TEST_DATABASE_URL"))
governancePool, e := pgxpool.New(fx.ctx, dsn)
if e != nil {
t.Fatal(e)
}
t.Cleanup(governancePool.Close)
if e = postgresstore.ApplyMigrations(fx.ctx, governancePool); e != nil {
t.Fatal(e)
}
if _, e = governancePool.Exec(fx.ctx, objectgateway.Schema); e != nil {
t.Fatal(e)
}
reader := gatewayReadOnlyGovernance(t, fx.ctx, governancePool, dsn)
authority := &GatewayGovernanceAuthority{Pool: reader, ArchiveRoot: fx.archiveRoot, TargetID: "r10-admission", Provider: "minio", Bucket: fx.bucket}
endpoint := os.Getenv("KG_LIBRARY_TEST_S3_ENDPOINT")
scope := objectgateway.Digest([]byte("minio\x00" + endpoint + "\x00" + fx.bucket))
gateway := &objectgateway.Gateway{Pool: governancePool, Scope: scope, Backend: &objectgateway.S3Backend{Client: fx.objects.s3.client, Bucket: fx.bucket, MaxBytes: 1 << 20}, Authority: authority, Isolation: gatewayFixtureIsolation{}, MaxBytes: 1 << 20}
token := objectgateway.Digest([]byte("r10-admission-data"))
server := httptest.NewServer(objectgateway.HTTPServer{Gateway: gateway, DataToken: token, ControlToken: objectgateway.Digest([]byte("r10-admission-control"))})
t.Cleanup(server.Close)
config := appconfig.Config{LibraryProductionDatabaseURL: os.Getenv("KG_LIBRARY_TEST_DATABASE_URL"), LibraryProductionElasticURL: fx.searchBase, LibraryProductionIndex: fx.index, LibraryProductionTenantID: wsTargetTenant, LibraryProductionInternalKB: wsTargetKB, LibraryProductionS3Endpoint: endpoint, LibraryProductionS3Provider: "minio", LibraryProductionS3Bucket: fx.bucket, LibraryProductionS3AccessKey: os.Getenv("KG_LIBRARY_TEST_S3_ACCESS_KEY"), LibraryProductionS3SecretKey: os.Getenv("KG_LIBRARY_TEST_S3_SECRET_KEY"), LibraryArchiveRoot: fx.archiveRoot, LibraryObjectGatewayURL: server.URL, LibraryObjectGatewayToken: token}
snapshot := sealSourceSnapshot(t, fx, nil)
state := func() map[string]any {
out := r10State(t, fx)
for _, table := range []string{"knowledge_bases", "models", "vector_stores", "schema_migrations"} {
var raw []byte
if e := fx.pool.QueryRow(fx.ctx, "SELECT coalesce(jsonb_agg(to_jsonb(t) ORDER BY to_jsonb(t)::text),'[]') FROM "+table+" t").Scan(&raw); e != nil {
t.Fatal(e)
}
out[table] = string(raw)
}
for _, table := range []string{"kg_object_gateway_plans", "kg_object_gateway_intents", "kg_object_gateway_receipts", "kg_object_gateway_deliveries", "kg_object_gateway_grants"} {
var count int64
if e := governancePool.QueryRow(fx.ctx, "SELECT count(*) FROM "+table).Scan(&count); e != nil {
t.Fatal(e)
}
out[table] = count
}
return out
}
return R10AdmissionFixture{Context: fx.ctx, Config: config, Snapshot: snapshot, Exec: func(sql string) { r10Exec(t, fx, sql) }, State: state, Save: func(label string, value any) { r10Save(t, label, value) }}
}