Repository navigation
fix(booking): never assign a round-robin host who is unavailable (#146) #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package handler_test | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/calnode/calnode/internal/uid" | ||
| ) | ||
|
|
||
| // An unavailability override must remove a rotation host from the pool for that | ||
| // day, not merely make the day look unavailable when every host is out. | ||
| // | ||
| // The bug (#146): validateBookingTime's round-robin branch only requires that AT | ||
| // LEAST ONE candidate be inside their own hours — correct for deciding whether | ||
| // the slot exists at all (mirrors slots.pickHosts), but wrong for deciding WHO | ||
| // is assigned. The full candidate list was then handed to booking.Service.Create, | ||
| // which picks by rotation strategy, so it could assign a host who had declared | ||
| // themselves unavailable. calendarFreeHosts does not catch this: it filters on | ||
| // calendar busy, and returns the pool untouched when no calendar provider is | ||
| // connected (the common self-hosted case). | ||
| func TestRoundRobin_neverAssignsHostWithUnavailableOverride(t *testing.T) { | ||
| h, database, key, ownerID := setupWorkspaceWithDB(t) | ||
| database.Exec(`INSERT INTO users (id,email,name,iana_timezone,is_admin) VALUES ('u2','u2@example.com','Two','UTC',0)`) | ||
| seedFullAvailabilityDB(t, database, ownerID) | ||
| seedFullAvailabilityDB(t, database, "u2") | ||
| slug, etID := seedEventTypeHTTP(t, h, key) | ||
| makeRoundRobin(t, h, slug, key, ownerID, "u2") | ||
|
|
||
| const day = "2027-05-03" | ||
|
|
||
| // The FIRST rotation host (priority 0) declares the whole day unavailable. | ||
| // Round-robin's default least-loaded strategy would otherwise pick it first. | ||
| if _, err := database.Exec( | ||
| `INSERT INTO availability_overrides (id,user_id,date,is_available,reason) VALUES (?,?,?,0,'day off')`, | ||
| uid.New(), ownerID, day); err != nil { | ||
| t.Fatalf("seed unavailability override: %v", err) | ||
| } | ||
|
|
||
| rec := postBooking(t, h, slug, day+"T10:00:00Z", "Alice", "alice@example.com") | ||
| if rec.Code != http.StatusCreated { | ||
| t.Fatalf("booking: %d — %s", rec.Code, rec.Body.String()) | ||
| } | ||
|
|
||
| var hostID string | ||
| if err := database.QueryRow(`SELECT host_id FROM bookings WHERE event_type_id=?`, etID).Scan(&hostID); err != nil { | ||
| t.Fatalf("read assigned host: %v", err) | ||
| } | ||
| if hostID == ownerID { | ||
| t.Errorf("booking assigned to %s, who declared %s unavailable", hostID, day) | ||
| } | ||
| } | ||
|
|
||
| // Same rule via the slug path that MCP and the booking assistant share. Without | ||
| // this, a fix applied only to the REST handler would leave those two entry points | ||
| // assigning unavailable hosts. | ||
| func TestRoundRobin_slugPathSkipsUnavailableHost(t *testing.T) { | ||
| h, database, key, ownerID := setupWorkspaceWithDB(t) | ||
| database.Exec(`INSERT INTO users (id,email,name,iana_timezone,is_admin) VALUES ('u2','u2@example.com','Two','UTC',0)`) | ||
| seedFullAvailabilityDB(t, database, ownerID) | ||
| seedFullAvailabilityDB(t, database, "u2") | ||
| slug, etID := seedEventTypeHTTP(t, h, key) | ||
| makeRoundRobin(t, h, slug, key, ownerID, "u2") | ||
|
|
||
| const day = "2027-05-03" | ||
| if _, err := database.Exec( | ||
| `INSERT INTO availability_overrides (id,user_id,date,is_available,reason) VALUES (?,?,?,0,'day off')`, | ||
| uid.New(), ownerID, day); err != nil { | ||
| t.Fatalf("seed unavailability override: %v", err) | ||
| } | ||
|
|
||
| if rec := postBooking(t, h, slug, day+"T14:00:00Z", "Alice", "alice@example.com"); rec.Code != http.StatusCreated { | ||
| t.Fatalf("booking: %d — %s", rec.Code, rec.Body.String()) | ||
| } | ||
| var hostID string | ||
| if err := database.QueryRow(`SELECT host_id FROM bookings WHERE event_type_id=?`, etID).Scan(&hostID); err != nil { | ||
| t.Fatalf("read assigned host: %v", err) | ||
| } | ||
| if hostID == ownerID { | ||
| t.Errorf("slug path assigned %s, who declared %s unavailable", hostID, day) | ||
| } | ||
| } | ||
|
|
||
| // The all-hosts-unavailable case must still be refused, not silently assigned. | ||
| func TestRoundRobin_allHostsUnavailableStillRejected(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This passes without the fix: with Technical details# TestRoundRobin_allHostsUnavailableStillRejected passes pre-fix
## Affected sites
- internal/handler/round_robin_unavailable_host_test.go:84-104 — asserts only "no 201"; the rejection is produced by internal/handler/booking_handler.go:350-359
- internal/handler/booking_handler.go:443 — `return nil, errSlotUnavailable` — unreachable: `validateBookingTime` (internal/handler/booking_handler.go:335-358) already fails the request unless at least one candidate is available, at both call sites (booking_handler.go:589 and :912, both immediately before the new call)
## Required outcome
- Either say plainly in the comment that this locks in pre-existing `validateBookingTime` behaviour, or drop the test so the PR's tests are all ones that fail before the fix.
## Suggested approach
- If the dead branch is meant to stay as a defensive guard, one line in `filterAvailableHosts`'s doc comment saying it cannot fire today would stop the next reader from hunting for the case it protects. |
||
| h, database, key, ownerID := setupWorkspaceWithDB(t) | ||
| database.Exec(`INSERT INTO users (id,email,name,iana_timezone,is_admin) VALUES ('u2','u2@example.com','Two','UTC',0)`) | ||
| seedFullAvailabilityDB(t, database, ownerID) | ||
| seedFullAvailabilityDB(t, database, "u2") | ||
| slug, _ := seedEventTypeHTTP(t, h, key) | ||
| makeRoundRobin(t, h, slug, key, ownerID, "u2") | ||
|
|
||
| const day = "2027-05-03" | ||
| for _, u := range []string{ownerID, "u2"} { | ||
| if _, err := database.Exec( | ||
| `INSERT INTO availability_overrides (id,user_id,date,is_available,reason) VALUES (?,?,?,0,'day off')`, | ||
| uid.New(), u, day); err != nil { | ||
| t.Fatalf("seed unavailability override for %s: %v", u, err) | ||
| } | ||
| } | ||
|
|
||
| if rec := postBooking(t, h, slug, day+"T10:00:00Z", "Alice", "alice@example.com"); rec.Code == http.StatusCreated { | ||
| t.Errorf("booking created with every host unavailable") | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test never reaches
createBookingForSlug—postBookingposts to/v1/bookings, i.e.h.CreateBooking, the same REST handler test 1 already covers. I confirmed it by deleting thefilterAvailableHostscall fromcreateBookingForSlugand re-running: all three tests still pass, so this one contributes nothing to the second entry point. Given that covering MCP/assistant is the reason the fix touchescreateBookingForSlugat all, the comment above currently promises coverage the test doesn't deliver. Driving the real path is easy —connectMCP(t, h)+client.CallTool(..., Name: "create_booking", ...)is the established pattern (seeTestMCPBookingCalendarErrorsinbooking_calendar_check_test.go).Technical details