Files
magpie/internal/plugin/pi_test.go
T
JoeyWu-tech d0a61b856a plugin: the pi tests keep the pinned pi's Bun cache between runs (#2865)
installPinnedPi's `bun install --frozen-lockfile` used the install
cache bunCommand names, which lies in the test's home. testenv makes a
new home for every go test process, so every run fetched pi's 118
tarballs before the host could start, inside the test's 4 minutes.
yetone saw TestPiPackage red at 86fef947e after 226s, most of it that
install, with gateway's tests run beside it on a Mac at load 26.

Now the install gets a Bun cache that outlives the home,
magpie-test-bun-cache-<uid>-<n> in os.TempDir() (pinnedPiCache), as
the last BUN_INSTALL_CACHE_DIR in its environment. The first run fills
it, and a run after that installs pi from it and asks npm for nothing.
Bun names each entry by package, version and registry, so a new pin,
or a run through another registry, fetches only what is missing. It
holds about 155 MB here. This is the first thing the tests leave
behind between runs, on purpose: testenv's own bound is the temp
folder (checkChild), so the folder is inside it, but it is outside the
test home, which goes when the run ends. agent-wiring.md's "Tests
never touch the real machine" now says so.

Why that folder:
- It is none of the user's own folders. testenv moves HOME, the XDG
  folders, APPDATA and LOCALAPPDATA into the sandbox, not the temp
  folder, and ~/.bun, ~/.cache and ~/.config/magpie are never named.
  Only the install's own process gets the variable, so the host and
  anything else a test starts never see it. The host keeps the home's
  cache, and pruneInstallCache, which prunes the app's cache, never
  touches this one.
