Files
knowledge-governance/internal/platform/postgres/reconciliation_integration_test.go
Zhero & Rin 4528a55e6a feat(reconciliation): 实现导入对账后端
为什么:T6a 需要一张批次/差异表、全量只读扫描、R1-R8 判定和 finding 级处置;现有解析监控只转述台账状态,无法回答库内是否一致。
做什么:迁移 000084(upload_md5、runs/findings/actions、共享 removal_candidates 视图);WeKnora 字段与 1000/页扫描;runtime 只读客户端(0400 Key、503 降级);扫描/矩阵/动作/定时/僵死回收;9 条接口与 RBAC;overview 队列与删两张常量卡。
边界:只对测试库;对账零写请求;R1/R2 单条确认;R6 复用 /removal;R2 只管理员;生产链路未改。
验证:make verify 全绿(17 项);新增矩阵/扫描 R1-R8/零写/失败整批/动作/运行时/密钥权限测试。
2026-12-01 12:19:54 +08:00

75 lines
2.6 KiB
Go

package postgres
import (
"context"
"errors"
"os"
"testing"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"knowledge-governance/internal/governance"
)
func TestReconciliationRunUniquenessStaleAndRetentionIntegration(t *testing.T) {
url := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL"))
if url == "" {
t.Skip("TEST_DATABASE_URL is not set")
}
ctx := context.Background()
pool, err := pgxpool.New(ctx, url)
if err != nil {
t.Fatal(err)
}
defer pool.Close()
if err := ApplyMigrations(ctx, pool); err != nil {
t.Fatal(err)
}
store := NewStore(pool)
now := time.Date(2026, 12, 1, 2, 0, 0, 0, time.UTC)
first, err := store.ClaimReconciliationRun(ctx, governance.ReconciliationRun{
ID: "rec-first", KBID: "kb-unique", State: "running", Trigger: "manual", StartedAt: now,
Counts: governance.ReconciliationCounts{},
}, now)
if err != nil {
t.Fatal(err)
}
if _, err := store.ClaimReconciliationRun(ctx, governance.ReconciliationRun{
ID: "rec-second", KBID: "kb-unique", State: "running", Trigger: "manual", StartedAt: now,
Counts: governance.ReconciliationCounts{},
}, now); !errors.Is(err, ErrReconciliationRunInProgress) {
t.Fatalf("second claim err=%v", err)
}
reaped, err := store.ReapStaleReconciliationRuns(ctx, now.Add(7*time.Hour), 6*time.Hour)
if err != nil || reaped != 1 {
t.Fatalf("reaped=%d err=%v", reaped, err)
}
second, err := store.ClaimReconciliationRun(ctx, governance.ReconciliationRun{
ID: "rec-second", KBID: "kb-unique", State: "running", Trigger: "manual", StartedAt: now.Add(7 * time.Hour),
Counts: governance.ReconciliationCounts{},
}, now.Add(7*time.Hour))
if err != nil {
t.Fatal(err)
}
finishedAt := now.Add(8 * time.Hour)
second.FinishedAt = &finishedAt
second.Counts = governance.ReconciliationCounts{"R1": 1}
if err := store.CompleteReconciliationRun(ctx, second, []governance.ReconciliationFinding{{
ID: "finding-second", RunID: second.ID, Type: "R1", DocumentID: "doc",
Title: "t", LedgerState: "completed", EvidenceLevel: "none", SuggestedAction: "requeue_upload",
}}); err != nil {
t.Fatal(err)
}
if err := store.CleanupReconciliationFindings(ctx, now.Add(9*time.Hour), 1, 90*24*time.Hour); err != nil {
t.Fatal(err)
}
var count int
if err := pool.QueryRow(ctx, `SELECT COUNT(*)::integer FROM reconciliation_findings WHERE run_id=$1`, first.ID).Scan(&count); err != nil || count != 0 {
t.Fatalf("old findings=%d err=%v", count, err)
}
if err := pool.QueryRow(ctx, `SELECT COUNT(*)::integer FROM reconciliation_findings WHERE run_id=$1`, second.ID).Scan(&count); err != nil || count != 1 {
t.Fatalf("new findings=%d err=%v", count, err)
}
}