Files
skillshare/internal/plugin/accounts_test.go
T
Willie bb57a9af9c feat(targets): add account targets for a second Claude, Codex or Pi config directory
A target declared with `agent` and `config_dir` is another config directory of a
built-in Agent, such as a second Claude account. Its skills and agents paths are
derived from the Agent's defaults at load time, so the config only records the
directory. The same target name works for MCP (written in the Agent's format into
the account's own file), for plugins (the Agent CLI runs with CLAUDE_CONFIG_DIR,
CODEX_HOME or PI_CODING_AGENT_DIR) and for `mcp import --from`. Removing such a
target warns when MCP config still names it instead of failing later at sync.

The dashboard gains an "another account" mode in the add-target dialog and lists
accounts next to the Agents on the MCP and plugin pages.

Refs #289
2026-09-22 11:21:35 +08:00

154 lines
5.5 KiB
Go

package plugin
import (
"context"
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
// accountPluginService has a second Claude account: the target claude-work is Claude with
// another config directory, so its plugins are installed through that directory.
func accountPluginService(t *testing.T, calls *[]string) *Service {
t.Helper()
home := t.TempDir()
t.Setenv("HOME", home)
s := &Service{
ConfigPath: filepath.Join(home, "config.yaml"),
StateDir: filepath.Join(home, "state"),
Accounts: map[string]Account{"claude-work": {Agent: "claude", Dir: filepath.Join(home, ".claude-work")}},
}
s.Run = func(_ context.Context, _ string, env []string, bin string, args ...string) ([]byte, error) {
*calls = append(*calls, bin+" "+strings.Join(env, " "))
if args[0] == "--version" {
return []byte("2.1.276"), nil
}
if slices.Contains(args, "--help") {
return []byte("install uninstall update --scope --json"), nil
}
return []byte(`[]`), nil
}
return s
}
func TestAccountRunsTheAgentCLIPointedAtItsDirectory(t *testing.T) {
var calls []string
s := accountPluginService(t, &calls)
if h := s.host(context.Background(), "claude-work"); h.Target != "claude-work" || h.Status != HostReady {
t.Fatalf("host = %+v", h)
}
want := "claude CLAUDE_CONFIG_DIR=" + s.Accounts["claude-work"].Dir
if !slices.Contains(calls, want) {
t.Fatalf("no call was %q: %v", want, calls)
}
calls = nil
s.host(context.Background(), "claude")
if slices.ContainsFunc(calls, func(c string) bool { return strings.Contains(c, "CLAUDE_CONFIG_DIR") }) {
t.Fatalf("the Agent itself was given a config directory: %v", calls)
}
}
func TestAccountTakesTheAgentsPluginOperations(t *testing.T) {
var calls []string
s := accountPluginService(t, &calls)
definitions := s.TargetDefinitions()
i := slices.IndexFunc(definitions, func(d TargetDefinition) bool { return d.Target == "claude-work" })
if i < 0 || definitions[i].Label != "claude-work" || definitions[i].Project || !slices.Contains(definitions[i].Operations, "add") {
t.Fatalf("definitions = %+v", definitions)
}
p, err := s.Preview(context.Background(), Request{Action: "add", Source: fixture(t), Plugin: "demo", Targets: []string{"claude-work"}})
if err != nil {
t.Fatal(err)
}
if p.Blocked || len(p.Changes) != 1 || p.Changes[0].Target != "claude-work" || p.Changes[0].Action != "install" {
t.Fatalf("changes = %+v", p.Changes)
}
}
func TestAccountIsOfferedWhereverItsAgentIs(t *testing.T) {
var calls []string
s := accountPluginService(t, &calls)
d, err := s.Discover(context.Background(), fixture(t), "", "")
if err != nil {
t.Fatal(err)
}
c := d.Candidates[0]
if !slices.Contains(c.Targets, "claude-work") || c.TargetInfo["claude-work"].Manifest != c.TargetInfo["claude"].Manifest {
t.Fatalf("candidate = %+v", c)
}
}
func TestAccountBindingNeedsTheAccountToBeDeclared(t *testing.T) {
raw := []byte("plugins:\n packages:\n demo:\n bindings:\n claude-work:\n id: demo@market\n")
if err := Validate(raw, nil); err == nil {
t.Fatal("a name that is no target was accepted")
}
if err := Validate(raw, map[string]string{"claude-work": "claude"}); err != nil {
t.Fatalf("the account's binding was refused: %v", err)
}
}
// agentAccountPluginService has one account of agent, named <agent>-work.
func agentAccountPluginService(t *testing.T, agent string, calls *[]string) *Service {
t.Helper()
s := accountPluginService(t, calls)
home := filepath.Dir(s.ConfigPath)
s.Accounts = map[string]Account{agent + "-work": {Agent: agent, Dir: filepath.Join(home, "."+agent+"-work")}}
return s
}
func TestCodexAccountRunsItsCLIWithCodexHome(t *testing.T) {
var calls []string
s := agentAccountPluginService(t, "codex", &calls)
s.host(context.Background(), "codex-work")
want := "codex CODEX_HOME=" + s.Accounts["codex-work"].Dir
if !slices.Contains(calls, want) {
t.Fatalf("no call was %q: %v", want, calls)
}
}
func TestPiAccountRunsItsCLIWithPiCodingAgentDir(t *testing.T) {
var calls []string
s := agentAccountPluginService(t, "pi", &calls)
s.host(context.Background(), "pi-work")
want := "pi PI_CODING_AGENT_DIR=" + s.Accounts["pi-work"].Dir
if !slices.Contains(calls, want) {
t.Fatalf("no call was %q: %v", want, calls)
}
}
// Pi's packages are a file, not a list command, so an account's inventory must be read
// from the account's own directory rather than from PI_CODING_AGENT_DIR.
func TestPiAccountInventoryComesFromItsDirectory(t *testing.T) {
var calls []string
s := agentAccountPluginService(t, "pi", &calls)
dir := s.Accounts["pi-work"].Dir
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "settings.json"), []byte(`{"packages":["npm:demo"]}`), 0o644); err != nil {
t.Fatal(err)
}
h := s.host(context.Background(), "pi-work")
if h.Status != HostReady {
t.Fatalf("host = %+v", h)
}
if len(h.Installed) != 1 || h.Installed[0].ID != "npm:demo" {
t.Fatalf("installed = %+v", h.Installed)
}
}
// An account's bindings are validated by the Agent that installs them: Pi identifiers are
// not Claude's name@marketplace.
func TestAccountBindingIsValidatedByItsAgent(t *testing.T) {
raw := []byte("plugins:\n packages:\n demo:\n bindings:\n pi-work:\n id: npm:demo\n")
if err := Validate(raw, map[string]string{"pi-work": "pi"}); err != nil {
t.Fatalf("a Pi identifier was refused for a Pi account: %v", err)
}
if err := Validate(raw, map[string]string{"pi-work": "claude"}); err == nil {
t.Fatal("a Pi identifier was accepted for a Claude account")
}
}