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 { orgLeaveCommand } from "./org-leave.js"; import { apiRequest } from "../lib/api-client.js"; const mockApiRequest = vi.mocked(apiRequest); beforeEach(() => { vi.clearAllMocks(); }); describe("org leave", () => { it("leaves org by calling /users/me then DELETE member", async () => { mockApiRequest .mockResolvedValueOnce({ id: "user-123" }) // /users/me .mockResolvedValueOnce(undefined); // DELETE member await orgLeaveCommand.parseAsync(["node", "leave", "acme"], { from: "node", }); expect(mockApiRequest).toHaveBeenCalledWith("/api/v1/users/me"); expect(mockApiRequest).toHaveBeenCalledWith( "/api/v1/orgs/acme/members/user-123", expect.objectContaining({ method: "DELETE" }), ); }); it("handles API error", async () => { mockApiRequest.mockRejectedValue( new Error("Cannot leave: you are the last admin"), ); const exit = vi.spyOn(process, "exit").mockImplementation(() => { throw new Error("exit"); }); await expect( orgLeaveCommand.parseAsync(["node", "leave", "acme"], { from: "node" }), ).rejects.toThrow("exit"); exit.mockRestore(); }); });