Files
knowledge-governance/internal/platform/postgres/reconciliation_integration_test.go
zheromiku 082f77fc18
build / build (push) Successful in 2m28s
fix(reconciliation): R6 改用 removal_candidates 视图,定时批每日只占一个名额
白盒补充复核提出的三项:
- P1 R6 与 /monitor/overview「需下架」计数口径不一致:R6 原先只在扫描结果里存在
  且上游 parse_status=completed 时才生成,上游仍在 processing/finalizing 或单条读取
  失败导致扫描缺行时,总览有数、对账无记录(陷阱 15)。改为直接由 removal_candidates
  视图行生成,台账细节按 publication 回填,计数与总览同一来源;补单测覆盖
  「上游 processing」「扫描未见」两种差异。
- P2 定时批失败后每个 tick 都重建:每日占位只排除 running/succeeded,failed 不占位,
  一次失败当天会每分钟刷批。占位条件加入 failed,重跑改为显式手动触发;补 PG 集成用例。
- P2 队列不可用时前端显示 0 等待 · 0 运行:改为按 available 判定并显示不可用文案;
  补组件用例。
2026-12-01 18:56:05 +08:00

115 lines
4.5 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)
}
}
// TestScheduledReconciliationClaimsOncePerDayAfterFailure freezes the operator
// expectation "每天最多一批": a failed scheduled batch keeps the day's slot so a
// transient upstream error cannot spawn a new batch on every scheduler tick.
// A retry after a failure is an explicit manual trigger.
func TestScheduledReconciliationClaimsOncePerDayAfterFailure(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)
dayStart := time.Date(2026, 12, 1, 0, 0, 0, 0, time.UTC)
run := func(id string, started time.Time) governance.ReconciliationRun {
return governance.ReconciliationRun{ID: id, KBID: "kb-sched", State: "running", Trigger: "schedule",
StartedAt: started, Counts: governance.ReconciliationCounts{}}
}
first, claimed, err := store.ClaimScheduledReconciliationRun(ctx, run("sched-1", dayStart.Add(2*time.Hour)), dayStart, dayStart.Add(2*time.Hour))
if err != nil || !claimed {
t.Fatalf("first claim claimed=%t err=%v", claimed, err)
}
if err := store.FailReconciliationRun(ctx, first.ID, "remote_scan_failed", dayStart.Add(2*time.Minute)); err != nil {
t.Fatal(err)
}
if _, claimed, err := store.ClaimScheduledReconciliationRun(ctx, run("sched-2", dayStart.Add(3*time.Minute)), dayStart, dayStart.Add(3*time.Minute)); err != nil || claimed {
t.Fatalf("same-day reclaim after failure claimed=%t err=%v", claimed, err)
}
nextDay := dayStart.Add(24 * time.Hour)
if _, claimed, err := store.ClaimScheduledReconciliationRun(ctx, run("sched-3", nextDay.Add(2*time.Hour)), nextDay, nextDay.Add(2*time.Hour)); err != nil || !claimed {
t.Fatalf("next-day claim claimed=%t err=%v", claimed, err)
}
}