Files
knowledge-governance/internal/workflows/testweknora/legacy_verify_test.go
T
zheromiku dcdc8d1ddd feat: 知识治理平台(含已验证发布/回滚契约)
全栈知识治理平台首次入库(P1-P10 + 发布链路模拟验证闭环):

- 治理核心:源目录/变更集/人工审核/发布批次/发布执行(fail-closed 闸口)
- 测试通道:WeKnora 测试 KB 上传/轮询/验收;poll 完成时写入
  parser_generation/index_generation,修复 manual/seed 路径发布闸口永久拦截
- 发布适配器 v071(本地 source=测试角色 / 云端 target=生产角色 模拟验证通过):
  * ES 导出/导入 mapping 自适应(.keyword 子字段、可选 tenant_id)
  * OSS locator 支持嵌套 scheme;chunks.seq_id 由目标序列分配
  * bundle 归档同身份重试原子替换;verifier 按 bundle chunk_type 计数
  * 回滚 job payload 支持 prior_execution_id(strict 解码)
- 三通道发布+回滚演练实测:publish 4/4 verified,检索命中,
  rollback 4/4 rolled_back,目标端 PG is_enabled=false、ES is_enabled=false
- 前端 A·Data-console 风格全站统一,120 用例通过
- 门禁:go vet / go test 全包通过,gofmt 干净
2026-09-30 10:54:06 +08:00

193 lines
7.2 KiB
Go

