import { describe, it, expect, vi, beforeEach } from "vitest"; vi.mock("../lib/config.js", () => ({ getConfig: vi.fn(() => ({ apiUrl: "https://api.skills-hub.ai" })), getAuthHeader: vi.fn(() => ({ Authorization: "Bearer test" })), ensureAuth: vi.fn(), saveConfig: vi.fn(), })); vi.mock("../lib/api-client.js", () => ({ apiRequest: vi.fn(), })); import { orgInviteCommand } from "./org-invite.js"; import { apiRequest } from "../lib/api-client.js"; const mockApiRequest = vi.mocked(apiRequest); beforeEach(() => { vi.clearAllMocks(); }); describe("org invite", () => { it("sends invite and shows success", async () => { mockApiRequest.mockResolvedValue({ id: "inv-1", role: "MEMBER", expiresAt: "2026-04-01T00:00:00Z", token: "abc123", }); const log = vi.spyOn(console, "log").mockImplementation(() => {}); await orgInviteCommand.parseAsync(["node", "invite", "acme", "alice"], { from: "node", }); expect(mockApiRequest).toHaveBeenCalledWith( "/api/v1/orgs/acme/invites", expect.objectContaining({ method: "POST", body: expect.stringContaining('"username":"alice"'), }), ); const output = log.mock.calls.map((c) => c[0]).join("\n"); expect(output).toContain("MEMBER"); log.mockRestore(); }); it("validates role option", async () => { const exit = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); }); const errLog = vi.spyOn(console, "error").mockImplementation(() => {}); await expect( orgInviteCommand.parseAsync( ["node", "invite", "acme", "alice", "--role", "superuser"], { from: "node" }, ), ).rejects.toThrow("exit"); exit.mockRestore(); errLog.mockRestore(); }); it("handles API error", async () => { mockApiRequest.mockRejectedValue(new Error("User not found")); const exit = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); }); await expect( orgInviteCommand.parseAsync(["node", "invite", "acme", "nobody"], { from: "node", }), ).rejects.toThrow("exit"); exit.mockRestore(); }); });