232 lines
9.2 KiB
Go
232 lines
9.2 KiB
Go
package invitation
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
// needDraft creates a draft under the need for a new visible candidate. The
|
|
// need is one stored requirements version of the organization.
|
|
func (test *harness) needDraft(t *testing.T, need uuid.UUID) domain.OrganizationInvitationView {
|
|
t.Helper()
|
|
test.transactor.state.needVersions[[3]string{test.organizationID.String(), need.String(), "1"}] = true
|
|
handle, _ := test.showCandidate(t)
|
|
command := test.createCommand(handle, "create-need-"+uuid.NewString())
|
|
command.Need = &domain.InvitationNeedRef{NeedID: need, RequirementsVersion: 1}
|
|
view, err := test.service.Create(t.Context(), command)
|
|
if err != nil {
|
|
t.Fatalf("Create(under the need) error = %v", err)
|
|
}
|
|
return view
|
|
}
|
|
|
|
func TestSendRefusesTheTwentyFirstWaitingInvitationOfANeed(t *testing.T) {
|
|
test := newHarness(t)
|
|
test.quotaLeft = 100
|
|
need := uuid.Must(uuid.NewV7())
|
|
drafts := make([]domain.OrganizationInvitationView, 0, 21)
|
|
for range 21 {
|
|
drafts = append(drafts, test.needDraft(t, need))
|
|
}
|
|
for index, draft := range drafts[:20] {
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(draft.ID, draft.Version, fmt.Sprint("send-", index))); err != nil {
|
|
t.Fatalf("Send(%d) error = %v, want the twenty waiting invitations to be allowed", index, err)
|
|
}
|
|
}
|
|
last := drafts[20]
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(last.ID, last.Version, "send-over-limit")); !errors.Is(err, ErrSendBlocked) {
|
|
t.Fatalf("Send(21st waiting) error = %v, want ErrSendBlocked", err)
|
|
}
|
|
state := test.transactor.snapshot()
|
|
if state.invitations[last.ID].State() != domain.InvitationDraft || len(state.charged) != 20 {
|
|
t.Fatalf("a refused send changed the draft to %v or charged %d invitations", state.invitations[last.ID].State(), len(state.charged))
|
|
}
|
|
// The refusal is stored under its key and replayed.
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(last.ID, last.Version, "send-over-limit")); !errors.Is(err, ErrSendBlocked) {
|
|
t.Fatalf("Send(replay of the refusal) error = %v, want ErrSendBlocked", err)
|
|
}
|
|
}
|
|
|
|
func TestConcurrentSendsOfTheTwentiethWaitingInvitationLetOnlyOneThrough(t *testing.T) {
|
|
test := newHarness(t)
|
|
test.quotaLeft = 100
|
|
need := uuid.Must(uuid.NewV7())
|
|
for index := range 19 {
|
|
draft := test.needDraft(t, need)
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(draft.ID, draft.Version, fmt.Sprint("nineteen-", index))); err != nil {
|
|
t.Fatalf("Send(%d) error = %v", index, err)
|
|
}
|
|
}
|
|
first, second := test.needDraft(t, need), test.needDraft(t, need)
|
|
|
|
var group sync.WaitGroup
|
|
errs := make([]error, 2)
|
|
for index, draft := range []domain.OrganizationInvitationView{first, second} {
|
|
group.Go(func() {
|
|
_, errs[index] = test.service.Send(t.Context(), test.sendCommand(draft.ID, draft.Version, fmt.Sprint("race-", index)))
|
|
})
|
|
}
|
|
group.Wait()
|
|
|
|
admitted, refused := 0, 0
|
|
for _, err := range errs {
|
|
switch {
|
|
case err == nil:
|
|
admitted++
|
|
case errors.Is(err, ErrSendBlocked):
|
|
refused++
|
|
default:
|
|
t.Fatalf("concurrent Send() error = %v", err)
|
|
}
|
|
}
|
|
if admitted != 1 || refused != 1 {
|
|
t.Fatalf("admitted %d and refused %d of two sends with nineteen waiting; want one each", admitted, refused)
|
|
}
|
|
count := 0
|
|
for _, draft := range []domain.OrganizationInvitationView{first, second} {
|
|
view, err := test.service.Get(t.Context(), test.actorID, test.organizationID, draft.ID)
|
|
if err != nil {
|
|
t.Fatalf("Get(%s) error = %v", draft.ID, err)
|
|
}
|
|
if view.State == domain.InvitationDelivered {
|
|
count++
|
|
}
|
|
}
|
|
if count != 1 {
|
|
t.Fatalf("%d of the two race invitations were delivered; want exactly one", count)
|
|
}
|
|
}
|
|
|
|
func TestLeavingTheWaitingStateFreesTheNeedSlotWithoutReturningTheQuota(t *testing.T) {
|
|
test := newHarness(t)
|
|
test.quotaLeft = 100
|
|
need := uuid.Must(uuid.NewV7())
|
|
sent := make([]domain.OrganizationInvitationView, 0, 20)
|
|
for index := range 20 {
|
|
draft := test.needDraft(t, need)
|
|
view, err := test.service.Send(t.Context(), test.sendCommand(draft.ID, draft.Version, fmt.Sprint("fill-", index)))
|
|
if err != nil {
|
|
t.Fatalf("Send(%d) error = %v", index, err)
|
|
}
|
|
sent = append(sent, view)
|
|
}
|
|
blocked := test.needDraft(t, need)
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(blocked.ID, blocked.Version, "blocked")); !errors.Is(err, ErrSendBlocked) {
|
|
t.Fatalf("Send(full need) error = %v, want ErrSendBlocked", err)
|
|
}
|
|
|
|
// The first invitation expires at its deadline, which frees one place.
|
|
deadline := *sent[0].ResponseDeadlineAt
|
|
if err := test.runDeadlineJob(t, claimOf(test.deadlineJob(t, sent[0].ID)), func() time.Time { return deadline }); err != nil {
|
|
t.Fatalf("deadline job error = %v", err)
|
|
}
|
|
chargesBefore := len(test.transactor.snapshot().charged)
|
|
if view, err := test.service.Send(t.Context(), test.sendCommand(blocked.ID, blocked.Version, "after-expiry")); err != nil || view.State != domain.InvitationDelivered {
|
|
t.Fatalf("Send(after one expiry) = %v, %v; want delivered", view.State, err)
|
|
}
|
|
if chargesAfter := len(test.transactor.snapshot().charged); chargesAfter != chargesBefore+1 {
|
|
t.Fatalf("charges went from %d to %d; the expiry must not return quota and the new send must charge once", chargesBefore, chargesAfter)
|
|
}
|
|
}
|
|
|
|
func TestPauseAfterADeclineHoldsForThirtyDaysForEveryNeedAndMember(t *testing.T) {
|
|
test := newHarness(t)
|
|
test.quotaLeft = 100
|
|
sent, candidate := test.deliveredTo(t)
|
|
declinedAt := sent.DeliveredAt.Add(time.Hour)
|
|
test.setTime(declinedAt)
|
|
if _, err := test.service.DeclineMine(t.Context(), DeclineCommand{
|
|
ActorID: candidate, InvitationID: sent.ID, IdempotencyKey: "decline-pause", ExpectedVersion: sent.Version,
|
|
Choice: domain.InvitationDeclineChoice{Reason: domain.InvitationDeclineOther},
|
|
}); err != nil {
|
|
t.Fatalf("DeclineMine() error = %v", err)
|
|
}
|
|
|
|
// A second owner of the same organization, with another need.
|
|
secondOwner := uuid.Must(uuid.NewV7())
|
|
verified := declinedAt.Add(-time.Hour)
|
|
test.transactor.state.users[secondOwner] = domain.User{ID: secondOwner, Scenario: domain.ScenarioEmployer, EmailVerifiedAt: &verified}
|
|
membership, err := domain.NewOwnerMembership(test.organizationID, secondOwner, declinedAt)
|
|
if err != nil {
|
|
t.Fatalf("NewOwnerMembership() error = %v", err)
|
|
}
|
|
test.transactor.state.memberships[[2]uuid.UUID{test.organizationID, secondOwner}] = membership
|
|
other := uuid.Must(uuid.NewV7())
|
|
test.transactor.state.needVersions[[3]string{test.organizationID.String(), other.String(), "1"}] = true
|
|
handle, err := test.key.ForOrganization(test.organizationID, candidate)
|
|
if err != nil {
|
|
t.Fatalf("ForOrganization() error = %v", err)
|
|
}
|
|
draftOther := func(key string) domain.OrganizationInvitationView {
|
|
t.Helper()
|
|
command := test.createCommand(handle, key)
|
|
command.ActorID = secondOwner
|
|
command.Need = &domain.InvitationNeedRef{NeedID: other, RequirementsVersion: 1}
|
|
view, err := test.service.Create(t.Context(), command)
|
|
if err != nil {
|
|
t.Fatalf("Create(%s) error = %v", key, err)
|
|
}
|
|
return view
|
|
}
|
|
draft := draftOther("create-pause")
|
|
sendAs := func(view domain.OrganizationInvitationView, key string) error {
|
|
command := test.sendCommand(view.ID, view.Version, key)
|
|
command.ActorID = secondOwner
|
|
_, err := test.service.Send(t.Context(), command)
|
|
return err
|
|
}
|
|
|
|
test.setTime(declinedAt.Add(29 * 24 * time.Hour))
|
|
if err := sendAs(draft, "send-day-29"); !errors.Is(err, ErrSendBlocked) {
|
|
t.Fatalf("Send(day 29 after the decline) error = %v, want ErrSendBlocked", err)
|
|
}
|
|
|
|
test.setTime(declinedAt.Add(30 * 24 * time.Hour))
|
|
if err := sendAs(draft, "send-day-30"); err != nil {
|
|
t.Fatalf("Send(day 30 after the decline) error = %v, want the pause to be over", err)
|
|
}
|
|
}
|
|
|
|
func TestPauseAfterADeclineDoesNotHoldAnotherCandidate(t *testing.T) {
|
|
test := newHarness(t)
|
|
test.quotaLeft = 100
|
|
sent, candidate := test.deliveredTo(t)
|
|
test.setTime(sent.DeliveredAt.Add(time.Hour))
|
|
if _, err := test.service.DeclineMine(t.Context(), DeclineCommand{
|
|
ActorID: candidate, InvitationID: sent.ID, IdempotencyKey: "decline-other", ExpectedVersion: sent.Version,
|
|
Choice: domain.InvitationDeclineChoice{Reason: domain.InvitationDeclineOther},
|
|
}); err != nil {
|
|
t.Fatalf("DeclineMine() error = %v", err)
|
|
}
|
|
next, _, _ := test.draftFor(t, nil)
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(next.ID, next.Version, "send-other-candidate")); err != nil {
|
|
t.Fatalf("Send(another candidate) error = %v, want delivered", err)
|
|
}
|
|
}
|
|
|
|
// The pause is judged only from declines, so an invitation that expired is
|
|
// not a decline and starts no pause.
|
|
func TestAnExpiredInvitationStartsNoPause(t *testing.T) {
|
|
test := newHarness(t)
|
|
test.quotaLeft = 100
|
|
sent, candidate := test.deliveredTo(t)
|
|
test.setTime(*sent.ResponseDeadlineAt)
|
|
if _, err := test.service.GetMine(t.Context(), candidate, sent.ID); err != nil {
|
|
t.Fatalf("GetMine(expiry) error = %v", err)
|
|
}
|
|
next, _, _ := test.draftFor(t, nil)
|
|
if _, err := test.service.Send(t.Context(), test.sendCommand(next.ID, next.Version, "send-after-expiry")); err != nil {
|
|
t.Fatalf("Send(after an expiry of another invitation) error = %v", err)
|
|
}
|
|
if _, found, err := (testInvitations{state: &test.transactor.state}).LatestDeclineTime(t.Context(), test.organizationID, candidate); err != nil || found {
|
|
t.Fatalf("LatestDeclineTime() = %v, %v; an expiry must not count as a decline", found, err)
|
|
}
|
|
}
|