8febe12d7d
真实本机 PG/ES/MinIO:v071 目录镜像 10 模式、结构引用/游标 15 模式、 8 表×2 外部范围身份矩阵、13 模式自然键(含软删除与合法同 parent 正例)、 备份期间三注入、收尾故障与授权撤销两个真实治理 PG 用例。 需隔离服务的用例在 test-scope.json 显式声明:普通 CI 跳过不算通过。
109 lines
3.4 KiB
Go
109 lines
3.4 KiB
Go
package v071
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
func TestContractNumericIdentityIsNotLost(t *testing.T) {
|
|
row := map[string]json.RawMessage{"id": json.RawMessage(`9007199254740993`)}
|
|
if got := archiveRowIdentity("wiki_log_entries", row); got != "9007199254740993" {
|
|
t.Fatalf("bigint identity lost: %q", got)
|
|
}
|
|
}
|
|
|
|
func TestContractUnknownNonemptyIdentityRejected(t *testing.T) {
|
|
for _, tc := range []struct{ name, table, body string }{
|
|
{"missing", "chunks", `[{}]`},
|
|
{"numeric_string", "wiki_log_entries", `[{"id":"18"}]`},
|
|
{"fraction", "wiki_log_entries", `[{"id":18.5}]`},
|
|
{"unknown_table", "unreviewed", `[{"id":"uuid"}]`},
|
|
{"duplicate", "chunks", `[{"id":"same"},{"id":"same"}]`},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
root := t.TempDir()
|
|
if err := os.Chmod(root, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
archive, err := librarysnapshot.Open(root, 1024*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer archive.Close()
|
|
part, err := archive.Put(bytes.NewBufferString(tc.body))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
part.Name = tc.table
|
|
if ids, err := archivedRowIdentities(archive, part); err == nil {
|
|
t.Fatalf("unsafe identity accepted: %v", ids)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestContractContentPreservesIntegersAndExpiry(t *testing.T) {
|
|
for _, pair := range [][2]string{
|
|
{`{"metadata":{"n":9007199254740992}}`, `{"metadata":{"n":9007199254740993}}`},
|
|
{`{"expires_at":"2026-01-01T00:00:00Z"}`, `{"expires_at":"2027-01-01T00:00:00Z"}`},
|
|
} {
|
|
var a, b map[string]json.RawMessage
|
|
if err := json.Unmarshal([]byte(pair[0]), &a); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal([]byte(pair[1]), &b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
x, err := canonicalRowDigest(a)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
y, err := canonicalRowDigest(b)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if x == y {
|
|
t.Fatalf("distinct content collapsed: %s vs %s", pair[0], pair[1])
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestContractExactTupleAndSameTenantIsolated(t *testing.T) {
|
|
fx := openWriteSetFixture(t)
|
|
seedTargetData(t, fx)
|
|
if _, err := fx.pool.Exec(fx.ctx, `INSERT INTO knowledge_tags(id,tenant_id,knowledge_base_id) VALUES('tag-target',10001,'kb-target'); INSERT INTO knowledge_tag_relations(knowledge_id,tag_id) VALUES('doc-target','tag-target')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
target := fx.target(t)
|
|
owners, err := target.existingRowOwners(fx.ctx, "knowledge_tag_relations", []string{"doc-target:tag-target", "doc-target:absent"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(owners) != 1 || owners["doc-target:tag-target"] != "10001/kb-target" {
|
|
t.Fatalf("tuple existence wrong: %v", owners)
|
|
}
|
|
snapshot := sealSourceSnapshotScope(t, fx, wsTargetTenant, wsSourceKB, nil, nil)
|
|
archive, err := librarysnapshot.Open(fx.archiveRoot, 256*1024*1024)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer archive.Close()
|
|
manifest, err := archive.Load(snapshot)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := fx.pool.Exec(fx.ctx, `INSERT INTO knowledges(id,tenant_id,knowledge_base_id) VALUES('doc-src',10001,'kb-target')`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := target.bindSnapshotScope(manifestScope{tenantID: wsTargetTenant, knowledgeBaseID: wsSourceKB}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := target.assertNoSourceScopeRows(fx.ctx, archive, manifest); err != nil {
|
|
t.Fatalf("correct same-tenant target mistaken for source residue: %v", err)
|
|
}
|
|
}
|