feat(profile): persist candidate edits and enforce company access

This commit is contained in:
TakuyaYagam1 committed 2026-10-07 14:15:16 +03:00
1 parent 3ab6bd91a3
commit 73c7caa225
67 files changed
+5286 -229

No files matched your search

+195
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"log/slog"
"reflect"
"slices"
"strings"
"testing"
"time"
@@ -283,6 +284,200 @@ func TestResolveCandidateProfileContentNormalizesValues(t *testing.T) {
}
}
func TestResolveCandidateProfileContentRequiresExplicitParallelPeriodAcknowledgements(t *testing.T) {
input := CandidateProfileInput{Experience: confirmedItems(
ProfileExperience{EmployerName: "First employer", Specialization: SpecializationBackend, StartedOn: "2020-01", EndedOn: "2021-06"},
ProfileExperience{EmployerName: "Second employer", Specialization: SpecializationQA, StartedOn: "2021-06", EndedOn: "2022-01"},
)}
assertProfileFieldError(t, resolveErr(CandidateProfileContent{}, input, candidateTestTime), "/experience/1/started_on")
input.ParallelPeriodAcknowledgements = []ProfileParallelPeriodAcknowledgement{{
FirstIndex: 0, SecondIndex: 1,
FirstPeriod: ProfileExperiencePeriod{StartedOn: "2020-01", EndedOn: "2021-06"},
SecondPeriod: ProfileExperiencePeriod{StartedOn: "2021-06", EndedOn: "2022-01"},
}}
content := mustCandidateContent(t, CandidateProfileContent{}, input, candidateTestTime)
overlaps := content.ExperienceOverlaps()
if len(overlaps) != 1 || overlaps[0].FirstIndex != 0 || overlaps[0].SecondIndex != 1 || overlaps[0].Status != ProfileExperienceOverlapConfirmed {
t.Fatalf("overlaps = %+v, want the acknowledged pair at indices 0 and 1", overlaps)
}
}
func TestResolveCandidateProfileContentRejectsInvalidParallelPeriodAcknowledgements(t *testing.T) {
base := CandidateProfileInput{Experience: confirmedItems(
ProfileExperience{EmployerName: "First employer", Specialization: SpecializationBackend, StartedOn: "2020-01", EndedOn: "2020-05"},
ProfileExperience{EmployerName: "Second employer", Specialization: SpecializationQA, StartedOn: "2020-06", EndedOn: "2021-01"},
)}
period := func(startedOn, endedOn string) ProfileExperiencePeriod {
return ProfileExperiencePeriod{StartedOn: startedOn, EndedOn: endedOn}
}
tests := []struct {
name string
confirm ProfileParallelPeriodAcknowledgement
pointer string
}{
{
name: "first period does not match the submitted entry",
confirm: ProfileParallelPeriodAcknowledgement{
FirstIndex: 0, SecondIndex: 1, FirstPeriod: period("2020-02", "2020-05"), SecondPeriod: period("2020-06", "2021-01"),
},
pointer: "/confirmed_parallel_periods/0/first_period/started_on",
},
{
name: "second period does not match the submitted entry",
confirm: ProfileParallelPeriodAcknowledgement{
FirstIndex: 0, SecondIndex: 1, FirstPeriod: period("2020-01", "2020-05"), SecondPeriod: period("2020-05", "2021-01"),
},
pointer: "/confirmed_parallel_periods/0/second_period/started_on",
},
{
name: "self pair",
confirm: ProfileParallelPeriodAcknowledgement{
FirstIndex: 0, SecondIndex: 0, FirstPeriod: period("2020-01", "2020-05"), SecondPeriod: period("2020-01", "2020-05"),
},
pointer: "/confirmed_parallel_periods/0/second_index",
},
{
name: "reversed indices",
confirm: ProfileParallelPeriodAcknowledgement{
FirstIndex: 1, SecondIndex: 0, FirstPeriod: period("2020-06", "2021-01"), SecondPeriod: period("2020-01", "2020-05"),
},
pointer: "/confirmed_parallel_periods/0/second_index",
},
{
name: "out-of-range index",
confirm: ProfileParallelPeriodAcknowledgement{
FirstIndex: 0, SecondIndex: 2, FirstPeriod: period("2020-01", "2020-05"), SecondPeriod: period("2020-06", "2021-01"),
},
pointer: "/confirmed_parallel_periods/0/second_index",
},
{
name: "periods do not overlap",
confirm: ProfileParallelPeriodAcknowledgement{
FirstIndex: 0, SecondIndex: 1, FirstPeriod: period("2020-01", "2020-05"), SecondPeriod: period("2020-06", "2021-01"),
},
pointer: "/confirmed_parallel_periods/0/second_index",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
input := base
input.ParallelPeriodAcknowledgements = []ProfileParallelPeriodAcknowledgement{test.confirm}
assertProfileFieldError(t, resolveErr(CandidateProfileContent{}, input, candidateTestTime), test.pointer)
})
}
}
func TestCandidateProfileReplaceCarriesParallelPeriodAcknowledgementsOnlyForTheSameIndexedDates(t *testing.T) {
userID := uuid.Must(uuid.NewV7())
profile, err := EmptyCandidateProfile(userID)
if err != nil {
t.Fatalf("EmptyCandidateProfile() error = %v", err)
}
input := CandidateProfileInput{Experience: confirmedItems(
ProfileExperience{EmployerName: "First employer", Specialization: SpecializationBackend, StartedOn: "2020-01", EndedOn: "2021-06", Summary: "First"},
ProfileExperience{EmployerName: "Second employer", Specialization: SpecializationQA, StartedOn: "2021-06", EndedOn: "2022-01", Summary: "Second"},
)}
input.ParallelPeriodAcknowledgements = []ProfileParallelPeriodAcknowledgement{{
FirstIndex: 0, SecondIndex: 1,
FirstPeriod: ProfileExperiencePeriod{StartedOn: "2020-01", EndedOn: "2021-06"},
SecondPeriod: ProfileExperiencePeriod{StartedOn: "2021-06", EndedOn: "2022-01"},
}}
profile, changed, err := profile.Replace(input, 0, candidateTestTime)
if err != nil || !changed {
t.Fatalf("Replace(first save) = changed %v, %v, want a confirmed change", changed, err)
}
t.Run("unrelated experience fields preserve acknowledgement", func(t *testing.T) {
edited := input
edited.ParallelPeriodAcknowledgements = nil
edited.Experience = slices.Clone(input.Experience)
edited.Experience[0].Value.Summary = "Updated work"
updated, changed, err := profile.Replace(edited, 0, candidateTestTime.Add(time.Hour))
if err != nil || !changed || updated.Content().ExperienceOverlaps()[0].Status != ProfileExperienceOverlapConfirmed {
t.Fatalf("Replace(summary edit) = changed %v, overlaps %+v, error %v", changed, updated.Content().ExperienceOverlaps(), err)
}
})
t.Run("changed period requires a fresh acknowledgement", func(t *testing.T) {
edited := input
edited.ParallelPeriodAcknowledgements = nil
edited.Experience = slices.Clone(input.Experience)
edited.Experience[0].Value.EndedOn = "2021-07"
assertProfileFieldError(t, errOnly(profile.Replace(edited, 0, candidateTestTime.Add(time.Hour))), "/experience/1/started_on")
})
t.Run("reordered periods require a fresh acknowledgement", func(t *testing.T) {
edited := input
edited.ParallelPeriodAcknowledgements = nil
edited.Experience = []ProfileClaimInput[ProfileExperience]{input.Experience[1], input.Experience[0]}
assertProfileFieldError(t, errOnly(profile.Replace(edited, 0, candidateTestTime.Add(time.Hour))), "/experience/1/started_on")
})
t.Run("changed period can be explicitly acknowledged", func(t *testing.T) {
edited := input
edited.Experience = slices.Clone(input.Experience)
edited.Experience[0].Value.EndedOn = "2021-07"
edited.ParallelPeriodAcknowledgements = []ProfileParallelPeriodAcknowledgement{{
FirstIndex: 0, SecondIndex: 1,
FirstPeriod: ProfileExperiencePeriod{StartedOn: "2020-01", EndedOn: "2021-07"},
SecondPeriod: ProfileExperiencePeriod{StartedOn: "2021-06", EndedOn: "2022-01"},
}}
updated, changed, err := profile.Replace(edited, 0, candidateTestTime.Add(time.Hour))
if err != nil || !changed || updated.Content().ExperienceOverlaps()[0].Status != ProfileExperienceOverlapConfirmed {
t.Fatalf("Replace(confirmed period edit) = changed %v, overlaps %+v, error %v", changed, updated.Content().ExperienceOverlaps(), err)
}
})
}
func TestCandidateProfileWithStoredUnconfirmedOverlapOnlyAllowsNoOpOrExplicitConfirmation(t *testing.T) {
profile, err := RestoreCandidateProfile(StoredCandidateProfile{
UserID: uuid.Must(uuid.NewV7()), Version: 1,
CreatedAt: candidateTestTime, UpdatedAt: candidateTestTime,
Claims: []StoredProfileClaim{
{
Kind: profileKindExperience, Ordinal: 0,
Value: []byte(`{"employer_name":"First","specialization":"backend","started_on":"2020-01","ended_on":"2021-06","summary":null}`),
Status: string(ProfileClaimConfirmed), Origin: string(ProfileOriginManual), TrustLevel: 0, ConfirmedAt: candidateTestTime,
},
{
Kind: profileKindExperience, Ordinal: 1,
Value: []byte(`{"employer_name":"Second","specialization":"qa","started_on":"2021-06","ended_on":"2022-01","summary":null}`),
Status: string(ProfileClaimConfirmed), Origin: string(ProfileOriginManual), TrustLevel: 0, ConfirmedAt: candidateTestTime,
},
},
})
if err != nil {
t.Fatalf("RestoreCandidateProfile() error = %v", err)
}
input := CandidateProfileInput{Experience: confirmedItems(
ProfileExperience{EmployerName: "First", Specialization: SpecializationBackend, StartedOn: "2020-01", EndedOn: "2021-06"},
ProfileExperience{EmployerName: "Second", Specialization: SpecializationQA, StartedOn: "2021-06", EndedOn: "2022-01"},
)}
if unchanged, changed, err := profile.Replace(input, 0, candidateTestTime.Add(time.Hour)); err != nil || changed || unchanged.Version() != profile.Version() {
t.Fatalf("Replace(no-op retry) = version %d, changed %v, error %v", unchanged.Version(), changed, err)
}
changedName := input
changedName.FullName = confirmed("Anna Sokolova")
assertProfileFieldError(t, errOnly(profile.Replace(changedName, 1, candidateTestTime.Add(time.Hour))), "/experience/1/started_on")
if _, _, err := profile.Replace(changedName, 0, candidateTestTime.Add(time.Hour)); !errors.Is(err, ErrCandidateProfileVersionConflict) {
t.Fatalf("Replace(stale changed profile) error = %v, want version conflict", err)
}
input.ParallelPeriodAcknowledgements = []ProfileParallelPeriodAcknowledgement{{
FirstIndex: 0, SecondIndex: 1,
FirstPeriod: ProfileExperiencePeriod{StartedOn: "2020-01", EndedOn: "2021-06"},
SecondPeriod: ProfileExperiencePeriod{StartedOn: "2021-06", EndedOn: "2022-01"},
}}
confirmedProfile, changed, err := profile.Replace(input, 1, candidateTestTime.Add(time.Hour))
if err != nil || !changed || confirmedProfile.Content().ExperienceOverlaps()[0].Status != ProfileExperienceOverlapConfirmed {
t.Fatalf("Replace(explicit confirmation) = changed %v, error %v", changed, err)
}
}
func TestCandidateProfileReplaceTellsAChangeFromANoOp(t *testing.T) {
userID := uuid.Must(uuid.NewV7())
empty, err := EmptyCandidateProfile(userID)