MinglunZhu asked twice for a provider by time of day and said the time setting in Routing rules is awkward. Three causes: - A conversation a night rule sent to its member stayed there after the hours: no rule matched the new turn, so Auto affinity kept it on the night's member by its cache (Smart and every routing but In order). ruleFor now sets RuleHit.Ended when the last turn's rule had hours that are over, and affine lets the conversation go (Affinity.Why "hours"); the trace says so in en, zh, zh-TW, ja, de. Tool rounds of a turn begun inside the hours stay put; Session, Strict and In turn keep their own way. - The time boxes took only ASCII "HH:MM", while their numeric keypad on a phone has no colon and a Chinese IME types ":". clockOf (GUI) and clockMinutes (CLI, saved files) now read "22", "2200", "930", "22:00", "22.00", "22点30" and full-width digits; "9:5" is still no time. time=22~08 takes ~ and ~ for the dash. - A new rule starts with an intent box; hours took an × and a +. Adding another condition now drops the intent box while it is empty. A time rule in a nested group doesn't give its conversation back yet: #5977. Run (mote): go test ./internal/provider (ok); go test -run 'TestRuleHoursOver|TestRuleTime|TestRule|Affinity|StandIn|Session|TestParse|Clock' ./internal/gateway (ok); go vet darwin, GOOS=linux -tags nogui, GOOS=windows for gateway and provider; gofmt. GUI, chromium and webkit: routing-time (8/8 after the new trace test's fix), and 28 rule/affinity/ i18n files (routing-rule-*, routing-time*, routing-intent-entry, routing-folder, routing-pause*, routing-smart-intent, routing-save*, routing-rules-line, routing-affinity-*, routing-behind-why, routing-kept-again, routing-session-folder, i18n-keys, i18n-en-no-cjk): 477/481, the 4 reds the new trace test's first wording, fixed and rerun. Broken by hand: affine's "hours" case off -> TestRuleHoursOverGiveThe ConversationBack red (Smart stays on kb by cache); origin's grouprule.go/ruleparse.go -> TestRuleTimeTypedLoosely red; client.js's "hours" case and the intent give-way off -> routing-time 0/4 in chromium; the old clockOf -> routing-time red on "9:00". Not tried: the real app with a real night/day pair of providers; the full gateway package and full GUI suite (the full Go suite runs after the tag); Windows run of the touched packages (vet only).
80 lines
3.6 KiB
Go
80 lines
3.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// #1016: a provider with one key on — Kimi Code (China), its plan's 5
|
|
// hours 49% used and its week 45% — is weighed against nobody, but the
|
|
// routing trace still tells what it has left, so the Routing page shows
|
|
// it as it does for several; a lone subscription account the same. A key
|
|
// whose windows aren't read stays not known.
|
|
func TestLoneOneTellsWhatItHasLeft(t *testing.T) {
|
|
now := time.Now()
|
|
five, week := now.Add(2*time.Hour), now.Add(26*time.Hour)
|
|
kimi := provider.Allowance{
|
|
{Used: 45, Resets: week, Span: 7 * 24 * time.Hour},
|
|
{Used: 49, Resets: five, Span: 5 * time.Hour},
|
|
}
|
|
keyWindows(t, map[string]provider.Allowance{"sk-kimi-a": kimi})
|
|
old := allowances
|
|
t.Cleanup(func() { allowances = old })
|
|
allowances = func(string) map[string]provider.Allowance {
|
|
return map[string]provider.Allowance{"me@example.com": kimi}
|
|
}
|
|
|
|
p := provider.Provider{ID: "kimi-code-cn", Name: "Kimi Code (China)", Key: "sk-kimi-a", Chat: "https://api.kimi.com/coding/v1"}
|
|
cs := []candidate{{p: p, model: "kimi-for-coding", rest: "kimi-code-cn"}}
|
|
got, wg := weigh(p, cs, "kimi-for-coding", provider.Chat)
|
|
if len(got) != 1 || got[0].rest != "kimi-code-cn" {
|
|
t.Fatalf("the one key: %s", restsOf(got))
|
|
}
|
|
w := weighed(got[0], p, wg, false, provider.Chat)
|
|
if w.Kind != "provider" || !w.Known || w.Used != 49 || len(w.Renews) != 2 || !w.Renews[0].Equal(week) {
|
|
t.Fatalf("the one key's trace: %+v", w)
|
|
}
|
|
|
|
codex := provider.Provider{ID: "codex", Account: &provider.Account{Agent: "codex", User: "me@example.com"}}
|
|
ac := []candidate{{p: codex, model: "gpt-6-astra", rest: "codex"}}
|
|
got, wg = weigh(codex, ac, "gpt-6-astra", provider.Responses)
|
|
if w := weighed(got[0], codex, wg, false, provider.Responses); w.Kind != "account" || !w.Known || w.Used != 49 {
|
|
t.Fatalf("the one account's trace: %+v", w)
|
|
}
|
|
|
|
plain := provider.Provider{ID: "deepseek", Key: "sk-ds", Chat: "https://api.deepseek.com/v1"}
|
|
pc := []candidate{{p: plain, model: "deepseek-chat", rest: "deepseek"}}
|
|
got, wg = weigh(plain, pc, "deepseek-chat", provider.Chat)
|
|
if w := weighed(got[0], plain, wg, false, provider.Chat); w.Known || w.Learns {
|
|
t.Fatalf("a key whose windows aren't read: %+v", w)
|
|
}
|
|
}
|
|
|
|
// The one key, all but used up, keeps the conversation it answered: there
|
|
// is nobody it could move to, and the routing story says it was kept,
|
|
// not that it moved.
|
|
func TestLoneSpentKeyKeepsItsConversation(t *testing.T) {
|
|
now := time.Now()
|
|
keyWindows(t, map[string]provider.Allowance{"sk-kimi-a": {{Used: 99, Resets: now.Add(2 * time.Hour), Span: 5 * time.Hour}}})
|
|
p := provider.Provider{ID: "kimi-code-cn", Key: "sk-kimi-a", Chat: "https://api.kimi.com/coding/v1"}
|
|
c := candidate{p: p, model: "kimi-for-coding", rest: "kimi-code-cn"}
|
|
cs, wg := weigh(p, []candidate{c}, "kimi-for-coding", provider.Chat)
|
|
pl := planned{order: []Weighed{weighed(cs[0], p, wg, false, provider.Chat)}}
|
|
if !pl.order[0].Known || pl.order[0].Used != 99 {
|
|
t.Fatalf("the trace: %+v", pl.order[0])
|
|
}
|
|
h := http.Header{}
|
|
h.Set("x-claude-code-session-id", "lone-spent-1016")
|
|
body := []byte(`{"messages":[{"role":"user","content":"hi"}]}`)
|
|
_, _, _, key := affine("t", provider.AffinitySession, false, false, false, false, h, provider.Chat, body, body, cs, pl)
|
|
answered(key, c, 1, Usage{})
|
|
t.Cleanup(func() { sticks.Lock(); delete(sticks.m, key); sticks.Unlock() })
|
|
got, _, aff, _ := affine("t", provider.AffinitySession, false, false, false, false, h, provider.Chat, body, body, cs, pl)
|
|
if got[0].rest != "kimi-code-cn" || aff.Why == "spent" || !aff.Kept {
|
|
t.Fatalf("kept %v, why %s", aff.Kept, aff.Why)
|
|
}
|
|
}
|