feat(invitations): add organization invitation drafts with salary range rules
This commit is contained in:
1 parent
28337d84cb
commit
bd23c839fd
36 files changed
+7018
-83
No files matched your search
@@ -0,0 +1,296 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func invitationTestString(value string) *string { return &value }
|
||||
|
||||
func validInvitationTerms() InvitationTerms {
|
||||
provided := func(text string) InvitationPackageItem {
|
||||
return InvitationPackageItem{Status: InvitationPackageProvided, Description: invitationTestString(text)}
|
||||
}
|
||||
return InvitationTerms{
|
||||
RoleTitle: "Middle Go developer",
|
||||
Specialization: SpecializationBackend,
|
||||
Grade: GradeMiddle,
|
||||
OfferType: InvitationOfferFullTime,
|
||||
Tasks: []string{"Move the payout service to an event model"},
|
||||
Team: "Six engineers\nworking in two-week iterations",
|
||||
TechStack: []string{"Go", "PostgreSQL"},
|
||||
WorkFormats: []NeedWorkFormat{NeedWorkFormatRemote},
|
||||
CommunicationMethod: InvitationCommunicationMethod{
|
||||
Channel: InvitationChannelEmail,
|
||||
},
|
||||
Salary: SalaryRange{SalaryFrom: 100_000, SalaryTo: 140_000, Currency: SalaryCurrencyRUB, Period: SalaryPeriodMonth, TaxBasis: SalaryTaxNet},
|
||||
Package: InvitationPackage{
|
||||
Bonus: provided("Quarterly bonus"),
|
||||
Equipment: provided("Laptop"),
|
||||
Training: InvitationPackageItem{Status: InvitationPackageNotProvided},
|
||||
HealthInsurance: InvitationPackageItem{Status: InvitationPackageNotProvided},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func validInvitationDraftInput() NewInvitationDraftInput {
|
||||
policyID, _ := NewPolicyVersionID()
|
||||
return NewInvitationDraftInput{
|
||||
ID: uuid.New(), OrganizationID: uuid.New(), CandidateID: uuid.New(), ActorID: uuid.New(),
|
||||
PolicyVersionID: policyID,
|
||||
Content: InvitationDraftContent{ResponsePeriod: 7, Terms: validInvitationTerms()},
|
||||
At: time.Date(2026, 10, 9, 12, 0, 0, 123456789, time.UTC),
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewInvitationDraftStartsAtVersionOne(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
snapshot := draft.Snapshot()
|
||||
if snapshot.State != InvitationDraft || snapshot.Version != 1 || snapshot.TermsVersion != 1 ||
|
||||
snapshot.ResponseDeadlineAt != nil || snapshot.DeliveredAt != nil || snapshot.SalaryRaiseUsed || snapshot.ExpiryReason != nil {
|
||||
t.Fatalf("draft snapshot = %+v, want an unsent version 1", snapshot)
|
||||
}
|
||||
event, err := draft.DraftEvent(input.ActorID, []InvitationChangedField{InvitationChangedTerms})
|
||||
if err != nil {
|
||||
t.Fatalf("DraftEvent() error = %v", err)
|
||||
}
|
||||
if event.Kind != InvitationEventDraftCreated || event.Version != 1 || len(event.ChangedFields) != 0 {
|
||||
t.Fatalf("creation event = %+v, want draft_created at version 1 with no changed fields", event)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvitationTermsValidationPointsAtTheFailingField(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
change func(*InvitationTerms)
|
||||
wantPointer string
|
||||
}{
|
||||
{"role title with a line break", func(t *InvitationTerms) { t.RoleTitle = "Go\ndeveloper" }, "/terms/role_title"},
|
||||
{"role title with a NUL", func(t *InvitationTerms) { t.RoleTitle = "Go\x00developer" }, "/terms/role_title"},
|
||||
{"role title with edge whitespace", func(t *InvitationTerms) { t.RoleTitle = " Go developer" }, "/terms/role_title"},
|
||||
{"unknown specialization", func(t *InvitationTerms) { t.Specialization = "wizard" }, "/terms/specialization"},
|
||||
{"unknown grade", func(t *InvitationTerms) { t.Grade = "guru" }, "/terms/grade"},
|
||||
{"unknown offer type", func(t *InvitationTerms) { t.OfferType = "volunteer" }, "/terms/offer_type"},
|
||||
{"no tasks", func(t *InvitationTerms) { t.Tasks = nil }, "/terms/tasks"},
|
||||
{"empty second task", func(t *InvitationTerms) { t.Tasks = []string{"First", ""} }, "/terms/tasks/1"},
|
||||
{"empty team", func(t *InvitationTerms) { t.Team = "" }, "/terms/team"},
|
||||
{"repeated technology ignoring case", func(t *InvitationTerms) { t.TechStack = []string{"Go", "PostgreSQL", "GO"} }, "/terms/tech_stack/2"},
|
||||
{"repeated work format", func(t *InvitationTerms) {
|
||||
t.WorkFormats = []NeedWorkFormat{NeedWorkFormatRemote, NeedWorkFormatRemote}
|
||||
t.Location = invitationTestString("Moscow")
|
||||
}, "/terms/work_formats/1"},
|
||||
{"office without a place", func(t *InvitationTerms) { t.WorkFormats = []NeedWorkFormat{NeedWorkFormatOffice} }, "/terms/location"},
|
||||
{"hybrid among remote without a place", func(t *InvitationTerms) {
|
||||
t.WorkFormats = []NeedWorkFormat{NeedWorkFormatRemote, NeedWorkFormatHybrid}
|
||||
}, "/terms/location"},
|
||||
{"unknown channel", func(t *InvitationTerms) { t.CommunicationMethod.Channel = "pigeon" }, "/terms/communication_method/channel"},
|
||||
{"empty communication note", func(t *InvitationTerms) { t.CommunicationMethod.Details = invitationTestString("") }, "/terms/communication_method/details"},
|
||||
{"reversed salary", func(t *InvitationTerms) { t.Salary.SalaryFrom, t.Salary.SalaryTo = 140_000, 100_000 }, "/terms/salary/salary_to"},
|
||||
{"salary without a basis", func(t *InvitationTerms) { t.Salary.TaxBasis = "" }, "/terms/salary/tax_basis"},
|
||||
{"zero lower bound", func(t *InvitationTerms) { t.Salary.SalaryFrom = 0 }, "/terms/salary/salary_from"},
|
||||
{"lower bound off the step", func(t *InvitationTerms) { t.Salary.SalaryFrom = 100_500 }, "/terms/salary/salary_from"},
|
||||
{"bonus left without a status", func(t *InvitationTerms) { t.Package.Bonus = InvitationPackageItem{} }, "/terms/package/bonus/status"},
|
||||
{"provided without a description", func(t *InvitationTerms) {
|
||||
t.Package.Training = InvitationPackageItem{Status: InvitationPackageProvided}
|
||||
}, "/terms/package/training/description"},
|
||||
{"not provided with a description", func(t *InvitationTerms) {
|
||||
t.Package.HealthInsurance = InvitationPackageItem{Status: InvitationPackageNotProvided, Description: invitationTestString("Premium plan")}
|
||||
}, "/terms/package/health_insurance"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
terms := validInvitationTerms()
|
||||
test.change(&terms)
|
||||
var field *InvitationFieldError
|
||||
err := terms.Validate()
|
||||
if !errors.As(err, &field) || field.Pointer != test.wantPointer || !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("Validate() error = %v, want a field error at %s", err, test.wantPointer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Every package condition may explicitly be "not provided"; the explicit
|
||||
// choice is a complete package.
|
||||
func TestInvitationPackageMayDeclareEveryConditionNotProvided(t *testing.T) {
|
||||
terms := validInvitationTerms()
|
||||
none := InvitationPackageItem{Status: InvitationPackageNotProvided}
|
||||
terms.Package = InvitationPackage{Bonus: none, Equipment: none, Training: none, HealthInsurance: none}
|
||||
if err := terms.Validate(); err != nil {
|
||||
t.Fatalf("Validate() error = %v, want an explicit empty package to be valid", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvitationTermsRoundTripThroughTheStoredSnapshot(t *testing.T) {
|
||||
terms := validInvitationTerms()
|
||||
terms.Location = invitationTestString("Kazan")
|
||||
encoded, err := EncodeInvitationTerms(terms)
|
||||
if err != nil {
|
||||
t.Fatalf("EncodeInvitationTerms() error = %v", err)
|
||||
}
|
||||
decoded, err := DecodeInvitationTerms(encoded)
|
||||
if err != nil || !decoded.Equal(terms) {
|
||||
t.Fatalf("DecodeInvitationTerms() = %+v, %v; want the same terms", decoded, err)
|
||||
}
|
||||
var generic map[string]any
|
||||
if err := json.Unmarshal(encoded, &generic); err != nil {
|
||||
t.Fatalf("decode snapshot: %v", err)
|
||||
}
|
||||
salary, _ := generic["salary"].(map[string]any)
|
||||
if salary["salary_from"] != float64(100000) || salary["currency"] != "RUB" || salary["tax_basis"] != "net" {
|
||||
t.Fatalf("snapshot salary = %v, want whole rubles with their basis", salary)
|
||||
}
|
||||
if _, present := generic["candidate_email"]; present {
|
||||
t.Fatal("snapshot carries a candidate field")
|
||||
}
|
||||
for name, mutate := range map[string]func(string) string{
|
||||
"unknown field": func(s string) string { return strings.Replace(s, `"team"`, `"salary_minimum":1,"team"`, 1) },
|
||||
"trailing data": func(s string) string { return s + " {}" },
|
||||
"salary string": func(s string) string { return strings.Replace(s, `"salary_from":100000`, `"salary_from":"100000"`, 1) },
|
||||
} {
|
||||
if _, err := DecodeInvitationTerms([]byte(mutate(string(encoded)))); err == nil {
|
||||
t.Errorf("DecodeInvitationTerms(%s) accepted a changed snapshot", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviseDraftCreatesNextVersionsOnlyWhenContentChanges(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
later := input.At.Add(time.Minute)
|
||||
policyID, _ := NewPolicyVersionID()
|
||||
|
||||
same, changed, err := draft.ReviseDraft(draft.DraftContent(), policyID, later)
|
||||
if err != nil || len(changed) != 0 || same.Version() != 1 || !same.UpdatedAt().Equal(draft.UpdatedAt()) {
|
||||
t.Fatalf("no-op revision = version %d changed %v err %v, want the draft untouched", same.Version(), changed, err)
|
||||
}
|
||||
|
||||
content := draft.DraftContent()
|
||||
content.ResponsePeriod = 14
|
||||
revised, changed, err := draft.ReviseDraft(content, policyID, later)
|
||||
if err != nil {
|
||||
t.Fatalf("ReviseDraft(period) error = %v", err)
|
||||
}
|
||||
if revised.Version() != 2 || revised.TermsVersion() != 1 || len(changed) != 1 || changed[0] != InvitationChangedResponsePeriod ||
|
||||
revised.PolicyVersionID() != policyID {
|
||||
t.Fatalf("period revision = version %d/%d changed %v, want version 2 with terms version 1", revised.Version(), revised.TermsVersion(), changed)
|
||||
}
|
||||
|
||||
content = revised.DraftContent()
|
||||
content.Terms.Salary.SalaryTo = 150_000
|
||||
content.Need = &InvitationNeedRef{NeedID: uuid.New(), RequirementsVersion: 2}
|
||||
third, changed, err := revised.ReviseDraft(content, policyID, later.Add(time.Minute))
|
||||
if err != nil {
|
||||
t.Fatalf("ReviseDraft(terms and need) error = %v", err)
|
||||
}
|
||||
if third.Version() != 3 || third.TermsVersion() != 2 || fmt.Sprint(changed) != "[terms need]" {
|
||||
t.Fatalf("terms revision = version %d/%d changed %v, want 3/2 with terms and need", third.Version(), third.TermsVersion(), changed)
|
||||
}
|
||||
event, err := third.DraftEvent(input.ActorID, changed)
|
||||
if err != nil || event.Kind != InvitationEventDraftUpdated || event.Version != 3 || event.TermsVersion != 2 {
|
||||
t.Fatalf("update event = %+v, %v; want draft_updated at 3/2", event, err)
|
||||
}
|
||||
// The earlier values were not changed in place.
|
||||
if draft.Snapshot().Terms.Salary.SalaryTo != 140_000 || revised.Snapshot().Terms.Salary.SalaryTo != 140_000 {
|
||||
t.Fatal("a revision changed an earlier invitation value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReviseDraftRefusesAnInvitationThatIsNotADraft(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
stored := draft.Stored()
|
||||
stored.State = string(InvitationWithdrawn)
|
||||
withdrawn, err := RestoreInvitation(stored)
|
||||
if err != nil {
|
||||
t.Fatalf("RestoreInvitation(withdrawn) error = %v", err)
|
||||
}
|
||||
content := withdrawn.DraftContent()
|
||||
content.ResponsePeriod = 14
|
||||
if _, _, err := withdrawn.ReviseDraft(content, input.PolicyVersionID, input.At.Add(time.Minute)); !errors.Is(err, ErrInvitationNotDraft) {
|
||||
t.Fatalf("ReviseDraft(withdrawn) error = %v, want ErrInvitationNotDraft", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRestoreInvitationRejectsInconsistentRows(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
now := input.At.Add(time.Hour)
|
||||
tests := map[string]func(*StoredInvitation){
|
||||
"unknown state": func(s *StoredInvitation) { s.State = "sent" },
|
||||
"expiry reason without expiry": func(s *StoredInvitation) { s.ExpiryReason = invitationTestString("invitation_timeout") },
|
||||
"terms version ahead": func(s *StoredInvitation) { s.TermsVersion = 2 },
|
||||
"draft with a delivery": func(s *StoredInvitation) {
|
||||
s.DeliveredAt, s.ResponseDeadlineAt = &now, &now
|
||||
},
|
||||
"salary range that is too wide": func(s *StoredInvitation) { s.Terms.Salary.SalaryTo = 500_000 },
|
||||
"updated before created": func(s *StoredInvitation) { s.UpdatedAt = s.CreatedAt.Add(-time.Second) },
|
||||
"no policy version": func(s *StoredInvitation) { s.PolicyVersionID = PolicyVersionID{} },
|
||||
}
|
||||
for name, mutate := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
stored := draft.Stored()
|
||||
mutate(&stored)
|
||||
if _, err := RestoreInvitation(stored); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("RestoreInvitation() error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvitationHidesTheCandidateAndTheTermsWhenPrinted(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
view := OrganizationInvitationView{InvitationSnapshot: draft.Snapshot(), CandidateHandle: "cv1_" + strings.Repeat("A", 43)}
|
||||
for _, printed := range []string{
|
||||
fmt.Sprintf("%v %+v %#v %s", draft, draft, draft, draft),
|
||||
fmt.Sprintf("%v %+v %#v", view, view, view),
|
||||
fmt.Sprintf("%v", draft.Snapshot()),
|
||||
} {
|
||||
if strings.Contains(printed, input.CandidateID.String()) || strings.Contains(printed, "Middle Go developer") {
|
||||
t.Fatalf("printed invitation leaks a candidate or term: %s", printed)
|
||||
}
|
||||
}
|
||||
encoded, err := json.Marshal(view)
|
||||
if err != nil || strings.Contains(string(encoded), input.CandidateID.String()) {
|
||||
t.Fatalf("serialized view = %s, %v; want no candidate identifier", encoded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvitationPolicyResolvesTheResponsePeriod(t *testing.T) {
|
||||
policy := InvitationPolicy{ResponseDeadlineOptions: []CalendarDays{3, 7, 14}, DefaultResponseDeadline: 7}
|
||||
if period, err := policy.ResolveResponsePeriod(nil); err != nil || period != 7 {
|
||||
t.Fatalf("ResolveResponsePeriod(nil) = %d, %v; want the default 7", period, err)
|
||||
}
|
||||
three := CalendarDays(3)
|
||||
if period, err := policy.ResolveResponsePeriod(&three); err != nil || period != 3 {
|
||||
t.Fatalf("ResolveResponsePeriod(3) = %d, %v; want 3", period, err)
|
||||
}
|
||||
five := CalendarDays(5)
|
||||
var field *InvitationFieldError
|
||||
if _, err := policy.ResolveResponsePeriod(&five); !errors.As(err, &field) || field.Pointer != "/response_period_days" {
|
||||
t.Fatalf("ResolveResponsePeriod(5) error = %v, want a field error", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user