|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createMockSpinner, runCommand } from "./helpers.js"; |
| 3 | + |
| 4 | +vi.mock("yocto-spinner", () => ({ default: () => createMockSpinner() })); |
| 5 | +vi.mock("../../src/lib/program.js", () => ({ |
| 6 | + program: { opts: () => ({}) }, |
| 7 | +})); |
| 8 | + |
| 9 | +const execAsync = vi.fn(); |
| 10 | +vi.mock("node:child_process", () => ({ |
| 11 | + exec: (...args: unknown[]) => { |
| 12 | + const cb = args.at(-1) as ( |
| 13 | + err: Error | null, |
| 14 | + result: { stdout: string; stderr: string } |
| 15 | + ) => void; |
| 16 | + const promise = execAsync(args[0]); |
| 17 | + promise |
| 18 | + .then(() => cb(null, { stdout: "", stderr: "" })) |
| 19 | + .catch((err: Error) => cb(err, { stdout: "", stderr: "" })); |
| 20 | + }, |
| 21 | +})); |
| 22 | + |
| 23 | +const { skillsCommand } = await import("../../src/commands/skills.js"); |
| 24 | + |
| 25 | +describe("skills", () => { |
| 26 | + it("add runs npx skills add with correct arguments", async () => { |
| 27 | + execAsync.mockResolvedValueOnce({ stdout: "", stderr: "" }); |
| 28 | + |
| 29 | + await runCommand(skillsCommand, ["add", "memberstack-cli"]); |
| 30 | + |
| 31 | + expect(execAsync).toHaveBeenCalledWith( |
| 32 | + expect.stringContaining( |
| 33 | + "npx skills add Flash-Brew-Digital/memberstack-skills --skill memberstack-cli --agent claude-code codex -y" |
| 34 | + ) |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it("remove runs npx skills remove with correct arguments", async () => { |
| 39 | + execAsync.mockResolvedValueOnce({ stdout: "", stderr: "" }); |
| 40 | + |
| 41 | + await runCommand(skillsCommand, ["remove", "memberstack-cli"]); |
| 42 | + |
| 43 | + expect(execAsync).toHaveBeenCalledWith( |
| 44 | + expect.stringContaining( |
| 45 | + "npx skills remove --skill memberstack-cli --agent claude-code codex -y" |
| 46 | + ) |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it("add handles errors gracefully", async () => { |
| 51 | + execAsync.mockRejectedValueOnce(new Error("Command failed")); |
| 52 | + |
| 53 | + const original = process.exitCode; |
| 54 | + await runCommand(skillsCommand, ["add", "bad-skill"]); |
| 55 | + expect(process.exitCode).toBe(1); |
| 56 | + process.exitCode = original; |
| 57 | + }); |
| 58 | + |
| 59 | + it("remove handles errors gracefully", async () => { |
| 60 | + execAsync.mockRejectedValueOnce(new Error("Command failed")); |
| 61 | + |
| 62 | + const original = process.exitCode; |
| 63 | + await runCommand(skillsCommand, ["remove", "bad-skill"]); |
| 64 | + expect(process.exitCode).toBe(1); |
| 65 | + process.exitCode = original; |
| 66 | + }); |
| 67 | +}); |
0 commit comments