| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | |
| 8 | "reasonix/internal/pluginpkg" |
| 9 | ) |
| 10 | |
| 11 | func TestLoadMergesInstalledPluginSkillRootsAndMCP(t *testing.T) { |
| 12 | home := t.TempDir() |
| 13 | t.Setenv("REASONIX_HOME", home) |
| 14 | root := filepath.Join(home, "plugins", "superpowers") |
| 15 | writeConfigTestFile(t, filepath.Join(root, pluginpkg.NativeManifest), `{ |
| 16 | "name": "superpowers", |
| 17 | "version": "1.0.0", |
| 18 | "skills": "skills", |
| 19 | "mcpServers": { |
| 20 | "helper": { "command": "bin/helper" } |
| 21 | } |
| 22 | }`) |
| 23 | if err := pluginpkg.Upsert(home, pluginpkg.InstalledPlugin{ |
| 24 | Name: "superpowers", |
| 25 | Root: "plugins/superpowers", |
| 26 | Version: "1.0.0", |
| 27 | ManifestKind: "reasonix", |
| 28 | Enabled: true, |
| 29 | }); err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | |
| 33 | cfg, err := LoadForRoot(t.TempDir()) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | if len(cfg.Skills.Paths) == 0 || cfg.Skills.Paths[len(cfg.Skills.Paths)-1] != filepath.Join(root, "skills") { |
| 38 | t.Fatalf("skills paths = %#v", cfg.Skills.Paths) |
| 39 | } |
| 40 | owners := cfg.PluginPackageSkillOwners()[CanonicalSkillPath(filepath.Join(root, "skills"))] |
| 41 | if len(owners) != 1 || owners[0] != "superpowers" { |
| 42 | t.Fatalf("plugin skill owners = %#v, want superpowers", owners) |
| 43 | } |
| 44 | var found bool |
| 45 | for _, p := range cfg.Plugins { |
| 46 | if p.Name == "helper" { |
| 47 | found = true |
| 48 | if p.Command != filepath.Join(root, "bin", "helper") { |
| 49 | t.Fatalf("plugin command = %q", p.Command) |
| 50 | } |
| 51 | if p.Env["REASONIX_PLUGIN_NAME"] != "superpowers" { |
| 52 | t.Fatalf("plugin env = %#v", p.Env) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | if !found { |
| 57 | t.Fatalf("plugin MCP server missing: %#v", cfg.Plugins) |
| 58 | } |
| 59 | if owner, ok := cfg.PluginPackageOwner("helper"); !ok || owner != "superpowers" { |
| 60 | t.Fatalf("plugin MCP owner = %q, %v; want superpowers, true", owner, ok) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestClaudePackageMCPExpandsRootAndDoesNotAutoStart(t *testing.T) { |
| 65 | home := t.TempDir() |
| 66 | t.Setenv("REASONIX_HOME", home) |
| 67 | root := filepath.Join(home, "plugins", "claude-mcp") |
| 68 | writeConfigTestFile(t, filepath.Join(root, pluginpkg.ClaudeManifest), `{"name":"claude-mcp"}`) |
| 69 | writeConfigTestFile(t, filepath.Join(root, ".mcp.json"), `{ |
| 70 | "mcpServers": { |
| 71 | "Local Search": { |
| 72 | "command": "${CLAUDE_PLUGIN_ROOT}/bin/server", |
| 73 | "args": ["--root", "${CLAUDE_PLUGIN_ROOT}/data", "--workspace", "${CLAUDE_PROJECT_DIR}"], |
| 74 | "env": {"DATA_DIR": "${CLAUDE_PLUGIN_ROOT}/data"} |
| 75 | } |
| 76 | } |
| 77 | }`) |
| 78 | if err := pluginpkg.Upsert(home, pluginpkg.InstalledPlugin{Name: "claude-mcp", Root: "plugins/claude-mcp", ManifestKind: "claude", Enabled: true}); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | workspace := t.TempDir() |
| 82 | cfg, err := LoadForRoot(workspace) |
| 83 | if err != nil { |
| 84 | t.Fatal(err) |
| 85 | } |
| 86 | if len(cfg.Plugins) != 1 { |
| 87 | t.Fatalf("plugins = %#v", cfg.Plugins) |
| 88 | } |
| 89 | got := cfg.Plugins[0] |
| 90 | if got.Command != filepath.Join(root, "bin", "server") || got.Args[1] != filepath.Join(root, "data") || got.Env["DATA_DIR"] != filepath.Join(root, "data") { |
| 91 | t.Fatalf("Claude root was not expanded: %#v", got) |
| 92 | } |
| 93 | if got.Env["CLAUDE_PLUGIN_ROOT"] != root { |
| 94 | t.Fatalf("CLAUDE_PLUGIN_ROOT = %q", got.Env["CLAUDE_PLUGIN_ROOT"]) |
| 95 | } |
| 96 | if got.Args[3] != workspace || got.Env["CLAUDE_PROJECT_DIR"] != workspace { |
| 97 | t.Fatalf("workspace expansion = %#v", got) |
| 98 | } |
| 99 | if got.ShouldAutoStart() { |
| 100 | t.Fatal("imported Claude MCP must require an explicit connection") |
| 101 | } |
| 102 | if len(cfg.AutoStartPlugins()) != 0 { |
| 103 | t.Fatalf("auto-start plugins = %#v", cfg.AutoStartPlugins()) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestClaudePackageMCPDeduplicatesSameConnectionAcrossPackages(t *testing.T) { |
| 108 | home := t.TempDir() |
| 109 | t.Setenv("REASONIX_HOME", home) |
| 110 | for i, name := range []string{"legal-one", "legal-two"} { |
| 111 | root := filepath.Join(home, "plugins", name) |
| 112 | writeConfigTestFile(t, filepath.Join(root, pluginpkg.ClaudeManifest), `{"name":"`+name+`"}`) |
| 113 | writeConfigTestFile(t, filepath.Join(root, ".mcp.json"), `{ |
| 114 | "mcpServers":{"飞书":{"type":"http","url":"https://open.feishu.cn/mcp","description":"package `+string(rune('A'+i))+` description"}} |
| 115 | }`) |
| 116 | if err := pluginpkg.Upsert(home, pluginpkg.InstalledPlugin{Name: name, Root: "plugins/" + name, ManifestKind: "claude", Enabled: true}); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | } |
| 120 | cfg, err := LoadForRoot(t.TempDir()) |
| 121 | if err != nil { |
| 122 | t.Fatal(err) |
| 123 | } |
| 124 | if len(cfg.Plugins) != 1 || cfg.Plugins[0].URL != "https://open.feishu.cn/mcp" { |
| 125 | t.Fatalf("deduplicated plugins = %#v", cfg.Plugins) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func writeConfigTestFile(t *testing.T, path, body string) { |
| 130 | t.Helper() |
| 131 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 132 | t.Fatal(err) |
| 133 | } |
| 134 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 135 | t.Fatal(err) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // TestCommandDirsIncludePluginPackageCommands pins the plugin-commands wiring: |
| 140 | // an enabled plugin package's command roots join command discovery at the |
| 141 | // before user/project entries, retaining package ownership, while a disabled |
| 142 | // package contributes nothing. |
| 143 | func TestCommandDirsIncludePluginPackageCommands(t *testing.T) { |
| 144 | home := t.TempDir() |
| 145 | t.Setenv("REASONIX_HOME", home) |
| 146 | root := filepath.Join(home, "plugins", "pwf") |
| 147 | writeConfigTestFile(t, filepath.Join(root, pluginpkg.ClaudeManifest), `{"name": "pwf"}`) |
| 148 | writeConfigTestFile(t, filepath.Join(root, "skills", "planner", "SKILL.md"), "---\ndescription: p\n---\nbody") |
| 149 | writeConfigTestFile(t, filepath.Join(root, "commands", "plan.md"), "---\ndescription: plan\n---\nPlan: $ARGUMENTS") |
| 150 | if err := pluginpkg.Upsert(home, pluginpkg.InstalledPlugin{ |
| 151 | Name: "pwf", |
| 152 | Root: "plugins/pwf", |
| 153 | ManifestKind: "claude", |
| 154 | Enabled: true, |
| 155 | }); err != nil { |
| 156 | t.Fatal(err) |
| 157 | } |
| 158 | |
| 159 | dirs := CommandDirsForRoot(t.TempDir()) |
| 160 | want := filepath.Join(root, "commands") |
| 161 | if len(dirs) == 0 || dirs[0] != want { |
| 162 | t.Fatalf("CommandDirsForRoot = %#v, want plugin commands dir first (lowest priority): %s", dirs, want) |
| 163 | } |
| 164 | roots := CommandRootsForRoot(t.TempDir()) |
| 165 | if len(roots) == 0 || roots[0].Path != want || roots[0].Plugin != "pwf" { |
| 166 | t.Fatalf("CommandRootsForRoot = %#v, want plugin ownership on first root", roots) |
| 167 | } |
| 168 | |
| 169 | if err := pluginpkg.SetEnabled(home, "pwf", false); err != nil { |
| 170 | t.Fatal(err) |
| 171 | } |
| 172 | for _, dir := range CommandDirsForRoot(t.TempDir()) { |
| 173 | if dir == want { |
| 174 | t.Fatalf("disabled plugin's commands dir must not join discovery: %#v", dir) |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // TestCommandDirsWithoutPluginState keeps the no-plugin fast path intact. |
| 180 | func TestCommandDirsWithoutPluginState(t *testing.T) { |
| 181 | home := t.TempDir() |
| 182 | t.Setenv("REASONIX_HOME", home) |
| 183 | if dirs := CommandDirsForRoot(t.TempDir()); len(dirs) == 0 { |
| 184 | t.Fatal("CommandDirsForRoot must still return the conventional dirs") |
| 185 | } |
| 186 | } |
| 187 |