package assessment import ( "testing" "time" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) func TestExpiredActiveAttemptCooldownDoesNotMoveWithAvailabilityReads(t *testing.T) { createdAt := time.Date(2026, time.January, 1, 12, 0, 0, 0, time.UTC) expiresAt := createdAt.Add(24 * time.Hour) chainDeadline := createdAt.Add(72 * time.Hour) root := outbound.AssessmentRootAvailabilitySummary{ RootTargetGrade: domain.GradeJunior, RootCreatedAt: createdAt, RootUpdatedAt: createdAt.Add(12 * time.Hour), RootExpiresAt: expiresAt, RootState: domain.AssessmentAttemptStateInProgress, RootChainDeadline: chainDeadline, LatestTargetGrade: domain.GradeJunior, LatestUpdatedAt: createdAt.Add(12 * time.Hour), LatestExpiresAt: expiresAt, LatestState: domain.AssessmentAttemptStateInProgress, LatestChainDeadline: chainDeadline, } policy := domain.AssessmentPolicy{SameGradeRetryAfter: 7} firstReadAt := expiresAt.Add(time.Hour) secondReadAt := firstReadAt.Add(6 * time.Hour) want := expiresAt.Add(7 * 24 * time.Hour) for _, now := range []time.Time{firstReadAt, secondReadAt} { got := rootCooldownUntil([]outbound.AssessmentRootAvailabilitySummary{root}, domain.GradeJunior, policy, outbound.AssessmentRootQuotaSnapshot{}, now) if got == nil || !got.Equal(want) { t.Fatalf("rootCooldownUntil(%s) = %v, want fixed expiry-based deadline %s", now, got, want) } } if got := rootCooldownUntil([]outbound.AssessmentRootAvailabilitySummary{root}, domain.GradeJunior, policy, outbound.AssessmentRootQuotaSnapshot{}, want); got != nil { t.Fatalf("rootCooldownUntil(after deadline) = %s, want no cooldown", got) } } func TestExpiredActiveAttemptUsesNaturalExpiryForRetryTimestamp(t *testing.T) { updatedAt := time.Date(2026, time.January, 2, 10, 0, 0, 0, time.UTC) expiresAt := updatedAt.Add(-time.Hour) now := updatedAt.Add(30 * time.Minute) if got := effectiveAvailabilityUpdatedAt(domain.AssessmentAttemptStateInProgress, updatedAt, expiresAt, now); !got.Equal(expiresAt) { t.Fatalf("effectiveAvailabilityUpdatedAt() = %s, want original expiry %s", got, expiresAt) } root := &outbound.AssessmentRootAvailabilitySummary{ RootCreatedAt: expiresAt.Add(-24 * time.Hour), LatestState: domain.AssessmentAttemptStateInProgress, LatestOutcome: "", LatestExpiresAt: expiresAt, LatestChainDeadline: expiresAt.Add(48 * time.Hour), } if !availabilityCorrectionWindowOpen(root, now) { t.Fatal("expired active attempt should hold the correction chain until its fixed deadline") } if availabilityCorrectionWindowOpen(root, root.LatestChainDeadline) { t.Fatal("correction chain should close at its fixed deadline") } } func TestPlatformFailureDoesNotAddPromotionCooldown(t *testing.T) { now := time.Date(2026, time.January, 4, 12, 0, 0, 0, time.UTC) updatedAt := now.Add(-time.Hour) policy := domain.AssessmentPolicy{PromotionMinInterval: 90} tests := []struct { name string state domain.AssessmentAttemptState reason string }{ {name: "sandbox failure result", state: domain.AssessmentAttemptStateCompleted, reason: "sandbox_unavailable"}, {name: "insufficient bank state", state: domain.AssessmentAttemptStateInsufficientBank}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { root := outbound.AssessmentRootAvailabilitySummary{ RootTargetGrade: domain.GradeJunior, RootCreatedAt: now.Add(-48 * time.Hour), RootUpdatedAt: now.Add(-48 * time.Hour), RootState: domain.AssessmentAttemptStateCompleted, LatestTargetGrade: domain.GradeMiddle, LatestUpdatedAt: updatedAt, LatestExpiresAt: updatedAt.Add(-time.Hour), LatestState: test.state, LatestPlatformError: test.reason, } if got := rootCooldownUntil([]outbound.AssessmentRootAvailabilitySummary{root}, domain.GradeSenior, policy, outbound.AssessmentRootQuotaSnapshot{}, now); got != nil { t.Fatalf("rootCooldownUntil() = %s, want no platform-failure cooldown", got) } }) } }