Files
skillshare/internal/plugin/host_status_test.go
T
Willie cd79530cda fix(plugins): read the Pi version from stderr
Pi prints its version to stderr, so the host showed an empty version.
The fallback applies only to --version with empty stdout, so commands
whose stdout is parsed never see stderr noise.
2026-09-21 15:50:19 +08:00

137 lines
4.3 KiB
Go

package plugin
import (
"context"
"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)
}
})
}
}
func TestRunCommandMarksAnAbsentBinary(t *testing.T) {
_, err := runCommand(context.Background(), t.TempDir(), "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, 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, 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, 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, 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)
}
}
}