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

+383 -64
View File
@@ -343,6 +343,56 @@ func (ProfileExperience) LogValue() slog.Value {
}
func (ProfileExperience) Format(state fmt.State, _ rune) { writeRedacted(state, redactedProfile) }
// ProfileExperiencePeriod is the exact month range used to acknowledge
// overlapping work. Its dates are personal data, so formatting redacts them.
type ProfileExperiencePeriod struct {
StartedOn string `json:"started_on"`
EndedOn string `json:"ended_on"`
}
func (ProfileExperiencePeriod) String() string { return redactedProfile }
func (ProfileExperiencePeriod) GoString() string { return redactedProfile }
func (ProfileExperiencePeriod) LogValue() slog.Value {
return slog.StringValue(redactedProfile)
}
func (ProfileExperiencePeriod) Format(state fmt.State, _ rune) { writeRedacted(state, redactedProfile) }
// ProfileParallelPeriodAcknowledgement records that the candidate reviewed
// the exact dates of two overlapping experience entries at their indices.
// Employer names and summaries are not part of the decision.
type ProfileParallelPeriodAcknowledgement struct {
FirstIndex int `json:"first_index"`
SecondIndex int `json:"second_index"`
FirstPeriod ProfileExperiencePeriod `json:"first_period"`
SecondPeriod ProfileExperiencePeriod `json:"second_period"`
}
func (ProfileParallelPeriodAcknowledgement) String() string { return redactedProfile }
func (ProfileParallelPeriodAcknowledgement) GoString() string { return redactedProfile }
func (ProfileParallelPeriodAcknowledgement) LogValue() slog.Value {
return slog.StringValue(redactedProfile)
}
func (ProfileParallelPeriodAcknowledgement) Format(state fmt.State, _ rune) {
writeRedacted(state, redactedProfile)
}
// ProfileExperienceOverlapStatus reports whether the candidate acknowledged
// an overlapping pair of periods in the current profile version.
type ProfileExperienceOverlapStatus string
const (
ProfileExperienceOverlapNeedsConfirmation ProfileExperienceOverlapStatus = "needs_confirmation"
ProfileExperienceOverlapConfirmed ProfileExperienceOverlapStatus = "confirmed"
)
// ProfileExperienceOverlap identifies two entries whose inclusive month
// ranges overlap. Indices refer to one profile version's experience list.
type ProfileExperienceOverlap struct {
FirstIndex int
SecondIndex int
Status ProfileExperienceOverlapStatus
}
// ProfileTaxBasis is the tax basis of a salary amount.
type ProfileTaxBasis string
@@ -379,17 +429,20 @@ type ProfileClaimInput[V comparable] struct {
// gave it. A nil single field is unknown and removes the stored value; a list
// replaces the stored list. It holds personal data: never log it.
type CandidateProfileInput struct {
FullName *ProfileClaimInput[string]
Email *ProfileClaimInput[string]
Phone *ProfileClaimInput[string]
Telegram *ProfileClaimInput[string]
Stack []ProfileClaimInput[string]
Roles []ProfileClaimInput[ProfileRole]
Experience []ProfileClaimInput[ProfileExperience]
SoftSkills []ProfileClaimInput[string]
Location *ProfileClaimInput[string]
Relocation *ProfileClaimInput[bool]
SalaryMinimum *ProfileClaimInput[ProfileSalaryMinimum]
FullName *ProfileClaimInput[string]
Email *ProfileClaimInput[string]
Phone *ProfileClaimInput[string]
Telegram *ProfileClaimInput[string]
Stack []ProfileClaimInput[string]
Roles []ProfileClaimInput[ProfileRole]
Experience []ProfileClaimInput[ProfileExperience]
// ParallelPeriodAcknowledgements contains explicit acknowledgements for
// the exact periods and indices in Experience.
ParallelPeriodAcknowledgements []ProfileParallelPeriodAcknowledgement
SoftSkills []ProfileClaimInput[string]
Location *ProfileClaimInput[string]
Relocation *ProfileClaimInput[bool]
SalaryMinimum *ProfileClaimInput[ProfileSalaryMinimum]
}
// CandidateProfileContent is the validated content of a profile: every claim
@@ -398,17 +451,18 @@ type CandidateProfileInput struct {
// and no reference to a file, the ML service, or an FSP account. It is
// personal data: String, GoString, Format, and LogValue redact it.
type CandidateProfileContent struct {
fullName *ProfileClaim[string]
email *ProfileClaim[string]
phone *ProfileClaim[string]
telegram *ProfileClaim[string]
stack []ProfileClaim[string]
roles []ProfileClaim[ProfileRole]
experience []ProfileClaim[ProfileExperience]
softSkills []ProfileClaim[string]
location *ProfileClaim[string]
relocation *ProfileClaim[bool]
salaryMinimum *ProfileClaim[ProfileSalaryMinimum]
fullName *ProfileClaim[string]
email *ProfileClaim[string]
phone *ProfileClaim[string]
telegram *ProfileClaim[string]
stack []ProfileClaim[string]
roles []ProfileClaim[ProfileRole]
experience []ProfileClaim[ProfileExperience]
parallelPeriodAcknowledgements []ProfileParallelPeriodAcknowledgement
softSkills []ProfileClaim[string]
location *ProfileClaim[string]
relocation *ProfileClaim[bool]
salaryMinimum *ProfileClaim[ProfileSalaryMinimum]
}
func optionalClaim[V comparable](claim *ProfileClaim[V]) (ProfileClaim[V], bool) {
@@ -469,6 +523,35 @@ func (content CandidateProfileContent) Experience() []ProfileClaim[ProfileExperi
return slices.Clone(content.experience)
}
// ParallelPeriodAcknowledgements returns a copy of the stored date-pair
// decisions for this profile version.
func (content CandidateProfileContent) ParallelPeriodAcknowledgements() []ProfileParallelPeriodAcknowledgement {
return slices.Clone(content.parallelPeriodAcknowledgements)
}
// ExperienceOverlaps returns every overlapping pair in index order. A pair's
// status is derived from an acknowledgement that still matches both periods.
func (content CandidateProfileContent) ExperienceOverlaps() []ProfileExperienceOverlap {
var overlaps []ProfileExperienceOverlap
acknowledged := make(map[experiencePair]struct{}, len(content.parallelPeriodAcknowledgements))
for _, acknowledgement := range content.parallelPeriodAcknowledgements {
acknowledged[experiencePair{first: acknowledgement.FirstIndex, second: acknowledgement.SecondIndex}] = struct{}{}
}
for first := range content.experience {
for second := first + 1; second < len(content.experience); second++ {
if !experiencePeriodsOverlap(content.experience[first].value, content.experience[second].value) {
continue
}
status := ProfileExperienceOverlapNeedsConfirmation
if _, ok := acknowledged[experiencePair{first: first, second: second}]; ok {
status = ProfileExperienceOverlapConfirmed
}
overlaps = append(overlaps, ProfileExperienceOverlap{FirstIndex: first, SecondIndex: second, Status: status})
}
}
return overlaps
}
// SoftSkills returns a copy of the soft skills in the order the candidate
// gave them.
func (content CandidateProfileContent) SoftSkills() []ProfileClaim[string] {
@@ -488,6 +571,7 @@ func (content CandidateProfileContent) Equal(other CandidateProfileContent) bool
slices.EqualFunc(content.stack, other.stack, claimsEqual) &&
slices.EqualFunc(content.roles, other.roles, claimsEqual) &&
slices.EqualFunc(content.experience, other.experience, claimsEqual) &&
slices.Equal(content.parallelPeriodAcknowledgements, other.parallelPeriodAcknowledgements) &&
slices.EqualFunc(content.softSkills, other.softSkills, claimsEqual)
}
@@ -730,6 +814,166 @@ func newProfileRules(now time.Time) profileRules {
}
}
type experiencePair struct {
first int
second int
}
func experiencePeriod(value ProfileExperience) ProfileExperiencePeriod {
return ProfileExperiencePeriod{StartedOn: value.StartedOn, EndedOn: value.EndedOn}
}
func experiencePeriodsOverlap(first, second ProfileExperience) bool {
return (first.EndedOn == "" || second.StartedOn <= first.EndedOn) &&
(second.EndedOn == "" || first.StartedOn <= second.EndedOn)
}
func invalidParallelPeriodField(location string, index int, pointer, reason string) error {
return invalidProfileField(location, fmt.Sprintf("/confirmed_parallel_periods/%d/%s", index, pointer), reason)
}
func resolveParallelPeriodAcknowledgements(
stored CandidateProfileContent,
experiences []ProfileClaim[ProfileExperience],
requested []ProfileParallelPeriodAcknowledgement,
location string,
) ([]ProfileParallelPeriodAcknowledgement, error) {
const maxPairs = maxProfileExperienceItems * (maxProfileExperienceItems - 1) / 2
if len(requested) > maxPairs {
return nil, invalidProfileField(location, "/confirmed_parallel_periods", "must not contain more pairs than the experience list allows")
}
acknowledgements := make(map[experiencePair]ProfileParallelPeriodAcknowledgement, len(requested)+len(stored.parallelPeriodAcknowledgements))
requestedPairs := make(map[experiencePair]struct{}, len(requested))
for index, acknowledgement := range requested {
if err := addRequestedParallelPeriodAcknowledgement(
acknowledgement, experiences, index, location, requestedPairs, acknowledgements,
); err != nil {
return nil, err
}
}
previousOverlaps := overlappingExperiencePairs(stored.experience)
carryMatchingParallelPeriodAcknowledgements(stored.parallelPeriodAcknowledgements, experiences, acknowledgements)
return resolveCurrentExperienceOverlaps(experiences, previousOverlaps, acknowledgements, location)
}
func addRequestedParallelPeriodAcknowledgement(
acknowledgement ProfileParallelPeriodAcknowledgement,
experiences []ProfileClaim[ProfileExperience],
index int,
location string,
requestedPairs map[experiencePair]struct{},
acknowledgements map[experiencePair]ProfileParallelPeriodAcknowledgement,
) error {
pair := experiencePair{first: acknowledgement.FirstIndex, second: acknowledgement.SecondIndex}
if pair.first < 0 || pair.first >= len(experiences) {
return invalidParallelPeriodField(location, index, "first_index", "must refer to an experience entry")
}
if pair.second <= pair.first || pair.second >= len(experiences) {
return invalidParallelPeriodField(location, index, "second_index", "must refer to a later experience entry")
}
if _, duplicate := requestedPairs[pair]; duplicate {
return invalidParallelPeriodField(location, index, "second_index", "must not repeat a pair")
}
requestedPairs[pair] = struct{}{}
first := experiences[pair.first].value
second := experiences[pair.second].value
firstPeriod := experiencePeriod(first)
secondPeriod := experiencePeriod(second)
if acknowledgement.FirstPeriod.StartedOn != firstPeriod.StartedOn {
return invalidParallelPeriodField(location, index, "first_period/started_on", "must match the indexed experience period")
}
if acknowledgement.FirstPeriod.EndedOn != firstPeriod.EndedOn {
return invalidParallelPeriodField(location, index, "first_period/ended_on", "must match the indexed experience period")
}
if acknowledgement.SecondPeriod.StartedOn != secondPeriod.StartedOn {
return invalidParallelPeriodField(location, index, "second_period/started_on", "must match the indexed experience period")
}
if acknowledgement.SecondPeriod.EndedOn != secondPeriod.EndedOn {
return invalidParallelPeriodField(location, index, "second_period/ended_on", "must match the indexed experience period")
}
if !experiencePeriodsOverlap(first, second) {
return invalidParallelPeriodField(location, index, "second_index", "must identify an overlapping period")
}
acknowledgements[pair] = ProfileParallelPeriodAcknowledgement{
FirstIndex: pair.first, SecondIndex: pair.second, FirstPeriod: firstPeriod, SecondPeriod: secondPeriod,
}
return nil
}
func overlappingExperiencePairs(experiences []ProfileClaim[ProfileExperience]) map[experiencePair]ProfileParallelPeriodAcknowledgement {
previous := make(map[experiencePair]ProfileParallelPeriodAcknowledgement)
for first := range experiences {
for second := first + 1; second < len(experiences); second++ {
firstValue := experiences[first].value
secondValue := experiences[second].value
if !experiencePeriodsOverlap(firstValue, secondValue) {
continue
}
previous[experiencePair{first: first, second: second}] = ProfileParallelPeriodAcknowledgement{
FirstIndex: first, SecondIndex: second,
FirstPeriod: experiencePeriod(firstValue), SecondPeriod: experiencePeriod(secondValue),
}
}
}
return previous
}
func carryMatchingParallelPeriodAcknowledgements(
stored []ProfileParallelPeriodAcknowledgement,
experiences []ProfileClaim[ProfileExperience],
acknowledgements map[experiencePair]ProfileParallelPeriodAcknowledgement,
) {
for _, previous := range stored {
pair := experiencePair{first: previous.FirstIndex, second: previous.SecondIndex}
if pair.first < 0 || pair.second < 0 || pair.first >= len(experiences) || pair.second >= len(experiences) {
continue
}
first := experiences[pair.first].value
second := experiences[pair.second].value
if experiencePeriod(first) != previous.FirstPeriod || experiencePeriod(second) != previous.SecondPeriod ||
!experiencePeriodsOverlap(first, second) {
continue
}
if _, explicitlyAcknowledged := acknowledgements[pair]; !explicitlyAcknowledged {
acknowledgements[pair] = previous
}
}
}
func resolveCurrentExperienceOverlaps(
experiences []ProfileClaim[ProfileExperience],
previousOverlaps map[experiencePair]ProfileParallelPeriodAcknowledgement,
acknowledgements map[experiencePair]ProfileParallelPeriodAcknowledgement,
location string,
) ([]ProfileParallelPeriodAcknowledgement, error) {
resolved := make([]ProfileParallelPeriodAcknowledgement, 0, len(acknowledgements))
for first := range experiences {
for second := first + 1; second < len(experiences); second++ {
firstValue := experiences[first].value
secondValue := experiences[second].value
if !experiencePeriodsOverlap(firstValue, secondValue) {
continue
}
pair := experiencePair{first: first, second: second}
if acknowledgement, ok := acknowledgements[pair]; ok {
resolved = append(resolved, acknowledgement)
continue
}
previous, existed := previousOverlaps[pair]
if existed && previous.FirstPeriod == experiencePeriod(firstValue) && previous.SecondPeriod == experiencePeriod(secondValue) {
continue
}
return nil, invalidStateDependentProfileField(location,
fmt.Sprintf("/experience/%d/started_on", second),
"overlaps another period and needs an explicit confirmation",
)
}
}
return resolved, nil
}
// ResolveCandidateProfileContent applies a request to the stored content and
// returns the new content. It validates every value, resolves each decision
// against the stored claim (see claimRules.resolve), and keeps a stored claim
@@ -798,6 +1042,12 @@ func ResolveCandidateProfileContent(stored CandidateProfileContent, input Candid
return CandidateProfileContent{}, err
}
}
content.parallelPeriodAcknowledgements, err = resolveParallelPeriodAcknowledgements(
stored, content.experience, input.ParallelPeriodAcknowledgements, location,
)
if err != nil {
return CandidateProfileContent{}, fmt.Errorf("%s - resolve parallel period acknowledgements: %w", location, err)
}
return content, nil
}
@@ -873,6 +1123,12 @@ func (profile CandidateProfile) Replace(input CandidateProfileInput, expectedVer
if content.Equal(profile.content) {
return profile, false, nil
}
if err := requireConfirmedExperienceOverlaps(content, location); err != nil {
if expectedVersion != profile.version {
return CandidateProfile{}, false, conflict
}
return CandidateProfile{}, false, err
}
if expectedVersion != profile.version {
return CandidateProfile{}, false, conflict
}
@@ -888,6 +1144,18 @@ func (profile CandidateProfile) Replace(input CandidateProfileInput, expectedVer
return updated, true, nil
}
func requireConfirmedExperienceOverlaps(content CandidateProfileContent, location string) error {
for _, overlap := range content.ExperienceOverlaps() {
if overlap.Status == ProfileExperienceOverlapNeedsConfirmation {
return invalidStateDependentProfileField(location,
fmt.Sprintf("/experience/%d/started_on", overlap.SecondIndex),
"overlaps another period and needs an explicit confirmation",
)
}
}
return nil
}
func (CandidateProfile) String() string { return redactedProfile }
func (CandidateProfile) GoString() string { return redactedProfile }
func (CandidateProfile) Format(state fmt.State, _ rune) {
@@ -924,15 +1192,15 @@ const (
// ConfirmedAt is a NULL column, and an empty Quote or ExtractionVersion is a
// NULL one.
type StoredProfileClaim struct {
Kind string
Ordinal int
Value []byte
Status string
Origin string
TrustLevel int
Quote string
ExtractionVersion string
ConfirmedAt time.Time
Kind string `json:"kind"`
Ordinal int `json:"ordinal"`
Value []byte `json:"value"`
Status string `json:"status"`
Origin string `json:"origin"`
TrustLevel int `json:"trust_level"`
Quote string `json:"quote"`
ExtractionVersion string `json:"extraction_version"`
ConfirmedAt time.Time `json:"confirmed_at"`
}
// The JSON shapes of structured values. Their keys are the ones that the
@@ -1053,6 +1321,16 @@ func (content CandidateProfileContent) StoredClaims() ([]StoredProfileClaim, err
// any order; a list is ordered by Ordinal and must have no gap, and a single
// field must appear once. The error never quotes a value.
func RestoreCandidateProfileContent(rows []StoredProfileClaim) (CandidateProfileContent, error) {
return RestoreCandidateProfileContentWithParallelPeriods(rows, nil)
}
// RestoreCandidateProfileContentWithParallelPeriods also restores the
// explicit date-pair decisions stored for this profile. Existing profiles may
// contain unresolved overlaps, which are returned for the owner to resolve.
func RestoreCandidateProfileContentWithParallelPeriods(
rows []StoredProfileClaim,
acknowledgements []ProfileParallelPeriodAcknowledgement,
) (CandidateProfileContent, error) {
const location = "domain.RestoreCandidateProfileContent"
rules := newProfileRules(time.Time{})
fail := func(row StoredProfileClaim, reason string) error {
@@ -1070,39 +1348,78 @@ func RestoreCandidateProfileContent(rows []StoredProfileClaim) (CandidateProfile
return CandidateProfileContent{}, fail(row, "has invalid provenance")
}
status := ProfileClaimStatus(row.Status)
switch row.Kind {
case profileKindFullName:
err = restoreSingle(&content.fullName, rules.fullName, row, status, source)
case profileKindEmail:
err = restoreSingle(&content.email, rules.email, row, status, source)
case profileKindPhone:
err = restoreSingle(&content.phone, rules.phone, row, status, source)
case profileKindTelegram:
err = restoreSingle(&content.telegram, rules.telegram, row, status, source)
case profileKindLocation:
err = restoreSingle(&content.location, rules.location, row, status, source)
case profileKindRelocation:
err = restoreSingle(&content.relocation, rules.relocation, row, status, source)
case profileKindSalaryMinimum:
err = restoreSingle(&content.salaryMinimum, rules.salaryMinimum, row, status, source)
case profileKindStack:
err = restoreItem(&content.stack, rules.stack, row, status, source)
case profileKindSoftSkill:
err = restoreItem(&content.softSkills, rules.softSkills, row, status, source)
case profileKindRole:
err = restoreItem(&content.roles, rules.roles, row, status, source)
case profileKindExperience:
err = restoreItem(&content.experience, rules.experience, row, status, source)
default:
err = errors.New("kind is unknown")
}
err = restoreCandidateProfileClaim(&content, rules, row, status, source)
if err != nil {
return CandidateProfileContent{}, fail(row, err.Error())
}
}
content.parallelPeriodAcknowledgements = slices.Clone(acknowledgements)
if err := validateStoredParallelPeriodAcknowledgements(content.experience, content.parallelPeriodAcknowledgements); err != nil {
return CandidateProfileContent{}, fmt.Errorf("%s - validate parallel period acknowledgements: %w", location, err)
}
return content, nil
}
func restoreCandidateProfileClaim(
content *CandidateProfileContent,
rules profileRules,
row StoredProfileClaim,
status ProfileClaimStatus,
source ProfileClaimSource,
) error {
switch row.Kind {
case profileKindFullName:
return restoreSingle(&content.fullName, rules.fullName, row, status, source)
case profileKindEmail:
return restoreSingle(&content.email, rules.email, row, status, source)
case profileKindPhone:
return restoreSingle(&content.phone, rules.phone, row, status, source)
case profileKindTelegram:
return restoreSingle(&content.telegram, rules.telegram, row, status, source)
case profileKindLocation:
return restoreSingle(&content.location, rules.location, row, status, source)
case profileKindRelocation:
return restoreSingle(&content.relocation, rules.relocation, row, status, source)
case profileKindSalaryMinimum:
return restoreSingle(&content.salaryMinimum, rules.salaryMinimum, row, status, source)
case profileKindStack:
return restoreItem(&content.stack, rules.stack, row, status, source)
case profileKindSoftSkill:
return restoreItem(&content.softSkills, rules.softSkills, row, status, source)
case profileKindRole:
return restoreItem(&content.roles, rules.roles, row, status, source)
case profileKindExperience:
return restoreItem(&content.experience, rules.experience, row, status, source)
default:
return errors.New("kind is unknown")
}
}
func validateStoredParallelPeriodAcknowledgements(
experiences []ProfileClaim[ProfileExperience],
acknowledgements []ProfileParallelPeriodAcknowledgement,
) error {
const location = "domain.validateStoredParallelPeriodAcknowledgements"
seen := make(map[experiencePair]struct{}, len(acknowledgements))
for _, acknowledgement := range acknowledgements {
pair := experiencePair{first: acknowledgement.FirstIndex, second: acknowledgement.SecondIndex}
if pair.first < 0 || pair.second <= pair.first || pair.second >= len(experiences) {
return fmt.Errorf("%s - pair indices are invalid: %w", location, ErrInvalidCandidateProfile)
}
if _, duplicate := seen[pair]; duplicate {
return fmt.Errorf("%s - pair is duplicated: %w", location, ErrInvalidCandidateProfile)
}
seen[pair] = struct{}{}
first := experiences[pair.first].value
second := experiences[pair.second].value
if experiencePeriod(first) != acknowledgement.FirstPeriod ||
experiencePeriod(second) != acknowledgement.SecondPeriod || !experiencePeriodsOverlap(first, second) {
return fmt.Errorf("%s - pair does not match the stored experience periods: %w", location, ErrInvalidCandidateProfile)
}
}
return nil
}
// decodeStoredValue decodes the canonical JSON of a stored value into V.
func decodeStoredValue[V comparable](row StoredProfileClaim) (V, error) {
var value V
@@ -1202,13 +1519,15 @@ func restoreItem[V comparable](
return nil
}
// StoredCandidateProfile carries the profile row and its claims.
// StoredCandidateProfile carries the profile row, claims, and acknowledged
// overlapping date pairs.
type StoredCandidateProfile struct {
UserID uuid.UUID
Version int64
CreatedAt time.Time
UpdatedAt time.Time
Claims []StoredProfileClaim
UserID uuid.UUID
Version int64
CreatedAt time.Time
UpdatedAt time.Time
Claims []StoredProfileClaim
ParallelPeriodAcknowledgements []ProfileParallelPeriodAcknowledgement
}
// RestoreCandidateProfile validates a stored profile with the rules of the
@@ -1224,7 +1543,7 @@ func RestoreCandidateProfile(stored StoredCandidateProfile) (CandidateProfile, e
case stored.CreatedAt.IsZero() || stored.UpdatedAt.Before(stored.CreatedAt):
return CandidateProfile{}, fmt.Errorf("%s - times must be set and ordered: %w", location, ErrInvalidCandidateProfile)
}
content, err := RestoreCandidateProfileContent(stored.Claims)
content, err := RestoreCandidateProfileContentWithParallelPeriods(stored.Claims, stored.ParallelPeriodAcknowledgements)
if err != nil {
return CandidateProfile{}, fmt.Errorf("%s - restore content: %w", location, err)
}
+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)