|
| 1 | +/** |
| 2 | + * Tests for blueprint delete command |
| 3 | + */ |
| 4 | + |
| 5 | +import { jest, describe, it, expect, beforeEach } from "@jest/globals"; |
| 6 | + |
| 7 | +// Mock dependencies using the path alias |
| 8 | +const mockDelete = jest.fn(); |
| 9 | +jest.unstable_mockModule("@/utils/client.js", () => ({ |
| 10 | + getClient: () => ({ |
| 11 | + blueprints: { |
| 12 | + delete: mockDelete, |
| 13 | + }, |
| 14 | + }), |
| 15 | +})); |
| 16 | + |
| 17 | +const mockOutput = jest.fn(); |
| 18 | +const mockOutputError = jest.fn(); |
| 19 | +jest.unstable_mockModule("@/utils/output.js", () => ({ |
| 20 | + output: mockOutput, |
| 21 | + outputError: mockOutputError, |
| 22 | +})); |
| 23 | + |
| 24 | +describe("deleteBlueprint", () => { |
| 25 | + beforeEach(() => { |
| 26 | + jest.clearAllMocks(); |
| 27 | + (console.log as jest.Mock).mockClear(); |
| 28 | + mockDelete.mockReset(); |
| 29 | + mockOutput.mockReset(); |
| 30 | + mockOutputError.mockReset(); |
| 31 | + }); |
| 32 | + |
| 33 | + it("should delete a blueprint by ID", async () => { |
| 34 | + mockDelete.mockResolvedValue(undefined); |
| 35 | + |
| 36 | + const { deleteBlueprint } = await import( |
| 37 | + "@/commands/blueprint/delete.js" |
| 38 | + ); |
| 39 | + await deleteBlueprint("bpt_abc123", {}); |
| 40 | + |
| 41 | + expect(mockDelete).toHaveBeenCalledWith("bpt_abc123"); |
| 42 | + expect(console.log).toHaveBeenCalledWith("bpt_abc123"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should output JSON format when requested", async () => { |
| 46 | + mockDelete.mockResolvedValue(undefined); |
| 47 | + |
| 48 | + const { deleteBlueprint } = await import( |
| 49 | + "@/commands/blueprint/delete.js" |
| 50 | + ); |
| 51 | + await deleteBlueprint("bpt_json123", { output: "json" }); |
| 52 | + |
| 53 | + expect(mockDelete).toHaveBeenCalledWith("bpt_json123"); |
| 54 | + expect(mockOutput).toHaveBeenCalledWith( |
| 55 | + { id: "bpt_json123", status: "deleted" }, |
| 56 | + { format: "json", defaultFormat: "json" }, |
| 57 | + ); |
| 58 | + expect(console.log).not.toHaveBeenCalledWith("bpt_json123"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should output YAML format when requested", async () => { |
| 62 | + mockDelete.mockResolvedValue(undefined); |
| 63 | + |
| 64 | + const { deleteBlueprint } = await import( |
| 65 | + "@/commands/blueprint/delete.js" |
| 66 | + ); |
| 67 | + await deleteBlueprint("bpt_yaml456", { output: "yaml" }); |
| 68 | + |
| 69 | + expect(mockDelete).toHaveBeenCalledWith("bpt_yaml456"); |
| 70 | + expect(mockOutput).toHaveBeenCalledWith( |
| 71 | + { id: "bpt_yaml456", status: "deleted" }, |
| 72 | + { format: "yaml", defaultFormat: "json" }, |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should output just the ID in text format (default)", async () => { |
| 77 | + mockDelete.mockResolvedValue(undefined); |
| 78 | + |
| 79 | + const { deleteBlueprint } = await import( |
| 80 | + "@/commands/blueprint/delete.js" |
| 81 | + ); |
| 82 | + await deleteBlueprint("bpt_text789", { output: "text" }); |
| 83 | + |
| 84 | + expect(console.log).toHaveBeenCalledWith("bpt_text789"); |
| 85 | + expect(mockOutput).not.toHaveBeenCalled(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should output just the ID when no output option is provided", async () => { |
| 89 | + mockDelete.mockResolvedValue(undefined); |
| 90 | + |
| 91 | + const { deleteBlueprint } = await import( |
| 92 | + "@/commands/blueprint/delete.js" |
| 93 | + ); |
| 94 | + await deleteBlueprint("bpt_default", {}); |
| 95 | + |
| 96 | + expect(console.log).toHaveBeenCalledWith("bpt_default"); |
| 97 | + expect(mockOutput).not.toHaveBeenCalled(); |
| 98 | + }); |
| 99 | + |
| 100 | + it("should handle API errors gracefully", async () => { |
| 101 | + const apiError = new Error("API Error: Forbidden"); |
| 102 | + mockDelete.mockRejectedValue(apiError); |
| 103 | + |
| 104 | + const { deleteBlueprint } = await import( |
| 105 | + "@/commands/blueprint/delete.js" |
| 106 | + ); |
| 107 | + await deleteBlueprint("bpt_error", {}); |
| 108 | + |
| 109 | + expect(mockOutputError).toHaveBeenCalledWith( |
| 110 | + "Failed to delete blueprint", |
| 111 | + apiError, |
| 112 | + ); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should handle dependent snapshot errors gracefully", async () => { |
| 116 | + const apiError = new Error( |
| 117 | + "Blueprint has dependent snapshots and cannot be deleted", |
| 118 | + ); |
| 119 | + mockDelete.mockRejectedValue(apiError); |
| 120 | + |
| 121 | + const { deleteBlueprint } = await import( |
| 122 | + "@/commands/blueprint/delete.js" |
| 123 | + ); |
| 124 | + await deleteBlueprint("bpt_has_snapshots", {}); |
| 125 | + |
| 126 | + expect(mockOutputError).toHaveBeenCalledWith( |
| 127 | + "Failed to delete blueprint", |
| 128 | + apiError, |
| 129 | + ); |
| 130 | + }); |
| 131 | +}); |
0 commit comments