Files
skillshare/internal/server/handler_pi_extensions_test.go
Willie 6a9e9efbe2 fix(server): release the config lock before slow plugin, Pi and MCP reads (#747)
Every /api/ request reloads config under the write lock of s.mu. The
Plugins page's inventory (Agent CLIs) and update check (network) held
the read lock for their whole run, and Go's RWMutex makes later readers
queue behind a waiting writer. Switching pages or reloading while a
check ran froze the whole dashboard until the check finished.

Build each service under the read lock and release it before the slow
work: plugin list, discover and preview; Pi extensions view and
preview, which run the Pi CLI; and MCP check, which looks up hosts and
probes servers live. Writes still take the write lock, and apply paths
still compare revisions, so a read that races a write is refused at
apply time rather than applied.
2026-11-02 15:13:01 +08:00

402 lines
18 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"time"
"skillshare/internal/config"
"skillshare/internal/plugin"
)
// newPiExtensionsServer serves a config with Pi, Claude, a Pi account and a project
// that syncs to Pi. A stub "pi" on PATH answers --version; nothing else runs.
func newPiExtensionsServer(t *testing.T, version string) (*Server, string) {
t.Helper()
tmp := t.TempDir()
home := filepath.Join(tmp, "home")
t.Setenv("HOME", home)
t.Setenv("XDG_DATA_HOME", filepath.Join(tmp, "data"))
t.Setenv("XDG_STATE_HOME", filepath.Join(tmp, "state"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(tmp, "xdg-config"))
t.Setenv("PI_CODING_AGENT_DIR", filepath.Join(home, ".pi", "agent"))
bin := filepath.Join(tmp, "bin")
writeFiles(t, bin, map[string]string{"pi": "#!/bin/sh\necho " + version + "\n"})
if err := os.Chmod(filepath.Join(bin, "pi"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
pkg := filepath.Join(home, "pkgs", "tools")
writeFiles(t, pkg, map[string]string{"package.json": `{"name":"tools"}`, "extensions/a.ts": "x", "extensions/b.ts": "x"})
settings := `{"packages": [{"source": "` + pkg + `", "extensions": ["-extensions/b.ts"], "x-keep": {"token": "do-not-send"}}]}`
writeFiles(t, filepath.Join(home, ".pi", "agent"), map[string]string{"settings.json": settings})
writeFiles(t, filepath.Join(home, ".pi-work", "agent"), map[string]string{"settings.json": `{"packages": ["` + pkg + `"]}`})
project := filepath.Join(tmp, "acme")
writeFiles(t, filepath.Join(project, ".pi"), map[string]string{"settings.json": `{"packages": [{"source": "` + pkg + `", "autoload": false, "extensions": ["+extensions/b.ts"]}]}`})
source := filepath.Join(tmp, "skills")
cfgPath := filepath.Join(tmp, "config", "config.yaml")
t.Setenv("SKILLSHARE_CONFIG", cfgPath)
writeFiles(t, filepath.Dir(cfgPath), map[string]string{"config.yaml": "source: " + source + "\nmode: merge\ntargets:\n" +
" pi:\n path: " + filepath.Join(tmp, "pi-skills") + "\n" +
" claude:\n path: " + filepath.Join(tmp, "claude-skills") + "\n" +
" pi-work:\n agent: pi\n config_dir: " + filepath.Join(home, ".pi-work", "agent") + "\n" +
"projects:\n " + project + ":\n targets: [pi]\n skills: {}\n"})
if err := os.MkdirAll(source, 0o755); err != nil {
t.Fatal(err)
}
cfg, err := config.Load()
if err != nil {
t.Fatal(err)
}
return New(cfg, "127.0.0.1:0", "", ""), pkg
}
func writeFiles(t *testing.T, dir string, files map[string]string) {
t.Helper()
for name, content := range files {
p := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(content), 0o644); err != nil {
t.Fatal(err)
}
}
}
// writeBlockingCommand writes an executable that marks itself started, waits for
// release, then prints output. started blocks until the command runs.
func writeBlockingCommand(t *testing.T, path, output string) (started, release func()) {
t.Helper()
dir := t.TempDir()
mark, done := filepath.Join(dir, "started"), filepath.Join(dir, "release")
script := "#!/bin/sh\n: > '" + mark + "'\nwhile [ ! -e '" + done + "' ]; do sleep 0.05; done\necho '" + output + "'\n"
if err := os.WriteFile(path, []byte(script), 0o755); err != nil {
t.Fatal(err)
}
started = func() {
t.Helper()
for deadline := time.Now().Add(10 * time.Second); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) {
if _, err := os.Stat(mark); err == nil {
return
}
}
t.Fatal("the command never started")
}
release = func() { _ = os.WriteFile(done, nil, 0o644) }
return started, release
}
func piRequest(t *testing.T, s *Server, method, url, body string) *httptest.ResponseRecorder {
t.Helper()
var reader *strings.Reader
if body != "" {
reader = strings.NewReader(body)
} else {
reader = strings.NewReader("")
}
r := httptest.NewRequest(method, "http://127.0.0.1"+url, reader)
r.RemoteAddr = "127.0.0.1:1234"
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, r)
return w
}
// piView is the part of the response the dashboard reads (ui/src/api/piExtensions.ts).
type piView struct {
Target string `json:"target"`
Scope string `json:"scope"`
Editable bool `json:"editable"`
ReadOnly string `json:"readOnly"`
Revision string `json:"revision"`
Packages []struct {
Index int `json:"index"`
Source string `json:"source"`
Shape string `json:"shape"`
Rules []string `json:"rules"`
OtherKeys []string `json:"otherKeys"`
Rows []struct {
Path string `json:"path"`
Selection string `json:"selection"`
Editable bool `json:"editable"`
} `json:"rows"`
} `json:"packages"`
}
func decodePi[T any](t *testing.T, w *httptest.ResponseRecorder) T {
t.Helper()
var v T
if err := json.Unmarshal(w.Body.Bytes(), &v); err != nil {
t.Fatalf("%d %s: %v", w.Code, w.Body.String(), err)
}
return v
}
func TestPiExtensionsAPIPreviewThenApply(t *testing.T) {
s, pkg := newPiExtensionsServer(t, "0.99.2")
w := piRequest(t, s, http.MethodGet, "/api/targets/pi/pi-extensions", "")
if w.Code != 200 {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
if strings.Contains(w.Body.String(), "do-not-send") {
t.Fatal("an unrelated settings value reached the dashboard")
}
view := decodePi[piView](t, w)
if !view.Editable || view.Scope != "global" || len(view.Packages) != 1 || view.Packages[0].OtherKeys[0] != "x-keep" || view.Packages[0].Rows[1].Selection != "skipped" {
t.Fatalf("view: %+v", view)
}
changes := `{"changes":[{"index":0,"source":"` + pkg + `","path":"extensions/b.ts","action":"select"}]`
w = piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/preview", changes+`}`)
if w.Code != 200 || strings.Contains(w.Body.String(), "do-not-send") {
t.Fatalf("preview %d: %s", w.Code, w.Body.String())
}
plan := decodePi[struct {
Revision string `json:"revision"`
Rows []struct {
Before string `json:"before"`
After string `json:"after"`
} `json:"rows"`
Entries []struct {
After []string `json:"after"`
KeptKeys []string `json:"keptKeys"`
} `json:"entries"`
}](t, w)
if plan.Revision == "" || plan.Rows[0].Before != "skipped" || plan.Rows[0].After != "loads" || plan.Entries[0].After[0] != "+extensions/b.ts" || plan.Entries[0].KeptKeys[0] != "x-keep" {
t.Fatalf("plan: %+v", plan)
}
// A preview of pi is not a preview of the account, even for the same package.
account := filepath.Join(filepath.Dir(os.Getenv("PI_CODING_AGENT_DIR")), "..", ".pi-work", "agent", "settings.json")
accountBefore, _ := os.ReadFile(account)
w = piRequest(t, s, http.MethodPost, "/api/targets/pi-work/pi-extensions/apply", changes+`,"revision":"`+plan.Revision+`"}`)
if accountAfter, _ := os.ReadFile(account); w.Code != http.StatusConflict || string(accountAfter) != string(accountBefore) {
t.Fatalf("account apply with pi's preview %d: %s", w.Code, w.Body.String())
}
w = piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/apply", changes+`,"revision":"`+plan.Revision+`"}`)
if w.Code != 200 {
t.Fatalf("apply %d: %s", w.Code, w.Body.String())
}
data, _ := os.ReadFile(filepath.Join(os.Getenv("PI_CODING_AGENT_DIR"), "settings.json"))
if !strings.Contains(string(data), `"extensions": ["+extensions/b.ts"], "x-keep": {"token": "do-not-send"}`) {
t.Fatalf("settings: %s", data)
}
// The same revision again is stale now.
w = piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/apply", changes+`,"revision":"`+plan.Revision+`"}`)
if w.Code != http.StatusConflict || decodePi[map[string]any](t, w)["error_code"] != "pi_extensions_stale" {
t.Fatalf("stale apply %d: %s", w.Code, w.Body.String())
}
}
func TestPiExtensionsAPIRefusesBadRequests(t *testing.T) {
s, pkg := newPiExtensionsServer(t, "0.99.2")
change := `{"index":0,"source":"` + pkg + `","path":"extensions/b.ts","action":"select"}`
for name, c := range map[string]struct {
url, body, code string
status int
}{
"not a Pi target": {"/api/targets/claude/pi-extensions/preview", `{"changes":[` + change + `]}`, "pi_extensions_not_pi", 404},
"unknown target": {"/api/targets/nope/pi-extensions/preview", `{"changes":[` + change + `]}`, "pi_extensions_not_pi", 404},
"no preview": {"/api/targets/pi/pi-extensions/apply", `{"changes":[` + change + `]}`, "pi_extensions_preview_first", 400},
"bad action": {"/api/targets/pi/pi-extensions/preview", `{"changes":[` + strings.Replace(change, "select", "enable", 1) + `]}`, "pi_extensions_invalid", 400},
} {
w := piRequest(t, s, http.MethodPost, c.url, c.body)
if w.Code != c.status || decodePi[map[string]any](t, w)["error_code"] != c.code {
t.Errorf("%s: %d %s", name, w.Code, w.Body.String())
}
}
if w := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/preview", `{"changes":[],"extra":1}`); w.Code != 400 {
t.Errorf("unknown field: %d", w.Code)
}
if w := piRequest(t, s, http.MethodGet, "/api/targets/claude/pi-extensions", ""); w.Code != 404 {
t.Errorf("claude view: %d", w.Code)
}
r := httptest.NewRequest(http.MethodGet, "http://attacker.example/api/targets/pi/pi-extensions", nil)
r.RemoteAddr = "127.0.0.1:1234"
w := httptest.NewRecorder()
s.mux.ServeHTTP(w, r)
if w.Code != 403 {
t.Errorf("rebind accepted: %d", w.Code)
}
}
// A slow Pi CLI must not hold the config lock, or every other API request waits for it.
func TestPiExtensionsAPIDoesNotHoldTheConfigLock(t *testing.T) {
s, _ := newPiExtensionsServer(t, "0.99.1")
pi, err := exec.LookPath("pi")
if err != nil {
t.Fatal(err)
}
started, release := writeBlockingCommand(t, pi, "0.99.1")
done := make(chan struct{})
go func() {
defer close(done)
piRequest(t, s, http.MethodGet, "/api/targets/pi/pi-extensions", "")
}()
defer func() { release(); <-done }()
started()
if !s.mu.TryLock() {
t.Fatal("a running Pi CLI holds the config lock")
}
s.mu.Unlock()
}
func TestPiExtensionsAPIAccountAndUnsupportedVersion(t *testing.T) {
s, _ := newPiExtensionsServer(t, "0.99.1")
for _, target := range []string{"pi", "pi-work"} {
w := piRequest(t, s, http.MethodGet, "/api/targets/"+target+"/pi-extensions", "")
view := decodePi[piView](t, w)
if w.Code != 200 || view.Editable || view.ReadOnly != "unsupportedVersion" || view.Packages[0].Rows[0].Editable {
t.Fatalf("%s: %d %+v", target, w.Code, view)
}
}
if view := decodePi[piView](t, piRequest(t, s, http.MethodGet, "/api/targets/pi-work/pi-extensions", "")); view.Scope != "account" {
t.Fatalf("account scope: %+v", view)
}
}
// A project change is saved to the project's .pi/settings.json only: the global
// settings stay byte for byte, the apply is logged under the project target, and
// the project's revision is refused by the global target.
func TestPiExtensionsAPIProjectSavesOnlyProjectSettings(t *testing.T) {
s, pkg := newPiExtensionsServer(t, "0.99.2")
w := piRequest(t, s, http.MethodGet, "/api/targets/acme@pi/pi-extensions", "")
view := decodePi[piView](t, w)
if w.Code != 200 || view.Scope != "project" || !view.Editable || view.ReadOnly != "" || view.Target != "acme@pi" || view.Packages[0].Shape != "delta" {
t.Fatalf("project view %d: %s", w.Code, w.Body.String())
}
var project string
for _, tc := range s.cfg.Targets {
if tc.ProjectRoot() != "" {
project = filepath.Join(tc.ProjectRoot(), ".pi", "settings.json")
}
}
global := filepath.Join(os.Getenv("PI_CODING_AGENT_DIR"), "settings.json")
globalBefore, _ := os.ReadFile(global)
changes := `{"changes":[{"scope":"project","index":0,"source":"` + pkg + `","path":"extensions/a.ts","action":"exclude"}]`
preview := piRequest(t, s, http.MethodPost, "/api/targets/acme@pi/pi-extensions/preview", changes+`}`)
plan := decodePi[plugin.PiExtensionsPlan](t, preview)
if preview.Code != 200 || plan.SettingsPath != project {
t.Fatalf("project preview %d: %s", preview.Code, preview.Body.String())
}
if w := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/apply", changes+`,"revision":"`+plan.Revision+`"}`); w.Code != 409 {
t.Fatalf("global apply of a project preview %d: %s", w.Code, w.Body.String())
}
if w := piRequest(t, s, http.MethodPost, "/api/targets/acme@pi/pi-extensions/apply", changes+`,"revision":"`+plan.Revision+`"}`); w.Code != 200 {
t.Fatalf("project apply %d: %s", w.Code, w.Body.String())
}
want := `{"packages": [{"source": "` + pkg + `", "autoload": false, "extensions": ["+extensions/b.ts","-extensions/a.ts"]}]}`
if got, _ := os.ReadFile(project); string(got) != want {
t.Fatalf("project settings:\n got %s\nwant %s", got, want)
}
if globalAfter, _ := os.ReadFile(global); string(globalAfter) != string(globalBefore) {
t.Fatal("a project request wrote the global settings")
}
if e := latestOpsEntry(t, s); e.Command != "pi-extensions" || e.Status != "ok" || e.Args["target"] != "acme@pi" {
t.Fatalf("ops log: %+v", e)
}
}
// Package sources may carry credentials in a URL's userinfo or query. GET and preview
// show them redacted and leave the settings file byte for byte as it was.
func TestPiExtensionsAPINeverSendsSourceCredentials(t *testing.T) {
s, pkg := newPiExtensionsServer(t, "0.99.2")
settings := filepath.Join(filepath.Dir(filepath.Dir(pkg)), ".pi", "agent", "settings.json")
raw := `{"packages": [{"source": "` + pkg + `", "extensions": ["-extensions/b.ts"]},
"git:https://dummy-user:dummy-pass@example.com/acme/a?access_token=dummy-token",
"https://example.com/acme/b?access_token=dummy-token",
{"source": "git:example.com/acme/c?token=dummy-token", "extensions": ["-x.ts"]}]}`
writeFiles(t, filepath.Dir(settings), map[string]string{"settings.json": raw})
get := piRequest(t, s, http.MethodGet, "/api/targets/pi/pi-extensions", "")
preview := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/preview",
`{"changes":[{"index":0,"source":"`+pkg+`","path":"extensions/b.ts","action":"select"}]}`)
if get.Code != 200 || preview.Code != 200 {
t.Fatalf("get %d %s\npreview %d %s", get.Code, get.Body.String(), preview.Code, preview.Body.String())
}
for name, body := range map[string]string{"get": get.Body.String(), "preview": preview.Body.String()} {
if strings.Contains(body, "dummy-") {
t.Errorf("%s sent a credential: %s", name, body)
}
}
if got, err := os.ReadFile(settings); err != nil || string(got) != raw {
t.Fatalf("settings changed: %v\n%s", err, got)
}
}
// A string entry whose package declares only its extension in package.json, but
// has a skill and a prompt in the convention folders, can't take a first rule:
// the object form it needs would load them. GET says so, and a forced preview or
// apply is refused with the file left byte for byte as it was.
func TestPiExtensionsAPIRefusesAConversionThatLoadsOtherResources(t *testing.T) {
s, pkg := newPiExtensionsServer(t, "0.99.2")
partial := filepath.Join(filepath.Dir(pkg), "partial")
writeFiles(t, partial, map[string]string{
"package.json": `{"name":"fixture","pi":{"extensions":["extensions/a.ts"]}}`,
"extensions/a.ts": "x",
"skills/review/SKILL.md": "x",
"prompts/commit.md": "x",
})
settings := filepath.Join(filepath.Dir(filepath.Dir(pkg)), ".pi", "agent", "settings.json")
raw := `{"packages": ["` + partial + `"]}`
writeFiles(t, filepath.Dir(settings), map[string]string{"settings.json": raw})
get := piRequest(t, s, http.MethodGet, "/api/targets/pi/pi-extensions", "")
var view struct {
Packages []struct {
ReadOnly string `json:"readOnly"`
Rows []struct {
Editable bool `json:"editable"`
} `json:"rows"`
} `json:"packages"`
}
if err := json.Unmarshal(get.Body.Bytes(), &view); err != nil || view.Packages[0].ReadOnly != "otherResources" || view.Packages[0].Rows[0].Editable {
t.Fatalf("view %d: %s", get.Code, get.Body.String())
}
changes := `"changes":[{"index":0,"source":"` + partial + `","path":"extensions/a.ts","action":"exclude"}]`
if w := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/preview", `{`+changes+`}`); w.Code == 200 {
t.Fatalf("preview accepted: %s", w.Body.String())
}
if w := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/apply", `{`+changes+`,"revision":"forced"}`); w.Code == 200 {
t.Fatalf("apply accepted: %s", w.Body.String())
}
if got, err := os.ReadFile(settings); err != nil || string(got) != raw {
t.Fatalf("settings changed: %v\n%s", err, got)
}
}
// A rule Go would decode differently from Pi (an unpaired surrogate escape) makes
// its entry unsupported: GET sends no rules or rows, and a forced preview or apply
// writes nothing.
func TestPiExtensionsAPIRefusesAnEntryGoCannotReadLosslessly(t *testing.T) {
s, pkg := newPiExtensionsServer(t, "0.99.2")
settings := filepath.Join(filepath.Dir(filepath.Dir(pkg)), ".pi", "agent", "settings.json")
raw := `{"packages": [{"source": "` + pkg + `", "extensions": ["*", "!extensions/\ud800.ts", "-extensions/a.ts"]}]}`
writeFiles(t, filepath.Dir(settings), map[string]string{"settings.json": raw})
get := piRequest(t, s, http.MethodGet, "/api/targets/pi/pi-extensions", "")
var view struct {
Packages []struct {
Problem string `json:"problem"`
Rules []string `json:"rules"`
Rows []json.RawMessage `json:"rows"`
} `json:"packages"`
}
if err := json.Unmarshal(get.Body.Bytes(), &view); err != nil || view.Packages[0].Problem != "unsupportedEntry" || len(view.Packages[0].Rules) != 0 || len(view.Packages[0].Rows) != 0 {
t.Fatalf("view %d: %s", get.Code, get.Body.String())
}
changes := `"changes":[{"index":0,"source":"` + pkg + `","path":"extensions/a.ts","action":"select"}]`
if w := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/preview", `{`+changes+`}`); w.Code == 200 {
t.Fatalf("preview accepted: %s", w.Body.String())
}
if w := piRequest(t, s, http.MethodPost, "/api/targets/pi/pi-extensions/apply", `{`+changes+`,"revision":"forced"}`); w.Code == 200 {
t.Fatalf("apply accepted: %s", w.Body.String())
}
if got, err := os.ReadFile(settings); err != nil || string(got) != raw {
t.Fatalf("settings changed: %v\n%s", err, got)
}
}