Files
magpie/internal/agent/cursorlocal_test.go
T
yetone 258eae592d agent: Cursor Private Inference, which magpie finds by its product.json and starts on magpie with a command its row copies, and whose /models gives each model's api_types and capabilities (#299, mamba on Discord) — the build (Cursor 3.23.x, the same app name, bundle id and ~/.cursor as Cursor) runs its agent locally and asks the endpoint in CURSOR_LOCAL_AGENT_BASE_URL / CURSOR_LOCAL_AGENT_API_KEY when its Open configuration has none; mamba confirmed it works with any account pointed at magpie. magpie writes nothing of Cursor's (its settings share Cursor's state.vscdb): the Agents page lists it when an app's product.json says "Cursor Private Inference" (/Applications, ~/Applications; %LOCALAPPDATA%\Programs on Windows; /opt and the like on Linux), and its row's one control, Copy launch command (复制启动命令), copies CURSOR_LOCAL_AGENT_BASE_URL=<gateway>/v1 CURSOR_LOCAL_AGENT_API_KEY=magpie-cursor-local '<app>' (PowerShell on Windows); in the tray panel the row copies it. Its notice says to quit it first (a second start hands over to the running one) and that a base URL in Open configuration comes first. /v1/models asked with its key gives each model api_types (the APIs its provider serves it on, all three for a group or a translated one; it asks Anthropic Messages only when no OpenAI one is listed) and capabilities (context_length, max_output_tokens, supports_vision); any other client's list is unchanged. usage.AgentOf matches a name before a UA prefix, so cursor-local isn't counted as Cursor. TestCursorLocal, TestCursorLocalModels and cursor-local-launch.test.cjs (WebKit, en and zh) fail on the old code; agy-launch and agent-* GUI tests pass on WebKit. Not tried with the real app (not installed here). go vet, builds (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 23:38:19 +08:00

79 lines
3.0 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/usage"
)
// Cursor Private Inference (#299) is found by its product.json among the
// apps, beside a regular Cursor of the same app name that isn't it, and its
// row gives the command that starts it on magpie, with its own key; its
// requests are its own in the usage, not Cursor's.
func TestCursorLocal(t *testing.T) {
root := t.TempDir()
app := func(dir, nameShort, exe string) {
t.Helper()
res := filepath.Join(root, dir, "Contents", "Resources", "app")
if err := os.MkdirAll(res, 0o755); err != nil {
t.Fatal(err)
}
os.WriteFile(filepath.Join(res, "product.json"), []byte(`{"nameShort":"`+nameShort+`","applicationName":"cursor","dataFolderName":".cursor"}`), 0o644)
if exe != "" {
os.WriteFile(filepath.Join(root, dir, "Contents", "Info.plist"), []byte("<plist><dict>\n\t<key>CFBundleExecutable</key>\n\t<string>"+exe+"</string>\n</dict></plist>"), 0o644)
}
}
app("Cursor.app", "Cursor", "Cursor")
if got := findCursorLocal([]string{root, filepath.Join(root, "none")}); got != "" {
t.Fatalf("a regular Cursor found as it: %q", got)
}
app("Cursor Private Inference.app", "Cursor Private Inference", "Cursor PI")
want := filepath.Join(root, "Cursor Private Inference.app", "Contents", "MacOS", "Cursor PI")
if got := findCursorLocal([]string{root}); got != want {
t.Fatalf("found %q, want %q", got, want)
}
os.Remove(filepath.Join(root, "Cursor Private Inference.app", "Contents", "Info.plist"))
if got := findCursorLocal([]string{root}); filepath.Base(got) != "Cursor" || !strings.Contains(got, "Private Inference.app") {
t.Fatalf("without an Info.plist: %q", got)
}
// the command: the gateway's /v1 and the agent's key, the app quoted
cmd := CursorLocalLaunch("/Applications/It's.app/Contents/MacOS/Cursor", "http://127.0.0.1:3425")
for _, s := range []string{"CURSOR_LOCAL_AGENT_BASE_URL", "http://127.0.0.1:3425/v1", "CURSOR_LOCAL_AGENT_API_KEY", gateway.TokenFor("cursor-local")} {
if !strings.Contains(cmd, s) {
t.Errorf("%q lacks %q", cmd, s)
}
}
if runtime.GOOS != "windows" && !strings.HasSuffix(cmd, ` '/Applications/It'\''s.app/Contents/MacOS/Cursor'`) {
t.Errorf("app not quoted: %q", cmd)
}
// the agent: there and launchable when the app is, else not there
old := cursorLocalApp
t.Cleanup(func() { cursorLocalApp = old })
a, err := Find(CursorLocalID)
if err != nil {
t.Fatal(err)
}
cursorLocalApp = func() string { return want }
if !a.detect() || !strings.Contains(a.Launch(), want) {
t.Errorf("installed: detect %v, launch %q", a.detect(), a.Launch())
}
cursorLocalApp = func() string { return "" }
if a.detect() || a.Launch() != "" {
t.Error("not installed, yet there")
}
if got := usage.AgentOf("cursor-local"); got != CursorLocalID {
t.Errorf("AgentOf(cursor-local) = %q", got)
}
if got := usage.AgentOf("Cursor/3.2"); got != "cursor" {
t.Errorf("AgentOf(Cursor/3.2) = %q", got)
}
}