Files
knowledge-governance/internal/releaseadapter/v071/library_artifact_policy_test.go
T
2026-10-28 08:01:46 +08:00

87 lines
2.4 KiB
Go

package v071
import (
"bytes"
"context"
"encoding/json"
"io"
"os"
"testing"
"time"
"knowledge-governance/internal/librarysnapshot"
)
func TestLibraryCandidateArtifactPolicyPreservesContextIndex(t *testing.T) {
pg, es, objects := libraryCandidateFixture()
es.Documents[0].Source = json.RawMessage(`{"knowledge_base_id":"kb-safe","knowledge_id":"doc-safe","chunk_id":"chunk-safe","source_id":"chunk-safe","content":"Title\nTransient heading\n\nchunk","embedding":[0.1,0.2]}`)
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
expected, err := json.Marshal(es.Documents)
if err != nil {
t.Fatal(err)
}
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()
contract := librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024, IndexMappingSHA256: es.MappingSHA256}
id, err := ArchiveLibraryCandidate(context.Background(), archive, "internal", PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, contract, pg, es, objects, time.Now().UTC())
if err != nil {
t.Fatalf("artifact candidate rejected because generation cannot be reconstructed: %v", err)
}
m, err := archive.Load(id)
if err != nil {
t.Fatal(err)
}
encoded, err := json.Marshal(m)
if err != nil {
t.Fatal(err)
}
var fields map[string]any
if err = json.Unmarshal(encoded, &fields); err != nil {
t.Fatal(err)
}
if fields["validation_policy"] != "artifact-acceptance-v1" {
t.Fatal("candidate lacks explicit artifact acceptance policy")
}
found := false
for _, part := range m.Parts {
if part.Kind != "index" {
continue
}
found = true
r, e := archive.OpenPart(part)
if e != nil {
t.Fatal(e)
}
got, e := io.ReadAll(r)
r.Close()
if e != nil {
t.Fatal(e)
}
if !bytes.Equal(got, expected) {
t.Fatal("archived index content or vector rewritten")
}
}
m.ValidationPolicy = "unsupported-policy"
if _, err := archive.Seal(m); err == nil {
t.Fatal("unknown policy accepted")
}
m.ValidationPolicy = ""
legacyID, err := archive.Seal(m)
if err != nil {
t.Fatal(err)
}
if legacyID == id {
t.Fatal("validation policy was not bound into snapshot identity")
}
if !found {
t.Fatal("index missing")
}
}