11import { expect } from 'chai' ;
22import { fancy } from 'fancy-test' ;
33import sinon from 'sinon' ;
4- import { managementSDKClient , configHandler , log , handleAndLogError , getLogPath } from '@contentstack/cli-utilities' ;
4+ import { managementSDKClient , configHandler , log , handleAndLogError , getLogPath , createLogContext } from '@contentstack/cli-utilities' ;
55import ImportCommand from '../../../../../src/commands/cm/stacks/import' ;
66import { ModuleImporter } from '../../../../../src/import' ;
77import { ImportConfig } from '../../../../../src/types' ;
@@ -201,18 +201,27 @@ describe('ImportCommand', () => {
201201 } ) ;
202202 } ) ;
203203
204- describe ( 'createImportContext ' , ( ) => {
204+ describe ( 'createLogContext ' , ( ) => {
205205 let configHandlerStub : sinon . SinonStub ;
206+ let configHandlerSetStub : sinon . SinonStub ;
206207
207208 beforeEach ( ( ) => {
208209 configHandlerStub = sinon . stub ( configHandler , 'get' ) ;
209- configHandlerStub . withArgs ( 'userUid' ) . returns ( 'user-123' ) ;
210+ configHandlerSetStub = sinon . stub ( configHandler , 'set' ) ;
211+ configHandlerStub . withArgs ( 'clientId' ) . returns ( 'user-123' ) ;
210212 configHandlerStub . withArgs ( 'email' ) . returns ( 'test@example.com' ) ;
213+ configHandlerStub . withArgs ( 'sessionId' ) . returns ( 'test-session-123' ) ;
211214 configHandlerStub . withArgs ( 'oauthOrgUid' ) . returns ( 'org-123' ) ;
215+ configHandlerStub . withArgs ( 'authorisationType' ) . returns ( 'BASIC' ) ;
216+ } ) ;
217+
218+ afterEach ( ( ) => {
219+ configHandlerStub . restore ( ) ;
220+ configHandlerSetStub . restore ( ) ;
212221 } ) ;
213222
214223 it ( 'should create context with all required properties' , ( ) => {
215- const context = command [ 'createImportContext' ] ( 'test' , 'Basic Auth' ) ;
224+ const context = createLogContext ( 'cm:stacks:import' , 'test' , 'Basic Auth' ) ;
216225
217226 expect ( context ) . to . have . property ( 'command' , 'cm:stacks:import' ) ;
218227 expect ( context ) . to . have . property ( 'module' , '' ) ;
@@ -222,10 +231,11 @@ describe('ImportCommand', () => {
222231 expect ( context ) . to . have . property ( 'apiKey' , 'test' ) ;
223232 expect ( context ) . to . have . property ( 'orgId' , 'org-123' ) ;
224233 expect ( context ) . to . have . property ( 'authenticationMethod' , 'Basic Auth' ) ;
234+ expect ( configHandlerSetStub . calledWith ( 'apiKey' , 'test' ) ) . to . be . true ;
225235 } ) ;
226236
227237 it ( 'should use default authentication method when not provided' , ( ) => {
228- const context = command [ 'createImportContext' ] ( 'test' ) ;
238+ const context = createLogContext ( 'cm:stacks:import' , 'test' ) ;
229239
230240 expect ( context . authenticationMethod ) . to . equal ( 'Basic Auth' ) ;
231241 } ) ;
@@ -234,22 +244,22 @@ describe('ImportCommand', () => {
234244 configHandlerStub . reset ( ) ;
235245 configHandlerStub . returns ( undefined ) ;
236246
237- const context = command [ 'createImportContext' ] ( 'test' , 'Management Token' ) ;
247+ const context = createLogContext ( 'cm:stacks:import' , 'test' , 'Management Token' ) ;
238248
239249 expect ( context . userId ) . to . equal ( '' ) ;
240250 expect ( context . email ) . to . equal ( '' ) ;
241251 expect ( context . orgId ) . to . equal ( '' ) ;
242252 expect ( context . authenticationMethod ) . to . equal ( 'Management Token' ) ;
243253 } ) ;
244254
245- it ( 'should use context command when available ' , ( ) => {
246- const context = command [ 'createImportContext' ] ( 'test' ) ;
255+ it ( 'should use provided command' , ( ) => {
256+ const context = createLogContext ( 'cm:stacks:import' , 'test' ) ;
247257
248258 expect ( context . command ) . to . equal ( 'cm:stacks:import' ) ;
249259 } ) ;
250260
251261 it ( 'should handle empty apiKey' , ( ) => {
252- const context = command [ 'createImportContext' ] ( '' ) ;
262+ const context = createLogContext ( 'cm:stacks:import' , '' ) ;
253263
254264 expect ( context . apiKey ) . to . equal ( '' ) ;
255265 } ) ;
@@ -503,35 +513,43 @@ describe('ImportCommand', () => {
503513 configHandlerStub = sinon . stub ( configHandler , 'get' ) ;
504514 } ) ;
505515
506- it ( 'should handle undefined context' , ( ) => {
507- ( command as any ) . context = undefined ;
516+ afterEach ( ( ) => {
517+ configHandlerStub . restore ( ) ;
518+ } ) ;
519+
520+ it ( 'should handle command string directly' , ( ) => {
521+ configHandlerStub . returns ( undefined ) ;
508522
509- const context = command [ 'createImportContext' ] ( 'test' ) ;
523+ const context = createLogContext ( 'cm:stacks:import' , 'test' ) ;
510524
511525 expect ( context . command ) . to . equal ( 'cm:stacks:import' ) ;
512526 } ) ;
513527
514- it ( 'should handle context without info' , ( ) => {
515- ( command as any ) . context = { sessionId : 'test-session' } ;
528+ it ( 'should use command string when provided' , ( ) => {
529+ configHandlerStub . withArgs ( 'clientId' ) . returns ( 'user-123' ) ;
530+ configHandlerStub . withArgs ( 'email' ) . returns ( 'test@example.com' ) ;
531+ configHandlerStub . withArgs ( 'sessionId' ) . returns ( 'test-session' ) ;
532+ configHandlerStub . withArgs ( 'oauthOrgUid' ) . returns ( 'org-123' ) ;
533+ configHandlerStub . withArgs ( 'authorisationType' ) . returns ( 'BASIC' ) ;
516534
517- const context = command [ 'createImportContext' ] ( 'test' ) ;
535+ const context = createLogContext ( 'cm:stacks:import' , 'test' ) ;
518536
519537 expect ( context . command ) . to . equal ( 'cm:stacks:import' ) ;
538+ expect ( context . sessionId ) . to . equal ( 'test-session' ) ;
520539 } ) ;
521540
522- it ( 'should handle context without sessionId' , ( ) => {
523- ( command as any ) . context = { info : { command : 'test' } } ;
541+ it ( 'should handle missing sessionId from configHandler ' , ( ) => {
542+ configHandlerStub . returns ( undefined ) ;
524543
525- const context = command [ 'createImportContext' ] ( 'test' ) ;
544+ const context = createLogContext ( 'cm:stacks:import' , 'test' ) ;
526545
527- expect ( context . sessionId ) . to . be . undefined ;
546+ expect ( context . sessionId ) . to . equal ( '' ) ;
528547 } ) ;
529548
530- it ( 'should handle configHandler throwing errors' , ( ) => {
531- configHandlerStub . reset ( ) ;
549+ it ( 'should handle configHandler returning undefined values' , ( ) => {
532550 configHandlerStub . returns ( undefined ) ;
533551
534- const context = command [ 'createImportContext' ] ( 'test' ) ;
552+ const context = createLogContext ( 'cm:stacks:import' , 'test' ) ;
535553
536554 expect ( context . userId ) . to . equal ( '' ) ;
537555 expect ( context . email ) . to . equal ( '' ) ;
0 commit comments