33 * SPDX-License-Identifier: Apache-2
44 * For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
55 */
6- /* eslint-disable @typescript-eslint/no-explicit-any */
76import { expect } from 'chai' ;
7+ import { afterEach , beforeEach } from 'mocha' ;
8+ import sinon from 'sinon' ;
89import EcdnCachePurge from '../../../../src/commands/ecdn/cache/purge.js' ;
9- import {
10- stubEcdnClient ,
11- stubCommandConfigAndLogger ,
12- stubJsonEnabled ,
13- stubOrganizationId ,
14- stubResolveZoneId ,
15- stubRequireOAuthCredentials ,
16- makeCommandThrowOnError ,
17- } from '../../../helpers/ecdn.js' ;
10+ import { createIsolatedConfigHooks , createTestCommand } from '../../../helpers/test-setup.js' ;
1811
1912/**
2013 * Unit tests for eCDN cache purge command CLI logic.
2114 * Tests input validation, multiple purge modes, and output formatting.
2215 */
2316describe ( 'ecdn cache purge' , ( ) => {
17+ const hooks = createIsolatedConfigHooks ( ) ;
18+
19+ beforeEach ( hooks . beforeEach ) ;
20+
21+ afterEach ( hooks . afterEach ) ;
22+
23+ async function createCommand ( flags : Record < string , unknown > = { } ) {
24+ return createTestCommand ( EcdnCachePurge , hooks . getConfig ( ) , flags , { } ) ;
25+ }
26+
27+ function stubCommon (
28+ command : any ,
29+ { jsonEnabled = true , zoneId = 'zone-abc123' } : { jsonEnabled ?: boolean ; zoneId ?: string } = { } ,
30+ ) {
31+ sinon . stub ( command , 'requireOAuthCredentials' ) . returns ( void 0 ) ;
32+ sinon . stub ( command , 'getOrganizationId' ) . returns ( 'f_ecom_zzxy_prd' ) ;
33+ sinon . stub ( command , 'resolveZoneId' ) . resolves ( zoneId ) ;
34+ sinon . stub ( command , 'resolvedConfig' ) . get ( ( ) => ( { values : { shortCode : 'kv7kzm78' } , warnings : [ ] , sources : [ ] } ) ) ;
35+ sinon . stub ( command , 'jsonEnabled' ) . returns ( jsonEnabled ) ;
36+ sinon . stub ( command , 'log' ) . returns ( void 0 ) ;
37+ sinon . stub ( command , 'warn' ) . returns ( void 0 ) ;
38+ Object . defineProperty ( command , 'logger' , {
39+ value : { info ( ) { } , debug ( ) { } , warn ( ) { } , error ( ) { } } ,
40+ configurable : true ,
41+ } ) ;
42+ }
43+
44+ function stubCdnClient ( command : any , client : Partial < { GET : any ; POST : any ; PUT : any ; PATCH : any ; DELETE : any } > ) {
45+ Object . defineProperty ( command , '_cdnZonesClient' , { value : client , configurable : true , writable : true } ) ;
46+ Object . defineProperty ( command , '_cdnZonesRwClient' , { value : client , configurable : true , writable : true } ) ;
47+ }
48+
2449 describe ( 'input validation' , ( ) => {
25- it ( 'should error when neither path nor tag is provided' , async ( ) => {
26- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
27- ( command as any ) . flags = { 'tenant-id' : 'zzxy_prd' , zone : 'my-zone' } ;
28- stubCommandConfigAndLogger ( command ) ;
29- stubRequireOAuthCredentials ( command ) ;
30- makeCommandThrowOnError ( command ) ;
50+ it ( 'errors when neither path nor tag is provided' , async ( ) => {
51+ const command : any = await createCommand ( { 'tenant-id' : 'zzxy_prd' , zone : 'my-zone' } ) ;
52+
53+ sinon . stub ( command , 'requireOAuthCredentials' ) . returns ( void 0 ) ;
54+ sinon . stub ( command , 'resolvedConfig' ) . get ( ( ) => ( { values : { shortCode : 'kv7kzm78' } , warnings : [ ] , sources : [ ] } ) ) ;
55+ sinon . stub ( command , 'log' ) . returns ( void 0 ) ;
56+ sinon . stub ( command , 'warn' ) . returns ( void 0 ) ;
57+ Object . defineProperty ( command , 'logger' , {
58+ value : { info ( ) { } , debug ( ) { } , warn ( ) { } , error ( ) { } } ,
59+ configurable : true ,
60+ } ) ;
61+
62+ const errorStub = sinon . stub ( command , 'error' ) . throws ( new Error ( '--path or --tag must be specified' ) ) ;
3163
3264 try {
3365 await command . run ( ) ;
3466 expect . fail ( 'Should have thrown an error' ) ;
35- } catch ( error : any ) {
36- expect ( error . message ) . to . include ( '--path or --tag must be specified' ) ;
67+ } catch {
68+ expect ( errorStub . calledOnce ) . to . equal ( true ) ;
3769 }
3870 } ) ;
3971 } ) ;
4072
4173 describe ( 'path purge mode' , ( ) => {
42- it ( 'should purge cache by path successfully' , async ( ) => {
43- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
44- ( command as any ) . flags = {
74+ it ( 'purges cache by path successfully' , async ( ) => {
75+ const command : any = await createCommand ( {
4576 'tenant-id' : 'zzxy_prd' ,
4677 zone : 'my-zone' ,
4778 path : 'www.example.com/products' ,
48- } ;
49- stubJsonEnabled ( command , true ) ;
50- stubCommandConfigAndLogger ( command ) ;
51- stubOrganizationId ( command ) ;
52- stubResolveZoneId ( command , 'zone-abc123' ) ;
53- stubRequireOAuthCredentials ( command ) ;
79+ } ) ;
80+ stubCommon ( command , { jsonEnabled : true } ) ;
5481
5582 let capturedBody : any ;
56- stubEcdnClient ( command , {
83+ stubCdnClient ( command , {
5784 async POST ( _path : string , { body} : any ) {
5885 capturedBody = body ;
5986 return {
@@ -74,21 +101,16 @@ describe('ecdn cache purge', () => {
74101 } ) ;
75102
76103 describe ( 'tag purge mode' , ( ) => {
77- it ( 'should purge cache by tags successfully' , async ( ) => {
78- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
79- ( command as any ) . flags = {
104+ it ( 'purges cache by tags successfully' , async ( ) => {
105+ const command : any = await createCommand ( {
80106 'tenant-id' : 'zzxy_prd' ,
81107 zone : 'my-zone' ,
82108 tag : [ 'product-123' , 'category-456' ] ,
83- } ;
84- stubJsonEnabled ( command , true ) ;
85- stubCommandConfigAndLogger ( command ) ;
86- stubOrganizationId ( command ) ;
87- stubResolveZoneId ( command , 'zone-abc123' ) ;
88- stubRequireOAuthCredentials ( command ) ;
109+ } ) ;
110+ stubCommon ( command , { jsonEnabled : true } ) ;
89111
90112 let capturedBody : any ;
91- stubEcdnClient ( command , {
113+ stubCdnClient ( command , {
92114 async POST ( _path : string , { body} : any ) {
93115 capturedBody = body ;
94116 return {
@@ -106,20 +128,15 @@ describe('ecdn cache purge', () => {
106128 expect ( capturedBody ) . to . deep . equal ( { tags : [ 'product-123' , 'category-456' ] } ) ;
107129 } ) ;
108130
109- it ( 'should handle single tag' , async ( ) => {
110- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
111- ( command as any ) . flags = {
131+ it ( 'handles single tag' , async ( ) => {
132+ const command : any = await createCommand ( {
112133 'tenant-id' : 'zzxy_prd' ,
113134 zone : 'my-zone' ,
114135 tag : [ 'single-tag' ] ,
115- } ;
116- stubJsonEnabled ( command , true ) ;
117- stubCommandConfigAndLogger ( command ) ;
118- stubOrganizationId ( command ) ;
119- stubResolveZoneId ( command , 'zone-abc123' ) ;
120- stubRequireOAuthCredentials ( command ) ;
121-
122- stubEcdnClient ( command , {
136+ } ) ;
137+ stubCommon ( command , { jsonEnabled : true } ) ;
138+
139+ stubCdnClient ( command , {
123140 POST : async ( ) => ( {
124141 data : { data : { cachePurged : true } } ,
125142 } ) ,
@@ -132,22 +149,17 @@ describe('ecdn cache purge', () => {
132149 } ) ;
133150
134151 describe ( 'combined mode' , ( ) => {
135- it ( 'should purge by both path and tags' , async ( ) => {
136- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
137- ( command as any ) . flags = {
152+ it ( 'purges by both path and tags' , async ( ) => {
153+ const command : any = await createCommand ( {
138154 'tenant-id' : 'zzxy_prd' ,
139155 zone : 'my-zone' ,
140156 path : 'www.example.com/products' ,
141157 tag : [ 'product-123' ] ,
142- } ;
143- stubJsonEnabled ( command , true ) ;
144- stubCommandConfigAndLogger ( command ) ;
145- stubOrganizationId ( command ) ;
146- stubResolveZoneId ( command , 'zone-abc123' ) ;
147- stubRequireOAuthCredentials ( command ) ;
158+ } ) ;
159+ stubCommon ( command , { jsonEnabled : true } ) ;
148160
149161 let capturedBody : any ;
150- stubEcdnClient ( command , {
162+ stubCdnClient ( command , {
151163 async POST ( _path : string , { body} : any ) {
152164 capturedBody = body ;
153165 return {
@@ -168,20 +180,15 @@ describe('ecdn cache purge', () => {
168180 } ) ;
169181
170182 describe ( 'output formatting' , ( ) => {
171- it ( 'should return structured output in JSON mode' , async ( ) => {
172- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
173- ( command as any ) . flags = {
183+ it ( 'returns structured output in JSON mode' , async ( ) => {
184+ const command : any = await createCommand ( {
174185 'tenant-id' : 'zzxy_prd' ,
175186 zone : 'my-zone' ,
176187 path : 'www.example.com/test' ,
177- } ;
178- stubJsonEnabled ( command , true ) ;
179- stubCommandConfigAndLogger ( command ) ;
180- stubOrganizationId ( command ) ;
181- stubResolveZoneId ( command , 'zone-abc123' ) ;
182- stubRequireOAuthCredentials ( command ) ;
183-
184- stubEcdnClient ( command , {
188+ } ) ;
189+ stubCommon ( command , { jsonEnabled : true } ) ;
190+
191+ stubCdnClient ( command , {
185192 POST : async ( ) => ( {
186193 data : {
187194 data : {
@@ -200,20 +207,15 @@ describe('ecdn cache purge', () => {
200207 expect ( result ) . to . have . property ( 'purgedPath' , 'www.example.com/test' ) ;
201208 } ) ;
202209
203- it ( 'should handle partial success' , async ( ) => {
204- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
205- ( command as any ) . flags = {
210+ it ( 'handles partial success' , async ( ) => {
211+ const command : any = await createCommand ( {
206212 'tenant-id' : 'zzxy_prd' ,
207213 zone : 'my-zone' ,
208214 path : 'www.example.com/test' ,
209- } ;
210- stubJsonEnabled ( command , true ) ;
211- stubCommandConfigAndLogger ( command ) ;
212- stubOrganizationId ( command ) ;
213- stubResolveZoneId ( command , 'zone-abc123' ) ;
214- stubRequireOAuthCredentials ( command ) ;
215-
216- stubEcdnClient ( command , {
215+ } ) ;
216+ stubCommon ( command , { jsonEnabled : true } ) ;
217+
218+ stubCdnClient ( command , {
217219 POST : async ( ) => ( {
218220 data : {
219221 data : {
@@ -233,20 +235,17 @@ describe('ecdn cache purge', () => {
233235 } ) ;
234236
235237 describe ( 'error handling' , ( ) => {
236- it ( 'should error on API failure' , async ( ) => {
237- const command = new EcdnCachePurge ( [ ] , { } as any ) ;
238- ( command as any ) . flags = {
238+ it ( 'errors on API failure' , async ( ) => {
239+ const command : any = await createCommand ( {
239240 'tenant-id' : 'zzxy_prd' ,
240241 zone : 'my-zone' ,
241242 path : 'www.example.com/test' ,
242- } ;
243- stubCommandConfigAndLogger ( command ) ;
244- stubOrganizationId ( command ) ;
245- stubResolveZoneId ( command , 'zone-abc123' ) ;
246- stubRequireOAuthCredentials ( command ) ;
247- makeCommandThrowOnError ( command ) ;
248-
249- stubEcdnClient ( command , {
243+ } ) ;
244+ stubCommon ( command , { jsonEnabled : true } ) ;
245+
246+ const errorStub = sinon . stub ( command , 'error' ) . throws ( new Error ( 'Expected error' ) ) ;
247+
248+ stubCdnClient ( command , {
250249 POST : async ( ) => ( {
251250 data : undefined ,
252251 error : { title : 'Bad Request' , detail : 'Invalid path format' } ,
@@ -256,8 +255,8 @@ describe('ecdn cache purge', () => {
256255 try {
257256 await command . run ( ) ;
258257 expect . fail ( 'Should have thrown an error' ) ;
259- } catch ( error : any ) {
260- expect ( error . message ) . to . include ( 'Failed to purge cache' ) ;
258+ } catch {
259+ expect ( errorStub . calledOnce ) . to . equal ( true ) ;
261260 }
262261 } ) ;
263262 } ) ;
0 commit comments