320 lines
13 KiB
Go
320 lines
13 KiB
Go
package profile
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
type profileTestTransactor struct {
|
|
unit outbound.UnitOfWork
|
|
}
|
|
|
|
func (transactor profileTestTransactor) WithinTransaction(
|
|
ctx context.Context,
|
|
fn func(context.Context, outbound.UnitOfWork) error,
|
|
) error {
|
|
return fn(ctx, transactor.unit)
|
|
}
|
|
|
|
type profileTestUnit struct {
|
|
outbound.UnitOfWork
|
|
|
|
users outbound.UserRepository
|
|
profiles outbound.CandidateProfileRepository
|
|
// attempts is the zero value by default: a candidate without categories.
|
|
attempts profileTestAttempts
|
|
}
|
|
|
|
func (unit profileTestUnit) Users() outbound.UserRepository { return unit.users }
|
|
|
|
func (unit profileTestUnit) CandidateProfiles() outbound.CandidateProfileRepository {
|
|
return unit.profiles
|
|
}
|
|
|
|
func (unit profileTestUnit) AssessmentAttempts() outbound.AssessmentAttemptRepository {
|
|
return unit.attempts
|
|
}
|
|
|
|
type profileTestAttempts struct {
|
|
outbound.AssessmentAttemptRepository
|
|
|
|
categories []domain.CandidateAssessmentCategory
|
|
err error
|
|
}
|
|
|
|
func (attempts profileTestAttempts) ListCandidateCategories(
|
|
_ context.Context,
|
|
_ uuid.UUID,
|
|
) ([]domain.CandidateAssessmentCategory, error) {
|
|
return attempts.categories, attempts.err
|
|
}
|
|
|
|
// profileTestCategory is a stored synthetic demonstration category of the
|
|
// candidate, as the assessment result that assigned it left it.
|
|
func profileTestCategory(
|
|
t *testing.T,
|
|
candidateID uuid.UUID,
|
|
specialization domain.SpecializationCode,
|
|
grade domain.GradeCode,
|
|
assignedAt time.Time,
|
|
) domain.CandidateAssessmentCategory {
|
|
t.Helper()
|
|
category, err := domain.RestoreCandidateAssessmentCategory(domain.CandidateAssessmentCategorySnapshot{
|
|
CandidateID: candidateID, Specialization: specialization, Grade: grade,
|
|
SourceAttemptID: domain.AssessmentAttemptID(uuid.Must(uuid.NewV7())), SourceReviewID: uuid.Must(uuid.NewV7()),
|
|
ProductPolicyVersionID: domain.PolicyVersionID(uuid.Must(uuid.NewV7())), ProductPolicyVersion: 1,
|
|
OpeningID: uuid.Must(uuid.NewV7()), OpeningManifestHash: [32]byte{1},
|
|
Source: domain.AssessmentCategorySourceSyntheticDemo, CalibrationStatus: domain.AssessmentCategoryCalibrationUncalibrated,
|
|
AssignedAt: assignedAt,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RestoreCandidateAssessmentCategory() error = %v", err)
|
|
}
|
|
return category
|
|
}
|
|
|
|
type profileTestUsers struct {
|
|
outbound.UserRepository
|
|
|
|
user domain.User
|
|
err error
|
|
getCalls int
|
|
}
|
|
|
|
func (users *profileTestUsers) GetByID(_ context.Context, userID uuid.UUID) (domain.User, error) {
|
|
users.getCalls++
|
|
if users.err != nil {
|
|
return domain.User{}, users.err
|
|
}
|
|
if userID != users.user.ID {
|
|
return domain.User{}, outbound.ErrUserNotFound
|
|
}
|
|
return users.user, nil
|
|
}
|
|
|
|
type profileTestRepository struct {
|
|
profile domain.CandidateProfile
|
|
getCalls int
|
|
saveCalls int
|
|
advanceCalls int
|
|
saveErr error
|
|
advanceErr error
|
|
}
|
|
|
|
func (repository *profileTestRepository) Get(_ context.Context, _ uuid.UUID) (domain.CandidateProfile, error) {
|
|
repository.getCalls++
|
|
return repository.profile, nil
|
|
}
|
|
|
|
func (repository *profileTestRepository) Save(
|
|
_ context.Context,
|
|
expectedVersion int64,
|
|
profile domain.CandidateProfile,
|
|
) (domain.CandidateProfile, error) {
|
|
repository.saveCalls++
|
|
if repository.saveErr != nil {
|
|
return domain.CandidateProfile{}, repository.saveErr
|
|
}
|
|
claims, err := profile.Content().StoredClaims()
|
|
if err != nil {
|
|
return domain.CandidateProfile{}, err
|
|
}
|
|
repository.profile, err = domain.RestoreCandidateProfile(domain.StoredCandidateProfile{
|
|
UserID: profile.UserID(), Version: expectedVersion + 1,
|
|
CreatedAt: profile.CreatedAt(), UpdatedAt: profile.UpdatedAt(), Claims: claims,
|
|
ParallelPeriodAcknowledgements: profile.Content().ParallelPeriodAcknowledgements(),
|
|
})
|
|
return repository.profile, err
|
|
}
|
|
|
|
func (repository *profileTestRepository) AdvanceOnboarding(_ context.Context, _ uuid.UUID, _ time.Time) error {
|
|
repository.advanceCalls++
|
|
return repository.advanceErr
|
|
}
|
|
|
|
func TestServiceRequiresVerifiedCandidateBeforeReadingOrWritingProfile(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
tests := []struct {
|
|
name string
|
|
scenario domain.Scenario
|
|
verified bool
|
|
wantErr error
|
|
}{
|
|
{name: "verified candidate", scenario: domain.ScenarioCandidate, verified: true},
|
|
{name: "unverified candidate", scenario: domain.ScenarioCandidate, wantErr: ErrEmailNotVerified},
|
|
{name: "verified employer", scenario: domain.ScenarioEmployer, verified: true, wantErr: ErrForbidden},
|
|
{name: "unverified employer", scenario: domain.ScenarioEmployer, wantErr: ErrEmailNotVerified},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
id := uuid.Must(uuid.NewV7())
|
|
user := domain.User{ID: id, Scenario: test.scenario}
|
|
if test.verified {
|
|
user.EmailVerifiedAt = &now
|
|
}
|
|
users := &profileTestUsers{user: user}
|
|
profiles := &profileTestRepository{}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles}}, func() time.Time { return now })
|
|
|
|
_, readErr := service.GetMyProfile(context.Background(), id)
|
|
if !errors.Is(readErr, test.wantErr) {
|
|
t.Fatalf("GetMyProfile() error = %v, want %v", readErr, test.wantErr)
|
|
}
|
|
_, saveErr := service.SaveMyProfile(context.Background(), id, 0, domain.CandidateProfileInput{})
|
|
if !errors.Is(saveErr, test.wantErr) {
|
|
t.Fatalf("SaveMyProfile() error = %v, want %v", saveErr, test.wantErr)
|
|
}
|
|
wantProfileReads := 0
|
|
wantAdvances := 0
|
|
if test.wantErr == nil {
|
|
wantProfileReads = 2
|
|
wantAdvances = 1
|
|
}
|
|
if profiles.getCalls != wantProfileReads || profiles.saveCalls != 0 || profiles.advanceCalls != wantAdvances {
|
|
t.Fatalf("profile calls = get %d, save %d, advance %d; want %d, 0, %d", profiles.getCalls, profiles.saveCalls, profiles.advanceCalls, wantProfileReads, wantAdvances)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSaveMyProfileWritesOnlyChangedVersionsAndAdvancesOnNoOp(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
id := uuid.Must(uuid.NewV7())
|
|
user := domain.User{ID: id, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
|
users := &profileTestUsers{user: user}
|
|
profiles := &profileTestRepository{}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles}}, func() time.Time { return now })
|
|
input := domain.CandidateProfileInput{FullName: &domain.ProfileClaimInput[string]{Value: "Anna Sokolova", Status: domain.ProfileClaimConfirmed}}
|
|
|
|
if _, err := service.SaveMyProfile(context.Background(), id, 0, input); err != nil {
|
|
t.Fatalf("SaveMyProfile(first save) error = %v", err)
|
|
}
|
|
if profiles.saveCalls != 1 || profiles.advanceCalls != 1 || profiles.profile.Version() != 1 {
|
|
t.Fatalf("first save calls = save %d, advance %d, version %d", profiles.saveCalls, profiles.advanceCalls, profiles.profile.Version())
|
|
}
|
|
if _, err := service.SaveMyProfile(context.Background(), id, 1, input); err != nil {
|
|
t.Fatalf("SaveMyProfile(no-op retry) error = %v", err)
|
|
}
|
|
if profiles.saveCalls != 1 || profiles.advanceCalls != 2 {
|
|
t.Fatalf("no-op calls = save %d, advance %d, want one saved version and two onboarding checks", profiles.saveCalls, profiles.advanceCalls)
|
|
}
|
|
}
|
|
|
|
func TestSaveMyProfileDoesNotAdvanceAfterProfileWriteFailure(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
id := uuid.Must(uuid.NewV7())
|
|
user := domain.User{ID: id, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
|
users := &profileTestUsers{user: user}
|
|
profiles := &profileTestRepository{saveErr: outbound.ErrTransactionConflict}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles}}, func() time.Time { return now })
|
|
input := domain.CandidateProfileInput{FullName: &domain.ProfileClaimInput[string]{Value: "Anna Sokolova", Status: domain.ProfileClaimConfirmed}}
|
|
|
|
if _, err := service.SaveMyProfile(context.Background(), id, 0, input); !errors.Is(err, outbound.ErrTransactionConflict) {
|
|
t.Fatalf("SaveMyProfile() error = %v, want transaction conflict", err)
|
|
}
|
|
if profiles.advanceCalls != 0 {
|
|
t.Fatalf("onboarding advances %d times after a failed write, want 0", profiles.advanceCalls)
|
|
}
|
|
}
|
|
|
|
func TestGetMyProfileReturnsConfirmedCategoriesInCatalogOrder(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
id := uuid.Must(uuid.NewV7())
|
|
user := domain.User{ID: id, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}
|
|
users := &profileTestUsers{user: user}
|
|
profiles := &profileTestRepository{}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
// The storage returns codes in alphabetical order; the catalog starts with backend.
|
|
categories := []domain.CandidateAssessmentCategory{
|
|
profileTestCategory(t, id, domain.SpecializationQA, domain.GradeIntern, now),
|
|
profileTestCategory(t, id, domain.SpecializationBackend, domain.GradeJunior, now),
|
|
profileTestCategory(t, id, domain.SpecializationFrontend, domain.GradeMiddle, now),
|
|
}
|
|
attempts := profileTestAttempts{categories: categories}
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles, attempts: attempts}}, func() time.Time { return now })
|
|
|
|
view, err := service.GetMyProfile(context.Background(), id)
|
|
if err != nil {
|
|
t.Fatalf("GetMyProfile() error = %v", err)
|
|
}
|
|
got := make([]domain.SpecializationCode, 0, len(view.ConfirmedCategories))
|
|
for _, category := range view.ConfirmedCategories {
|
|
got = append(got, category.Snapshot().Specialization)
|
|
}
|
|
want := []domain.SpecializationCode{domain.SpecializationBackend, domain.SpecializationFrontend, domain.SpecializationQA}
|
|
if fmt.Sprint(got) != fmt.Sprint(want) {
|
|
t.Fatalf("confirmed categories = %v, want catalog order %v", got, want)
|
|
}
|
|
if snapshot := view.ConfirmedCategories[0].Snapshot(); snapshot.Source != domain.AssessmentCategorySourceSyntheticDemo || snapshot.Grade != domain.GradeJunior {
|
|
t.Fatalf("first category = %+v, want the synthetic junior category", snapshot)
|
|
}
|
|
if view.Profile.UserID() != id {
|
|
t.Fatalf("profile owner = %s, want %s", view.Profile.UserID(), id)
|
|
}
|
|
}
|
|
|
|
func TestGetMyProfileWithoutCategoriesReturnsAnEmptyList(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
id := uuid.Must(uuid.NewV7())
|
|
users := &profileTestUsers{user: domain.User{ID: id, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}}
|
|
profiles := &profileTestRepository{}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles}}, func() time.Time { return now })
|
|
|
|
view, err := service.GetMyProfile(context.Background(), id)
|
|
if err != nil {
|
|
t.Fatalf("GetMyProfile() error = %v", err)
|
|
}
|
|
if len(view.ConfirmedCategories) != 0 {
|
|
t.Fatalf("confirmed categories = %d, want none", len(view.ConfirmedCategories))
|
|
}
|
|
}
|
|
|
|
func TestGetMyProfileFailsWhenCategoriesCannotBeRead(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
id := uuid.Must(uuid.NewV7())
|
|
users := &profileTestUsers{user: domain.User{ID: id, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}}
|
|
profiles := &profileTestRepository{}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
readErr := errors.New("categories unavailable")
|
|
attempts := profileTestAttempts{err: readErr}
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles, attempts: attempts}}, func() time.Time { return now })
|
|
|
|
if _, err := service.GetMyProfile(context.Background(), id); !errors.Is(err, readErr) {
|
|
t.Fatalf("GetMyProfile() error = %v, want the category read failure", err)
|
|
}
|
|
}
|
|
|
|
func TestSaveMyProfileReturnsTheConfirmedCategoriesAsWell(t *testing.T) {
|
|
now := time.Date(2026, 10, 7, 12, 0, 0, 0, time.UTC)
|
|
id := uuid.Must(uuid.NewV7())
|
|
users := &profileTestUsers{user: domain.User{ID: id, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: &now}}
|
|
profiles := &profileTestRepository{}
|
|
profiles.profile, _ = domain.EmptyCandidateProfile(id)
|
|
attempts := profileTestAttempts{categories: []domain.CandidateAssessmentCategory{
|
|
profileTestCategory(t, id, domain.SpecializationDSML, domain.GradeMiddle, now),
|
|
}}
|
|
service := NewService(profileTestTransactor{unit: profileTestUnit{users: users, profiles: profiles, attempts: attempts}}, func() time.Time { return now })
|
|
input := domain.CandidateProfileInput{FullName: &domain.ProfileClaimInput[string]{Value: "Anna Sokolova", Status: domain.ProfileClaimConfirmed}}
|
|
|
|
view, err := service.SaveMyProfile(context.Background(), id, 0, input)
|
|
if err != nil {
|
|
t.Fatalf("SaveMyProfile() error = %v", err)
|
|
}
|
|
if view.Profile.Version() != 1 || len(view.ConfirmedCategories) != 1 ||
|
|
view.ConfirmedCategories[0].Snapshot().Specialization != domain.SpecializationDSML {
|
|
t.Fatalf("saved view = version %d with categories %v, want version 1 with the ds_ml category", view.Profile.Version(), view.ConfirmedCategories)
|
|
}
|
|
}
|