|
|
|
@@ -0,0 +1,193 @@
|
|
|
|
|
package v071
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Source v0.7.1 parent-child ingestion indexes only text chunks with a real
|
|
|
|
|
// parent, or all text when the whole document is flat. Standalone text in a
|
|
|
|
|
// mixed document is persisted and can have question indexes, but has no
|
|
|
|
|
// original-text ES document. Governance must mirror that selection per
|
|
|
|
|
// knowledge, without weakening explicit/required index checks.
|
|
|
|
|
func TestArchiveLibraryCandidateAcceptsNativeMixedParentChild(t *testing.T) {
|
|
|
|
|
ArchiveLibraryCandidateAcceptance(t, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArchiveLibraryCandidateRejectsFlatDocumentWithoutIndex(t *testing.T) {
|
|
|
|
|
fixture := nativeMixedFixture(false, true)
|
|
|
|
|
_, _, err := archiveNativeFixture(t, fixture)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "unverified legacy library chunk index coverage") {
|
|
|
|
|
t.Fatalf("flat document missing index must be rejected, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArchiveLibraryCandidateRejectsRequiredParentedChildWithoutIndex(t *testing.T) {
|
|
|
|
|
fixture := nativeMixedFixture(true, true)
|
|
|
|
|
fixture.es.Documents = fixture.es.Documents[:1] // keep parent? first doc is child
|
|
|
|
|
// Remove the child document, leave flat document; recompute digest.
|
|
|
|
|
fixture.es.Documents = []SearchDocument{fixture.es.Documents[0]}
|
|
|
|
|
fixture.es.Documents[0].Source = json.RawMessage(`{"knowledge_base_id":"kb-safe","knowledge_id":"doc-flat","chunk_id":"chunk-flat","content":"flat"}`)
|
|
|
|
|
fixture.es.DocumentsSHA256, _ = searchDocumentsDigest(fixture.es.Documents)
|
|
|
|
|
_, _, err := archiveNativeFixture(t, fixture)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "unverified legacy library chunk index coverage") {
|
|
|
|
|
t.Fatalf("parented child missing index must be rejected, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArchiveLibraryCandidateRejectsExplicitIndexedChunkWithoutIndex(t *testing.T) {
|
|
|
|
|
fixture := nativeMixedFixture(true, true)
|
|
|
|
|
// Make standalone text explicitly indexed (status 2); it must not be
|
|
|
|
|
// exempted by the mixed native rule.
|
|
|
|
|
fixture.pg.Tables = replaceChunkStatusForTest(fixture.pg.Tables, "chunk-standalone", 2)
|
|
|
|
|
_, _, err := archiveNativeFixture(t, fixture)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "missing indexed library chunk") {
|
|
|
|
|
t.Fatalf("status=2 chunk without index must be rejected, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type nativeMixedFixtureState struct {
|
|
|
|
|
pg PostgresSnapshot
|
|
|
|
|
es SearchSnapshot
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func nativeMixedFixture(includeFlatIndex, includeChildIndex bool) nativeMixedFixtureState {
|
|
|
|
|
knowledges := []json.RawMessage{
|
|
|
|
|
json.RawMessage(`{"id":"doc-mixed","tenant_id":10000,"knowledge_base_id":"kb-safe","parse_status":"completed","file_path":"resource://ABCDEFGHIJKLMNOPQRSTUV"}`),
|
|
|
|
|
json.RawMessage(`{"id":"doc-flat","tenant_id":10000,"knowledge_base_id":"kb-safe","parse_status":"completed","file_path":"resource://ABCDEFGHIJKLMNOPQRSTUV"}`),
|
|
|
|
|
}
|
|
|
|
|
chunks := []json.RawMessage{
|
|
|
|
|
json.RawMessage(`{"id":"chunk-parent","tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-mixed","chunk_type":"parent_text","content":"parent","status":0}`),
|
|
|
|
|
json.RawMessage(`{"id":"chunk-child","tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-mixed","chunk_type":"text","parent_chunk_id":"chunk-parent","content":"child","status":0}`),
|
|
|
|
|
json.RawMessage(`{"id":"chunk-standalone","tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-mixed","chunk_type":"text","content":"standalone","status":0}`),
|
|
|
|
|
json.RawMessage(`{"id":"chunk-flat","tenant_id":10000,"knowledge_base_id":"kb-safe","knowledge_id":"doc-flat","chunk_type":"text","content":"flat","status":0}`),
|
|
|
|
|
}
|
|
|
|
|
tables := []TableSnapshot{
|
|
|
|
|
newTableSnapshot("knowledges", knowledges),
|
|
|
|
|
newTableSnapshot("chunks", chunks),
|
|
|
|
|
newTableSnapshot("resources", []json.RawMessage{json.RawMessage(`{"id":"res-safe","tenant_id":10000,"handle":"ABCDEFGHIJKLMNOPQRSTUV","provider":"oss","physical_path":"oss://fixture/source.txt","size":4}`)}),
|
|
|
|
|
}
|
|
|
|
|
for _, name := range []string{"resource_bindings", "knowledge_tags", "knowledge_tag_relations", "wiki_pages", "wiki_folders", "wiki_page_issues", "wiki_log_entries"} {
|
|
|
|
|
tables = append(tables, newTableSnapshot(name, nil))
|
|
|
|
|
}
|
|
|
|
|
es := SearchSnapshot{MappingSHA256: shaBytes([]byte("mapping"))}
|
|
|
|
|
if includeChildIndex {
|
|
|
|
|
es.Documents = append(es.Documents, SearchDocument{ID: "es-child", Source: json.RawMessage(`{"knowledge_base_id":"kb-safe","knowledge_id":"doc-mixed","chunk_id":"chunk-child","content":"child"}`)})
|
|
|
|
|
}
|
|
|
|
|
if includeFlatIndex {
|
|
|
|
|
es.Documents = append(es.Documents, SearchDocument{ID: "es-flat", Source: json.RawMessage(`{"knowledge_base_id":"kb-safe","knowledge_id":"doc-flat","chunk_id":"chunk-flat","content":"flat"}`)})
|
|
|
|
|
}
|
|
|
|
|
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
|
|
|
|
|
return nativeMixedFixtureState{pg: PostgresSnapshot{Tables: tables}, es: es}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func archiveNativeFixture(t *testing.T, fixture nativeMixedFixtureState) (string, librarysnapshot.Manifest, error) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
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()
|
|
|
|
|
id, err := ArchiveLibraryCandidate(context.Background(), archive, "internal",
|
|
|
|
|
PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"},
|
|
|
|
|
librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024, IndexMappingSHA256: fixture.es.MappingSHA256},
|
|
|
|
|
fixture.pg, fixture.es, &libraryCandidateReader{data: []byte("file")},
|
|
|
|
|
time.Date(2026, 11, 5, 0, 0, 0, 0, time.UTC))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", librarysnapshot.Manifest{}, err
|
|
|
|
|
}
|
|
|
|
|
saved, err := archive.Load(id)
|
|
|
|
|
if err == nil {
|
|
|
|
|
err = validateArchivedLibraryClosure(archive, saved)
|
|
|
|
|
}
|
|
|
|
|
return id, saved, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestArchiveLibraryCandidateRejectsDanglingParentReference(t *testing.T) {
|
|
|
|
|
fixture := nativeMixedFixture(true, true)
|
|
|
|
|
fixture.pg.Tables = replaceChunkParentForTest(fixture.pg.Tables, "chunk-child", "missing-parent")
|
|
|
|
|
_, _, err := archiveNativeFixture(t, fixture)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "parent_chunk_id reference") {
|
|
|
|
|
t.Fatalf("dangling parent reference must be rejected, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func replaceChunkParentForTest(tables []TableSnapshot, chunkID, parentID string) []TableSnapshot {
|
|
|
|
|
out := make([]TableSnapshot, 0, len(tables))
|
|
|
|
|
for _, table := range tables {
|
|
|
|
|
if table.Name != "chunks" {
|
|
|
|
|
out = append(out, table)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rows := make([]json.RawMessage, 0, len(table.Rows))
|
|
|
|
|
for _, raw := range table.Rows {
|
|
|
|
|
var row map[string]any
|
|
|
|
|
_ = json.Unmarshal(raw, &row)
|
|
|
|
|
if row["id"] == chunkID {
|
|
|
|
|
row["parent_chunk_id"] = parentID
|
|
|
|
|
encoded, _ := json.Marshal(row)
|
|
|
|
|
rows = append(rows, encoded)
|
|
|
|
|
} else {
|
|
|
|
|
rows = append(rows, raw)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out = append(out, newTableSnapshot("chunks", rows))
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func replaceChunkStatusForTest(tables []TableSnapshot, chunkID string, status int) []TableSnapshot {
|
|
|
|
|
out := make([]TableSnapshot, 0, len(tables))
|
|
|
|
|
for _, table := range tables {
|
|
|
|
|
if table.Name != "chunks" {
|
|
|
|
|
out = append(out, table)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rows := make([]json.RawMessage, 0, len(table.Rows))
|
|
|
|
|
for _, raw := range table.Rows {
|
|
|
|
|
var row map[string]any
|
|
|
|
|
_ = json.Unmarshal(raw, &row)
|
|
|
|
|
if row["id"] == chunkID {
|
|
|
|
|
row["status"] = status
|
|
|
|
|
encoded, _ := json.Marshal(row)
|
|
|
|
|
rows = append(rows, encoded)
|
|
|
|
|
} else {
|
|
|
|
|
rows = append(rows, raw)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
out = append(out, newTableSnapshot("chunks", rows))
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArchiveLibraryCandidateAcceptance is a named helper so the positive case can
|
|
|
|
|
// fail with a clear message while the test still calls the real archive entry.
|
|
|
|
|
func ArchiveLibraryCandidateAcceptance(t *testing.T, includeFlatIndex bool) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
_, saved, err := archiveNativeFixture(t, nativeMixedFixture(includeFlatIndex, true))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("native mixed parent-child candidate rejected: %v", err)
|
|
|
|
|
}
|
|
|
|
|
var chunks, index int64
|
|
|
|
|
for _, part := range saved.Parts {
|
|
|
|
|
if part.Kind == "database" && part.Name == "chunks" {
|
|
|
|
|
chunks = part.Count
|
|
|
|
|
}
|
|
|
|
|
if part.Kind == "index" {
|
|
|
|
|
index = part.Count
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if chunks != 4 || index != 2 {
|
|
|
|
|
t.Fatalf("archive filtered rows: chunks=%d index=%d", chunks, index)
|
|
|
|
|
}
|
|
|
|
|
}
|