Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / internal/modules/recruiting/placement_math_test.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 2178e7298d0e5d9af83afa12402fd587f0ba6ed8 |
| Branch | feature/aioe-upgrade |
| Path or archive member | internal/modules/recruiting/placement_math_test.go |
| Original SHA-256 | f038c83449c851b4bd5f27d95d49133491821a6ef9dd68a36b521b4abcb48193 |
| Git blob SHA | 23b7cfb76f2673aab632e9f14c7f21389a98eb42 |
| Reading-copy SHA-256 | f038c83449c851b4bd5f27d95d49133491821a6ef9dd68a36b521b4abcb48193 |
Recovered source transcript
Download complete source text ↓276 lines · 10.9 KB
package recruiting
import (
"math/rand"
"testing"
"time"
"github.com/shopspring/decimal"
)
func d(s string) *decimal.Decimal { v := decimal.RequireFromString(s); return &v }
// TestMarginIsExactNotFloat. A bill rate is multiplied by hours and by a wrap rate downstream, so a
// margin that went through a float64 would not reconcile with an invoice. These are the cases binary
// floating point gets wrong.
func TestMarginIsExactNotFloat(t *testing.T) {
cases := []struct {
name, bill, pay, wantGross, wantPct string
}{
{"plain", "150.0000", "100.0000", "50", "33.3333"},
{"tenths that float cannot represent", "0.3000", "0.1000", "0.2", "66.6667"},
{"repeating third", "3.0000", "2.0000", "1", "33.3333"},
{"four decimal rates", "145.7500", "101.2500", "44.5", "30.5317"},
{"equal rates are a zero margin, not an error", "100.0000", "100.0000", "0", "0"},
{"large", "9999.9999", "0.0001", "9999.9998", "100"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
m := marginOf("contract", d(c.bill), d(c.pay), nil, nil, nil)
if m.GrossPerHour == nil {
t.Fatal("no gross margin computed")
}
if !m.GrossPerHour.Equal(decimal.RequireFromString(c.wantGross)) {
t.Errorf("gross = %s, want %s", m.GrossPerHour, c.wantGross)
}
if m.Pct == nil {
t.Fatal("no percentage computed")
}
if !m.Pct.Equal(decimal.RequireFromString(c.wantPct)) {
t.Errorf("pct = %s, want %s", m.Pct, c.wantPct)
}
})
}
}
// TestZeroBillRateGivesNoPercentageRatherThanZero. A share of nothing is undefined. Returning 0.00 would
// render as a real margin of nothing, which is a different and false claim, and dividing would panic.
func TestZeroBillRateGivesNoPercentage(t *testing.T) {
m := marginOf("contract", d("0"), d("0"), nil, nil, nil)
if m.GrossPerHour == nil || !m.GrossPerHour.IsZero() {
t.Errorf("gross = %v, want 0", m.GrossPerHour)
}
if m.Pct != nil {
t.Errorf("pct = %v on a zero bill rate, want absent because the share is undefined", m.Pct)
}
}
// TestMarginNeedsBothRates. A missing rate is not a zero rate, and treating it as one would report a
// hundred percent margin on work whose cost simply has not been entered.
func TestMarginNeedsBothRates(t *testing.T) {
for _, c := range []struct{ bill, pay *decimal.Decimal }{
{d("150"), nil}, {nil, d("100")}, {nil, nil},
} {
if m := marginOf("contract", c.bill, c.pay, nil, nil, nil); m.GrossPerHour != nil {
t.Errorf("a margin of %s was computed from bill=%v pay=%v", m.GrossPerHour, c.bill, c.pay)
}
}
}
// TestDirectHireFee. A permanent placement earns a one time fee, never an hourly spread, and the two
// shapes must never be blended into one revenue figure.
func TestDirectHireFee(t *testing.T) {
// An explicit agreed amount wins over the percentage.
m := marginOf("direct_hire", nil, nil, d("150000.00"), d("0.2000"), d("35000.00"))
if m.Fee == nil || !m.Fee.Equal(decimal.RequireFromString("35000")) {
t.Errorf("fee = %v, want the explicit 35000", m.Fee)
}
// Otherwise salary times the fraction. 20 percent of 150000 is exactly 30000.
m = marginOf("direct_hire", nil, nil, d("150000.00"), d("0.2000"), nil)
if m.Fee == nil || !m.Fee.Equal(decimal.RequireFromString("30000")) {
t.Errorf("fee = %v, want 30000", m.Fee)
}
// A direct hire never reports an hourly spread even if rates were entered by mistake.
m = marginOf("direct_hire", d("150"), d("100"), d("150000"), d("0.2"), nil)
if m.GrossPerHour != nil || m.Pct != nil {
t.Error("a direct hire reported an hourly margin, blending two revenue shapes")
}
// A contract never reports a fee.
m = marginOf("contract", d("150"), d("100"), d("150000"), d("0.2"), d("35000"))
if m.Fee != nil {
t.Errorf("a contract placement reported a fee of %v", m.Fee)
}
}
// TestMarginPropertyHoldsForAnyRates. Gross is never negative when bill is at least pay, which the
// database enforces, and the percentage never exceeds 100 nor falls below 0.
func TestMarginPropertyHoldsForAnyRates(t *testing.T) {
rng := rand.New(rand.NewSource(20260722))
hundred := decimal.NewFromInt(100)
for i := 0; i < 200000; i++ {
pay := decimal.NewFromInt(rng.Int63n(2_000_000)).Div(decimal.NewFromInt(10000))
extra := decimal.NewFromInt(rng.Int63n(2_000_000)).Div(decimal.NewFromInt(10000))
bill := pay.Add(extra)
m := marginOf("contract", &bill, &pay, nil, nil, nil)
if m.GrossPerHour == nil {
t.Fatalf("no gross for bill=%s pay=%s", bill, pay)
}
if m.GrossPerHour.IsNegative() {
t.Fatalf("negative gross %s for bill=%s pay=%s", m.GrossPerHour, bill, pay)
}
if !m.GrossPerHour.Equal(bill.Sub(pay)) {
t.Fatalf("gross %s is not bill minus pay for %s and %s", m.GrossPerHour, bill, pay)
}
if bill.IsZero() {
if m.Pct != nil {
t.Fatalf("a percentage %s was computed on a zero bill rate", m.Pct)
}
continue
}
if m.Pct.IsNegative() || m.Pct.GreaterThan(hundred) {
t.Fatalf("pct %s outside [0,100] for bill=%s pay=%s", m.Pct, bill, pay)
}
}
}
// TestGuaranteeBoundaryIsInclusive. A 90 day guarantee starting on the 1st covers through day 90. Getting
// this off by one either forgives a real breach or claws back a fee that was earned.
func TestGuaranteeBoundaryIsInclusive(t *testing.T) {
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
today := time.Date(2026, 1, 15, 0, 0, 0, 0, time.UTC)
g := guaranteeOf(90, start, nil, "active", today)
if g.EndsOn != "2026-03-31" { // 1 Jan plus 89 days
t.Errorf("a 90 day guarantee from 1 Jan ends on %s, want 2026-03-31", g.EndsOn)
}
// The last covered day breaches.
last := time.Date(2026, 3, 31, 0, 0, 0, 0, time.UTC)
if g := guaranteeOf(90, start, &last, "fell_off", today); !g.Breached {
t.Error("falling off on the final covered day did not breach")
}
// The day after does not.
after := time.Date(2026, 4, 1, 0, 0, 0, 0, time.UTC)
if g := guaranteeOf(90, start, &after, "fell_off", today); g.Breached {
t.Error("falling off the day after the guarantee ended was counted as a breach")
}
// Falling off on day one certainly does.
if g := guaranteeOf(90, start, &start, "fell_off", today); !g.Breached {
t.Error("falling off on the first day did not breach")
}
}
// TestCompletedAssignmentNeverBreaches. An assignment the client ended on its own terms is not a failure
// to deliver, and counting it as one would understate a recruiter's performance for someone else's decision.
func TestCompletedAssignmentNeverBreaches(t *testing.T) {
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
end := time.Date(2026, 1, 10, 0, 0, 0, 0, time.UTC)
today := time.Date(2026, 2, 1, 0, 0, 0, 0, time.UTC)
if g := guaranteeOf(90, start, &end, "completed", today); g.Breached {
t.Error("a completed assignment inside the guarantee window was counted as a breach")
}
for _, s := range []string{"fell_off", "terminated"} {
if g := guaranteeOf(90, start, &end, s, today); !g.Breached {
t.Errorf("a %s inside the window did not breach", s)
}
}
}
// TestGuaranteeDaysRemainingGoesNegative. A live placement past its guarantee must say so plainly rather
// than clamping to zero, which would read as "ends today" forever.
func TestGuaranteeDaysRemaining(t *testing.T) {
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
g := guaranteeOf(30, start, nil, "active", time.Date(2026, 1, 11, 0, 0, 0, 0, time.UTC))
if g.DaysRemaining == nil || *g.DaysRemaining != 19 { // ends 30 Jan, 19 days after 11 Jan
t.Errorf("days remaining = %v, want 19", g.DaysRemaining)
}
g = guaranteeOf(30, start, nil, "active", time.Date(2026, 3, 1, 0, 0, 0, 0, time.UTC))
if g.DaysRemaining == nil || *g.DaysRemaining >= 0 {
t.Errorf("days remaining = %v past the guarantee, want a negative number", g.DaysRemaining)
}
// No guarantee at all is not a guarantee of zero days.
if g := guaranteeOf(0, start, nil, "active", start); g.EndsOn != "" || g.Breached {
t.Errorf("a zero day guarantee produced %+v", g)
}
}
// TestOfferTransitionsMatchTheMigration pins the Go graph against rm_offer_transition in 0055.
func TestOfferTransitionsMatchTheMigration(t *testing.T) {
legal := map[string][]string{
"extended": {"accepted", "declined", "rescinded", "expired"},
"accepted": {"rescinded"},
}
for from := range offerStatuses {
for to := range offerStatuses {
want := false
for _, l := range legal[from] {
if l == to {
want = true
}
}
if got := offerCanMove(from, to); got != want {
t.Errorf("offerCanMove(%q, %q) = %v, want %v", from, to, got, want)
}
}
}
// The terminal statuses are terminal.
for _, s := range []string{"declined", "rescinded", "expired"} {
for to := range offerStatuses {
if offerCanMove(s, to) {
t.Errorf("%q is terminal but a move to %q was allowed", s, to)
}
}
}
// An accepted offer can still be rescinded, because a client pulling a role after acceptance is real.
if !offerCanMove("accepted", "rescinded") {
t.Error("an accepted offer cannot be rescinded, so a pulled role could not be recorded")
}
}
// TestVocabulariesMatchTheMigrations pins every enum against the CHECK constraints in 0048.
func TestPlacementVocabulariesMatchMigrations(t *testing.T) {
for name, got := range map[string]map[string]bool{
"offer status": offerStatuses,
"placement status": placementStatuses,
"interview kind": interviewKinds,
"interview outcome": interviewOutcomes,
} {
if len(got) == 0 {
t.Errorf("%s vocabulary is empty", name)
}
}
for _, s := range []string{"extended", "accepted", "declined", "rescinded", "expired"} {
if !offerStatuses[s] {
t.Errorf("offer status %q is in migration 0048 but missing here", s)
}
}
if len(offerStatuses) != 5 {
t.Errorf("offer statuses = %d, migration 0048 permits 5", len(offerStatuses))
}
for _, s := range []string{"pending_start", "active", "completed", "terminated", "fell_off"} {
if !placementStatuses[s] {
t.Errorf("placement status %q is in migration 0048 but missing here", s)
}
}
if len(placementStatuses) != 5 {
t.Errorf("placement statuses = %d, migration 0048 permits 5", len(placementStatuses))
}
for _, s := range []string{"completed", "terminated", "fell_off"} {
if !placementEnded(s) {
t.Errorf("%q ends an assignment but is not classified as ended", s)
}
}
for _, s := range []string{"pending_start", "active"} {
if placementEnded(s) {
t.Errorf("%q is live but is classified as ended", s)
}
}
if len(interviewKinds) != 5 || len(interviewOutcomes) != 6 {
t.Errorf("interview vocabularies are %d kinds and %d outcomes, migration 0048 permits 5 and 6",
len(interviewKinds), len(interviewOutcomes))
}
for _, o := range []string{"passed", "failed", "no_show"} {
if !interviewConcluded(o) {
t.Errorf("%q requires a completed_at in migration 0048 but is not classified as concluded", o)
}
}
for _, o := range []string{"scheduled", "cancelled", "rescheduled"} {
if interviewConcluded(o) {
t.Errorf("%q does not require a completed_at but is classified as concluded", o)
}
}
if interviewRoundOK(0) || interviewRoundOK(21) || !interviewRoundOK(1) || !interviewRoundOK(20) {
t.Error("interview round bounds do not match the 1 to 20 CHECK in migration 0048")
}
}