Files
LektorenPlan/web/domain/planner_test.go
T

230 lines
8.4 KiB
Go

package domain
import "testing"
func TestPersonLabelsUsesShortCodeAndNumbersDuplicates(t *testing.T) {
people := []Person{
{ID: "p1", FirstName: "Anna", LastName: "Berg"},
{ID: "p2", FirstName: "Anton", LastName: "Berg"},
{ID: "p3", ShortCode: "MK", FirstName: "Markus", LastName: "Klein"},
{ID: "p4", FirstName: "Mia", LastName: "Sommer"},
}
labels := PersonLabels(people)
cases := map[string]string{"p1": "AB1", "p2": "AB2", "p3": "MK", "p4": "MS"}
for id, want := range cases {
if got := labels[id]; got != want {
t.Errorf("label for %s = %q, want %q", id, got, want)
}
}
}
func TestMatchesWeekdayPreferenceTreatsHighFeastsLikeSundays(t *testing.T) {
sundayOnly := Person{PreferredWeekdays: []int{1}}
if !sundayOnly.MatchesWeekdayPreference(2, 10*60, true) {
t.Fatal("a Sunday preference must cover a high feast on another weekday")
}
if sundayOnly.MatchesWeekdayPreference(2, 10*60, false) {
t.Fatal("an ordinary Monday must not match a Sunday preference")
}
morningSunday := Person{
PreferredWeekdays: []int{1},
PreferredWeekdayPeriods: []WeekdayPeriodPreference{
{Weekday: 1, Period: PeriodMorning},
},
}
if !morningSunday.MatchesWeekdayPreference(2, 10*60, true) {
t.Fatal("a high feast in the morning should match the Sunday morning preference")
}
if morningSunday.MatchesWeekdayPreference(2, 18*60, true) {
t.Fatal("a high feast in the evening must not match the Sunday morning preference")
}
}
func TestMatchesWeekdayPreferenceUsesPeriodBounds(t *testing.T) {
person := Person{
PreferredWeekdays: []int{2},
PreferredWeekdayPeriods: []WeekdayPeriodPreference{
{Weekday: 2, Period: PeriodMorning},
},
}
if !person.MatchesWeekdayPreference(2, 11*60+59, false) {
t.Error("11:59 belongs to the morning")
}
if person.MatchesWeekdayPreference(2, 12*60, false) {
t.Error("12:00 must no longer count as morning")
}
if person.MatchesWeekdayPreference(2, 16*60+59, false) {
t.Error("16:59 belongs to the afternoon, not the morning")
}
person.PreferredWeekdayPeriods = []WeekdayPeriodPreference{{Weekday: 2, Period: PeriodEvening}}
if !person.MatchesWeekdayPreference(2, 17*60, false) {
t.Error("17:00 belongs to the evening")
}
}
func plannerFixture(person Person) (ServicePlanner, ServiceOccurrence) {
church := Church{ID: "c1", Name: "St. Andreas"}
// 2026-11-16 is a Monday.
service := ServiceOccurrence{
ID: "s1", ChurchID: church.ID, Date: MustDate("2026-11-16"),
TimeMinutes: 18 * 60, Title: "Abendmesse", LectorsNeeded: 1,
}
planner := ServicePlanner{
People: []Person{person},
Services: []ServiceOccurrence{service},
Churches: []Church{church},
Options: DefaultPlannerOptions(),
}
return planner, service
}
func TestPlannerBeyondPreferencesRequiresExplicitPass(t *testing.T) {
person := Person{
ID: "p1", FirstName: "Anna", LastName: "Berg", Roles: []Role{RoleLector},
PreferredWeekdays: []int{1}, Active: true, MaxPerMonth: 1,
}
planner, _ := plannerFixture(person)
planner.Options.RespectPreferredWeekdays = true
result := planner.Plan()
if len(result.Unfilled) != 1 || len(result.Assignments) != 0 {
t.Fatalf("without permission the slot must stay open, got %#v", result)
}
person.AllowBeyondPreferences = true
planner, _ = plannerFixture(person)
planner.Options.RespectPreferredWeekdays = true
planner.Options.AllowBeyondPreferences = true
result = planner.Plan()
if len(result.Unfilled) != 0 || len(result.Assignments) != 1 {
t.Fatalf("expected a fallback assignment, got %#v", result)
}
if !result.Assignments[0].BeyondPreferences {
t.Fatal("fallback assignment must be marked as beyond preferences")
}
person.AllowBeyondPreferences = false
planner, _ = plannerFixture(person)
planner.Options.AllowBeyondPreferences = true
if result := planner.Plan(); len(result.Assignments) != 0 {
t.Fatal("a person without permission must not be planned beyond preferences")
}
}
func TestPlannerTreatsHighFeastAsSunday(t *testing.T) {
person := Person{
ID: "p1", FirstName: "Anna", LastName: "Berg", Roles: []Role{RoleLector},
PreferredWeekdays: []int{1}, Active: true, MaxPerMonth: 6,
}
planner, service := plannerFixture(person)
planner.LiturgicalDays = []LiturgicalDay{{
Date: service.Date, Title: "Hochfest", Rank: RankSolemnity,
}}
result := planner.Plan()
if len(result.Assignments) != 1 || len(result.Unfilled) != 0 {
t.Fatalf("high feast must match a Sunday preference, got %#v", result)
}
if result.Assignments[0].BeyondPreferences {
t.Fatal("a matched high feast is not beyond preferences")
}
}
func TestCandidateSuggestionsAllowPreferenceOverrides(t *testing.T) {
service := ServiceOccurrence{
ID: "s1", ChurchID: "c1", Date: MustDate("2026-11-16"),
TimeMinutes: 18 * 60, Title: "Abendmesse", LectorsNeeded: 1,
}
sundayOnly := Person{
ID: "p1", FirstName: "Anna", LastName: "Berg", Roles: []Role{RoleLector},
PreferredWeekdays: []int{1}, Active: true, MaxPerMonth: 6,
}
suggestions := CandidateSuggestions(service, RoleLector,
[]Person{sundayOnly}, nil, nil, []ServiceOccurrence{service}, nil, nil)
if len(suggestions) != 1 || suggestions[0].Blocked || !suggestions[0].BeyondPreferences {
t.Fatalf("manual suggestions should allow preference overrides with a warning, got %#v", suggestions)
}
if len(suggestions[0].Warnings) == 0 {
t.Fatal("preference override should carry a warning")
}
sundayOnly.AllowBeyondPreferences = true
suggestions = CandidateSuggestions(service, RoleLector,
[]Person{sundayOnly}, nil, nil, []ServiceOccurrence{service}, nil, nil)
if len(suggestions) != 1 || suggestions[0].Blocked || !suggestions[0].BeyondPreferences {
t.Fatalf("flagged person should be suggested beyond preferences, got %#v", suggestions)
}
if suggestions[0].Person.PreferenceNote() != "" {
t.Fatal("unexpected preference note")
}
}
func TestCandidateSuggestionsAllowMaxLimitOverride(t *testing.T) {
service := ServiceOccurrence{
ID: "s2", ChurchID: "c1", Date: MustDate("2026-11-17"),
TimeMinutes: 18 * 60, Title: "Abendmesse", LectorsNeeded: 1,
}
earlier := ServiceOccurrence{
ID: "s1", ChurchID: "c1", Date: MustDate("2026-11-16"),
TimeMinutes: 18 * 60, Title: "Abendmesse", LectorsNeeded: 1,
}
person := Person{
ID: "p1", FirstName: "Anna", LastName: "Berg", Roles: []Role{RoleLector},
Active: true, MaxPerMonth: 1,
}
existing := []Assignment{{
ID: "a1", ServiceID: earlier.ID, PersonID: person.ID, Role: RoleLector,
}}
suggestions := CandidateSuggestions(service, RoleLector,
[]Person{person}, nil, existing, []ServiceOccurrence{earlier, service}, nil, nil)
if len(suggestions) != 1 || suggestions[0].Blocked || !suggestions[0].BeyondPreferences {
t.Fatalf("manual suggestions should allow exceeding the monthly limit with a warning, got %#v", suggestions)
}
}
func TestCandidateSuggestionsAllowSameDayConflict(t *testing.T) {
service := ServiceOccurrence{
ID: "s2", ChurchID: "c1", Date: MustDate("2026-11-16"),
TimeMinutes: 18 * 60, Title: "Abendmesse", LectorsNeeded: 1,
}
earlier := ServiceOccurrence{
ID: "s1", ChurchID: "c1", Date: MustDate("2026-11-16"),
TimeMinutes: 9 * 60, Title: "Frühmesse", LectorsNeeded: 1,
}
person := Person{
ID: "p1", FirstName: "Anna", LastName: "Berg", Roles: []Role{RoleLector},
Active: true, MaxPerMonth: 6,
}
existing := []Assignment{{
ID: "a1", ServiceID: earlier.ID, PersonID: person.ID, Role: RoleLector,
}}
suggestions := CandidateSuggestions(service, RoleLector,
[]Person{person}, nil, existing, []ServiceOccurrence{earlier, service}, nil, nil)
if len(suggestions) != 1 || suggestions[0].Blocked {
t.Fatalf("only absences should hard-block manual suggestions, got %#v", suggestions)
}
if !StringContains(suggestions[0].Warnings, "bereits an diesem Tag") {
t.Fatalf("same-day conflict should still be warned about, got %#v", suggestions[0].Warnings)
}
}
func TestCandidateSuggestionsBlockAbsences(t *testing.T) {
service := ServiceOccurrence{
ID: "s1", ChurchID: "c1", Date: MustDate("2026-11-16"),
TimeMinutes: 18 * 60, Title: "Abendmesse", LectorsNeeded: 1,
}
person := Person{
ID: "p1", FirstName: "Anna", LastName: "Berg", Roles: []Role{RoleLector},
PreferredWeekdays: []int{1}, Active: true, MaxPerMonth: 6,
}
absences := []Absence{{
ID: "a1", PersonID: person.ID,
Start: MustDate("2026-11-16"), End: MustDate("2026-11-16"),
}}
suggestions := CandidateSuggestions(service, RoleLector,
[]Person{person}, absences, nil, []ServiceOccurrence{service}, nil, nil)
if len(suggestions) != 1 || !suggestions[0].Blocked {
t.Fatalf("absence must remain a hard block, got %#v", suggestions)
}
}