package assessment import ( "testing" "time" "github.com/google/uuid" "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) } }) } } // The confirmed grade of a specialization and its basis come from the same // stored category, so a demonstration grade is never returned without its basis. func TestAvailabilityCarriesTheConfirmedGradeWithItsBasis(t *testing.T) { now := time.Date(2026, time.October, 9, 12, 0, 0, 0, time.UTC) policy := domain.AssessmentPolicy{Attempt: &domain.AssessmentAttemptPolicy{RealMode: domain.AssessmentRealModeDemoBasic}} category, err := domain.RestoreCandidateAssessmentCategory(domain.CandidateAssessmentCategorySnapshot{ CandidateID: uuid.New(), Specialization: domain.SpecializationBackend, Grade: domain.GradeJunior, SourceAttemptID: domain.AssessmentAttemptID(uuid.New()), SourceReviewID: uuid.New(), ProductPolicyVersionID: domain.PolicyVersionID(uuid.New()), ProductPolicyVersion: 1, OpeningID: uuid.New(), OpeningManifestHash: [32]byte{1}, Source: domain.AssessmentCategorySourceSyntheticDemo, CalibrationStatus: domain.AssessmentCategoryCalibrationUncalibrated, AssignedAt: now, }) if err != nil { t.Fatalf("RestoreCandidateAssessmentCategory() error = %v", err) } reads := assessmentAvailabilityReads{categories: map[domain.SpecializationCode]domain.CandidateAssessmentCategory{ domain.SpecializationBackend: category, }} confirmed := buildSpecializationAvailability( domain.QuestionnaireSpecialization{Specialization: domain.SpecializationBackend}, reads, policy, now, 60, 12) if confirmed.ConfirmedGrade == nil || *confirmed.ConfirmedGrade != domain.GradeJunior { t.Fatalf("ConfirmedGrade = %v, want junior", confirmed.ConfirmedGrade) } if confirmed.ConfirmedBasis == nil || *confirmed.ConfirmedBasis != domain.AssessmentCategorySourceSyntheticDemo { t.Fatalf("ConfirmedBasis = %v, want synthetic_demo", confirmed.ConfirmedBasis) } unconfirmed := buildSpecializationAvailability( domain.QuestionnaireSpecialization{Specialization: domain.SpecializationFrontend}, reads, policy, now, 60, 12) if unconfirmed.ConfirmedGrade != nil || unconfirmed.ConfirmedBasis != nil { t.Fatalf("unconfirmed specialization reports grade %v and basis %v, want none", unconfirmed.ConfirmedGrade, unconfirmed.ConfirmedBasis) } }