1
+ import { removeUndefinedValues } from '@rudderstack/integrations-lib' ;
1
2
import {
2
3
Metadata ,
3
4
Destination ,
@@ -14,6 +15,7 @@ import {
14
15
RouterTestData ,
15
16
ProxyV1TestData ,
16
17
} from '../integrations/testTypes' ;
18
+ import { method } from 'lodash' ;
17
19
18
20
// Default metadata to fill in missing fields
19
21
const defaultMetadata : Metadata = {
@@ -100,20 +102,22 @@ export function migrateTestCase(oldTestCase: any): TestCaseData {
100
102
}
101
103
102
104
// Utility function to migrate processor test cases
103
- export function migrateProcessorTestCase ( oldTestCase : any ) : ProcessorTestData {
105
+ export function migrateProcessorTestCase ( oldTestCase : any , index : number ) : ProcessorTestData {
104
106
const processorRequest : ProcessorTransformationRequest = {
105
107
message : oldTestCase . input ?. request ?. body [ 0 ] ?. message || { } ,
106
108
metadata : { ...defaultMetadata , ...oldTestCase . input ?. request ?. body [ 0 ] ?. metadata } ,
107
109
destination : { ...defaultDestination , ...oldTestCase . input ?. request ?. body [ 0 ] ?. destination } ,
108
110
} ;
109
111
110
112
const processorResponse : ProcessorTransformationResponse = {
111
- output : oldTestCase . output ?. response ?. body [ 0 ] ?. output || { } ,
113
+ output : oldTestCase . output ?. response ?. body [ 0 ] ?. output ,
112
114
metadata : { ...defaultMetadata , ...oldTestCase . output ?. response ?. body [ 0 ] ?. metadata } ,
113
115
statusCode : oldTestCase . output ?. response ?. status || 200 ,
116
+ error : oldTestCase . output ?. response ?. body [ 0 ] ?. error ,
117
+ statTags : oldTestCase . output ?. response ?. body [ 0 ] ?. statTags ,
114
118
} ;
115
119
116
- return {
120
+ return removeUndefinedValues ( {
117
121
id : oldTestCase . id || `processor-${ Date . now ( ) } ` ,
118
122
name : oldTestCase . name || 'Processor Test Case' ,
119
123
description : oldTestCase . description || 'Migrated processor test case' ,
@@ -134,30 +138,46 @@ export function migrateProcessorTestCase(oldTestCase: any): ProcessorTestData {
134
138
body : [ processorResponse ] ,
135
139
} ,
136
140
} ,
137
- } ;
141
+ mockFns : oldTestCase . mockFns ? `Add mock of index ${ index } ` : undefined ,
142
+ } ) as ProcessorTestData ;
138
143
}
139
144
140
145
// Utility function to migrate router test cases
141
- export function migrateRouterTestCase ( oldTestCase : any ) : RouterTestData {
146
+ export function migrateRouterTestCase ( oldTestCase : any , index : number ) : RouterTestData {
147
+ const input = Array . isArray ( oldTestCase . input . request . body . input )
148
+ ? oldTestCase . input . request . body . input . map ( ( item : any ) => ( {
149
+ message : item . message || { } ,
150
+ metadata : { ...defaultMetadata , ...item . metadata } ,
151
+ destination : { ...defaultDestination , ...item . destination } ,
152
+ } ) )
153
+ : {
154
+ message : oldTestCase . input . request . body . input ?. message || { } ,
155
+ metadata : { ...defaultMetadata , ...oldTestCase . input . request . body . input ?. metadata } ,
156
+ destination : {
157
+ ...defaultDestination ,
158
+ ...oldTestCase . input . request . body . input ?. destination ,
159
+ } ,
160
+ } ;
142
161
const routerRequest : RouterTransformationRequest = {
143
- input : [
144
- {
145
- message : oldTestCase . input ?. request ?. body ?. message || { } ,
146
- metadata : { ...defaultMetadata , ...oldTestCase . input ?. request ?. body ?. metadata } ,
147
- destination : { ...defaultDestination , ...oldTestCase . input ?. request ?. body ?. destination } ,
148
- } ,
149
- ] ,
162
+ input : input ,
150
163
destType : oldTestCase . input ?. request ?. body ?. destType || 'default-destination-type' ,
151
164
} ;
152
165
153
- const routerResponse : RouterTransformationResponse = {
154
- metadata : [ { ...defaultMetadata , ...oldTestCase . output ?. response ?. body ?. metadata } ] ,
155
- destination : { ...defaultDestination , ...oldTestCase . output ?. response ?. body ?. destination } ,
156
- batched : false ,
157
- statusCode : oldTestCase . output ?. response ?. status || 200 ,
158
- } ;
166
+ const routerResponse : RouterTransformationResponse = oldTestCase . output . response . body . output . map (
167
+ ( op ) => {
168
+ return removeUndefinedValues ( {
169
+ batchedRequest : op . batchedRequest ,
170
+ metadata : op . metadata . map ( ( m : any ) => ( { ...defaultMetadata , ...m } ) ) ,
171
+ statusCode : op . statusCode || 200 ,
172
+ destination : { ...defaultDestination , ...op . destination } ,
173
+ batched : op . batched || false ,
174
+ error : op . error ,
175
+ statTags : op . statTags ,
176
+ } ) ;
177
+ } ,
178
+ ) ;
159
179
160
- return {
180
+ return removeUndefinedValues ( {
161
181
id : oldTestCase . id || `router-${ Date . now ( ) } ` ,
162
182
name : oldTestCase . name || 'Router Test Case' ,
163
183
description : oldTestCase . description || 'Migrated router test case' ,
@@ -169,21 +189,23 @@ export function migrateRouterTestCase(oldTestCase: any): RouterTestData {
169
189
input : {
170
190
request : {
171
191
body : routerRequest ,
192
+ method : oldTestCase . input ?. request ?. method || 'POST' ,
172
193
} ,
173
194
} ,
174
195
output : {
175
196
response : {
176
197
status : 200 ,
177
198
body : {
178
- output : [ routerResponse ] ,
199
+ output : routerResponse ,
179
200
} ,
180
201
} ,
181
202
} ,
182
- } ;
203
+ mockFns : oldTestCase . mockFns ? `Add mock of index ${ index } ` : undefined ,
204
+ } ) as RouterTestData ;
183
205
}
184
206
185
207
// Utility function to migrate proxy test cases
186
- export function migrateProxyTestCase ( oldTestCase : any ) : ProxyV1TestData {
208
+ export function migrateProxyTestCase ( oldTestCase : any , index : number ) : ProxyV1TestData {
187
209
const proxyRequest : ProxyV1Request = {
188
210
version : oldTestCase . input ?. request ?. body ?. version || '1.0.0' ,
189
211
type : oldTestCase . input ?. request ?. body ?. type || 'default-type' ,
@@ -360,30 +382,58 @@ const baseDestination: Destination = ${JSON.stringify(commonValues.destination,
360
382
const processedCase = { ...testCase } ;
361
383
362
384
if ( commonValues . metadata && testCase . input ?. request ?. body ) {
385
+ // Handle input metadata
363
386
if ( Array . isArray ( testCase . input . request . body ) ) {
364
387
processedCase . input . request . body = testCase . input . request . body . map ( ( item : any ) => ( {
365
388
...item ,
366
- metadata : { _ref : 'baseMetadata' } , // special marker
389
+ metadata : 'baseMetadata' , // special marker
390
+ } ) ) ;
391
+ } else {
392
+ processedCase . input . request . body . metadata = 'baseMetadata' ; // special marker
393
+ processedCase . output . metadata = 'baseMetadata' ; // special marker
394
+ }
395
+ // Handle output metadata
396
+ if ( Array . isArray ( testCase . output . response . body ) ) {
397
+ processedCase . output . response . body = testCase . output . response . body . map ( ( item : any ) => ( {
398
+ ...item ,
399
+ metadata : 'baseMetadata' , // special marker
367
400
} ) ) ;
368
401
} else {
369
- processedCase . input . request . body . metadata = { __ref : 'baseMetadata' } ; // special marker
402
+ processedCase . output . response . body . metadata = 'baseMetadata' ; // special marker
370
403
}
371
404
}
372
405
373
406
if ( commonValues . destination && testCase . input ?. request ?. body ) {
407
+ // Handle input destination
374
408
if ( Array . isArray ( testCase . input . request . body ) ) {
375
409
processedCase . input . request . body = testCase . input . request . body . map ( ( item : any ) => ( {
376
410
...item ,
377
- destination : { _ref : 'baseDestination' } , // special marker
411
+ destination : 'baseDestination' , // special marker
378
412
} ) ) ;
379
413
} else {
380
- processedCase . input . request . body . destination = { _ref : 'baseDestination' } ; // special marker
414
+ processedCase . input . request . body . destination = 'baseDestination' ; // special marker
415
+ }
416
+ // Handle output destination
417
+ if ( Array . isArray ( testCase . output . response . body ) ) {
418
+ processedCase . output . response . body = testCase . output . response . body . map ( ( item : any ) => ( {
419
+ ...item ,
420
+ metadata : 'baseMetadata' , // special marker
421
+ } ) ) ;
422
+ } else {
423
+ processedCase . output . response . body . metadata = 'baseMetadata' ; // special marker
381
424
}
382
425
}
383
426
384
427
return processedCase ;
385
428
} ) ;
386
429
430
+ // const functionReplacer = (key, value) => {
431
+ // if (typeof value === 'function') {
432
+ // return value.toString();
433
+ // }
434
+ // return value;
435
+ // };
436
+
387
437
// Generate the final file content
388
438
const content = `/**
389
439
* Auto-migrated and optimized test cases
@@ -395,12 +445,11 @@ const baseDestination: Destination = ${JSON.stringify(commonValues.destination,
395
445
396
446
${ variables . join ( '\n' ) }
397
447
398
- export const testData : TestCaseData[] = ${ JSON . stringify ( processedTests , null , 2 ) } ;
448
+ export const data : TestCaseData[] = ${ JSON . stringify ( processedTests , null , 2 ) } ;
399
449
` ;
400
450
401
451
// Replace our special markers with actual variable references
402
- return content . replace (
403
- / { \s * " _ _ r e f e r e n c e " : \s * " ( b a s e M e t a d a t a | b a s e D e s t i n a t i o n ) " \s * } / g,
404
- ( _ , varName ) => varName ,
405
- ) ;
452
+ return content
453
+ . replaceAll ( '"baseMetadata"' , 'baseMetadata' )
454
+ . replaceAll ( '"baseDestination"' , 'baseDestination' ) ;
406
455
}
0 commit comments