oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package schedule
2
3import (
4 "fmt"
5 "math/rand/v2"
6 "reflect"
7 "slices"
8 "testing"
9 "time"
10)
11
12var t0 = time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC)
13
14func at(d time.Duration) *time.Time { t := t0.Add(d); return &t }
15
16func sub(id, provider string, used ...float64) Conn {
17 c := Conn{ID: id, Provider: provider, Family: provider, Account: id, Billing: Subscription, Capable: true, State: StateHealthy}
18 for i, u := range used {
19 c.Windows = append(c.Windows, Window{Name: fmt.Sprintf("w%d", i), Used: u, Minutes: int64(300 * (i + 1)), ResetsAt: at(time.Duration(i+1) * time.Hour)})
20 }
21 return c
22}
23
24func meter(id string, day, month int64) Conn {
25 return Conn{ID: id, Provider: "tensorx", Family: "glm", Account: id, Billing: Metered, Capable: true, State: StateHealthy, SpendDay: day, SpendMonth: month, BudgetDay: 1_000_000, BudgetMonth: 10_000_000, Models: []Model{glm53}}
26}
27
28// TensorX's models as the runner passes them (§16.2's tiers; prices in
29// micro-units per million tokens).
30var (
31 glm53 = Model{ID: "z-ai/glm-5.3", Family: "glm", Tier: TierStrong, Output: 4_500_000, Input: 1_750_000}
32 glm52 = Model{ID: "z-ai/glm-5.2", Family: "glm", Tier: TierStandard, Output: 4_500_000, Input: 1_500_000}
33 glmFlash = Model{ID: "z-ai/glm-5.3-flash", Family: "glm", Tier: TierCheap, Output: 500_000, Input: 200_000}
34 kimiPro = Model{ID: "moonshotai/kimi-k3.1-pro", Family: "kimi", Tier: TierStrong, Output: 15_000_000, Input: 3_000_000}
35 kimi3 = Model{ID: "moonshotai/kimi-k3", Family: "kimi", Tier: TierStrong, Output: 15_000_000, Input: 3_000_000}
36 dsPro = Model{ID: "deepseek/deepseek-v4-pro-0813", Family: "deepseek", Tier: TierStrong, Output: 4_000_000, Input: 2_000_000}
37 dsFlash = Model{ID: "deepseek/deepseek-v4.1-flash", Family: "deepseek", Tier: TierStandard, Output: 1_500_000, Input: 500_000}
38 dsCheap = Model{ID: "deepseek/deepseek-v4-flash-0731", Family: "deepseek", Tier: TierCheap, Output: 300_000, Input: 250_000}
39 qwen = Model{ID: "qwen/qwen3.8-27b", Family: "qwen", Tier: TierStandard, Output: 2_400_000, Input: 400_000}
40 minimax = Model{ID: "minimax/minimax-m3.1-flash", Family: "minimax", Tier: TierCheap, Output: 1_200_000, Input: 300_000}
41 tensorxTen = []Model{glm53, kimiPro, kimi3, dsPro, glm52, qwen, dsFlash, glmFlash, dsCheap, minimax}
42)
43
44func in(step Step, cs ...Conn) Input {
45 if step.Role == "" {
46 step.Role = RoleEngineering
47 }
48 return Input{Version: Version, Now: t0, Step: step, Conns: cs}
49}
50
51// TestDecideRules is one table row per rule of §7 (and the bars).
52func TestDecideRules(t *testing.T) {
53 limited := sub("c-lim", "claude")
54 limited.State, limited.LimitedUntil = StateLimited, t0.Add(2*time.Hour)
55 passed := sub("c-back", "claude")
56 passed.State, passed.LimitedUntil = StateLimited, t0.Add(-time.Minute)
57 expired := sub("c-exp", "claude", 0.95)
58 expired.Windows[0].ResetsAt = at(-time.Minute)
59 busy := meter("t-busy", 0, 0)
60 busy.Busy = true
61 unknown := meter("t-unk", 0, 0)
62 unknown.SpendUnknown = true
63 refused := sub("c-ref", "claude")
64 refused.State = "sign_in_again"
65 cases := []struct {
66 name string
67 in Input
68 run bool
69 conn string
70 reason string
71 }{
72 // Bars.
73 {"not capable", in(Step{}, Conn{ID: "x", Billing: Subscription, State: StateHealthy}), false, "", ReasonNoConnection},
74 {"refused is not usable", in(Step{}, refused), false, "", ReasonNoConnection},
75 {"limited is not usable", in(Step{}, limited), false, "c-lim", ReasonHeadroom},
76 {"limited with its reset passed is usable", in(Step{}, passed), true, "c-back", ReasonHeadroom},
77 {"avoided", in(Step{Avoid: []string{"a"}}, sub("a", "claude"), sub("b", "codex")), true, "b", ReasonHeadroom},
78 // Thresholds.
79 {"low holds at 0.61", in(Step{Priority: Low}, sub("a", "claude", 0.61)), false, "a", ReasonHeadroom},
80 {"low runs at 0.59", in(Step{Priority: Low}, sub("a", "claude", 0.59)), true, "a", ReasonHeadroom},
81 {"normal holds at 0.80", in(Step{}, sub("a", "claude", 0.80)), false, "a", ReasonHeadroom},
82 {"normal runs at 0.79", in(Step{Priority: Normal}, sub("a", "claude", 0.79)), true, "a", ReasonHeadroom},
83 {"urgent runs at 0.99", in(Step{Priority: Urgent}, sub("a", "claude", 0.99)), true, "a", ReasonHeadroom},
84 {"urgent holds at 1.0", in(Step{Priority: Urgent}, sub("a", "claude", 1.0)), false, "a", ReasonHeadroom},
85 {"every window counts", in(Step{}, sub("a", "claude", 0.1, 0.85)), false, "a", ReasonHeadroom},
86 {"no window reported admits", in(Step{Priority: Low}, sub("a", "claude")), true, "a", ReasonHeadroom},
87 {"a window past its reset is not reported", in(Step{Priority: Low}, expired), true, "c-exp", ReasonHeadroom},
88 {"a release is urgent for thresholds", in(Step{Priority: Low, Release: "r1"}, sub("a", "claude", 0.9)), true, "a", ReasonHeadroom},
89 // Rule 1: the kept session.
90 {"kept stays whatever the threshold", in(Step{Kept: "a", Priority: Low}, sub("a", "claude", 0.95), sub("b", "codex")), true, "a", ReasonSticky},
91 {"kept not usable moves", in(Step{Kept: "c-lim"}, limited, sub("b", "codex")), true, "b", ReasonHeadroom},
92 {"kept avoided moves", in(Step{Kept: "a", Avoid: []string{"a"}}, sub("a", "claude"), sub("b", "codex")), true, "b", ReasonHeadroom},
93 {"kept metered with leave stays", in(Step{Kept: "t", Priority: Urgent, KeptModel: "z-ai/glm-5.3"}, meter("t", 0, 0), sub("b", "codex")), true, "t", ReasonSticky},
94 {"kept metered busy holds there", in(Step{Kept: "t-busy", Priority: Urgent, KeptModel: "z-ai/glm-5.3"}, busy, sub("b", "codex")), false, "t-busy", ReasonBusy},
95 {"kept metered over budget holds there", in(Step{Kept: "t", Release: "r", KeptModel: "z-ai/glm-5.3"}, meter("t", 1_000_000, 0), sub("b", "codex")), false, "t", ReasonBudget},
96 {"kept metered spend unknown holds there", in(Step{Kept: "t-unk", Priority: Urgent, KeptModel: "z-ai/glm-5.3"}, unknown, sub("b", "codex")), false, "t-unk", ReasonSpendUnknown},
97 {"kept metered without leave moves", in(Step{Kept: "t"}, meter("t", 0, 0), sub("b", "codex")), true, "b", ReasonHeadroom},
98 // Rule 2: the preference.
99 {"preferred when it admits", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.5), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude"}}, true, "a", ReasonPreference},
100 {"preferred over threshold is skipped", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.85), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude"}}, true, "b", ReasonHeadroom},
101 {"an expired preference is absent", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.5), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude", Until: at(-time.Second)}}, true, "b", ReasonHeadroom},
102 {"a preference in force", Input{Now: t0, Step: Step{Role: RoleEngineering}, Conns: []Conn{sub("a", "claude", 0.5), sub("b", "codex", 0.1)}, Preference: Preference{Provider: "claude", Until: at(time.Hour)}}, true, "a", ReasonPreference},
103 {"a metered preference never beats a subscription", Input{Now: t0, Step: Step{Role: RoleEngineering, Priority: Urgent}, Conns: []Conn{meter("t", 0, 0), sub("b", "codex", 0.9)}, Preference: Preference{Provider: "tensorx"}}, true, "b", ReasonHeadroom},
104 // Rule 3: the subscription with the most room.
105 {"reported before unreported", in(Step{}, sub("a", "claude"), sub("b", "codex", 0.7)), true, "b", ReasonHeadroom},
106 {"lowest largest used", in(Step{}, sub("a", "claude", 0.1, 0.5), sub("b", "codex", 0.4, 0.4)), true, "b", ReasonHeadroom},
107 {"then the first window", in(Step{}, sub("b", "claude", 0.3, 0.5), sub("a", "codex", 0.5, 0.2)), true, "b", ReasonHeadroom},
108 {"then the id", in(Step{}, sub("b", "claude", 0.5), sub("a", "codex", 0.5)), true, "a", ReasonHeadroom},
109 {"a subscription before metered", in(Step{Priority: Urgent}, meter("t", 0, 0), sub("b", "codex", 0.95)), true, "b", ReasonHeadroom},
110 // Rule 4: metered.
111 {"metered only with urgent", in(Step{Priority: Urgent}, meter("t", 0, 0)), true, "t", ReasonMeteredUrgent},
112 {"metered with a release", in(Step{Release: "r1"}, meter("t", 0, 0)), true, "t", ReasonMeteredRelease},
113 {"metered never for normal", in(Step{}, meter("t", 0, 0)), false, "", ReasonNoConnection},
114 {"metered never for low", in(Step{Priority: Low}, meter("t", 0, 0)), false, "", ReasonNoConnection},
115 {"metered most room", in(Step{Priority: Urgent}, meter("t2", 500_000, 0), meter("t1", 0, 6_000_000)), true, "t2", ReasonMeteredUrgent},
116 {"metered tie by id", in(Step{Priority: Urgent}, meter("t2", 0, 0), meter("t1", 0, 0)), true, "t1", ReasonMeteredUrgent},
117 {"metered at the day budget holds", in(Step{Priority: Urgent}, meter("t", 1_000_000, 0)), false, "t", ReasonBudget},
118 {"metered at the month budget holds", in(Step{Priority: Urgent}, meter("t", 0, 10_000_000)), false, "t", ReasonBudget},
119 {"metered spend unknown holds", in(Step{Priority: Urgent}, unknown), false, "t-unk", ReasonSpendUnknown},
120 {"metered busy holds", in(Step{Priority: Urgent}, busy), false, "t-busy", ReasonBusy},
121 {"a subscription over threshold, metered with leave", in(Step{Release: "r"}, sub("a", "claude", 1.0), meter("t", 0, 0)), true, "t", ReasonMeteredRelease},
122 // Rule 5: the hold names what it waits for.
123 {"nothing at all", in(Step{}), false, "", ReasonNoConnection},
124 {"headroom names the first reset", in(Step{}, sub("a", "claude", 0.9, 0.9), sub("b", "codex", 0.95)), false, "b", ReasonHeadroom},
125 }
126 for _, c := range cases {
127 t.Run(c.name, func(t *testing.T) {
128 d := Decide(c.in)
129 if d.Run != c.run || d.Connection != c.conn || d.Reason != c.reason {
130 t.Fatalf("Decide = run %v %q %q, want run %v %q %q", d.Run, d.Connection, d.Reason, c.run, c.conn, c.reason)
131 }
132 })
133 }
134}
135
136// A housekeeping attempt counts as low and never spends, whatever its
137// task's priority or a release (§7, Housekeeping).
138func TestDecideHousekeeping(t *testing.T) {
139 for _, step := range []Step{{Housekeeping: true, Priority: Urgent}, {Housekeeping: true, Release: "r"}, {Housekeeping: true, Priority: Urgent, Release: "r"}} {
140 if d := Decide(in(step, meter("t", 0, 0))); d.Run {
141 t.Fatalf("housekeeping spent: %+v", d)
142 }
143 if d := Decide(in(step, sub("a", "claude", 0.61))); d.Run || d.Effective != Low {
144 t.Fatalf("housekeeping above low's threshold: %+v", d)
145 }
146 if d := Decide(in(step, sub("a", "claude", 0.59))); !d.Run {
147 t.Fatalf("housekeeping under low's threshold: %+v", d)
148 }
149 }
150}
151
152// TestDecideReviewFamily: Review prefers another family, then its own;
153// within each, §7's order, another connection than Engineering's at
154// equal rank; a release never overrides the order.
155func TestDecideReviewFamily(t *testing.T) {
156 eng := &Engineer{ID: "a", Family: "claude", Account: "a"}
157 review := func(extra Step, cs ...Conn) Decision {
158 extra.Role, extra.Engineer = RoleReview, eng
159 return Decide(in(extra, cs...))
160 }
161 if d := review(Step{}, sub("a", "claude", 0.1), sub("b", "codex", 0.5)); d.Connection != "b" {
162 t.Fatalf("another family first: %+v", d)
163 }
164 // Within a family, §7's rules in order; at equal rank (the same
165 // rule) another connection than Engineering's, whatever its room
166 // (rev 3.3 §16.3).
167 if d := review(Step{}, sub("a", "claude", 0.1), sub("a2", "claude", 0.5)); d.Connection != "a2" {
168 t.Fatalf("another account next: %+v", d)
169 }
170 if d := review(Step{}, sub("a", "claude", 0.1)); d.Connection != "a" {
171 t.Fatalf("the same account last: %+v", d)
172 }
173 // Another family over threshold: the same family's other account
174 // reviews now rather than waiting.
175 if d := review(Step{}, sub("a", "claude", 0.1), sub("a2", "claude", 0.3), sub("b", "codex", 0.9)); d.Connection != "a2" {
176 t.Fatalf("other family held: %+v", d)
177 }
178 // Metered of another family with a release, before Engineering's own
179 // subscription.
180 if d := review(Step{Release: "r"}, sub("a", "claude", 0.1), meter("t", 0, 0)); d.Connection != "t" || d.Reason != ReasonMeteredRelease {
181 t.Fatalf("other family metered with a release: %+v", d)
182 }
183 if d := review(Step{}, sub("a", "claude", 0.1), meter("t", 0, 0)); d.Connection != "a" {
184 t.Fatalf("no leave: Engineering's own: %+v", d)
185 }
186}
187
188// TestDecideWakeUps: wake_at is the earliest time strictly after now
189// among the resets, the budget boundaries, the preference's end and the
190// deadline check; null when none.
191func TestDecideWakeUps(t *testing.T) {
192 a := sub("a", "claude", 0.9) // resets in 1h
193 lim := sub("l", "codex")
194 lim.State, lim.LimitedUntil = StateLimited, t0.Add(30*time.Minute)
195 d := Decide(in(Step{}, a, lim))
196 if d.Run || d.WakeAt == nil || !d.WakeAt.Equal(t0.Add(30*time.Minute)) {
197 t.Fatalf("a limited one's reset: %+v", d)
198 }
199 // The preference's end comes first.
200 i := in(Step{}, a)
201 i.Preference = Preference{Provider: "claude", Until: at(10 * time.Minute)}
202 if d := Decide(i); d.WakeAt == nil || !d.WakeAt.Equal(t0.Add(10*time.Minute)) {
203 t.Fatalf("the preference's end: %+v", d)
204 }
205 // The deadline check, unless it fired already.
206 by := t0.Add(24*time.Hour + 5*time.Minute)
207 if d := Decide(in(Step{By: &by}, a)); d.WakeAt == nil || !d.WakeAt.Equal(t0.Add(5*time.Minute)) {
208 t.Fatalf("the deadline check: %+v", d)
209 }
210 if d := Decide(in(Step{By: &by, DeadlineNotified: true}, a)); !d.WakeAt.Equal(t0.Add(time.Hour)) {
211 t.Fatalf("the deadline check fired already: %+v", d)
212 }
213 // The budget day boundary for a metered hold.
214 if d := Decide(in(Step{Priority: Urgent}, meter("t", 1_000_000, 0))); d.WakeAt == nil || !d.WakeAt.Equal(time.Date(2026, 11, 13, 0, 0, 0, 0, time.UTC)) {
215 t.Fatalf("the next budget day: %+v", d)
216 }
217 // Past times never wake: a reset that passed, a preference that ended.
218 old := sub("o", "claude", 0.9)
219 old.Windows[0].ResetsAt = at(-time.Hour)
220 old.Windows = append(old.Windows, Window{Name: "w9", Used: 0.95}, Window{Name: "w8", Used: 0.95, ResetsAt: at(0)})
221 i = in(Step{}, old)
222 i.Preference = Preference{Provider: "claude", Until: at(-time.Minute)}
223 if d := Decide(i); d.Run || d.WakeAt != nil {
224 t.Fatalf("no future wake-up: %+v", d)
225 }
226}
227
228// TestDecideNeedsYou: the deadline check, no reset in sight and
229// nothing that can run it ask the person; a reset in time does not.
230func TestDecideNeedsYou(t *testing.T) {
231 a := sub("a", "claude", 0.9)
232 if d := Decide(in(Step{}, a)); d.NeedsPerson != "" {
233 t.Fatalf("a reset in an hour asks nobody: %+v", d)
234 }
235 far := sub("f", "claude", 0.9)
236 far.Windows[0].ResetsAt = at(73 * time.Hour)
237 if d := Decide(in(Step{}, far)); d.NeedsPerson != NeedsNoReset {
238 t.Fatalf("a reset 73 h away: %+v", d)
239 }
240 none := sub("n", "claude", 0.9)
241 none.Windows[0].ResetsAt = nil
242 if d := Decide(in(Step{}, none)); d.NeedsPerson != NeedsNoReset || d.WakeAt != nil {
243 t.Fatalf("no reset reported: %+v", d)
244 }
245 if d := Decide(in(Step{})); d.NeedsPerson != NeedsNoConnection {
246 t.Fatalf("nothing connected: %+v", d)
247 }
248 by := t0.Add(23 * time.Hour)
249 if d := Decide(in(Step{By: &by}, a)); d.NeedsPerson != NeedsDeadline {
250 t.Fatalf("the deadline check: %+v", d)
251 }
252 if d := Decide(in(Step{By: &by, DeadlineNotified: true}, a)); d.NeedsPerson != "" {
253 t.Fatalf("the deadline check fired already: %+v", d)
254 }
255 busy := meter("t", 0, 0)
256 busy.Busy = true
257 if d := Decide(in(Step{Priority: Urgent}, busy)); d.NeedsPerson != "" {
258 t.Fatalf("a busy slot ends by itself: %+v", d)
259 }
260 unknown := meter("t", 0, 0)
261 unknown.SpendUnknown = true
262 if d := Decide(in(Step{Priority: Urgent}, unknown)); d.NeedsPerson != NeedsNoReset {
263 t.Fatalf("spend unknown waits for the person: %+v", d)
264 }
265}
266
267// randomInput is a random Input over a small, adversarial domain:
268// thresholds' edges, past and future resets, every bar.
269func randomInput(r *rand.Rand) Input {
270 pick := func(xs ...float64) float64 { return xs[r.IntN(len(xs))] }
271 dur := func() *time.Time {
272 switch r.IntN(4) {
273 case 0:
274 return nil
275 case 1:
276 return at(-time.Duration(r.IntN(120)) * time.Minute)
277 }
278 return at(time.Duration(r.IntN(100*60)) * time.Minute)
279 }
280 providers := []string{"claude", "codex", "tensorx"}
281 var cs []Conn
282 for i := range r.IntN(5) {
283 p := providers[r.IntN(len(providers))]
284 c := Conn{ID: fmt.Sprintf("c%d", i), Provider: p, Family: p, Account: fmt.Sprintf("acct%d", r.IntN(3)), Capable: r.IntN(6) > 0}
285 c.State = []string{StateHealthy, StateHealthy, StateLimited, "sign_in_again", ""}[r.IntN(5)]
286 if c.State == StateLimited {
287 if t := dur(); t != nil {
288 c.LimitedUntil = *t
289 }
290 }
291 if p == "tensorx" {
292 c.Billing = Metered
293 c.Busy, c.SpendUnknown = r.IntN(4) == 0, r.IntN(5) == 0
294 c.BudgetDay, c.BudgetMonth = 1_000_000, 10_000_000
295 c.SpendDay = int64(pick(0, 500_000, 999_999, 1_000_000, 2_000_000))
296 c.SpendMonth = int64(pick(0, 9_999_999, 10_000_000))
297 // Some of TensorX's models, some needing a probe (§16.3).
298 for _, m := range tensorxTen {
299 if r.IntN(3) == 0 {
300 m.Unprobed = r.IntN(4) == 0
301 c.Models = append(c.Models, m)
302 }
303 }
304 } else {
305 c.Billing = Subscription
306 for j := range r.IntN(3) {
307 c.Windows = append(c.Windows, Window{Name: fmt.Sprintf("w%d", j), Used: pick(0, 0.3, 0.59, 0.6, 0.61, 0.79, 0.8, 0.81, 0.99, 1.0), Minutes: int64(pick(0, 300, 10080)), ResetsAt: dur()})
308 }
309 }
310 cs = append(cs, c)
311 }
312 step := Step{Role: []string{RoleEngineering, RoleReview, RoleResearch}[r.IntN(3)], Priority: []string{"", Low, Normal, Urgent, "bogus"}[r.IntN(5)], DeadlineNotified: r.IntN(2) == 0}
313 if r.IntN(3) == 0 {
314 step.Release = "r1"
315 }
316 step.Housekeeping = r.IntN(6) == 0
317 if r.IntN(2) == 0 {
318 step.By = dur()
319 }
320 if len(cs) > 0 && r.IntN(3) == 0 {
321 k := cs[r.IntN(len(cs))]
322 step.Kept = k.ID
323 if len(k.Models) > 0 && r.IntN(4) > 0 {
324 step.KeptModel = k.Models[r.IntN(len(k.Models))].ID
325 } else if k.Billing == Metered {
326 step.KeptModel = tensorxTen[r.IntN(len(tensorxTen))].ID
327 }
328 }
329 if len(cs) > 0 && r.IntN(3) == 0 {
330 step.Avoid = []string{cs[r.IntN(len(cs))].ID}
331 }
332 if step.Role == RoleReview && len(cs) > 0 {
333 e := cs[r.IntN(len(cs))]
334 family := e.Family
335 if e.Billing == Metered {
336 // Engineering's recorded family: one of the models it ran.
337 family = []string{"glm", "kimi", "deepseek", "qwen", "minimax"}[r.IntN(5)]
338 }
339 step.Engineer = &Engineer{ID: e.ID, Family: family, Account: e.Account}
340 }
341 pref := Preference{Provider: []string{"", "claude", "codex", "tensorx"}[r.IntN(4)]}
342 if r.IntN(2) == 0 {
343 pref.Until = dur()
344 }
345 return Input{Version: Version, Now: t0, Step: step, Conns: cs, Preference: pref}
346}
347
348func connOf(i Input, id string) (Conn, bool) {
349 for _, c := range i.Conns {
350 if c.ID == id {
351 return c, true
352 }
353 }
354 return Conn{}, false
355}
356
357// someAdmits reports whether any candidate passes every bar and its
358// threshold: rules 1 to 4 would yield it.
359func someAdmits(i Input) bool {
360 for _, c := range i.Conns {
361 if !eligible(c, i.Step, i.Now) {
362 continue
363 }
364 if c.Billing == Metered {
365 if leave(i.Step) && spendBar(c) == "" {
366 return true
367 }
368 continue
369 }
370 if Admits(c, Effective(i.Step), i.Now) || c.ID == i.Step.Kept {
371 return true
372 }
373 }
374 return false
375}
376
377// TestDecideInvariants are §7's invariants (a) to (f), and wake-ups
378// strictly in the future, over many random inputs.
379func TestDecideInvariants(t *testing.T) {
380 r := rand.New(rand.NewPCG(7, 13))
381 runs, holds := 0, 0
382 for n := range 20000 {
383 i := randomInput(r)
384 before := fmt.Sprintf("%#v", i)
385 d := Decide(i)
386 if fmt.Sprintf("%#v", i) != before {
387 t.Fatalf("#%d: Decide changed its input", n)
388 }
389 // (f) the same input, the same output.
390 if again := Decide(i); !reflect.DeepEqual(again, d) {
391 t.Fatalf("#%d: not deterministic: %+v then %+v", n, d, again)
392 }
393 if d.WakeAt != nil && !d.WakeAt.After(i.Now) {
394 t.Fatalf("#%d: a wake-up not after now: %v", n, d.WakeAt)
395 }
396 if !d.Run {
397 holds++
398 // (e) a hold only when rules 1 to 4 all fail. A kept metered
399 // session held by its spending bar is rule 1's hold.
400 if k, ok := connOf(i, i.Step.Kept); ok && k.Billing == Metered && eligible(k, i.Step, i.Now) && leave(i.Step) && spendBar(k) != "" {
401 if m, ok := modelByID(k.Models, i.Step.KeptModel); ok && !m.Unprobed {
402 continue
403 }
404 }
405 if someAdmits(i) {
406 t.Fatalf("#%d: held though a candidate admits: %+v\n%#v", n, d, i)
407 }
408 continue
409 }
410 runs++
411 c, ok := connOf(i, d.Connection)
412 if !ok {
413 t.Fatalf("#%d: ran on an unknown connection %q", n, d.Connection)
414 }
415 if !eligible(c, i.Step, i.Now) {
416 t.Fatalf("#%d: a bar passed: %+v on %#v", n, d, c)
417 }
418 if c.Billing == Metered {
419 // (b) no metered admission without urgent or a release, and
420 // never for housekeeping.
421 if i.Step.Priority != Urgent && i.Step.Release == "" || i.Step.Housekeeping {
422 t.Fatalf("#%d: metered without leave: %+v", n, d)
423 }
424 // (c) never two attempts on one slot; (d) never at the budget
425 // or with the spend unknown.
426 if c.Busy || c.SpendUnknown || c.SpendDay >= c.BudgetDay || c.SpendMonth >= c.BudgetMonth {
427 t.Fatalf("#%d: metered admitted past its guard: %+v on %#v", n, d, c)
428 }
429 continue
430 }
431 // (a) no subscription admission at or above t(effective), except a
432 // kept session.
433 if d.Reason != ReasonSticky {
434 for _, w := range c.Windows {
435 if (w.ResetsAt == nil || i.Now.Before(*w.ResetsAt)) && w.Used >= Threshold(Effective(i.Step)) {
436 t.Fatalf("#%d: admitted at %.2f, threshold %.2f: %+v", n, w.Used, Threshold(Effective(i.Step)), d)
437 }
438 }
439 } else if c.ID != i.Step.Kept {
440 t.Fatalf("#%d: sticky on a connection that is not kept: %+v", n, d)
441 }
442 }
443 if runs < 1000 || holds < 1000 {
444 t.Fatalf("the generator is lopsided: %d runs, %d holds", runs, holds)
445 }
446}
447
448// TestDecideOrderFree: the order the connections are listed in never
449// changes the decision (the precedence is total).
450func TestDecideOrderFree(t *testing.T) {
451 r := rand.New(rand.NewPCG(3, 5))
452 for n := range 5000 {
453 i := randomInput(r)
454 d := Decide(i)
455 j := i
456 j.Conns = slices.Clone(i.Conns)
457 r.Shuffle(len(j.Conns), func(a, b int) { j.Conns[a], j.Conns[b] = j.Conns[b], j.Conns[a] })
458 if e := Decide(j); !reflect.DeepEqual(e, d) {
459 t.Fatalf("#%d: order changed the decision: %+v then %+v", n, d, e)
460 }
461 }
462}
463