Files
skillshare/internal/server/handler_plugin_test.go
T
Willie 99c7911bf3 feat(plugins): update a batch of plugins over one stream (#534)
Updating several plugins from the dashboard was slow: each plugin took a
preview and an apply round trip, apply previewed again, every plugin cloned
its source on its own, and config reloaded after each one.

The dashboard now sends the picks to POST /api/plugins/update/stream. The
server holds the plugin lock once, previews each plugin once and applies that
plan, lets plugins from one source share a single read of it, and reloads
config once at the end. POST /api/plugins/update/stop lets the plugin under
way finish and starts no other; a closed page does not cut an install short.

The check-to-update safety moves to the server: an Agent whose source moved
since the check, or that no check showed, installs nothing. npm packages in
Pi are exempt, since Pi installs whatever npm names latest; the old
client-side comparison flagged them as moved on every update.

The update dialog follows the Skills update page, shows per-Agent old -> new
versions, fails rows a broken stream never finished, and lists every plugin
with its Agent and version when all are current.

Release-As: 0.25.6
2026-10-12 16:09:12 +08:00

95 lines
3.7 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"strings"
"testing"
"skillshare/internal/config"
)
func TestPluginAPIRequiresPreviewAndLocalOrigin(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
for _, body := range []string{`{"request":{"action":"sync"}}`, `{"request":{"action":"sync"},"unknown":true}`} {
w := httptest.NewRecorder()
s.handlePluginApply(w, httptest.NewRequest(http.MethodPost, "/api/plugins/apply", strings.NewReader(body)))
if w.Code != 400 {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
}
r := httptest.NewRequest(http.MethodPost, "http://attacker.example/api/plugins/apply", strings.NewReader(`{}`))
r.RemoteAddr = "127.0.0.1:1234"
w := httptest.NewRecorder()
s.requireLocalPlugin(s.handlePluginApply)(w, r)
if w.Code != 403 {
t.Fatalf("rebind accepted: %d", w.Code)
}
}
// The Plugins page draws its Agents from targetDefinitions, so a target that is another
// config directory of an Agent has to be one of them.
func TestPluginListIncludesAnAccountOfAnAgent(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
s.cfg.Targets = map[string]config.TargetConfig{"claude-work": {Agent: "claude", ConfigDir: t.TempDir()}}
w := httptest.NewRecorder()
s.handlePluginList(w, httptest.NewRequest(http.MethodGet, "/api/plugins?hosts=false", nil))
if w.Code != 200 {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
type definition struct {
Target string `json:"target"`
Label string `json:"label"`
}
var inventory struct {
TargetDefinitions []definition `json:"targetDefinitions"`
}
if err := json.Unmarshal(w.Body.Bytes(), &inventory); err != nil {
t.Fatal(err)
}
if !slices.ContainsFunc(inventory.TargetDefinitions, func(d definition) bool { return d.Target == "claude-work" }) {
t.Fatalf("the account is missing: %+v", inventory.TargetDefinitions)
}
}
// A source that cannot be reached answers with a key the dashboard translates, not git's output.
func TestPluginDiscoverNamesANetworkFailure(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
w := httptest.NewRecorder()
s.handlePluginDiscover(w, httptest.NewRequest(http.MethodPost, "/api/plugins/discover", strings.NewReader(`{"name":"demo","source":"https://127.0.0.1:1/demo.git"}`)))
var body struct {
Code string `json:"error_code"`
}
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil || w.Code != 400 || body.Code != "plugins.error.network" {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
}
// Only one plugin change runs at a time; a batch update that cannot start says so before streaming.
func TestPluginUpdateStreamRefusesWhilePluginsAreLocked(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
unlock, err := s.pluginService().Lock()
if err != nil {
t.Fatal(err)
}
defer unlock()
w := httptest.NewRecorder()
s.handlePluginUpdateStream(w, httptest.NewRequest(http.MethodPost, "/api/plugins/update/stream", strings.NewReader(`{"items":[{"name":"demo","targets":["claude"]}]}`)))
if w.Code != http.StatusConflict {
t.Fatalf("status %d: %s", w.Code, w.Body.String())
}
}
// Each plugin ends with its own result, so one that fails leaves the others to run.
func TestPluginUpdateStreamReportsEachPluginThenDone(t *testing.T) {
s, _ := newTestServerWithExtras(t, nil, "")
w := httptest.NewRecorder()
s.handlePluginUpdateStream(w, httptest.NewRequest(http.MethodPost, "/api/plugins/update/stream", strings.NewReader(`{"items":[{"name":"gone","targets":[]},{"name":"other","targets":[]}]}`)))
body := w.Body.String()
if strings.Count(body, "event: result") != 2 || !strings.Contains(body, `not found`) || !strings.HasSuffix(strings.TrimSpace(body), `data: {"failure":""}`) {
t.Fatalf("stream:\n%s", body)
}
}