package scheduler import ( "testing" "gitea.mynodes.xyz/ilker/llmgw/internal/router/endpoint" "gitea.mynodes.xyz/ilker/llmgw/internal/router/kvindex" "gitea.mynodes.xyz/ilker/llmgw/internal/router/prefix" ) func kvScorer(got *[]kvindex.Decision) *PrefixScorer { return NewPrefixScorer(nil, prefix.DefaultThresholds(), nil). WithKV(2, func(d kvindex.Decision) { *got = append(*got, d) }) } func TestReplicasAreScoredByTheShareOfThePromptTheyHold(t *testing.T) { var got []kvindex.Decision q := &KVQuery{Matched: map[string]int{"a": 2, "b": 8}, Total: map[string]int{"a": 8, "b": 8}} s := kvScorer(&got).Score(newCtx(Request{KV: q}), []endpoint.Snapshot{ep("a"), ep("b"), ep("c")}) if s[0] != 0.25 || s[1] != 1 || s[2] != 0 || got[0] != kvindex.Hit { t.Fatalf("scores %v, decisions %v", s, got) } } func TestACertainHitIsStillIgnoredWhenLoadIsLopsided(t *testing.T) { var got []kvindex.Decision q := &KVQuery{Matched: map[string]int{"a": 8}, Total: map[string]int{"a": 8, "b": 8}} busy := ep("a", func(s *endpoint.Snapshot) { s.Stats.Waiting = 40 }) s := kvScorer(&got).Score(newCtx(Request{KV: q}), []endpoint.Snapshot{busy, ep("b")}) if s[0] != 0 || got[0] != kvindex.LoadOverride { t.Fatalf("scores %v, decisions %v", s, got) } } func TestAFallbackWithTier1OffScoresNothing(t *testing.T) { var got []kvindex.Decision q := &KVQuery{Fallback: kvindex.FallbackNoIndex} s := kvScorer(&got).Score(newCtx(Request{KV: q, PromptPrefix: "x"}), []endpoint.Snapshot{ep("a"), ep("b")}) if s[0] != 0 || s[1] != 0 || len(got) != 1 || got[0] != kvindex.FallbackNoIndex { t.Fatalf("scores %v, decisions %v", s, got) } }