@@ -6,12 +6,10 @@ import { jest, describe, it, expect, beforeEach } from "@jest/globals";
66
77// Mock dependencies using the path alias
88const mockDelete = jest . fn ( ) ;
9- const mockList = jest . fn ( ) ;
109jest . unstable_mockModule ( "@/utils/client.js" , ( ) => ( {
1110 getClient : ( ) => ( {
1211 blueprints : {
1312 delete : mockDelete ,
14- list : mockList ,
1513 } ,
1614 } ) ,
1715} ) ) ;
@@ -28,106 +26,22 @@ describe("deleteBlueprint", () => {
2826 jest . clearAllMocks ( ) ;
2927 ( console . log as jest . Mock ) . mockClear ( ) ;
3028 mockDelete . mockReset ( ) ;
31- mockList . mockReset ( ) ;
3229 mockOutput . mockReset ( ) ;
3330 mockOutputError . mockReset ( ) ;
3431 } ) ;
3532
36- it ( "should delete a blueprint by ID directly " , async ( ) => {
33+ it ( "should delete a blueprint by ID" , async ( ) => {
3734 mockDelete . mockResolvedValue ( undefined ) ;
3835
3936 const { deleteBlueprint } = await import (
4037 "@/commands/blueprint/delete.js"
4138 ) ;
4239 await deleteBlueprint ( "bpt_abc123" , { } ) ;
4340
44- expect ( mockList ) . not . toHaveBeenCalled ( ) ;
4541 expect ( mockDelete ) . toHaveBeenCalledWith ( "bpt_abc123" ) ;
4642 expect ( console . log ) . toHaveBeenCalledWith ( "bpt_abc123" ) ;
4743 } ) ;
4844
49- it ( "should resolve blueprint by name and delete" , async ( ) => {
50- mockList . mockResolvedValue ( {
51- blueprints : [ { id : "bpt_resolved" , name : "my-blueprint" } ] ,
52- } ) ;
53- mockDelete . mockResolvedValue ( undefined ) ;
54-
55- const { deleteBlueprint } = await import (
56- "@/commands/blueprint/delete.js"
57- ) ;
58- await deleteBlueprint ( "my-blueprint" , { } ) ;
59-
60- expect ( mockList ) . toHaveBeenCalledWith ( { name : "my-blueprint" } ) ;
61- expect ( mockDelete ) . toHaveBeenCalledWith ( "bpt_resolved" ) ;
62- expect ( console . log ) . toHaveBeenCalledWith ( "bpt_resolved" ) ;
63- } ) ;
64-
65- it ( "should prefer exact name match when resolving by name" , async ( ) => {
66- mockList . mockResolvedValue ( {
67- blueprints : [
68- { id : "bpt_partial" , name : "my-blueprint-v2" } ,
69- { id : "bpt_exact" , name : "my-blueprint" } ,
70- ] ,
71- } ) ;
72- mockDelete . mockResolvedValue ( undefined ) ;
73-
74- const { deleteBlueprint } = await import (
75- "@/commands/blueprint/delete.js"
76- ) ;
77- await deleteBlueprint ( "my-blueprint" , { } ) ;
78-
79- expect ( mockDelete ) . toHaveBeenCalledWith ( "bpt_exact" ) ;
80- expect ( console . log ) . toHaveBeenCalledWith ( "bpt_exact" ) ;
81- } ) ;
82-
83- it ( "should fall back to first result when no exact name match" , async ( ) => {
84- mockList . mockResolvedValue ( {
85- blueprints : [
86- { id : "bpt_first" , name : "my-blueprint-v1" } ,
87- { id : "bpt_second" , name : "my-blueprint-v2" } ,
88- ] ,
89- } ) ;
90- mockDelete . mockResolvedValue ( undefined ) ;
91-
92- const { deleteBlueprint } = await import (
93- "@/commands/blueprint/delete.js"
94- ) ;
95- await deleteBlueprint ( "my-blueprint" , { } ) ;
96-
97- expect ( mockDelete ) . toHaveBeenCalledWith ( "bpt_first" ) ;
98- expect ( console . log ) . toHaveBeenCalledWith ( "bpt_first" ) ;
99- } ) ;
100-
101- it ( "should output error when blueprint name is not found" , async ( ) => {
102- mockList . mockResolvedValue ( { blueprints : [ ] } ) ;
103-
104- const { deleteBlueprint } = await import (
105- "@/commands/blueprint/delete.js"
106- ) ;
107- await deleteBlueprint ( "nonexistent-blueprint" , { } ) ;
108-
109- expect ( mockOutputError ) . toHaveBeenCalledWith (
110- "Blueprint not found: nonexistent-blueprint" ,
111- expect . any ( Error ) ,
112- ) ;
113- expect ( mockDelete ) . not . toHaveBeenCalled ( ) ;
114- } ) ;
115-
116- it ( "should handle empty blueprints array from API" , async ( ) => {
117- mockList . mockResolvedValue ( { } ) ;
118-
119- const { deleteBlueprint } = await import (
120- "@/commands/blueprint/delete.js"
121- ) ;
122- await deleteBlueprint ( "nonexistent" , { } ) ;
123-
124- expect ( mockOutputError ) . toHaveBeenCalledWith (
125- "Blueprint not found: nonexistent" ,
126- expect . any ( Error ) ,
127- ) ;
128- expect ( mockDelete ) . not . toHaveBeenCalled ( ) ;
129- } ) ;
130-
13145 it ( "should output JSON format when requested" , async ( ) => {
13246 mockDelete . mockResolvedValue ( undefined ) ;
13347
@@ -183,7 +97,7 @@ describe("deleteBlueprint", () => {
18397 expect ( mockOutput ) . not . toHaveBeenCalled ( ) ;
18498 } ) ;
18599
186- it ( "should handle API errors on delete gracefully" , async ( ) => {
100+ it ( "should handle API errors gracefully" , async ( ) => {
187101 const apiError = new Error ( "API Error: Forbidden" ) ;
188102 mockDelete . mockRejectedValue ( apiError ) ;
189103
@@ -198,36 +112,20 @@ describe("deleteBlueprint", () => {
198112 ) ;
199113 } ) ;
200114
201- it ( "should handle API errors on list gracefully" , async ( ) => {
202- const apiError = new Error ( "API Error: Network failure" ) ;
203- mockList . mockRejectedValue ( apiError ) ;
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 ) ;
204120
205121 const { deleteBlueprint } = await import (
206122 "@/commands/blueprint/delete.js"
207123 ) ;
208- await deleteBlueprint ( "some-name " , { } ) ;
124+ await deleteBlueprint ( "bpt_has_snapshots " , { } ) ;
209125
210126 expect ( mockOutputError ) . toHaveBeenCalledWith (
211127 "Failed to delete blueprint" ,
212128 apiError ,
213129 ) ;
214- expect ( mockDelete ) . not . toHaveBeenCalled ( ) ;
215- } ) ;
216-
217- it ( "should output resolved ID in text format when deleting by name" , async ( ) => {
218- mockList . mockResolvedValue ( {
219- blueprints : [ { id : "bpt_resolved_id" , name : "named-blueprint" } ] ,
220- } ) ;
221- mockDelete . mockResolvedValue ( undefined ) ;
222-
223- const { deleteBlueprint } = await import (
224- "@/commands/blueprint/delete.js"
225- ) ;
226- await deleteBlueprint ( "named-blueprint" , { output : "json" } ) ;
227-
228- expect ( mockOutput ) . toHaveBeenCalledWith (
229- { id : "bpt_resolved_id" , status : "deleted" } ,
230- { format : "json" , defaultFormat : "json" } ,
231- ) ;
232130 } ) ;
233131} ) ;
0 commit comments