1 import { promptClaudeCodePlugin } from "../claude-code-plugin"
2 import { spawnSync } from "child_process"
3 import fs from "fs"
4 import inquirer from "inquirer"
5 import logMessage from "../log-message"
6
7 jest.mock("child_process")
8 jest.mock("fs")
9 jest.mock("inquirer", () => ({ prompt: jest.fn() }))
10 jest.mock("../log-message")
11 jest.mock("os", () => ({
12 homedir: jest.fn().mockReturnValue("/mock/home"),
13 }))
14 jest.mock("@medusajs/telemetry", () => ({
15 Store: jest.fn(),
16 track: jest.fn(),
17 }))
18
19 const mockSpawnSync = spawnSync as jest.MockedFunction<typeof spawnSync>
20 const mockExistsSync = fs.existsSync as jest.MockedFunction<typeof fs.existsSync>
21 const mockReadFileSync = fs.readFileSync as jest.Mock
22 const mockInquirerPrompt = (inquirer as any).prompt as jest.Mock
23 const mockLogMessage = logMessage as jest.MockedFunction<typeof logMessage>
24
25 const CLAUDE_DIR = "/mock/home/.claude"
26 const PLUGIN_ID = "medusa-dev@medusa"
27 const CONFIG_KEY = "claude-code-plugin.prompted"
28
29 describe("promptClaudeCodePlugin", () => {
30 let originalEnv: NodeJS.ProcessEnv
31 let mockGetConfig: jest.Mock
32 let mockSetConfig: jest.Mock
33 let MockStore: jest.Mock
34 let mockTrack: jest.Mock
35
36 beforeEach(() => {
37 originalEnv = { ...process.env }
38 jest.clearAllMocks()
39 jest.spyOn(console, "log").mockImplementation()
40
41 // Default: interactive, not CI, no CLAUDE_CODE env
42 delete process.env.CI
43 delete process.env.CLAUDE_CODE
44 Object.defineProperty(process.stdout, "isTTY", {
45 value: true,
46 writable: true,
47 configurable: true,
48 })
49
50 // Set up Store mock
51 mockGetConfig = jest.fn().mockReturnValue(false)
52 mockSetConfig = jest.fn()
53 const telemetry = jest.requireMock("@medusajs/telemetry")
54 MockStore = telemetry.Store
55 mockTrack = telemetry.track
56 MockStore.mockImplementation(() => ({
57 getConfig: mockGetConfig,
58 setConfig: mockSetConfig,
59 config_: { path: "/mock/config/path" },
60 }))
61
62 // Default: ~/.claude exists (Claude Code present), plugin not installed
63 mockExistsSync.mockImplementation((filePath: unknown) => {
64 return filePath === CLAUDE_DIR
65 })
66 mockReadFileSync.mockReturnValue(
67 JSON.stringify({ version: 2, plugins: {} })
68 )
69
70 // Default: user says yes
71 mockInquirerPrompt.mockResolvedValue({ install: true })
72
73 // Default: both install commands succeed
74 mockSpawnSync.mockReturnValue({ error: undefined } as any)
75 })
76
77 afterEach(() => {
78 process.env = originalEnv
79 jest.restoreAllMocks()
80 })
81
82 describe("non-interactive environments", () => {
83 it("skips when CI=true", async () => {
84 process.env.CI = "true"
85
86 await promptClaudeCodePlugin()
87
88 expect(mockInquirerPrompt).not.toHaveBeenCalled()
89 })
90
91 it("skips when stdout is not a TTY", async () => {
92 Object.defineProperty(process.stdout, "isTTY", {
93 value: false,
94 writable: true,
95 configurable: true,
96 })
97
98 await promptClaudeCodePlugin()
99
100 expect(mockInquirerPrompt).not.toHaveBeenCalled()
101 })
102 })
103
104 describe("Claude Code detection", () => {
105 it("skips when ~/.claude does not exist and CLAUDE_CODE env is not set", async () => {
106 mockExistsSync.mockReturnValue(false)
107
108 await promptClaudeCodePlugin()
109
110 expect(mockInquirerPrompt).not.toHaveBeenCalled()
111 })
112
113 it("proceeds when ~/.claude exists", async () => {
114 await promptClaudeCodePlugin()
115
116 expect(mockInquirerPrompt).toHaveBeenCalled()
117 })
118
119 it("proceeds when CLAUDE_CODE env var is set even without ~/.claude dir", async () => {
120 mockExistsSync.mockReturnValue(false)
121 process.env.CLAUDE_CODE = "1"
122
123 await promptClaudeCodePlugin()
124
125 expect(mockInquirerPrompt).toHaveBeenCalled()
126 })
127 })
128
129 describe("plugin installation detection", () => {
130 it("skips when installed_plugins.json contains the plugin", async () => {
131 mockExistsSync.mockReturnValue(true)
132 mockReadFileSync.mockReturnValue(
133 JSON.stringify({ version: 2, plugins: { [PLUGIN_ID]: [{}] } })
134 )
135
136 await promptClaudeCodePlugin()
137
138 expect(mockInquirerPrompt).not.toHaveBeenCalled()
139 })
140
141 it("proceeds when installed_plugins.json exists but does not contain the plugin", async () => {
142 mockExistsSync.mockReturnValue(true)
143 mockReadFileSync.mockReturnValue(
144 JSON.stringify({ version: 2, plugins: { "other-plugin@other": [{}] } })
145 )
146
147 await promptClaudeCodePlugin()
148
149 expect(mockInquirerPrompt).toHaveBeenCalled()
150 })
151
152 it("proceeds when installed_plugins.json does not exist", async () => {
153 mockExistsSync.mockImplementation((filePath: unknown) => {
154 return filePath === CLAUDE_DIR
155 })
156
157 await promptClaudeCodePlugin()
158
159 expect(mockInquirerPrompt).toHaveBeenCalled()
160 })
161
162 it("proceeds when installed_plugins.json contains malformed JSON", async () => {
163 mockExistsSync.mockReturnValue(true)
164 mockReadFileSync.mockReturnValue("not valid json")
165
166 await promptClaudeCodePlugin()
167
168 expect(mockInquirerPrompt).toHaveBeenCalled()
169 })
170 })
171
172 describe("already prompted", () => {
173 it("skips when configStore already has the prompted flag set", async () => {
174 mockGetConfig.mockReturnValue(true)
175
176 await promptClaudeCodePlugin()
177
178 expect(mockInquirerPrompt).not.toHaveBeenCalled()
179 })
180
181 it("checks the correct config key", async () => {
182 await promptClaudeCodePlugin()
183
184 expect(mockGetConfig).toHaveBeenCalledWith(CONFIG_KEY)
185 })
186
187 it("marks as prompted before showing the prompt", async () => {
188 let setConfigCalledBeforePrompt = false
189 mockInquirerPrompt.mockImplementation(async () => {
190 setConfigCalledBeforePrompt = mockSetConfig.mock.calls.length > 0
191 return { install: false }
192 })
193
194 await promptClaudeCodePlugin()
195
196 expect(setConfigCalledBeforePrompt).toBe(true)
197 expect(mockSetConfig).toHaveBeenCalledWith(CONFIG_KEY, true)
198 })
199 })
200
201 describe("user says yes", () => {
202 beforeEach(() => {
203 mockInquirerPrompt.mockResolvedValue({ install: true })
204 })
205
206 it("runs marketplace add command", async () => {
207 await promptClaudeCodePlugin()
208
209 expect(mockSpawnSync).toHaveBeenCalledWith(
210 "claude",
211 ["plugin", "marketplace", "add", "medusajs/medusa-agent-skills"],
212 { stdio: "inherit" }
213 )
214 })
215
216 it("runs plugin install command", async () => {
217 await promptClaudeCodePlugin()
218
219 expect(mockSpawnSync).toHaveBeenCalledWith(
220 "claude",
221 ["plugin", "install", PLUGIN_ID],
222 { stdio: "inherit" }
223 )
224 })
225
226 it("tracks telemetry with install=true", async () => {
227 await promptClaudeCodePlugin()
228
229 expect(mockTrack).toHaveBeenCalledWith("CMA_CLAUDE_CODE_PLUGIN_PROMPT", {
230 install: true,
231 })
232 })
233
234 it("does not show fallback message when both commands succeed", async () => {
235 await promptClaudeCodePlugin()
236
237 expect(mockLogMessage).not.toHaveBeenCalled()
238 })
239
240 it("shows fallback message when marketplace command fails", async () => {
241 mockSpawnSync.mockReturnValue({ error: new Error("not found") } as any)
242
243 await promptClaudeCodePlugin()
244
245 expect(mockLogMessage).toHaveBeenCalledWith({
246 message: expect.stringContaining(
247 "/plugin marketplace add medusajs/medusa-agent-skills"
248 ),
249 })
250 })
251
252 it("shows fallback message when plugin install command fails", async () => {
253 mockSpawnSync
254 .mockReturnValueOnce({ error: undefined } as any)
255 .mockReturnValueOnce({ error: new Error("not found") } as any)
256
257 await promptClaudeCodePlugin()
258
259 expect(mockLogMessage).toHaveBeenCalledWith({
260 message: expect.stringContaining(
261 "/plugin marketplace add medusajs/medusa-agent-skills"
262 ),
263 })
264 })
265 })
266
267 describe("user says no", () => {
268 beforeEach(() => {
269 mockInquirerPrompt.mockResolvedValue({ install: false })
270 })
271
272 it("does not run any install commands", async () => {
273 await promptClaudeCodePlugin()
274
275 expect(mockSpawnSync).not.toHaveBeenCalled()
276 })
277
278 it("does not show fallback message", async () => {
279 await promptClaudeCodePlugin()
280
281 expect(mockLogMessage).not.toHaveBeenCalled()
282 })
283
284 it("tracks telemetry with install=false", async () => {
285 await promptClaudeCodePlugin()
286
287 expect(mockTrack).toHaveBeenCalledWith("CMA_CLAUDE_CODE_PLUGIN_PROMPT", {
288 install: false,
289 })
290 })
291 })
292 })