1- import { expect } from '@oclif/test' ;
1+ import sinon from 'sinon' ;
2+ import { expect } from 'chai' ;
23import { FsUtility } from '@contentstack/cli-utilities' ;
3- import { fancy } from '@contentstack/cli-dev-dependencies' ;
44
55import exportConf from '../mock/export-config.json' ;
66import { Export , ExportConfig , VariantHttpClient , VariantsOption } from '../../../src' ;
@@ -14,80 +14,96 @@ describe('Variant Entries Export', () => {
1414 entries : [ { uid : 'E-UID-1' , title : 'Entry 1' } ] ,
1515 } ;
1616
17- const test = fancy
18- . stdout ( { print : process . env . PRINT === 'true' || false } )
19- . stub ( FsUtility . prototype , 'completeFile' , ( ) => { } )
20- . stub ( FsUtility . prototype , 'writeIntoFile' , ( ) => { } )
21- . stub ( FsUtility . prototype , 'createFolderIfNotExist' , ( ) => { } ) ;
22-
2317 beforeEach ( ( ) => {
2418 config = exportConf as unknown as ExportConfig ;
2519 } ) ;
2620
21+ afterEach ( ( ) => sinon . restore ( ) ) ;
22+
2723 describe ( 'path construction' , ( ) => {
28- test . it ( 'should use exportDir as base path (no branch segment in path)' , ( ) => {
29- const configWithExportDir = {
30- ...config ,
31- exportDir : '/base/export' ,
32- branchName : 'dev' ,
33- } as ExportConfig ;
34- const instance = new Export . VariantEntries ( configWithExportDir ) ;
24+ it ( 'should use exportDir as base path (no branch segment in path)' , ( ) => {
25+ const instance = new Export . VariantEntries ( {
26+ ...config , exportDir : '/base/export' , branchName : 'dev' ,
27+ } as ExportConfig ) ;
3528 expect ( instance . entriesDirPath ) . to . not . include ( 'dev' ) ;
3629 expect ( instance . entriesDirPath ) . to . include ( 'entries' ) ;
3730 } ) ;
3831 } ) ;
3932
33+ describe ( 'branch header' , ( ) => {
34+ const getHeaders = ( instance : any ) => instance . variantInstance . adapterConfig . headers ;
35+
36+ it ( 'sets branch header in adapter headers when branchName is configured' , ( ) => {
37+ const instance = new Export . VariantEntries ( {
38+ ...config , apiKey : 'TEST-KEY' , branchName : 'feature-branch' , org_uid : 'TEST-ORG' , project_id : 'TEST-PROJECT' ,
39+ } as ExportConfig ) ;
40+ expect ( getHeaders ( instance ) . branch ) . to . equal ( 'feature-branch' ) ;
41+ } ) ;
42+
43+ it ( 'branch header is undefined when branchName is not set' , ( ) => {
44+ const instance = new Export . VariantEntries ( {
45+ ...config , apiKey : 'TEST-KEY' , org_uid : 'TEST-ORG' , project_id : 'TEST-PROJECT' ,
46+ } as ExportConfig ) ;
47+ expect ( getHeaders ( instance ) . branch ) . to . be . undefined ;
48+ } ) ;
49+
50+ it ( 'always sets api_key in adapter headers' , ( ) => {
51+ const instance = new Export . VariantEntries ( {
52+ ...config , apiKey : 'TEST-STACK-API-KEY' , branchName : 'staging' , org_uid : 'TEST-ORG' , project_id : 'TEST-PROJECT' ,
53+ } as ExportConfig ) ;
54+ expect ( getHeaders ( instance ) . api_key ) . to . equal ( 'TEST-STACK-API-KEY' ) ;
55+ } ) ;
56+
57+ it ( 'branch header value matches branchName exactly' , ( ) => {
58+ const instance = new Export . VariantEntries ( {
59+ ...config , apiKey : 'TEST-KEY' , branchName : 'eu-release-2025' , org_uid : 'TEST-ORG' , project_id : 'TEST-PROJECT' ,
60+ } as ExportConfig ) ;
61+ expect ( getHeaders ( instance ) . branch ) . to . equal ( 'eu-release-2025' ) ;
62+ } ) ;
63+ } ) ;
64+
4065 describe ( 'exportVariantEntry method' , ( ) => {
41- test
42- . stub ( VariantHttpClient . prototype , 'variantEntries' , async ( ) => { } )
43- . spy ( VariantHttpClient . prototype , 'variantEntries' )
44- . spy ( FsUtility . prototype , 'completeFile' )
45- . spy ( FsUtility . prototype , 'createFolderIfNotExist' )
46- . it ( 'should call export variant entry method (API call)' , async ( { spy } ) => {
47- let entryVariantInstace = new Export . VariantEntries ( config ) ;
48- await entryVariantInstace . exportVariantEntry ( exportEntryData ) ;
49-
50- expect ( spy . variantEntries . callCount ) . to . be . equals ( 1 ) ;
51- expect ( spy . completeFile . callCount ) . to . be . equals ( 1 ) ;
52- expect ( spy . createFolderIfNotExist . callCount ) . to . be . equals ( 1 ) ;
53- expect ( spy . completeFile . alwaysCalledWith ( true ) ) . to . be . true ;
66+ beforeEach ( ( ) => {
67+ sinon . stub ( VariantHttpClient . prototype , 'init' ) . resolves ( ) ;
68+ } ) ;
69+
70+ it ( 'should call variantEntries once per entry' , async ( ) => {
71+ const variantEntriesStub = sinon . stub ( VariantHttpClient . prototype , 'variantEntries' as any ) . resolves ( ) ;
72+ sinon . stub ( FsUtility . prototype , 'completeFile' as any ) ;
73+ sinon . stub ( FsUtility . prototype , 'writeIntoFile' as any ) ;
74+
75+ const instance = new Export . VariantEntries ( config ) ;
76+ await instance . exportVariantEntry ( exportEntryData ) ;
77+
78+ expect ( variantEntriesStub . callCount ) . to . equal ( 1 ) ;
79+ expect ( variantEntriesStub . firstCall . args [ 0 ] ) . to . include ( { entry_uid : 'E-UID-1' , locale : 'en-us' } ) ;
80+ } ) ;
81+
82+ it ( 'should write data in files when callback is invoked with entries' , async ( ) => {
83+ sinon . stub ( VariantHttpClient . prototype , 'variantEntries' as any ) . callsFake ( async ( opts : VariantsOption ) => {
84+ if ( opts . callback ) opts . callback ( [ { uid : 'E-UID-1' , title : 'Entry 1' } ] ) ;
5485 } ) ;
86+ const writeIntoFileStub = sinon . stub ( FsUtility . prototype , 'writeIntoFile' as any ) ;
5587
56- test
57- . stub ( VariantHttpClient . prototype , 'variantEntries' , async ( ...args : any ) => {
58- const { callback } = args [ 0 ] as VariantsOption ;
59- if ( callback ) {
60- callback ( [ { uid : 'E-UID-1' , title : 'Entry 1' } ] ) ;
61- }
62- } )
63- . spy ( FsUtility . prototype , 'writeIntoFile' )
64- . it ( 'should write data in files (As chunk)' , async ( { spy } ) => {
65- let entryVariantInstace = new Export . VariantEntries ( config ) ;
66- await entryVariantInstace . exportVariantEntry ( exportEntryData ) ;
67-
68- expect ( spy . writeIntoFile . callCount ) . to . be . equals ( 1 ) ;
69- expect ( spy . writeIntoFile . alwaysCalledWith ( [ { uid : 'E-UID-1' , title : 'Entry 1' } ] ) ) . to . be . true ;
88+ const instance = new Export . VariantEntries ( config ) ;
89+ await instance . exportVariantEntry ( exportEntryData ) ;
90+
91+ expect ( writeIntoFileStub . callCount ) . to . equal ( 1 ) ;
92+ expect ( writeIntoFileStub . alwaysCalledWith ( [ { uid : 'E-UID-1' , title : 'Entry 1' } ] ) ) . to . be . true ;
93+ } ) ;
94+
95+ it ( 'should skip write when callback returns empty array; default chunk size to 1MB' , async ( ) => {
96+ const variantEntriesStub = sinon . stub ( VariantHttpClient . prototype , 'variantEntries' as any ) . callsFake ( async ( opts : VariantsOption ) => {
97+ if ( opts . callback ) opts . callback ( [ ] ) ;
7098 } ) ;
99+ const writeIntoFileStub = sinon . stub ( FsUtility . prototype , 'writeIntoFile' as any ) ;
71100
72- test
73- . stub ( VariantHttpClient . prototype , 'variantEntries' , async ( ...args : any ) => {
74- const { callback } = args [ 0 ] as VariantsOption ;
75- if ( callback ) {
76- callback ( [ ] ) ; // NOTE API callback with empty response
77- }
78- } )
79- . spy ( FsUtility . prototype , 'writeIntoFile' )
80- . spy ( VariantHttpClient . prototype , 'variantEntries' )
81- . it (
82- 'should skip write data in files (Empty data check validation), should set default file chunk 1MB if chunk size is not passed in config' ,
83- async ( { spy } ) => {
84- config . modules . variantEntry . chunkFileSize = null as any ;
85- let entryVariantInstace = new Export . VariantEntries ( config , ( ) => { } ) ;
86- await entryVariantInstace . exportVariantEntry ( exportEntryData ) ;
87-
88- expect ( spy . writeIntoFile . callCount ) . to . be . equals ( 0 ) ;
89- expect ( spy . variantEntries . callCount ) . to . be . equals ( 1 ) ;
90- } ,
91- ) ;
101+ config . modules . variantEntry . chunkFileSize = null as any ;
102+ const instance = new Export . VariantEntries ( config , ( ) => { } ) ;
103+ await instance . exportVariantEntry ( exportEntryData ) ;
104+
105+ expect ( writeIntoFileStub . callCount ) . to . equal ( 0 ) ;
106+ expect ( variantEntriesStub . callCount ) . to . equal ( 1 ) ;
107+ } ) ;
92108 } ) ;
93109} ) ;
0 commit comments