Grok ("grok") joins Zed in retiredBuiltins. It is served by
@magpie-community/opencode-grok-auth, moved or not.
Removed, the built-in's own paths:
- the sign-in (startGrokSignIn) and the per-account homes it made;
- the Logins/switch/on/forget cases and the alsoOn branch;
- grokAccount and the account list entry;
- the usage fetch (grokSubscriptionUsage, grokLoginUsage, the windows);
- the model list (grokModels, grokContextModels, the window labels);
- the token refresh, the headers, the version probe, and routing's usage
clearing.
grok_usage_test.go is deleted, along with the six built-in-only tests in
grok_test.go and the four in grok_accounts_test.go.
Kept, because something still uses them:
- grokLogins and readGrokCredential, for the mover (migrate_side.go);
- grokBody, for the plugin's request bodies;
- GrokHome, the CLI install (pluginRunsCLI) and GrokBase;
- the gateway's grok draw, video, search and namespace code. It asks
PluginProvider()=="grok" too.
Each remaining grok file has a PLUGIN-SERVED notice.
Fixes the retirement needed:
- movedAgent asks Moved again, not PluginServes. A retired Grok that is
not moved yet still records the CLI's own sign-in, so the move takes it.
TestMoveGrok fails without this: only the second account moved.
- In Logins(""), a retired built-in keeps its place, with no list. Grok's
and Zed's accounts list where the built-ins' stood. Without this,
TestAllLoginsMovedKeepTheirPlace fails ("[zed grok]").
Tests whose meaning changed:
- The gateway's grokSignedIn now serves Grok through the fake plugin
moved onto "grok". It proxies to provider.GrokBase, read per request
and refused unless loopback, so no test reaches the network. It names
127.0.0.1 in fetchPrivate, because the vendor's own host is now the
fake's.
- TestVideoContentAtPrivateAddressNotFetched clears that setting. Without
the clear it fails with "200 secret-bytes".
- TestGrokAccountDraws checks the plugin's Authorization only. The real
plugin's headers are the plugin repo's to test.
- The Grok cases in TestVideosRedacted and TestMediaRedactedErrors reach
their echo through GrokBase (grokEcho).
- TestVideoModelByItsBareName's chat model is the fake's fake-1.
- TestGrokPluginSearchesItself expects "grok", not grok-plugin.
- TestGrokHideOwnSignIn is removed: hiding the CLI's own sign-in was the
built-in's ForgetLogin.
- grok_accounts_test.go keeps test-only newGrokHome and addGrokLogin
fixtures for the mover's tests.
Run in the worktree:
- gofmt -l internal: empty.
- go vet -tags nogui ./... on darwin, GOOS=linux and GOOS=windows: clean.
- go test -tags nogui -count=1 -run
'Private|Grok|grok|CallerUsageVideos|GatewayKeyModelsHoldTheImageAndVideoLists|RemoteMagpie|Video|Redact'
./internal/gateway: ok.
- go test -tags nogui -count=1 -run
'Grok|grok|Migrat|Move|Moved|Retired|Retir|SignIn|AsksOwnBackend|SideLogin|Hide|OwnRow|Logins|Unmoved'
./internal/provider ./internal/cli: ok, ok.
- go test -tags nogui -count=1 -run 'Grok|grok|Plugin|Retired|Moved'
./internal/gui: ok.
Not run: the full suite, the GUI node suite, and a real Grok account.
The machine is under heavy load.
260 lines
8.5 KiB
Go
260 lines
8.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/plugin"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
func TestGrok(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("GROK_HOME", filepath.Join(home, "grok"))
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(home, "grok", "config.toml")
|
|
os.MkdirAll(filepath.Dir(path), 0o755)
|
|
os.WriteFile(path, []byte("# mine\n[ui]\ntheme = \"dark\"\n\n[model.my-own]\nmodel = \"x\"\nbase_url = \"https://x/v1\"\n\n[models]\ndefault = \"grok-4.6\"\n"), 0o644)
|
|
read := func() string { b, _ := os.ReadFile(path); return string(b) }
|
|
table := func(name string) map[string]string {
|
|
t.Helper()
|
|
got, err := edit.GetTOMLTable(path, name)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return got
|
|
}
|
|
a := grok(home)
|
|
if a.Path != path {
|
|
t.Fatalf("path: %s", a.Path)
|
|
}
|
|
f := a.Field("model")
|
|
if f.Get() != "grok-4.6" {
|
|
t.Fatalf("get: %q", f.Get())
|
|
}
|
|
|
|
if err := f.Set("magpie/deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw := read()
|
|
m := table(`model."magpie/deepseek/pro"`)
|
|
if m["model"] != "deepseek/pro" || m["api_key"] != "magpie" || !strings.HasSuffix(m["base_url"], "/v1") ||
|
|
m["api_backend"] != "chat_completions" || table(`model."magpie/deepseek/flash"`) == nil {
|
|
t.Fatalf("tables:\n%s", raw)
|
|
}
|
|
// a campaign of xAI's would set the default over it
|
|
if table("features")["campaigns"] != "false" {
|
|
t.Fatalf("campaigns:\n%s", raw)
|
|
}
|
|
if f.Get() != "magpie/deepseek/pro" || !strings.Contains(raw, "# mine") || !strings.Contains(raw, "[model.my-own]") ||
|
|
table("ui")["theme"] != "dark" {
|
|
t.Fatalf("config:\n%s", raw)
|
|
}
|
|
|
|
// picked again, and synced: the tables are replaced, never repeated
|
|
if err := f.Set("magpie/deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw = read()
|
|
if n := strings.Count(raw, `[model."magpie/deepseek/pro"]`); n != 1 {
|
|
t.Fatalf("%d tables:\n%s", n, raw)
|
|
}
|
|
|
|
// a provider removed leaves Grok's list on the next sync
|
|
provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"flash"}})
|
|
if err := a.Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(read(), `"magpie/deepseek/pro"]`) {
|
|
t.Fatalf("stale:\n%s", read())
|
|
}
|
|
|
|
// its own model: magpie steps out, the user's own model stays
|
|
if err := f.Set("grok-4.6"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw = read()
|
|
if f.Get() != "grok-4.6" || strings.Contains(raw, "magpie") || !strings.Contains(raw, "[model.my-own]") {
|
|
t.Fatalf("own:\n%s", raw)
|
|
}
|
|
// nothing through magpie: sync leaves the file alone
|
|
if err := a.Sync(); err != nil || read() != raw {
|
|
t.Fatalf("sync touched it:\n%s", read())
|
|
}
|
|
|
|
e := a.Field("effort")
|
|
f.Set("magpie/deepseek/flash")
|
|
if err := e.Set("high"); err != nil || e.Get() != "high" {
|
|
t.Fatalf("effort: %v %q", err, e.Get())
|
|
}
|
|
if err := f.Set(""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw = read()
|
|
if f.Get() != "" || strings.Contains(raw, "magpie") || strings.Contains(raw, "campaigns") || e.Get() != "high" {
|
|
t.Fatalf("reset:\n%s", raw)
|
|
}
|
|
}
|
|
|
|
func TestGrokReadErrorStopsChanges(t *testing.T) {
|
|
const wired = "[model.\"magpie/keep\"]\nmodel = \"keep\"\n\n"
|
|
for _, tc := range []struct {
|
|
name, input string
|
|
}{
|
|
{"models", wired + "[models]\ndefault = [\n"},
|
|
{"features", "[models]\ndefault = \"magpie/keep\"\n\n" + wired + "[features]\ncampaigns = [\n"},
|
|
} {
|
|
for _, action := range []string{"set", "clear", "sync"} {
|
|
t.Run(tc.name+"/"+action, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("GROK_HOME", home)
|
|
path := filepath.Join(home, "config.toml")
|
|
if err := os.WriteFile(path, []byte(tc.input), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a := grok(home)
|
|
var err error
|
|
switch action {
|
|
case "set":
|
|
err = a.Field("model").Set("grok-native")
|
|
case "clear":
|
|
err = a.Field("model").Set("")
|
|
case "sync":
|
|
err = a.Sync()
|
|
}
|
|
if err == nil || !strings.HasPrefix(err.Error(), path+": ") {
|
|
t.Fatalf("expected the config path in the error, got %v", err)
|
|
}
|
|
got, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != tc.input {
|
|
t.Fatalf("changed config after a read error:\n%s", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGrokSyncRequiresOrdinaryModelTable(t *testing.T) {
|
|
for _, body := range []string{
|
|
"note = '''\n[model.\"magpie/fake\"]\n'''\n",
|
|
"[[model.\"magpie/array\"]]\nmodel = \"array\"\n",
|
|
} {
|
|
t.Run(body, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("GROK_HOME", home)
|
|
path := filepath.Join(home, "config.toml")
|
|
input := "[models]\ndefault = \"grok-native\"\n" + body
|
|
if err := os.WriteFile(path, []byte(input), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := grok(home).Sync(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(got) != input {
|
|
t.Fatalf("synced a config without a magpie model table:\n%s", got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGrokCheckReportsTOMLParseErrors(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("GROK_HOME", home)
|
|
path := filepath.Join(home, "config.toml")
|
|
input := "[models]\ndefault = \"magpie/fake\"\n\n[model.\"magpie/fake\"]\ninvalid = [\n"
|
|
if err := os.WriteFile(path, []byte(input), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
message := grok(home).Check()
|
|
if !strings.HasPrefix(message, path+": line 5, column 11:") || !strings.Contains(message, "array is incomplete") {
|
|
t.Fatalf("check did not report the parse location: %q", message)
|
|
}
|
|
}
|
|
|
|
// The models magpie serves on the account Grok Build is signed in to are
|
|
// Grok's own a second time, beside its own: they fold into one row of the
|
|
// picker (Fate on Discord: grokbuild 在登录态下会加载重复的模型), the
|
|
// other providers' rows left as they are.
|
|
func TestGrokFoldsItsOwnAccount(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
t.Setenv("GROK_HOME", filepath.Join(home, "grok"))
|
|
exe := provider.GrokExecutable
|
|
provider.GrokExecutable = func() string { return filepath.Join(home, "grok", "bin", "grok") }
|
|
t.Cleanup(func() { provider.GrokExecutable = exe })
|
|
provider.ForgetAccounts()
|
|
t.Cleanup(provider.ForgetAccounts)
|
|
os.MkdirAll(filepath.Join(home, "grok"), 0o755)
|
|
os.WriteFile(filepath.Join(home, "grok", "auth.json"), []byte(`{"x":{"key":"k","email":"me@x.ai"}}`), 0o600)
|
|
// the Grok plugin serves the account, taken from Grok Build's own
|
|
spec := "@magpie-community/opencode-grok-auth"
|
|
write := func(name string, v any) {
|
|
t.Helper()
|
|
b, _ := json.Marshal(v)
|
|
if err := os.MkdirAll(settings.Dir(), 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(settings.Dir(), name), b, 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
write("plugins.json", map[string]any{"plugins": []map[string]any{{"spec": spec}}})
|
|
write("plugin-auth.json", map[string]map[string]any{"grok": {"type": "oauth", "refresh": filepath.Join(home, "grok")}})
|
|
plugin.UseCached([]plugin.Provider{{ID: "grok", Spec: spec, Name: "Grok",
|
|
Models: []plugin.Model{{ID: "grok-4.3", Name: "Grok 4.3"}}, Accounts: []plugin.Account{{Key: "grok", Type: "oauth"}}}})
|
|
t.Cleanup(plugin.Settle)
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a := grok(home)
|
|
var same, others []string
|
|
for _, o := range a.Field("model").Options(a.Values()) {
|
|
if o.Same {
|
|
same = append(same, o.Value)
|
|
} else if strings.HasPrefix(o.Value, "magpie/") {
|
|
others = append(others, o.Value)
|
|
}
|
|
}
|
|
if len(same) == 0 {
|
|
t.Fatalf("nothing folded; magpie's rows: %v", others)
|
|
}
|
|
for _, v := range same {
|
|
if !strings.HasPrefix(v, "magpie/grok/") {
|
|
t.Errorf("folded %s, not on the Grok account", v)
|
|
}
|
|
}
|
|
for _, v := range others {
|
|
if strings.HasPrefix(v, "magpie/grok/") {
|
|
t.Errorf("%s on the Grok account not folded", v)
|
|
}
|
|
}
|
|
if !slices.Contains(others, "magpie/deepseek/pro") {
|
|
t.Errorf("DeepSeek's row: %v", others)
|
|
}
|
|
}
|