package testweknora
import (
"context"
"strings"
"sync/atomic"
"testing"
"time"
"knowledge-governance/internal/platform/weknora"
)
func TestVerifyLegacyClassifiesWithoutLeaking(t *testing.T) {
client := &legacyClientStub{getResults: map[string]legacyGetResult{
"knowledge-completed-a": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}},
"knowledge-completed-b": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}},
"knowledge-pending": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "pending"}},
"knowledge-processing": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "processing"}},
"knowledge-finalizing": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "finalizing"}},
"knowledge-failed": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "failed"}},
"knowledge-wrong-kb": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-other", ParseStatus: "completed"}},
"knowledge-missing": {err: &weknora.HTTPError{StatusCode: 404}},
"knowledge-server-error": {err: &weknora.HTTPError{StatusCode: 500}},
}, chunkTotal: map[string]int64{
"knowledge-completed-a": 2,
"knowledge-completed-b": 0,
}}
candidates := []LegacyCandidate{
{ID: "pub-1", KnowledgeID: "knowledge-completed-a", TestKBID: "kb-1"},
{ID: "pub-2", KnowledgeID: "knowledge-completed-b", TestKBID: "kb-1"},
{ID: "pub-3", KnowledgeID: "knowledge-pending", TestKBID: "kb-1"},
{ID: "pub-4", KnowledgeID: "knowledge-processing", TestKBID: "kb-1"},
{ID: "pub-5", KnowledgeID: "knowledge-finalizing", TestKBID: "kb-1"},
{ID: "pub-6", KnowledgeID: "knowledge-failed", TestKBID: "kb-1"},
{ID: "pub-7", KnowledgeID: "knowledge-wrong-kb", TestKBID: "kb-1"},
{ID: "pub-8", KnowledgeID: "knowledge-missing", TestKBID: "kb-1"},
{ID: "pub-9", KnowledgeID: "knowledge-server-error", TestKBID: "kb-1"},
}
report, err := VerifyLegacy(context.Background(), client, candidates, 1)
if err != nil {
t.Fatalf("VerifyLegacy() error = %v", err)
}
want := LegacyVerificationReport{
Total: 9, Existing: 6, Missing: 1, WrongKB: 1,
Pending: 1, Processing: 1, Finalizing: 1, Completed: 2, Failed: 1,
ChunkChecked: 2, ChunkEmpty: 1, HTTPErrors: 1,
}
if report != want {
t.Fatalf("report = %#v, want %#v", report, want)
}
}
func TestVerifyLegacyChunkSampleEverySkipsUnsampledCompleted(t *testing.T) {
client := &legacyClientStub{getResults: map[string]legacyGetResult{
"knowledge-a": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}},
"knowledge-b": {knowledge: weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}},
}, chunkTotal: map[string]int64{
"knowledge-b": 5,
}}
candidates := []LegacyCandidate{
{ID: "pub-1", KnowledgeID: "knowledge-a", TestKBID: "kb-1"},
{ID: "pub-2", KnowledgeID: "knowledge-b", TestKBID: "kb-1"},
}
report, err := VerifyLegacy(context.Background(), client, candidates, 2)
if err != nil {
t.Fatal(err)
}
if report.Completed != 2 || report.ChunkChecked != 1 {
t.Fatalf("report = %#v", report)
}
if client.chunkQueries != 1 {
t.Fatalf("chunk queries = %d, want 1", client.chunkQueries)
}
}
func TestVerifyLegacyConcurrentBoundsConcurrencyAndAggregates(t *testing.T) {
client := &concurrentLegacyClient{}
candidates := make([]LegacyCandidate, 12)
for index := range candidates {
candidates[index] = LegacyCandidate{ID: "publication", KnowledgeID: "knowledge", TestKBID: "kb-1"}
}
report, err := VerifyLegacyConcurrent(context.Background(), client, candidates, 0, 3)
if err != nil {
t.Fatalf("VerifyLegacyConcurrent() error = %v", err)
}
if report.Total != 12 || report.Existing != 12 || report.Completed != 12 {
t.Fatalf("report = %#v", report)
}
if maximum := client.maximum.Load(); maximum < 2 || maximum > 3 {
t.Fatalf("maximum concurrency = %d, want 2..3", maximum)
}
}
func TestVerifyLegacyConcurrentReturnsImmediatelyForEmptyInput(t *testing.T) {
report, err := VerifyLegacyConcurrent(context.Background(), &concurrentLegacyClient{}, nil, 10, 4)
if err != nil {
t.Fatalf("VerifyLegacyConcurrent() error = %v", err)
}
if report != (LegacyVerificationReport{}) {
t.Fatalf("report = %#v", report)
}
}
func TestFormatDryRunReportAggregatesWithoutKnowledgeIDs(t *testing.T) {
candidates := []LegacyCandidate{
{ID: "pub-1", KnowledgeID: "knowledge-1", TestKBID: "kb-1"},
{ID: "pub-2", KnowledgeID: "knowledge-2", TestKBID: "kb-1"},
{ID: "pub-3", KnowledgeID: "knowledge-3", TestKBID: "kb-2"},
}
report := FormatDryRunReport(candidates, 4)
if !strings.Contains(report, "total=3") || !strings.Contains(report, "concurrency=4") ||
!strings.Contains(report, "knowledge_base=kb-1 count=2") || !strings.Contains(report, "knowledge_base=kb-2 count=1") {
t.Fatalf("report = %q", report)
}
if strings.Contains(report, "knowledge-1") || strings.Contains(report, "pub-1") {
t.Fatalf("report leaked knowledge/publication ID: %q", report)
}
}
func TestFormatLegacyVerificationReportContainsOnlyAggregateCounts(t *testing.T) {
report := FormatLegacyVerificationReport(LegacyVerificationReport{
Total: 530, Existing: 500, Missing: 20, WrongKB: 10,
Completed: 490, Failed: 10, ChunkChecked: 20, ChunkEmpty: 2, HTTPErrors: 3,
})
for _, expected := range []string{"total=530", "existing=500", "missing=20", "wrong_kb=10", "completed=490", "failed=10", "chunk_checked=20", "chunk_empty=2", "http_errors=3"} {
if !strings.Contains(report, expected) {
t.Fatalf("report missing %q: %q", expected, report)
}
}
for _, forbidden := range []string{"knowledge_id", "knowledge_base", "publication_id"} {
if strings.Contains(report, forbidden) {
t.Fatalf("report leaked identifier field %q: %q", forbidden, report)
}
}
}
type legacyClientStub struct {
getResults map[string]legacyGetResult
chunkTotal map[string]int64
chunkQueries int
}
type legacyGetResult struct {
knowledge weknora.Knowledge
err error
}
type concurrentLegacyClient struct {
current atomic.Int64
maximum atomic.Int64
}
func (client *concurrentLegacyClient) GetKnowledge(context.Context, string) (weknora.Knowledge, error) {
current := client.current.Add(1)
for {
maximum := client.maximum.Load()
if current <= maximum || client.maximum.CompareAndSwap(maximum, current) {
break
}
}
time.Sleep(10 * time.Millisecond)
client.current.Add(-1)
return weknora.Knowledge{KnowledgeBaseID: "kb-1", ParseStatus: "completed"}, nil
}
func (*concurrentLegacyClient) ListChunks(context.Context, string, int, int, []string) ([]weknora.Chunk, int64, error) {
return nil, 1, nil
}
func (client *legacyClientStub) GetKnowledge(_ context.Context, knowledgeID string) (weknora.Knowledge, error) {
result, ok := client.getResults[knowledgeID]
if !ok {
return weknora.Knowledge{}, &weknora.HTTPError{StatusCode: 404}
}
return result.knowledge, result.err
}
func (client *legacyClientStub) ListChunks(_ context.Context, knowledgeID string, _ int, _ int, _ []string) ([]weknora.Chunk, int64, error) {
client.chunkQueries++
total, ok := client.chunkTotal[knowledgeID]
if !ok {
total = 0
}
chunks := make([]weknora.Chunk, 0, total)
for i := int64(0); i < total; i++ {
chunks = append(chunks, weknora.Chunk{ID: "chunk", KnowledgeID: knowledgeID})
}
return chunks, total, nil
}