mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
When an Agent's plugin inventory failed (for example OpenCode with both opencode.json and opencode.jsonc present), the inventory call overwrote the host's installed list with nil, which the API encoded as null. The Plugins page read its length unguarded, so one misconfigured Agent crashed the whole page. Host.fail now restores an empty list, covering every inventory path that reports through it, and the dashboard reads a null list as empty so a single Agent can no longer take the page down. Refs #296
166 lines
5.5 KiB
Go
166 lines
5.5 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
// The dashboard groups Agents by remedy, so a missing CLI has to stay distinguishable
|
|
// from a reachable Agent that failed for its own reasons, without reading the message.
|
|
func TestHostStatusSeparatesAMissingCLIFromOtherFailures(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
err error
|
|
want string
|
|
key string
|
|
}{
|
|
{"cli not on path", agentError{cause: ErrCLIMissing, key: "plugins.error.cliMissing", message: "claude CLI is not installed"}, HostMissing, "plugins.error.cliMissing"},
|
|
{"agent refused", errors.New("claude command failed; open the native client"), HostBlocked, ""},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
var h Host
|
|
h.fail(tc.err)
|
|
if h.Status != tc.want {
|
|
t.Fatalf("status = %q, want %q", h.Status, tc.want)
|
|
}
|
|
if h.Error != tc.err.Error() {
|
|
t.Fatalf("message was rewritten: %q", h.Error)
|
|
}
|
|
if h.ErrorKey != tc.key {
|
|
t.Fatalf("key = %q, want %q", h.ErrorKey, tc.key)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A host whose inventory fails still has to list nothing, not null, or the dashboard
|
|
// cannot draw the page for the Agents that answered.
|
|
func TestFailedInventoryStillEncodesAnEmptyList(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("OPENCODE_CONFIG", "")
|
|
t.Setenv("OPENCODE_CONFIG_DIR", "")
|
|
t.Setenv("OPENCODE_CONFIG_CONTENT", "")
|
|
writePluginFile(t, home, ".config/opencode/opencode.json", "{}")
|
|
writePluginFile(t, home, ".config/opencode/opencode.jsonc", "{}")
|
|
s := &Service{ConfigPath: filepath.Join(home, "config.yaml"), StateDir: filepath.Join(home, "state")}
|
|
s.Run = func(_ context.Context, _ string, _ []string, _ string, args ...string) ([]byte, error) {
|
|
return []byte("1.18.32"), nil
|
|
}
|
|
h := s.host(context.Background(), "opencode")
|
|
if h.Status != HostBlocked {
|
|
t.Fatalf("status = %q (error %q), want %q", h.Status, h.Error, HostBlocked)
|
|
}
|
|
data, err := json.Marshal(h)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(string(data), `"installed":[]`) {
|
|
t.Fatalf("installed not an empty list: %s", data)
|
|
}
|
|
}
|
|
|
|
func TestRunCommandMarksAnAbsentBinary(t *testing.T) {
|
|
_, err := runCommand(context.Background(), t.TempDir(), nil, "skillshare-no-such-binary", "--version")
|
|
if !errors.Is(err, exec.ErrNotFound) && !errors.Is(err, ErrCLIMissing) {
|
|
t.Fatalf("err = %v, want it to match ErrCLIMissing", err)
|
|
}
|
|
if !errors.Is(err, ErrCLIMissing) {
|
|
t.Fatalf("err = %v, want ErrCLIMissing", err)
|
|
}
|
|
}
|
|
|
|
func TestRunCommandReadsVersionFromStderr(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "pi"), []byte("#!/bin/sh\necho 0.73.1 >&2\n"), 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := runCommand(context.Background(), dir, nil, filepath.Join(dir, "pi"), "--version")
|
|
if err != nil || strings.TrimSpace(string(out)) != "0.73.1" {
|
|
t.Fatalf("version = %q %v", out, err)
|
|
}
|
|
}
|
|
|
|
func TestHostReportsReadyWhenTheCLIAnswers(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
s := &Service{ConfigPath: filepath.Join(home, "config.yaml"), StateDir: filepath.Join(home, "state")}
|
|
s.Run = func(_ context.Context, _ string, _ []string, _ string, args ...string) ([]byte, error) {
|
|
if args[0] == "--version" {
|
|
return []byte("2.1.276"), nil
|
|
}
|
|
return []byte(`[]`), nil
|
|
}
|
|
if h := s.host(context.Background(), "claude"); h.Status != HostReady {
|
|
t.Fatalf("status = %q (error %q), want %q", h.Status, h.Error, HostReady)
|
|
}
|
|
}
|
|
|
|
// A fixed sentence without a key silently stays English in the dashboard, so every
|
|
// message that is a constant has to carry one.
|
|
func TestFixedMessagesCarryATranslationKey(t *testing.T) {
|
|
for _, d := range TargetDefinitions() {
|
|
if d.Reason != "" && d.ReasonKey == "" {
|
|
t.Errorf("%s: reason has no key", d.Target)
|
|
}
|
|
}
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
s := &Service{ConfigPath: filepath.Join(home, "config.yaml"), StateDir: filepath.Join(home, "state")}
|
|
s.Run = func(_ context.Context, _ string, _ []string, _ string, args ...string) ([]byte, error) {
|
|
if args[0] == "--version" {
|
|
return []byte("1.0.0"), nil
|
|
}
|
|
return []byte(`[]`), nil
|
|
}
|
|
for _, target := range Targets {
|
|
h := s.host(context.Background(), target)
|
|
if h.Note != "" && h.NoteKey == "" {
|
|
t.Errorf("%s: note has no key", target)
|
|
}
|
|
if automationProblem(target) != "" && h.ErrorKey == "" {
|
|
t.Errorf("%s: automation problem has no key", target)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The dashboard draws the plugin list from Packages before any Agent has answered, so it
|
|
// must not wait on one. Inventory asks them all at once and still lists them in order.
|
|
func TestPackagesSkipsTheAgentsAndInventoryKeepsTheirOrder(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
var asked atomic.Int32
|
|
s := &Service{ConfigPath: filepath.Join(home, "config.yaml"), StateDir: filepath.Join(home, "state")}
|
|
s.Run = func(_ context.Context, _ string, _ []string, _ string, args ...string) ([]byte, error) {
|
|
asked.Add(1)
|
|
if args[0] == "--version" {
|
|
return []byte("1.0.0"), nil
|
|
}
|
|
return []byte(`[]`), nil
|
|
}
|
|
quick, err := s.Packages()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if asked.Load() != 0 || len(quick.Hosts) != 0 {
|
|
t.Fatalf("Packages asked %d Agents and returned %d hosts, want none", asked.Load(), len(quick.Hosts))
|
|
}
|
|
full, err := s.Inventory(context.Background())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, target := range Targets {
|
|
if full.Hosts[i].Target != target {
|
|
t.Fatalf("hosts[%d] = %q, want %q", i, full.Hosts[i].Target, target)
|
|
}
|
|
}
|
|
}
|