Files
Agent-System/internal/ctxmgr/chat_turn_sweep_test.go
T
agent-bot ee1e53b510
CI / go-vet (pull_request) Failing after 10s
CI / backup-restore-drill (pull_request) Failing after 25s
CI / web (pull_request) Successful in 33s
fix(tests): per-package test databases + legitimate approval fixtures (issue #2 close)
Root causes of the remaining 5 CI PG failures (and the hidden flaky set):
1. All PG-integration packages shared ONE database under parallel package
   execution - migrations/seeds/truncates raced (nondeterministic failure
   sets, locally and in CI).
   Fix: RequireTestDatabaseURL/SkipIfNoTestDatabaseURL now derive a
   per-package database (<base>_<pkgHash>, advisory-lock-guarded
   CREATE); 9 raw-env straggler tests funnelled through the helpers;
   db-package tests call it unqualified (no import cycle).
2. Approval-chain fixtures were not production-shaped: freezeReviewSnapshot
   only honors head_snapshot_id, fake content hashes never froze a
   snapshot, so ApproverApprove rejected with 'approved requires frozen
   review_snapshot_id'. Errors were swallowed (_, _ =) hiding this.
   Fix (ownership-respecting, arch-enforced):
   - docrevision.SeedContentSnapshotForTest seeds the ledger tables it owns
   - docgov GovernanceEngine.FreezeHeadSnapshotForTest freezes head/review
     pointers on document_records (docgov-owned) and clears engine cache
   - all four integration fixtures use the pair; swallowed errors checked
3. Fixed-name fixtures collide once a package shares one DB (projects
   uq_projects_name_global): uniquified in projectinstance + server tests.
4. CAS/ctxmgr tests never bootstrapped migrations (relied on other packages
   having migrated the shared DB): explicit RunMigrationsOnceForTest.

Verified: full parallel short suite = 75 packages, 0 failures against an
isolated throwaway PG. CI skip list removed - short tests fully required.
2026-09-29 09:54:43 +00:00

70 lines
2.2 KiB
Go

package ctxmgr
import (
"context"
"testing"
"time"
)
// TestSweepExpiredProcessingTurns_Memory 验证 T20-B5 孤儿批量扫描:
// 过租约的 processing 回合被标 failed;未过期/已终态不受影响;幂等可重复。
func TestSweepExpiredProcessingTurns_Memory(t *testing.T) {
mgr := NewContextManager(nil, nil, DefaultTimeDecayConfig())
ctx := context.Background()
// 直接在内存 map 构造三个状态:过期 processing / 新鲜 processing / 已完成
past := time.Now().Add(-1 * time.Minute)
future := time.Now().Add(5 * time.Minute)
mgr.mu.Lock()
mgr.turns[turnMemKey("sess-sweep", "turn-expired")] = &ChatTurnRecord{
SessionID: "sess-sweep",
ClientTurnID: "turn-expired",
Status: TurnStatusProcessing,
LeaseExpiresAt: &past,
}
mgr.turns[turnMemKey("sess-sweep", "turn-fresh")] = &ChatTurnRecord{
SessionID: "sess-sweep",
ClientTurnID: "turn-fresh",
Status: TurnStatusProcessing,
LeaseExpiresAt: &future,
}
done := time.Now()
mgr.turns[turnMemKey("sess-sweep", "turn-done")] = &ChatTurnRecord{
SessionID: "sess-sweep",
ClientTurnID: "turn-done",
Status: TurnStatusCompleted,
CompletedAt: &done,
}
mgr.mu.Unlock()
n, err := mgr.SweepExpiredProcessingTurns(ctx, 100)
if err != nil {
t.Fatalf("sweep: %v", err)
}
if n != 1 {
t.Fatalf("expected exactly 1 swept, got %d", n)
}
if rec, err := mgr.GetChatTurn(ctx, "sess-sweep", "turn-expired"); err != nil {
t.Fatalf("get after sweep: %v", err)
} else if rec.Status != TurnStatusFailed {
t.Fatalf("expired turn must be failed, got %s", rec.Status)
}
if rec, err := mgr.GetChatTurn(ctx, "sess-sweep", "turn-fresh"); err != nil {
t.Fatalf("get fresh: %v", err)
} else if rec.Status != TurnStatusProcessing {
t.Fatalf("fresh turn must stay processing, got %s", rec.Status)
}
if rec, err := mgr.GetChatTurn(ctx, "sess-sweep", "turn-done"); err != nil {
t.Fatalf("get done: %v", err)
} else if rec.Status != TurnStatusCompleted {
t.Fatalf("done turn must stay completed, got %s", rec.Status)
}
// 幂等:再扫一次无新变化
n2, _ := mgr.SweepExpiredProcessingTurns(ctx, 100)
if n2 != 0 {
t.Fatalf("second sweep expect 0, got %d", n2)
}
}