package assignment import ( "testing" "time" "doormile/constants" ) func TestSweepInterval(t *testing.T) { cases := []struct { env string want time.Duration }{ {"", defaultSweepSeconds * time.Second}, {"0", 0}, // off {"120", 120 * time.Second}, {"5", 30 * time.Second}, // floored: one attempt per pending booking per sweep {"soon", defaultSweepSeconds * time.Second}, {"-1", defaultSweepSeconds * time.Second}, } for _, c := range cases { t.Setenv("ASSIGNMENT_SWEEP_SECONDS", c.env) if got := sweepInterval(); got != c.want { t.Errorf("ASSIGNMENT_SWEEP_SECONDS=%q: %v, want %v", c.env, got, c.want) } } } func TestSweepMaxAge(t *testing.T) { cases := map[string]time.Duration{ "": defaultSweepMaxAgeHours * time.Hour, "24": 24 * time.Hour, "0": defaultSweepMaxAgeHours * time.Hour, // 0 would sweep nothing "two": defaultSweepMaxAgeHours * time.Hour, } for env, want := range cases { t.Setenv("ASSIGNMENT_SWEEP_MAX_AGE_HOURS", env) if got := sweepMaxAge(); got != want { t.Errorf("ASSIGNMENT_SWEEP_MAX_AGE_HOURS=%q: %v, want %v", env, got, want) } } } func TestKindFor(t *testing.T) { if kindFor(constants.BookingSourceCustomerApp) != kindCustomer { t.Error("customer-app bookings take the customer path") } for _, s := range []string{constants.BookingSourceExpress, "", "anything"} { if kindFor(s) != kindExpress { t.Errorf("%q should take the express path", s) } } } func TestSweepSkipsExpressFollowsTheAgentFlag(t *testing.T) { t.Setenv("EXPRESS_AGENT_ENABLED", "") if sweepSkipsExpress() { t.Error("agent off: console bookings are swept") } t.Setenv("EXPRESS_AGENT_ENABLED", "true") if !sweepSkipsExpress() { t.Error("agent on: console bookings are left to the agent") } } func TestMaxGPSAgeMinutes(t *testing.T) { cases := map[string]int{ "": defaultMaxGPSAgeMinutes, "0": 0, // check off "30": 30, "-5": defaultMaxGPSAgeMinutes, "half": defaultMaxGPSAgeMinutes, // a typo must not switch the check off } for env, want := range cases { t.Setenv("ASSIGNMENT_MAX_GPS_AGE_MINUTES", env) if got := maxGPSAgeMinutes(); got != want { t.Errorf("ASSIGNMENT_MAX_GPS_AGE_MINUTES=%q: %d, want %d", env, got, want) } } }