package boxchoice import ( "reflect" "testing" "time" ) var policyNow = time.Date(2026, 10, 16, 12, 0, 0, 0, time.UTC) // roomy is a candidate every ยง7 rule passes at policyNow. func roomy(id string, memFree int64) Candidate { return Candidate{BoxID: id, Region: "eu-west-2", MemFreeMiB: memFree, MemTotalMiB: 8000, DiskFreeMiB: 50000, DiskTotalMiB: 100000, Generation: policyNow.Add(-5 * time.Minute).Format(time.RFC3339)} } func TestPlaceableAppliesEachRule(t *testing.T) { gen := func(d time.Duration) string { return policyNow.Add(d).Format(time.RFC3339) } cases := []struct { name string mut func(*Candidate) role string want bool }{ {"every rule holds", nil, "", true}, {"memory exactly 15%", func(c *Candidate) { c.MemFreeMiB = 1200 }, "", true}, {"memory under 15%", func(c *Candidate) { c.MemFreeMiB = 1199 }, "", false}, {"no memory total", func(c *Candidate) { c.MemTotalMiB = 0 }, "", false}, {"disk exactly 10%", func(c *Candidate) { c.DiskFreeMiB = 10000 }, "", true}, {"disk under 10%", func(c *Candidate) { c.DiskFreeMiB = 9999 }, "", false}, {"no disk total", func(c *Candidate) { c.DiskTotalMiB = 0 }, "", false}, {"edges file exactly 15 minutes old", func(c *Candidate) { c.Generation = gen(-15 * time.Minute) }, "", true}, {"edges file older than 15 minutes", func(c *Candidate) { c.Generation = gen(-15*time.Minute - time.Second) }, "", false}, {"edges file a minute in the future", func(c *Candidate) { c.Generation = gen(time.Minute) }, "", true}, {"edges file further in the future", func(c *Candidate) { c.Generation = gen(time.Minute + time.Second) }, "", false}, {"no generation", func(c *Candidate) { c.Generation = "" }, "", false}, {"unreadable generation", func(c *Candidate) { c.Generation = "yesterday" }, "", false}, {"legacy role", nil, "legacy", false}, {"platform role", nil, "platform", false}, {"a role this build does not know", nil, "draining", false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { c := roomy("i-0aaa", 4000) if tc.mut != nil { tc.mut(&c) } got := Placeable([]Candidate{c}, map[string]string{"i-0aaa": tc.role}, policyNow) if (len(got) == 1) != tc.want { t.Fatalf("placeable = %v, want %v", ids(got), tc.want) } }) } } func TestExcludeRolesKeepsOrderAndDefaults(t *testing.T) { cands := []Candidate{roomy("c", 1), roomy("a", 1), roomy("b", 1)} got := ExcludeRoles(cands, map[string]string{"a": "legacy", "b": ""}) if !reflect.DeepEqual(ids(got), []string{"c", "b"}) { t.Fatalf("got %v", ids(got)) } if got := ExcludeRoles(cands, nil); len(got) != 3 { t.Fatalf("no roles excluded %v", ids(got)) } } func TestChooseRoomiestSpreadsThenMostMemoryThenHash(t *testing.T) { running := map[string]Load{"a": {Hosts: 1, Apps: map[string]int{"bab/jam": 1}}} // a has the most memory but already runs the app: spread wins. got, ok := ChooseRoomiest("h.example", []Candidate{roomy("a", 7000), roomy("b", 2000), roomy("c", 3000)}, running, "bab/jam") if !ok || got.BoxID != "c" { t.Fatalf("spread then memory: got %q", got.BoxID) } // Spread never refuses: every box runs the app, so memory decides. all := map[string]Load{"a": running["a"], "b": running["a"]} got, _ = ChooseRoomiest("h.example", []Candidate{roomy("a", 1000), roomy("b", 2000)}, all, "bab/jam") if got.BoxID != "b" { t.Fatalf("all run the app: got %q", got.BoxID) } // Fewer hosts is no longer a rule: memory beats it. busy := map[string]Load{"a": {Hosts: 50}, "b": {Hosts: 1}} got, _ = ChooseRoomiest("h.example", []Candidate{roomy("a", 5000), roomy("b", 4000)}, busy, "bab/jam") if got.BoxID != "a" { t.Fatalf("memory over host count: got %q", got.BoxID) } // Equal memory: the P5-D2 hash, whatever the order. one, _ := ChooseRoomiest("h.example", []Candidate{roomy("a", 1000), roomy("b", 1000)}, nil, "bab/jam") two, _ := ChooseRoomiest("h.example", []Candidate{roomy("b", 1000), roomy("a", 1000)}, nil, "bab/jam") if one.BoxID != two.BoxID { t.Fatalf("order changed the answer: %q %q", one.BoxID, two.BoxID) } want, _ := Choose("h.example", []Candidate{roomy("a", 1000), roomy("b", 1000)}, nil, "bab/jam") if one.BoxID != want.BoxID { t.Fatalf("hash tie-break differs from spec 1's: %q, want %q", one.BoxID, want.BoxID) } if _, ok := ChooseRoomiest("h.example", nil, nil, "bab/jam"); ok { t.Fatal("chose from nothing") } } func TestEligibleCarriesTotalsAndGeneration(t *testing.T) { body := haBox("i-0aaa", func(m map[string]any) { m["headroom"] = map[string]any{"mem_free_mib": 1000, "disk_free_mib": 2000, "mem_total_mib": 8000, "disk_total_mib": 40000, "awake": 1} m["generation"] = "2026-10-16T11:55:00Z" }) got := Eligible(views(t, body), rosterFor("i-0aaa"), "uk", "eu-west-2", "ie", testKeyFP, nil) want := Candidate{BoxID: "i-0aaa", Region: "eu-west-2", MemFreeMiB: 1000, DiskFreeMiB: 2000, MemTotalMiB: 8000, DiskTotalMiB: 40000, Awake: 1, Generation: "2026-10-16T11:55:00Z"} if len(got) != 1 || got[0] != want { t.Fatalf("got %+v, want %+v", got, want) } }