3f2626f621
build / build (push) Failing after 1m32s
按 schema 版本选 profile 并以 information_schema 能力探测放行,取代写死的 version==74;未知版本、脏库、缺表缺列、跨版本搬运一律拒绝并点名原因。 0.9.0 的索引名改从部署配置解析,wiki_log_entries 按 profile 变为可选, 归档部件集合随之一致。
127 lines
4.6 KiB
Go
127 lines
4.6 KiB
Go
package v071
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
|
|
"knowledge-governance/internal/libraryprofile"
|
|
)
|
|
|
|
func TestSchemaProfileFailsClosedOnUnknownVersion(t *testing.T) {
|
|
_, err := profileForMigration(135, false, "test")
|
|
if err == nil || !strings.Contains(err.Error(), "135") || !strings.Contains(err.Error(), "134") {
|
|
t.Fatalf("unknown version must fail with the real version number, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSchemaProfileRejectsDirtySchema(t *testing.T) {
|
|
_, err := profileForMigration(134, true, "test")
|
|
if err == nil || !strings.Contains(err.Error(), "dirty") {
|
|
t.Fatalf("dirty schema must be refused, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSchemaProfileNamesMissingTableAndColumn(t *testing.T) {
|
|
profile, err := libraryprofile.For(134)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
columns := map[string]map[string]bool{}
|
|
for _, table := range append(append([]string{}, profile.RequiredTables...), profile.OptionalTables...) {
|
|
columns[table] = map[string]bool{}
|
|
for _, column := range profile.RequiredColumns[table] {
|
|
columns[table][column] = true
|
|
}
|
|
}
|
|
delete(columns, "wiki_pages")
|
|
if _, err := verifySchemaCapabilities(profile, columns, "test", 134); err == nil || !strings.Contains(err.Error(), "wiki_pages") {
|
|
t.Fatalf("missing required table must be named, got %v", err)
|
|
}
|
|
|
|
columns["wiki_pages"] = map[string]bool{"id": true, "tenant_id": true, "knowledge_base_id": true}
|
|
if _, err := verifySchemaCapabilities(profile, columns, "test", 134); err == nil || !strings.Contains(err.Error(), "wiki_pages.slug") {
|
|
t.Fatalf("missing required column must be named, got %v", err)
|
|
}
|
|
columns["wiki_pages"]["slug"] = true
|
|
if _, err := verifySchemaCapabilities(profile, columns, "test", 134); err != nil {
|
|
t.Fatalf("complete capabilities must pass: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestSchemaProfileV071BehaviourUnchanged(t *testing.T) {
|
|
legacy, err := libraryprofile.For(74)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !legacy.HasTable("wiki_log_entries") || !legacy.RequiresPart("wiki/wiki_log_entries") {
|
|
t.Fatal("0.7.1 must still require wiki_log_entries")
|
|
}
|
|
if legacy.EmbeddingDimension != 1024 {
|
|
t.Fatalf("0.7.1 dimension contract must stay 1024, got %d", legacy.EmbeddingDimension)
|
|
}
|
|
v090, err := libraryprofile.For(134)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if v090.RequiresPart("wiki/wiki_log_entries") || !v090.AllowsPart("wiki/wiki_log_entries") {
|
|
t.Fatal("0.9.0 wiki_log_entries must be optional")
|
|
}
|
|
if v090.HasTable("wiki_log_entries") == false {
|
|
t.Fatal("optional table should still be addressable when present")
|
|
}
|
|
resolved := SchemaProfile{Profile: v090, Tables: v090.RequiredTables}
|
|
if resolved.HasTable("wiki_log_entries") {
|
|
t.Fatal("0.9.0 export table set must exclude an absent optional table")
|
|
}
|
|
}
|
|
|
|
func TestTargetIndexNameResolvesToConfigurationOnly(t *testing.T) {
|
|
profile, err := libraryprofile.For(134)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
name, err := resolveTargetIndexName("", "weknora_v090_chunks", profile)
|
|
if err != nil || name != "weknora_v090_chunks" {
|
|
t.Fatalf("empty vector store must fall back to the configured index, got %q/%v", name, err)
|
|
}
|
|
if _, err := resolveTargetIndexName("", "", profile); err == nil || !strings.Contains(err.Error(), "index name unresolved") {
|
|
t.Fatalf("no source must fail instead of resolving to an empty name, got %v", err)
|
|
}
|
|
if _, err := resolveTargetIndexName("idx_a", "idx_b", profile); err == nil || !strings.Contains(err.Error(), "does not match") {
|
|
t.Fatalf("conflicting index sources must be refused, got %v", err)
|
|
}
|
|
}
|
|
|
|
// 0.9.0 与 0.7.1 的映射必须算出不同摘要:同样的 1024 维不等于同一份契约。
|
|
func TestIndexMappingDigestSeparatesSchemaVersions(t *testing.T) {
|
|
legacy := []byte(`{"weknora_v071_chunks":{"mappings":{"properties":{
|
|
"chunk_id":{"type":"text","fields":{"keyword":{"type":"keyword"}}},
|
|
"knowledge_id":{"type":"text","fields":{"keyword":{"type":"keyword"}}},
|
|
"knowledge_base_id":{"type":"text","fields":{"keyword":{"type":"keyword"}}},
|
|
"embedding":{"type":"dense_vector","dims":1024}}}}}`)
|
|
legacyDigest, err := IndexMappingDigest(legacy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if path := os.Getenv("KG_V090_MAPPING_JSON"); path != "" {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v090Digest, err := IndexMappingDigest(raw)
|
|
if err != nil {
|
|
t.Fatalf("real 0.9.0 mapping must be digestible: %v", err)
|
|
}
|
|
if v090Digest == legacyDigest {
|
|
t.Fatal("0.9.0 and 0.7.1 mappings must not share a digest")
|
|
}
|
|
if len(v090Digest) != 64 {
|
|
t.Fatalf("unexpected digest shape %q", v090Digest)
|
|
}
|
|
}
|
|
if _, err := IndexMappingDigest([]byte(`{"a":{"mappings":{}},"b":{"mappings":{}}}`)); err == nil {
|
|
t.Fatal("multi-index mapping responses must be refused")
|
|
}
|
|
}
|