Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

3.8 KB
Raw Blame
package provider

import (
	"context"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"github.com/yetone/magpie/internal/settings"
)

// #957 (Evan26Ma): two Plus accounts started at one daily time run out
// together; each can have its own, so one takes over as the other's
// window ends. An account with none follows the time for all, and one set
// off is never started at a time of day.
func TestWarmAtEachAccountsOwn(t *testing.T) {
	path := filepath.Join(t.TempDir(), "codex-warmup.json")
	f := &fakeWarm{now: at8(28, 6, 0), errs: map[string]error{}}
	idleAll := func() {
		for _, u := range []string{"[email protected]", "[email protected]", "[email protected]"} {
			f.ws[u] = []QuotaWindow{win("5 hours", fiveHours, 0, f.now.Add(fiveHours))}
		}
	}
	f.ws = map[string][]QuotaWindow{}
	idleAll()
	own := map[string]string{"[email protected]": "09:00", "[email protected]": ""}
	run := func(at string) []string {
		t.Helper()
		f.sent = nil
		w := f.warmer(path)
		w.ownAt = func() map[string]string { return own }
		w.warmNow(context.Background(), "", times(at))
		return f.sent
	}
	// six: b, which follows the time for all; a waits for its nine, c for none
	if got := strings.Join(run("06:00"), ","); got != "[email protected]" {
		t.Fatalf("at six sent %q", got)
	}
	// nine: a, as its own name is kept in any case
	f.now = at8(28, 9, 0)
	idleAll()
	own = map[string]string{"[email protected]": "09:00", "[email protected]": ""}
	f.ws["[email protected]"] = f.ws["[email protected]"]
	delete(f.ws, "[email protected]")
	if got := strings.Join(run("06:00"), ","); got != "[email protected]" {
		t.Fatalf("at nine sent %q", got)
	}
	// the time for all off: an account's own still starts it, the rest none
	f.now = at8(29, 9, 0)
	idleAll()
	delete(f.ws, "[email protected]")
	if got := strings.Join(run(""), ","); got != "[email protected]" {
		t.Fatalf("the next day, the time for all off, sent %q", got)
	}
}

// keepWarm looks at the windows when an account's own time comes, even
// with nothing else on, and never with nothing on at all.
func TestWarmLookAtAnAccountsOwnTime(t *testing.T) {
	last, now := at8(28, 8, 59), at8(28, 9, 0)
	if !warmLook("", times(""), map[string]string{"a@x": "09:00"}, false, last, now) {
		t.Error("an account's own nine not looked at")
	}
	if warmLook("", times(""), map[string]string{"a@x": "10:00"}, false, last, now) {
		t.Error("looked at before its time")
	}
	if warmLook("", times(""), map[string]string{"a@x": ""}, true, time.Time{}, now) {
		t.Error("looked at with nothing on")
	}
	if !warmLook("", times("06:00"), nil, false, time.Time{}, now) || !warmLook("week", times(""), nil, false, now.Add(-codexWarmEvery), now) {
		t.Error("the time for all, or the warm-up on reset, not looked at")
	}
}

// An account's own time is kept by its name in lower case, as 06:00
// whichever way it came; "" follows the time for all again; one that isn't
// a time is refused.
func TestSetCodexWarmAt(t *testing.T) {
	azureHome(t)
	if err := SetCodexWarmAt("[email protected] ", "9:00"); err != nil {
		t.Fatal(err)
	}
	if err := SetCodexWarmAt("[email protected]", "OFF"); err != nil {
		t.Fatal(err)
	}
	if at, ok := CodexWarmAtOf("[email protected]"); !ok || at != "09:00" {
		t.Fatalf("a: %q %v", at, ok)
	}
	got := codexWarmAtOf()
	if len(got) != 2 || got["[email protected]"] != "09:00" || got["[email protected]"] != "" {
		t.Fatalf("own times %v", got)
	}
	if err := SetCodexWarmAt("[email protected]", "nine"); err == nil {
		t.Fatal("nine taken")
	}
	if err := SetCodexWarmAt("[email protected]", ""); err != nil {
		t.Fatal(err)
	}
	if _, ok := CodexWarmAtOf("[email protected]"); ok {
		t.Fatal("still its own")
	}
	if err := SetCodexWarmAt("[email protected]", ""); err != nil {
		t.Fatal(err)
	}
	if s := settings.Load(); s.CodexWarmAtOf != nil {
		t.Fatalf("kept %v", s.CodexWarmAtOf)
	}
	if err := SetCodexWarmAt(" ", "06:00"); err == nil {
		t.Fatal("no account taken")
	}
}