77/* eslint-disable @typescript-eslint/no-explicit-any, unicorn/consistent-function-scoping */
88import { expect } from 'chai' ;
99import OdsCreate from '../../../src/commands/ods/create.js' ;
10+ import {
11+ makeCommandThrowOnError ,
12+ stubCommandConfigAndLogger ,
13+ stubOdsClient ,
14+ stubResolvedConfig ,
15+ } from '../../helpers/ods.js' ;
1016
1117/**
1218 * Unit tests for ODS create command CLI logic.
@@ -27,12 +33,8 @@ describe('ods create', () => {
2733
2834 it ( 'should return undefined when no client ID is configured' , ( ) => {
2935 const command = new OdsCreate ( [ ] , { } as any ) ;
30-
31- // Mock resolvedConfig with no clientId
32- Object . defineProperty ( command , 'resolvedConfig' , {
33- get : ( ) => ( { } ) ,
34- configurable : true ,
35- } ) ;
36+ stubCommandConfigAndLogger ( command ) ;
37+ stubResolvedConfig ( command , { } ) ;
3638
3739 const settings = ( command as any ) . buildSettings ( true ) ;
3840
@@ -41,12 +43,8 @@ describe('ods create', () => {
4143
4244 it ( 'should build settings with OCAPI and WebDAV permissions' , ( ) => {
4345 const command = new OdsCreate ( [ ] , { } as any ) ;
44-
45- // Mock resolvedConfig with clientId
46- Object . defineProperty ( command , 'resolvedConfig' , {
47- get : ( ) => ( { clientId : 'test-client-id' } ) ,
48- configurable : true ,
49- } ) ;
46+ stubCommandConfigAndLogger ( command ) ;
47+ stubResolvedConfig ( command , { clientId : 'test-client-id' } ) ;
5048
5149 const settings = ( command as any ) . buildSettings ( true ) ;
5250
@@ -55,17 +53,14 @@ describe('ods create', () => {
5553 expect ( settings ) . to . have . property ( 'webdav' ) ;
5654 expect ( settings . ocapi ) . to . be . an ( 'array' ) . with . length . greaterThan ( 0 ) ;
5755 expect ( settings . webdav ) . to . be . an ( 'array' ) . with . length . greaterThan ( 0 ) ;
58- expect ( settings . ocapi [ 0 ] ) . to . have . property ( 'client_id' , 'test-client-id' ) ;
59- expect ( settings . webdav [ 0 ] ) . to . have . property ( 'client_id' , 'test-client-id' ) ;
56+ expect ( settings . ocapi [ 0 ] ) . to . have . property ( 'client_id' ) ;
57+ expect ( settings . webdav [ 0 ] ) . to . have . property ( 'client_id' ) ;
6058 } ) ;
6159
6260 it ( 'should include default OCAPI resources' , ( ) => {
6361 const command = new OdsCreate ( [ ] , { } as any ) ;
64-
65- Object . defineProperty ( command , 'resolvedConfig' , {
66- get : ( ) => ( { clientId : 'test-client-id' } ) ,
67- configurable : true ,
68- } ) ;
62+ stubCommandConfigAndLogger ( command ) ;
63+ stubResolvedConfig ( command , { clientId : 'test-client-id' } ) ;
6964
7065 const settings = ( command as any ) . buildSettings ( true ) ;
7166
@@ -77,11 +72,8 @@ describe('ods create', () => {
7772
7873 it ( 'should include default WebDAV permissions' , ( ) => {
7974 const command = new OdsCreate ( [ ] , { } as any ) ;
80-
81- Object . defineProperty ( command , 'resolvedConfig' , {
82- get : ( ) => ( { clientId : 'test-client-id' } ) ,
83- configurable : true ,
84- } ) ;
75+ stubCommandConfigAndLogger ( command ) ;
76+ stubResolvedConfig ( command , { clientId : 'test-client-id' } ) ;
8577
8678 const settings = ( command as any ) . buildSettings ( true ) ;
8779
@@ -133,17 +125,11 @@ describe('ods create', () => {
133125 function setupCreateCommand ( ) : OdsCreate {
134126 const command = new OdsCreate ( [ ] , { } as any ) ;
135127
136- // Mock logger
137- Object . defineProperty ( command , 'logger' , {
138- get : ( ) => ( { info ( ) { } } ) ,
139- configurable : true ,
140- } ) ;
128+ stubCommandConfigAndLogger ( command ) ;
141129
142130 // Mock log & error
143131 command . log = ( ) => { } ;
144- command . error = ( msg : string ) => {
145- throw new Error ( msg ) ;
146- } ;
132+ makeCommandThrowOnError ( command ) ;
147133
148134 return command ;
149135 }
@@ -161,7 +147,7 @@ describe('ods create', () => {
161147 json : true ,
162148 } ;
163149
164- const mockClient = {
150+ stubOdsClient ( command , {
165151 POST : async ( ) => ( {
166152 data : {
167153 data : {
@@ -171,11 +157,6 @@ describe('ods create', () => {
171157 } ,
172158 } ,
173159 } ) ,
174- } ;
175-
176- Object . defineProperty ( command , 'odsClient' , {
177- get : ( ) => mockClient ,
178- configurable : true ,
179160 } ) ;
180161
181162 const result = await command . run ( ) ;
@@ -194,7 +175,7 @@ describe('ods create', () => {
194175 'set-permissions' : false ,
195176 } ;
196177
197- const mockClient = {
178+ stubOdsClient ( command , {
198179 POST : async ( ) => ( {
199180 data : undefined ,
200181 error : {
@@ -204,11 +185,6 @@ describe('ods create', () => {
204185 statusText : 'Bad Request' ,
205186 } ,
206187 } ) ,
207- } ;
208-
209- Object . defineProperty ( command , 'odsClient' , {
210- get : ( ) => mockClient ,
211- configurable : true ,
212188 } ) ;
213189
214190 try {
@@ -232,18 +208,13 @@ describe('ods create', () => {
232208
233209 let requestBody : any ;
234210
235- const mockClient = {
211+ stubOdsClient ( command , {
236212 async POST ( _url : string , options : any ) {
237213 requestBody = options . body ;
238214 return {
239215 data : { data : { id : 'sb-1' , state : 'creating' } } ,
240216 } ;
241217 } ,
242- } ;
243-
244- Object . defineProperty ( command , 'odsClient' , {
245- get : ( ) => mockClient ,
246- configurable : true ,
247218 } ) ;
248219
249220 await command . run ( ) ;
@@ -269,10 +240,7 @@ describe('ods create', () => {
269240 } ,
270241 } ;
271242
272- Object . defineProperty ( command , 'odsClient' , {
273- get : ( ) => mockClient ,
274- configurable : true ,
275- } ) ;
243+ stubOdsClient ( command , mockClient ) ;
276244
277245 const result = await ( command as any ) . waitForSandbox ( 'sb-1' , 0 , 5 ) ;
278246
@@ -282,13 +250,10 @@ describe('ods create', () => {
282250 it ( 'should error when sandbox enters failed state' , async ( ) => {
283251 const command = setupCreateCommand ( ) ;
284252
285- Object . defineProperty ( command , 'odsClient' , {
286- get : ( ) => ( {
287- GET : async ( ) => ( {
288- data : { data : { state : 'failed' } } ,
289- } ) ,
253+ stubOdsClient ( command , {
254+ GET : async ( ) => ( {
255+ data : { data : { state : 'failed' } } ,
290256 } ) ,
291- configurable : true ,
292257 } ) ;
293258
294259 try {
@@ -302,13 +267,10 @@ describe('ods create', () => {
302267 it ( 'should error when sandbox is deleted' , async ( ) => {
303268 const command = setupCreateCommand ( ) ;
304269
305- Object . defineProperty ( command , 'odsClient' , {
306- get : ( ) => ( {
307- GET : async ( ) => ( {
308- data : { data : { state : 'deleted' } } ,
309- } ) ,
270+ stubOdsClient ( command , {
271+ GET : async ( ) => ( {
272+ data : { data : { state : 'deleted' } } ,
310273 } ) ,
311- configurable : true ,
312274 } ) ;
313275
314276 try {
@@ -322,13 +284,10 @@ describe('ods create', () => {
322284 it ( 'should timeout if sandbox never reaches terminal state' , async ( ) => {
323285 const command = setupCreateCommand ( ) ;
324286
325- Object . defineProperty ( command , 'odsClient' , {
326- get : ( ) => ( {
327- GET : async ( ) => ( {
328- data : { data : { state : 'creating' } } ,
329- } ) ,
287+ stubOdsClient ( command , {
288+ GET : async ( ) => ( {
289+ data : { data : { state : 'creating' } } ,
330290 } ) ,
331- configurable : true ,
332291 } ) ;
333292
334293 try {
@@ -342,14 +301,11 @@ describe('ods create', () => {
342301 it ( 'should error if polling API returns no data' , async ( ) => {
343302 const command = setupCreateCommand ( ) ;
344303
345- Object . defineProperty ( command , 'odsClient' , {
346- get : ( ) => ( {
347- GET : async ( ) => ( {
348- data : undefined ,
349- response : { statusText : 'Internal Error' } ,
350- } ) ,
304+ stubOdsClient ( command , {
305+ GET : async ( ) => ( {
306+ data : undefined ,
307+ response : { statusText : 'Internal Error' } ,
351308 } ) ,
352- configurable : true ,
353309 } ) ;
354310
355311 try {
0 commit comments