Files
knowledge-governance/internal/librarysnapshot/usage_content_test.go
T
姚章浩 c5e033dd0e
build / build (push) Failing after 15s
fix(library): 幂等重放、跨版本去重用量与契约复核
- 同作业号重跑读持久日志+已封存清单按首次请求重放(source_read=false,
  writers_stopped=false),换范围/绑定库或清单校验失败即拒绝
- 归档用量按跨版本去重统计内容字节,清单/未引用 blob/实际分配单列,
  不再用各版本 unique 之和冒充归档占用
- 冻结窗口内复核来源契约,不一致即中止;拒绝组外同镜像运行容器
- 补齐整库只读接口的 OpenAPI 契约
- 按用户确认取消 A06:文档/页面/接口不再把效果测试列为封版门槛
2026-10-28 12:01:09 +08:00

150 lines
4.9 KiB
Go

package librarysnapshot
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"time"
)
// sealFixtureVersion seals one complete manifest whose parts are all derived
// from the same rows, so two versions of the same content share every blob.
func sealFixtureVersion(t *testing.T, archive *Archive, scope string, createdAt time.Time, documents int) string {
t.Helper()
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", int64(documents), rows(documents)},
{"database", "chunks", int64(documents), rows(documents)},
{"database", "resources", 0, rows(0)},
{"database", "resource_bindings", 0, rows(0)},
{"database", "knowledge_tags", 0, rows(0)},
{"database", "knowledge_tag_relations", 0, rows(0)},
{"index", "chunks", int64(documents), rows(documents)},
{"wiki", "wiki_pages", 0, rows(0)},
{"wiki", "wiki_folders", 0, rows(0)},
{"wiki", "wiki_page_issues", 0, rows(0)},
{"wiki", "wiki_log_entries", 0, rows(0)},
}
manifest := Manifest{ValidationPolicy: ArtifactAcceptancePolicy, Format: Format, Scope: scope,
TenantID: 10000, KnowledgeBaseID: "kb-safe", CreatedAt: createdAt,
Compatibility: Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74,
RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024,
IndexMappingSHA256: strings.Repeat("a", 64)}}
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)
}
id, err := archive.Seal(manifest)
if err != nil {
t.Fatal(err)
}
return id
}
func fixtureArchive(t *testing.T) (*Archive, string) {
t.Helper()
root, err := os.MkdirTemp(os.TempDir(), "kgsnap-")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(root) })
if err = os.Chmod(root, 0700); err != nil {
t.Fatal(err)
}
archive, err := Open(root, 1024*1024)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { archive.Close() })
return archive, root
}
// Two sealed versions with identical content share every blob: summing the
// per-version UniqueBytes double counts them, which is the reporting bug this
// accounting fixes.
func TestContentUsageDeduplicatesAcrossVersions(t *testing.T) {
archive, _ := fixtureArchive(t)
sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 1, 0, 0, 0, time.UTC), 4)
sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 2, 0, 0, 0, time.UTC), 4)
perVersion, err := archive.Usage()
if err != nil {
t.Fatal(err)
}
if len(perVersion) != 2 {
t.Fatalf("expected two versions, got %d", len(perVersion))
}
sum := perVersion[0].UniqueBytes + perVersion[1].UniqueBytes
total, err := archive.ContentUsage()
if err != nil {
t.Fatal(err)
}
if total.Versions != 2 {
t.Fatalf("versions=%d", total.Versions)
}
if total.ContentBytes <= 0 {
t.Fatal("no content bytes reported")
}
if total.ContentBytes*2 != sum {
t.Fatalf("shared content not deduplicated: content=%d sum-of-versions=%d", total.ContentBytes, sum)
}
if total.UnreferencedBlobs != 0 || total.UnreferencedBytes != 0 {
t.Fatalf("unexpected unreferenced blobs: %+v", total)
}
if total.BlobFileBytes != total.ContentBytes {
t.Fatalf("blob files=%d content=%d", total.BlobFileBytes, total.ContentBytes)
}
if total.BlobAllocatedBytes < total.BlobFileBytes {
t.Fatalf("allocated=%d smaller than apparent=%d", total.BlobAllocatedBytes, total.BlobFileBytes)
}
if total.ManifestFileBytes <= 0 || total.ManifestAllocatedBytes < total.ManifestFileBytes {
t.Fatalf("manifest accounting wrong: %+v", total)
}
}
// A blob written by an interrupted capture is referenced by no manifest and must
// be reported separately, never folded into content bytes.
func TestContentUsageSeparatesUnreferencedBlobs(t *testing.T) {
archive, _ := fixtureArchive(t)
sealFixtureVersion(t, archive, "internal", time.Date(2026, 10, 28, 3, 0, 0, 0, time.UTC), 2)
before, err := archive.ContentUsage()
if err != nil {
t.Fatal(err)
}
stray, err := archive.Put(bytes.NewReader([]byte("interrupted-write")))
if err != nil {
t.Fatal(err)
}
after, err := archive.ContentUsage()
if err != nil {
t.Fatal(err)
}
if after.ContentBytes != before.ContentBytes {
t.Fatal("unreferenced blob changed content bytes")
}
if after.UnreferencedBlobs != 1 || after.UnreferencedBytes != stray.Bytes {
t.Fatalf("unreferenced accounting wrong: %+v (stray=%d)", after, stray.Bytes)
}
if after.BlobFileBytes != before.BlobFileBytes+stray.Bytes {
t.Fatal("blob file bytes do not include the unreferenced blob")
}
}