From 20fc40a147c3cb17576734c5a0a8c4e0394bd2e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Wed, 28 Oct 2026 11:44:05 +0800 Subject: [PATCH] =?UTF-8?q?test(library):=20=E9=AA=8C=E8=AF=81=E5=8F=AA?= =?UTF-8?q?=E8=AF=BB=E6=95=B4=E5=BA=93=E7=89=88=E6=9C=AC=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E5=B9=B6=E4=BF=AE=E6=AD=A3=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 合成归档断言数量/字节/契约/策略与"未接线"行为 - 部署校验改用容器内读回,不再依赖需登录的 build-info --- internal/httpapi/library_snapshots_test.go | 128 +++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 internal/httpapi/library_snapshots_test.go diff --git a/internal/httpapi/library_snapshots_test.go b/internal/httpapi/library_snapshots_test.go new file mode 100644 index 0000000..788278c --- /dev/null +++ b/internal/httpapi/library_snapshots_test.go @@ -0,0 +1,128 @@ +package httpapi + +import ( + "bytes" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "knowledge-governance/internal/librarysnapshot" +) + +// The 整库版本 page reads a sealed archive through this exact function. A +// synthetic archive keeps the assertion about what the page shows independent of +// whether a real capture has run on this host. +func TestReadLibrarySnapshotsReportsSealedTotals(t *testing.T) { + root, err := os.MkdirTemp(os.TempDir(), "kglib-") + if err != nil { + t.Fatal(err) + } + defer os.RemoveAll(root) + if err = os.Chmod(root, 0700); err != nil { + t.Fatal(err) + } + archive, err := librarysnapshot.Open(root, 1024*1024) + if err != nil { + t.Fatal(err) + } + // The archive re-derives row counts from the stored bytes, so the bodies must + // carry exactly the declared number of rows. + rows := func(count int) string { + if count == 0 { + return "[]" + } + items := make([]string, count) + for i := range items { + items[i] = fmt.Sprintf(`{"id":"00000000-0000-4000-8000-%012d"}`, i) + } + return "[" + strings.Join(items, ",") + "]" + } + parts := []struct { + kind, name string + count int64 + body string + }{ + {"database", "knowledges", 4, rows(4)}, + {"database", "chunks", 38, rows(38)}, + {"database", "resources", 21, rows(21)}, + {"database", "resource_bindings", 4, rows(4)}, + {"database", "knowledge_tags", 0, rows(0)}, + {"database", "knowledge_tag_relations", 0, rows(0)}, + {"index", "chunks", 122, rows(122)}, + {"wiki", "wiki_pages", 31, rows(31)}, + {"wiki", "wiki_folders", 21, rows(21)}, + {"wiki", "wiki_page_issues", 0, rows(0)}, + {"wiki", "wiki_log_entries", 11, rows(11)}, + } + // Every resource row must have its own archived object: the archive refuses a + // snapshot whose resource dependencies are incomplete. + for i := 0; i < 21; i++ { + parts = append(parts, struct { + kind, name string + count int64 + body string + }{"resource", fmt.Sprintf("00000000-0000-4000-8000-%012d", i), 1, fmt.Sprintf("object-%d", i)}) + } + manifest := librarysnapshot.Manifest{ValidationPolicy: librarysnapshot.ArtifactAcceptancePolicy, + Format: librarysnapshot.Format, Scope: "internal", TenantID: 10000, + KnowledgeBaseID: "0c84a098-d3ed-4b82-a165-e6de9691d6a8", CreatedAt: time.Now().UTC(), + Compatibility: librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, + RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "text-embedding-v4", EmbeddingDimension: 1024, + IndexMappingSHA256: "137cde2965f3b56521f5fb722b4e0617f46d0e509e571b217fe01e090d5e4d01"}} + for _, part := range parts { + stored, err := archive.Put(bytes.NewReader([]byte(part.body))) + if err != nil { + t.Fatal(err) + } + stored.Kind, stored.Name, stored.Count = part.kind, part.name, part.count + manifest.Parts = append(manifest.Parts, stored) + } + snapshotID, err := archive.Seal(manifest) + if err != nil { + t.Fatal(err) + } + if err = archive.Close(); err != nil { + t.Fatal(err) + } + list, err := readLibrarySnapshots(root) + if err != nil { + t.Fatal(err) + } + if !list.Available || len(list.Snapshots) != 1 || list.Snapshots[0].SnapshotID != snapshotID { + t.Fatalf("unexpected list %+v", list) + } + snapshot := list.Snapshots[0] + want := map[string]int64{"documents": 4, "chunks": 38, "index_documents": 122, "resources": 21, "wiki_pages": 31} + for key, value := range want { + if snapshot.Totals[key] != value { + t.Fatalf("total %s = %d, want %d", key, snapshot.Totals[key], value) + } + } + // Stored bytes are deduplicated per blob, so they can be smaller than the + // logical sum but never larger. + if snapshot.LogicalBytes <= 0 || list.StoredBytes <= 0 || list.StoredBytes > snapshot.LogicalBytes { + t.Fatalf("bytes not reported: logical=%d stored=%d", snapshot.LogicalBytes, list.StoredBytes) + } + if snapshot.Compatibility.EmbeddingDimension != 1024 || snapshot.ValidationPolicy != librarysnapshot.ArtifactAcceptancePolicy { + t.Fatalf("contract not reported: %+v", snapshot) + } + if list.CaptureNote == "" { + t.Fatal("page must explain who runs the capture") + } + if _, err = os.Stat(filepath.Join(root, "versions", snapshotID+".json")); err != nil { + t.Fatalf("sealed manifest missing: %v", err) + } +} + +func TestReadLibrarySnapshotsWithoutArchiveIsExplicitlyUnwired(t *testing.T) { + list, err := readLibrarySnapshots("") + if err != nil { + t.Fatal(err) + } + if list.Available || list.Reason == "" || len(list.Snapshots) != 0 { + t.Fatalf("empty configuration must be reported as unwired, got %+v", list) + } +}