- It is this user's. The uid is in its name, as Linux's temp folder is
  shared (macOS' and Windows' are the user's own). It is made 0700, and
  used only when it is a folder, not a link, that no one else has any
  permission on (not checked on Windows, where Go reports no real modes)
  and that this user can make a file in. One someone else made fails
  one of those, and the install then uses the home's cache as before.
  Root always uses the home's cache: root passes those checks in a
  folder another user made, and would run what that user put there.
- Its files are never old enough for a temp folder's cleaner. macOS
  takes files under its temp folder that went unread for about 3 days,
  and Bun doesn't notice a file missing from a cached package: it
  installs the rest, and pi doesn't load (shown below). So the folder
  is a new one every two days (<n> counts two day periods), the one
  before is kept for a run still using it, and older ones are taken
  out by the next run. The first run in a new period fetches pi again.
  TestPinnedPiCacheMovesOn checks the folder moves, the one before
  stays and older ones go.
- Two go test runs can share it. Bun unpacks each tarball into a folder
  of its own and renames it into the cache. When another install got
  there first, it swaps the two or removes the old one and renames
  again (move_to_cache_directory in Bun 1.4.2's extract_tarball.rs,
  written for concurrent installs sharing one cache; on Windows it
  retries the move). Two runs started at once on an empty cache both
  passed here.
- Bun's documented default on Linux is to hardlink the installed files
  to the cache (not looked at here), so a test that wrote into pi's
  installed files in place could change the cache. None does.

No assertion in the pi tests goes red when the cache is taken out: they
pass either way while npm answers inside their 4 minutes. What
regresses is the fetches, counted below; the times move with npm.

What was run (tree 93b344b1, on origin/main acdc85ac, go1.26.8,
linux/amd64, Linux 5.15, umask 077):
- npm's registry through a local relay (BUN_CONFIG_REGISTRY, Bun 1.4.2)
  that passes every request on at once and logs it, with `go test -tags
  nogui -count=1 -v -run <tests> ./internal/plugin/`, load 16 to 26,
  the cache folders removed first:
  - TestPiPackage alone: pass, 29.38s, 118 tarballs asked (no
    manifest). Run again: pass, 1.13s, nothing asked.
  - Then TestGatewayHookMixedPiPackage and TestPiPackage: pass, 0.92s
    and 1.04s, nothing asked.
  - Two TestPiPackage runs started at once: both pass (227.40s and
    115.58s), with every tarball asked twice (236 asked, 118
    distinct). A run after them: 1.13s, nothing asked.
- Broken by hand:
  - The cache not given to the install (the home's cache, as before
    this commit), the folder left full: TestPiPackage passes in 141.92s
    with 118 tarballs asked again.
  - One file of pi-coding-agent's cached package taken out, as a temp
    cleaner would: TestPiPackage red in 0.49s, nothing asked,
    `@earendil-works/pi-coding-agent isn't installed: add it as a
    plugin's dependency, or reinstall the pi plugin`.
  - The folder's name without its period: TestPinnedPiCacheMovesOn red,
    `after two days the cache is ".../magpie-test-bun-cache-<uid>-0",
    not a folder other than ...`. Older folders kept: red, `a folder
    four days old is kept (<nil>)`.
- go test -tags nogui -count=1 -timeout 45m ./internal/plugin/ (Bun
  1.4.2, npm direct): ok, 341 PASS lines counting subtests, 2 skip
  (TestFlowBigReplyProd wants MAGPIE_FLOW_BIG, TestRenewOtherHost is
  run by TestRenewOnceAcrossHosts). TestGatewayHookMixedPiPackage
  9.88s, TestPiPackage 1.08s.
- gofmt -l clean. go vet -tags nogui for linux, windows and darwin, go
  vet for windows, go build -tags nogui for linux and darwin and go
  build for windows: exit 0 each. go vet and go build without the tag
  on linux: exit 1 (no libsoup-3.0 or gio-unix-2.0 here).
- Not run again at this tree: -race and the full suite. This tree
  differs from tree f2b71fdd only in pi_test.go and agent-wiring.md;
  at f2b71fdd internal/plugin passed with -race, and the full suite
  had 79 packages ok, internal/plugin among them, and 7 tests red in 2
  packages, each filed: agent TestInstalledVersion,
  TestCursorFolderFromTheEditorIsNotTheCLI,
  TestQoderSettingsFromAnIDEPluginIsNotTheCLI, TestOmpDetectedWithPiDir
  and TestZedAppPath (#3141); gui TestGatewayHeldByAContainer and
  TestGatewayFixQuitsOnlyAMagpie (#3649).

Not tried: macOS and Windows (their checks and cleaners were only
read), a run as root, a real temp cleaner, and the pi tests under a
load like yetone's.

Reviewed and run again by the maintainer at the stack's tree 45994757c
(this commit and the six beside it, on origin/main 90518de26), go1.26.8:
- mote (macOS, arm64): gofmt -l clean; go vet and go build for darwin,
  GOOS=linux -tags nogui and GOOS=windows (library, plugin, gui): exit 0.
  go test -tags nogui -count=1 -timeout 45m ./...: every package ok but
  internal/gui TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
  internal/proc TestNoCommandBypassesProc (#4491), both red on main
  from b09349d17 and c59c547c5, in files this stack doesn't touch.
  go test -race -run 'TestWSL|TestTagged|TestPinnedPiCache' for
  library and plugin: ok.
- magpie-desktop (Linux, wsl-run; WebKit hangs on mote), Chromium and
  WebKit: every library-*, sync-*, s3-sync, github-sync and i18n-keys
  file, 4 at a time: 1003 of 1020. The 17 reds are in 6 files no commit here
  touches: i18n-keys ("Unmarked", #4524), library-chip-fan, -items,
  -mcp-health, -mcp-hub (#4483) and -mcp-tools. Those 6 alone, one at a
  time, at load ~60: 74 of 92 here, 77 of 92 at 90518de26 itself. Red
  in both: i18n-keys, -chip-fan and -items in WebKit, -mcp-tools in
  Chromium ja 420; -mcp-health green in both; -mcp-hub's WebKit case
  red here only, the case #4483 has red on main. -chip-fan and -items
  are #3072's and #4501's kind, WebKit under load.
- On macOS (mote), the pi tests twice in a row: TestGatewayHookMixedPiPackage
  21.41s then 0.96s, TestPiPackage 1.23s then 1.24s, and
  TestPinnedPiCacheMovesOn ok: the second run installs from the kept
  cache. It sits in os.TempDir(), outside a test's HOME, by design.
- The stack rebased on origin/main e4c784177 (main's 7 commits since
  share only docs/subsystems/agent-wiring.md with it, a line apart),
  with no conflict: on mote gofmt -l, go vet (linux nogui,
  windows), go test -tags nogui -count=1 ./internal/library
  ./internal/plugin ok and internal/gui's budget tests ok; on Linux
  sync-note-show and library-wsl-asleep, both engines: 92 of 92.
2026-10-31 17:03:55 +08:00

596 lines
24 KiB
Go

package plugin
import (
"bufio"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"slices"
"strconv"
"strings"
"testing"
"time"
)
// piReply is what a request to a pi provider answered: its status and
// headers, and its events (streamed) or message.
type piReply struct {
status int
header map[string]string
events []map[string]any
body map[string]any
}
func piAsk(t *testing.T, ctx context.Context, provider, model string, req map[string]any) piReply {
t.Helper()
req["model"] = model
b, _ := json.Marshal(req)
res, err := Fetch(ctx, FetchRequest{
Provider: provider, Model: model, NPM: "@ai-sdk/anthropic",
URL: "http://pi.magpie/v1/messages", Method: "POST",
Headers: map[string]string{"content-type": "application/json"},
Body: b,
})
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
r := piReply{status: res.StatusCode, header: map[string]string{}}
for k := range res.Header {
r.header[strings.ToLower(k)] = res.Header.Get(k)
}
if !strings.HasPrefix(res.Header.Get("Content-Type"), "text/event-stream") {
all, _ := io.ReadAll(res.Body)
if err := json.Unmarshal(all, &r.body); err != nil {
t.Fatalf("%s: %d %s", model, res.StatusCode, all)
}
return r
}
sc := bufio.NewScanner(res.Body)
sc.Buffer(nil, 1<<20)
for sc.Scan() {
if d, ok := strings.CutPrefix(sc.Text(), "data: "); ok {
var e map[string]any
if err := json.Unmarshal([]byte(d), &e); err != nil {
t.Fatalf("event %q: %v", d, err)
}
r.events = append(r.events, e)
}
}
return r
}
// blocks are the content blocks a stream told, as a message has them.
func (r piReply) blocks() []map[string]any {
var out []map[string]any
for _, e := range r.events {
switch e["type"] {
case "content_block_start":
out = append(out, e["content_block"].(map[string]any))
case "content_block_delta":
b, d := out[int(e["index"].(float64))], e["delta"].(map[string]any)
switch d["type"] {
case "text_delta":
b["text"] = b["text"].(string) + d["text"].(string)
case "thinking_delta":
b["thinking"] = b["thinking"].(string) + d["thinking"].(string)
case "signature_delta":
b["signature"] = d["signature"]
case "input_json_delta":
var in any
json.Unmarshal([]byte(d["partial_json"].(string)), &in)
b["input"] = in
}
}
}
return out
}
// seen is what the fake extension's stream said pi handed it.
func (r piReply) seen(t *testing.T) map[string]any {
t.Helper()
bs := r.blocks()
if r.body != nil {
for _, b := range r.body["content"].([]any) {
bs = append(bs, b.(map[string]any))
}
}
for _, b := range bs {
if b["type"] == "text" {
var s map[string]any
if err := json.Unmarshal([]byte(b["text"].(string)), &s); err != nil {
t.Fatalf("text %q: %v", b["text"], err)
}
return s
}
}
t.Fatalf("no text in %+v %+v", r.events, r.body)
return nil
}
// A pi package (pi coding agent's extensions) is a plugin: pi is
// installed with it, its extensions load with their commands and tools
// left aside, its providers list their models, sign in as pi signs in
// (the OAuth login's questions asked one at a time, the code pasted
// back; a key), renew through magpie, and answer Anthropic's Messages
// from pi's stream: text, thinking with its signature, tool calls and
// their thought signatures back, usage, and a failure's status.
func TestPiPackage(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute)
defer cancel()
installPinnedPi(t, ctx)
abs := filepath.Join(Dir(), "fake-pi-package")
if err := os.CopyFS(abs, os.DirFS("testdata/pi")); err != nil {
t.Fatal(err)
}
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
if v := Version(piAgent); v != pinnedPi {
t.Fatalf("pi is %q after adding the package, not the %s the lock has", v, pinnedPi)
}
ls, err := Plugins(ctx)
if err != nil || len(ls) != 1 || ls[0].Error != "" {
t.Fatalf("Plugins = %+v, %v", ls, err)
}
ps, err := Providers(ctx)
if err != nil {
t.Fatal(err)
}
byID := map[string]Provider{}
for _, p := range ps {
byID[p.ID] = p
}
fp, fk := byID["fakepi"], byID["fakepi-key"]
if len(ps) != 2 || fp.Name != "FakePi" || fk.Name != "FakePi Key" {
t.Fatalf("Providers = %+v", ps)
}
if len(fp.Methods) != 1 || fp.Methods[0].Type != "oauth" || fp.Methods[0].Label != "Sign in with FakePi Account" {
t.Fatalf("fakepi's methods = %+v", fp.Methods)
}
if len(fk.Methods) != 1 || fk.Methods[0].Type != "api" {
t.Fatalf("fakepi-key's methods = %+v", fk.Methods)
}
models := map[string]Model{}
for _, m := range fp.Models {
models[m.ID] = m
}
if m := models["fp-think"]; m.NPM != "@ai-sdk/anthropic" || m.Context != 100000 || m.Output != 8000 || !m.Reasoning || !m.Image || !slices.Equal(m.Variants, []string{"low", "high"}) {
t.Fatalf("fp-think = %+v", m)
}
if m := models["fp-plain"]; m.Reasoning || m.Image || m.Context != 1000 {
t.Fatalf("fp-plain = %+v", m)
}
// the login asks how on pi's terminal UI (a list, then a field), its
// region and its team, shows its page, and waits for the code
signIn := func(code string) (Saved, error) {
p, err := NextPrompt(ctx, "fakepi", 0, map[string]string{})
if err != nil || p == nil || p.Type != "select" || p.Message != "FakePi Login" || len(p.Options) != 2 || p.Options[1].Label != "Your organization" || p.Options[1].Value != "org" || p.Options[1].Hint != "its start URL" {
t.Fatalf("first prompt = %+v, %v", p, err)
}
in := map[string]string{p.Key: "org"}
p, err = NextPrompt(ctx, "fakepi", 0, in)
if err != nil || p == nil || p.Type != "text" || p.Message != "Start URL (https://…)" {
t.Fatalf("the field = %+v, %v", p, err)
}
in[p.Key] = "https://acme.invalid/start"
p, err = NextPrompt(ctx, "fakepi", 0, in)
if err != nil || p == nil || p.Type != "select" || len(p.Options) != 2 || p.Options[0].Label != "China" || p.Options[0].Value != "cn" {
t.Fatalf("the region = %+v, %v", p, err)
}
in[p.Key] = p.Options[0].Value
p, err = NextPrompt(ctx, "fakepi", 0, in)
if err != nil || p == nil || p.Message != "Team?" || p.Placeholder != "blue" {
t.Fatalf("second prompt = %+v, %v", p, err)
}
in[p.Key] = "blue"
if p, err := NextPrompt(ctx, "fakepi", 0, in); err != nil || p != nil {
t.Fatalf("asked after the team: %+v, %v", p, err)
}
a, err := Authorize(ctx, "fakepi", 0, in, NewAccount)
if err != nil || a.Method != "code" || a.URL != "https://fakepi.invalid/auth?team=blue" || a.Instructions != "Sign in as blue" {
t.Fatalf("Authorize = %+v, %v", a, err)
}
return Finish(ctx, a.Session, code)
}
if _, err := signIn("bad"); !errors.Is(err, ErrFailed) || !strings.Contains(err.Error(), "that code isn't right") {
t.Fatalf("a bad code: %v", err)
}
if got, err := signIn("good"); err != nil || got != (Saved{"fakepi", "fakepi"}) {
t.Fatalf("Finish = %+v, %v", got, err)
}
if b, err := os.ReadFile(filepath.Join(filepath.Dir(AuthPath()), "pi", "stolen")); err == nil {
t.Fatalf("a program the login started read magpie's messages: %s", b)
}
var saved map[string]map[string]any
b, _ := os.ReadFile(AuthPath())
json.Unmarshal(b, &saved)
if a := saved["fakepi"]; a["type"] != "oauth" || a["refresh"] != "r-blue" || a["access"] != "a-blue" || a["team"] != "blue" || a["region"] != "cn" || a["org"] != "https://acme.invalid/start" {
t.Fatalf("saved %v", saved)
}
// pi's auth.json has the sign-in as pi keeps it, and the package, told
// the session started, reads it back and adds a model
late := false
for i := 0; i < 100 && !late; i++ {
ps, _ := Providers(ctx)
for _, p := range ps {
for _, m := range p.Models {
late = late || p.ID == "fakepi" && m.ID == "fp-blue"
}
}
time.Sleep(100 * time.Millisecond)
}
var piAuth map[string]map[string]any
b, _ = os.ReadFile(filepath.Join(filepath.Dir(AuthPath()), "pi", "auth.json"))
json.Unmarshal(b, &piAuth)
if a := piAuth["fakepi"]; a["type"] != "oauth" || a["refresh"] != "r-blue" || !late {
t.Fatalf("pi's auth.json %v; the model added on session_start listed: %v", piAuth, late)
}
if got, err := APIKey(ctx, "fakepi-key", 0, nil, "k-123", NewAccount); err != nil || got.Account != "fakepi-key" {
t.Fatalf("APIKey = %+v, %v", got, err)
}
// a request as pi's context, with the account's sign-in, as hard a
// think as the model can, and the output it can give
r := piAsk(t, ctx, "fakepi", "fp-think", map[string]any{
"max_tokens": 99999, "stream": true,
"system": []map[string]any{{"type": "text", "text": "be brief"}},
"thinking": map[string]any{"type": "enabled", "budget_tokens": 30000},
"tools": []map[string]any{{"name": "lookup", "description": "looks up", "input_schema": map[string]any{"type": "object"}}},
"messages": []map[string]any{{"role": "user", "content": []map[string]any{
{"type": "text", "text": "hello"},
{"type": "image", "source": map[string]any{"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo="}},
}}},
})
s := r.seen(t)
// the extension's import reaches the pi the lock installed, not npm's
// latest that Bun installs for a folder with no node_modules above it
// (#2865)
if s["pi"] != pinnedPi {
t.Fatalf("the extension's import reached pi %v, not the %s the lock installed", s["pi"], pinnedPi)
}
if r.status != 200 || s["apiKey"] != "a-blue" || s["reasoning"] != "high" || s["maxTokens"] != 8000.0 || s["system"] != "be brief" ||
!slices.Equal(anys(s["tools"]), []string{"lookup"}) || !slices.Equal(anys(s["images"]), []string{"image/png"}) {
t.Fatalf("pi was handed %v (%d)", s, r.status)
}
// the empty text the vendor sent isn't told
if bs := r.blocks(); len(bs) != 1 {
t.Fatalf("blocks = %+v", bs)
}
var types []string
for _, e := range r.events {
types = append(types, e["type"].(string))
}
if types[0] != "message_start" || types[len(types)-1] != "message_stop" {
t.Fatalf("events = %v", types)
}
md := r.events[len(r.events)-2]
u := md["usage"].(map[string]any)
if md["type"] != "message_delta" || md["delta"].(map[string]any)["stop_reason"] != "end_turn" || u["input_tokens"] != 11.0 || u["output_tokens"] != 7.0 || u["cache_read_input_tokens"] != 3.0 || u["cache_creation_input_tokens"] != 2.0 {
t.Fatalf("message_delta = %v", md)
}
// message_start tells the prompt's usage pi's start named, which
// Claude Code's context meter reads before message_delta (#1887)
if u := r.events[0]["message"].(map[string]any)["usage"].(map[string]any); u["input_tokens"] != 11.0 || u["output_tokens"] != 0.0 || u["cache_read_input_tokens"] != 3.0 || u["cache_creation_input_tokens"] != 2.0 {
t.Fatalf("message_start's usage = %v", u)
}
// and the prompt pi-ai's anthropic-messages counts only after its
// start, on the message the events after it share, which goes on being
// written: its output isn't the start's
r = piAsk(t, ctx, "fakepi", "fp-plain", map[string]any{"max_tokens": 100, "stream": true, "messages": []map[string]any{{"role": "user", "content": "late"}}})
if u := r.events[0]["message"].(map[string]any)["usage"].(map[string]any); u["input_tokens"] != 11.0 || u["output_tokens"] != 0.0 || u["cache_read_input_tokens"] != 3.0 || u["cache_creation_input_tokens"] != 2.0 {
t.Fatalf("message_start's usage, counted after pi's start = %v", u)
}
if u := r.events[len(r.events)-2]["usage"].(map[string]any); u["input_tokens"] != 11.0 || u["output_tokens"] != 7.0 || u["cache_read_input_tokens"] != 3.0 || u["cache_creation_input_tokens"] != 2.0 {
t.Fatalf("message_delta's usage, counted after pi's start = %v", u)
}
// a model that can't think less than "low" is asked to, one that
// doesn't think isn't
for model, want := range map[string]any{"fp-think": "low", "fp-plain": nil} {
r := piAsk(t, ctx, "fakepi", model, map[string]any{"max_tokens": 100, "messages": []map[string]any{{"role": "user", "content": "hi"}}})
if s := r.seen(t); s["reasoning"] != want {
t.Fatalf("%s thought %v", model, s["reasoning"])
}
}
// thinking with its signature, and a tool call
r = piAsk(t, ctx, "fakepi", "fp-think", map[string]any{"max_tokens": 100, "stream": true, "messages": []map[string]any{{"role": "user", "content": "think, then use a tool"}}})
bs := r.blocks()
if len(bs) != 2 || bs[0]["type"] != "thinking" || bs[0]["thinking"] != "hmm" || bs[0]["signature"] != "sig-1" ||
bs[1]["type"] != "tool_use" || bs[1]["id"] != "call_1" || bs[1]["name"] != "lookup" || bs[1]["input"].(map[string]any)["q"] != "x" {
t.Fatalf("blocks = %+v", bs)
}
if md := r.events[len(r.events)-2]; md["delta"].(map[string]any)["stop_reason"] != "tool_use" {
t.Fatalf("message_delta = %v", md)
}
// and back: the signatures go with the turn (the call's kept by
// magpie, as tool_use has no field for it), the result named
r = piAsk(t, ctx, "fakepi", "fp-think", map[string]any{"max_tokens": 100, "stream": false, "messages": []map[string]any{
{"role": "user", "content": "think, then use a tool"},
{"role": "assistant", "content": []map[string]any{
{"type": "thinking", "thinking": "hmm", "signature": "sig-1"},
{"type": "tool_use", "id": "call_1", "name": "lookup", "input": map[string]any{"q": "x"}},
}},
{"role": "user", "content": []map[string]any{
{"type": "tool_result", "tool_use_id": "call_1", "content": "found"},
{"type": "text", "text": "go on"},
}},
}})
s = r.seen(t)
if r.status != 200 || r.body["stop_reason"] != "end_turn" ||
!slices.Equal(anys(s["roles"]), []string{"user", "assistant", "toolResult", "user"}) ||
!slices.Equal(anys(s["sigs"]), []string{"sig-1", "ts-1"}) || !slices.Equal(anys(s["results"]), []string{"lookup:found"}) {
t.Fatalf("pi was handed %v; %+v", s, r.body)
}
// a failure before anything is said is the vendor's status
r = piAsk(t, ctx, "fakepi", "fp-think", map[string]any{"max_tokens": 100, "stream": true, "messages": []map[string]any{{"role": "user", "content": "fail429"}}})
if e, _ := r.body["error"].(map[string]any); r.status != 429 || r.header["retry-after"] != "7" || e["type"] != "rate_limit_error" || !strings.Contains(e["message"].(string), "slow down") {
t.Fatalf("a 429: %d %v %v", r.status, r.header, r.body)
}
// a refusal the package didn't read, with nothing said, is told
r = piAsk(t, ctx, "fakepi", "fp-plain", map[string]any{"max_tokens": 100, "stream": true, "messages": []map[string]any{{"role": "user", "content": "quota"}}})
if e, _ := r.body["error"].(map[string]any); r.status != 429 || e["message"] != "exceed quota limit (1005)" {
t.Fatalf("a refusal sent as JSON: %d %v", r.status, r.body)
}
if r := piAsk(t, ctx, "fakepi", "nope", map[string]any{"max_tokens": 100, "messages": []map[string]any{{"role": "user", "content": "hi"}}}); r.status != 404 {
t.Fatalf("an unknown model: %d %v", r.status, r.body)
}
// the key's provider is asked with the key
if s := piAsk(t, ctx, "fakepi-key", "fk-1", map[string]any{"max_tokens": 100, "messages": []map[string]any{{"role": "user", "content": "hi"}}}).seen(t); s["apiKey"] != "k-123" {
t.Fatalf("fakepi-key was handed %v", s)
}
// a sign-in about to expire is renewed by magpie, with pi's refresh,
// and kept
b, _ = os.ReadFile(AuthPath())
var all map[string]map[string]any
json.Unmarshal(b, &all)
all["fakepi"]["expires"] = time.Now().Add(time.Minute).UnixMilli()
b, _ = json.Marshal(all)
if err := os.WriteFile(AuthPath(), b, 0o600); err != nil {
t.Fatal(err)
}
if s := piAsk(t, ctx, "fakepi", "fp-plain", map[string]any{"max_tokens": 100, "messages": []map[string]any{{"role": "user", "content": "hi"}}}).seen(t); s["apiKey"] != "renewed-r-blue" {
t.Fatalf("after the renewal pi was handed %v", s)
}
b, _ = os.ReadFile(AuthPath())
json.Unmarshal(b, &saved)
if a := saved["fakepi"]; a["access"] != "renewed-r-blue" || a["team"] != "blue" || a["type"] != "oauth" {
t.Fatalf("the renewal kept %v", a)
}
}
func anys(v any) []string {
var out []string
for _, x := range v.([]any) {
s, _ := x.(string)
out = append(out, s)
}
return out
}
// The Plugins page's search finds pi packages that bring a provider as
// well as OpenCode plugins, and not pi packages that only add pi a
// command or a guard.
func TestSearchFindsPiPackages(t *testing.T) {
fakeNPM(t, func(w http.ResponseWriter, r *http.Request) {
obj := func(name string, kw ...string) string {
k, _ := json.Marshal(kw)
return `{"package":{"name":"` + name + `","version":"1.0.0","keywords":` + string(k) + `}}`
}
var objs []string
if q := r.URL.Query().Get("text"); strings.HasPrefix(q, "keywords:pi-package ") {
objs = []string{obj("pi-antigravity", "pi-package", "oauth", "provider"), obj("pi-antigravity-guard", "pi-package", "antigravity"), obj("opencode-antigravity-auth", "opencode")}
} else {
objs = []string{obj("opencode-antigravity-auth", "opencode", "plugin"), obj("antigravity-cli")}
}
w.Write([]byte(`{"objects":[` + strings.Join(objs, ",") + `]}`))
})
hits, err := Search(context.Background(), "antigravity")
var names []string
for _, h := range hits {
names = append(names, h.Package)
}
if err != nil || !slices.Equal(names, []string{"opencode-antigravity-auth", "pi-antigravity"}) {
t.Fatalf("Search = %v, %v", names, err)
}
}
// pinnedPi is the pi testdata/pi-lock installs. Its bun.lock names every
// package of pi at a version and its npm integrity, so the test runs on
// the same pi each time. Installed as ensurePi installs it, at npm's
// latest, the test ran on whatever pi's packages were that minute: in the
// three minutes between @earendil-works/pi-codemode 1.2.0 (which dropped
// MCP_TYPESCRIPT_PREAMBLE) and pi-coding-agent 1.2.0, latest was
// pi-coding-agent 1.1.0, whose ^1.1.0 took pi-codemode 1.2.0, and pi
// didn't load (#1879). To move to a newer pi, set its version in
// testdata/pi-lock/package.json and run `bun install --ignore-scripts`
// there, with npm's registry, to write the lock again.
const pinnedPi = "1.2.0"
// installPinnedPi installs testdata/pi-lock's pi into the plugins folder
// with its lock frozen, as the folder would have it after ensurePi. A
// test's pi package is put in the plugins folder too, so pi and the
// package's imports of it resolve to that pi: from a folder with no
// node_modules above it, testdata/pi or a TempDir, Bun installs npm's
// latest pi as the host starts (its auto-install), and the host waited
// on npm for over 100 manifests and ran that pi, not this one (#2865).
func installPinnedPi(t *testing.T, ctx context.Context) {
t.Helper()
if err := os.MkdirAll(Dir(), 0o755); err != nil {
t.Fatal(err)
}
for _, f := range []string{"package.json", "bun.lock"} {
b, err := os.ReadFile(filepath.Join("testdata", "pi-lock", f))
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(Dir(), f), b, 0o644); err != nil {
t.Fatal(err)
}
}
bun, err := Bun(ctx)
if err != nil {
t.Fatal(err)
}
cmd := bunCommand(ctx, bun, Dir(), "install", "--frozen-lockfile", "--ignore-scripts")
if cache := pinnedPiCache(time.Now()); cache != "" {
cmd.Env = append(cmd.Env, "BUN_INSTALL_CACHE_DIR="+cache) // os/exec takes a key's last
}
if out, err := cmd.CombinedOutput(); err != nil {
t.Skipf("pi couldn't be installed: %v: %s", err, lastLines(string(out), 6))
}
if v := Version(piAgent); v != pinnedPi {
t.Fatalf("installed pi %q, the lock has %s", v, pinnedPi)
}
}
// pinnedPiCache is the Bun install cache installPinnedPi installs with at
// now, in the temp folder, which outlives the test's home: a run after the
// first installs pi from it, where the home's own cache started empty each
// run and every run fetched pi's 118 tarballs, most of TestPiPackage's 4
// minutes on a busy machine (#2865). It is this user's (their uid in its
// name, as the temp folder is shared on Linux; Windows' is the user's
// own), and used only when no one else can read or write it and this
// user can write in it; else "", and the home's cache is used as before.
// Never as root: root passes those checks in a folder another user made.
// It is a new folder every two days (pinnedPiCacheDays), so none of its
// files is old enough for the temp folder's cleaner to have taken it
// (macOS' takes what went unread for 3 days): Bun installs a package
// whose cached folder lost a file as it is, and pi didn't load. The
// folder before stays for a run still using it; older ones go. Bun
// unpacks a package in a folder of its own and renames it into the
// cache, so two go test runs can share it. It isn't the app's cache
// (installCacheDir), which pruneInstallCache prunes.
func pinnedPiCache(now time.Time) string {
if os.Getuid() == 0 {
return ""
}
name := "magpie-test-bun-cache-" + strconv.Itoa(os.Getuid()) + "-"
n := now.Unix() / int64(pinnedPiCacheDays*24*time.Hour/time.Second)
old, _ := filepath.Glob(filepath.Join(os.TempDir(), name+"*"))
for _, o := range old {
if k, err := strconv.ParseInt(strings.TrimPrefix(filepath.Base(o), name), 10, 64); err == nil && k < n-1 {
os.RemoveAll(o)
}
}
dir := filepath.Join(os.TempDir(), name+strconv.FormatInt(n, 10))
if err := os.MkdirAll(dir, 0o700); err != nil {
return ""
}
fi, err := os.Lstat(dir)
if err != nil || !fi.IsDir() || runtime.GOOS != "windows" && fi.Mode().Perm()&0o077 != 0 {
return ""
}
f, err := os.CreateTemp(dir, "probe-")
if err != nil {
return ""
}
f.Close()
os.Remove(f.Name())
return dir
}
// pinnedPiCacheDays is how long pinnedPiCache keeps one folder.
const pinnedPiCacheDays = 2
// The pinned pi's cache moves to a new folder every two days, the one
// before kept and older ones taken out, so the temp folder's cleaner
// never takes a file from the one in use (#2865).
func TestPinnedPiCacheMovesOn(t *testing.T) {
if os.Getuid() == 0 {
t.Skip("root gets no cache")
}
tmp := t.TempDir()
t.Setenv("TMPDIR", tmp) // os.TempDir: TMPDIR on Unix, TMP on Windows
t.Setenv("TMP", tmp)
day := 24 * time.Hour
period := int64(pinnedPiCacheDays * 24 * 3600)
start := time.Unix(1800000000/period*period, 0) // a folder's first second
a := pinnedPiCache(start)
if a == "" || filepath.Dir(a) != tmp {
t.Fatalf("the cache is %q, not a folder in %s", a, tmp)
}
if got := pinnedPiCache(start.Add(2*day - time.Second)); got != a {
t.Fatalf("within its two days the cache moved from %s to %s", a, got)
}
b := pinnedPiCache(start.Add(2 * day))
if b == "" || b == a {
t.Fatalf("after two days the cache is %q, not a folder other than %s", b, a)
}
if _, err := os.Stat(a); err != nil {
t.Fatalf("the folder before was taken out while a run may use it: %v", err)
}
c := pinnedPiCache(start.Add(4 * day))
if _, err := os.Stat(a); !os.IsNotExist(err) {
t.Fatalf("a folder four days old is kept (%v)", err)
}
if _, err := os.Stat(b); err != nil || c == "" || c == b {
t.Fatalf("after four days: %q, and the one before %s: %v", c, b, err)
}
}
// A pi package added without pi has pi installed with it (ensurePi), once;
// with pi there already, and for an OpenCode plugin, nothing is installed.
func TestEnsurePi(t *testing.T) {
// sandbox's folders, without the Bun on PATH it needs: bun never runs
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"))
self, err := os.Executable()
if err != nil {
t.Fatal(err)
}
t.Setenv("MAGPIE_BUN", self) // never downloaded
var ran [][]string
orig := bunCommand
bunCommand = func(ctx context.Context, bun, dir string, args ...string) *exec.Cmd {
ran = append(ran, args)
if dir != Dir() {
t.Errorf("bun ran in %s, not the plugins folder", dir)
}
// as bun add leaves it; the test binary running no test stands in
// for bun
if err := os.MkdirAll(Target(piAgent), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(Target(piAgent), "package.json"), []byte(`{"version":"9.9.9"}`), 0o644); err != nil {
t.Fatal(err)
}
return exec.CommandContext(ctx, self, "-test.run=^$")
}
t.Cleanup(func() { bunCommand = orig })
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
opencode, _ := filepath.Abs("testdata/fake/index.js")
if err := ensurePi(ctx, opencode); err != nil || len(ran) != 0 {
t.Fatalf("an OpenCode plugin: bun ran %q, %v", ran, err)
}
pi, _ := filepath.Abs("testdata/pi")
if err := ensurePi(ctx, pi); err != nil {
t.Fatal(err)
}
if want := [][]string{{"add", "--ignore-scripts", piAgent}}; !reflect.DeepEqual(ran, want) {
t.Fatalf("bun ran %q, want %q", ran, want)
}
if err := ensurePi(ctx, pi); err != nil || len(ran) != 1 {
t.Fatalf("with pi installed: bun ran %q, %v", ran, err)
}
}