Skip to content

Commit 0a1fbeb

Browse files
committed
chore: fix scenario
1 parent 1c88908 commit 0a1fbeb

File tree

2 files changed

+84
-35
lines changed

2 files changed

+84
-35
lines changed

test/scripts/migrateTest.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ async function migrateTestFiles(): Promise<void> {
122122
const testCases = readTestFile(filePath);
123123
if (!testCases) continue;
124124

125-
const migratedTests = testCases.map((testCase: any) => {
125+
const migratedTests = testCases.map((testCase: any, index: number) => {
126126
try {
127127
switch (feature.toLowerCase()) {
128128
case 'processor':
129-
return migrateProcessorTestCase(testCase);
129+
return migrateProcessorTestCase(testCase, index);
130130
case 'router':
131-
return migrateRouterTestCase(testCase);
131+
return migrateRouterTestCase(testCase, index);
132132
case 'proxy':
133-
return migrateProxyTestCase(testCase);
133+
return migrateProxyTestCase(testCase, index);
134134
default:
135135
throw new Error(`Unsupported feature type: ${feature}`);
136136
}

test/scripts/migrationUtils.ts

+80-31
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { removeUndefinedValues } from '@rudderstack/integrations-lib';
12
import {
23
Metadata,
34
Destination,
@@ -14,6 +15,7 @@ import {
1415
RouterTestData,
1516
ProxyV1TestData,
1617
} from '../integrations/testTypes';
18+
import { method } from 'lodash';
1719

1820
// Default metadata to fill in missing fields
1921
const defaultMetadata: Metadata = {
@@ -100,20 +102,22 @@ export function migrateTestCase(oldTestCase: any): TestCaseData {
100102
}
101103

102104
// Utility function to migrate processor test cases
103-
export function migrateProcessorTestCase(oldTestCase: any): ProcessorTestData {
105+
export function migrateProcessorTestCase(oldTestCase: any, index: number): ProcessorTestData {
104106
const processorRequest: ProcessorTransformationRequest = {
105107
message: oldTestCase.input?.request?.body[0]?.message || {},
106108
metadata: { ...defaultMetadata, ...oldTestCase.input?.request?.body[0]?.metadata },
107109
destination: { ...defaultDestination, ...oldTestCase.input?.request?.body[0]?.destination },
108110
};
109111

110112
const processorResponse: ProcessorTransformationResponse = {
111-
output: oldTestCase.output?.response?.body[0]?.output || {},
113+
output: oldTestCase.output?.response?.body[0]?.output,
112114
metadata: { ...defaultMetadata, ...oldTestCase.output?.response?.body[0]?.metadata },
113115
statusCode: oldTestCase.output?.response?.status || 200,
116+
error: oldTestCase.output?.response?.body[0]?.error,
117+
statTags: oldTestCase.output?.response?.body[0]?.statTags,
114118
};
115119

116-
return {
120+
return removeUndefinedValues({
117121
id: oldTestCase.id || `processor-${Date.now()}`,
118122
name: oldTestCase.name || 'Processor Test Case',
119123
description: oldTestCase.description || 'Migrated processor test case',
@@ -134,30 +138,46 @@ export function migrateProcessorTestCase(oldTestCase: any): ProcessorTestData {
134138
body: [processorResponse],
135139
},
136140
},
137-
};
141+
mockFns: oldTestCase.mockFns ? `Add mock of index ${index}` : undefined,
142+
}) as ProcessorTestData;
138143
}
139144

140145
// 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+
};
142161
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,
150163
destType: oldTestCase.input?.request?.body?.destType || 'default-destination-type',
151164
};
152165

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+
);
159179

160-
return {
180+
return removeUndefinedValues({
161181
id: oldTestCase.id || `router-${Date.now()}`,
162182
name: oldTestCase.name || 'Router Test Case',
163183
description: oldTestCase.description || 'Migrated router test case',
@@ -169,21 +189,23 @@ export function migrateRouterTestCase(oldTestCase: any): RouterTestData {
169189
input: {
170190
request: {
171191
body: routerRequest,
192+
method: oldTestCase.input?.request?.method || 'POST',
172193
},
173194
},
174195
output: {
175196
response: {
176197
status: 200,
177198
body: {
178-
output: [routerResponse],
199+
output: routerResponse,
179200
},
180201
},
181202
},
182-
};
203+
mockFns: oldTestCase.mockFns ? `Add mock of index ${index}` : undefined,
204+
}) as RouterTestData;
183205
}
184206

185207
// Utility function to migrate proxy test cases
186-
export function migrateProxyTestCase(oldTestCase: any): ProxyV1TestData {
208+
export function migrateProxyTestCase(oldTestCase: any, index: number): ProxyV1TestData {
187209
const proxyRequest: ProxyV1Request = {
188210
version: oldTestCase.input?.request?.body?.version || '1.0.0',
189211
type: oldTestCase.input?.request?.body?.type || 'default-type',
@@ -360,30 +382,58 @@ const baseDestination: Destination = ${JSON.stringify(commonValues.destination,
360382
const processedCase = { ...testCase };
361383

362384
if (commonValues.metadata && testCase.input?.request?.body) {
385+
// Handle input metadata
363386
if (Array.isArray(testCase.input.request.body)) {
364387
processedCase.input.request.body = testCase.input.request.body.map((item: any) => ({
365388
...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
367400
}));
368401
} else {
369-
processedCase.input.request.body.metadata = { __ref: 'baseMetadata' }; // special marker
402+
processedCase.output.response.body.metadata = 'baseMetadata'; // special marker
370403
}
371404
}
372405

373406
if (commonValues.destination && testCase.input?.request?.body) {
407+
// Handle input destination
374408
if (Array.isArray(testCase.input.request.body)) {
375409
processedCase.input.request.body = testCase.input.request.body.map((item: any) => ({
376410
...item,
377-
destination: { _ref: 'baseDestination' }, // special marker
411+
destination: 'baseDestination', // special marker
378412
}));
379413
} 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
381424
}
382425
}
383426

384427
return processedCase;
385428
});
386429

430+
// const functionReplacer = (key, value) => {
431+
// if (typeof value === 'function') {
432+
// return value.toString();
433+
// }
434+
// return value;
435+
// };
436+
387437
// Generate the final file content
388438
const content = `/**
389439
* Auto-migrated and optimized test cases
@@ -395,12 +445,11 @@ const baseDestination: Destination = ${JSON.stringify(commonValues.destination,
395445
396446
${variables.join('\n')}
397447
398-
export const testData: TestCaseData[] = ${JSON.stringify(processedTests, null, 2)};
448+
export const data: TestCaseData[] = ${JSON.stringify(processedTests, null, 2)};
399449
`;
400450

401451
// Replace our special markers with actual variable references
402-
return content.replace(
403-
/{\s*"__reference":\s*"(baseMetadata|baseDestination)"\s*}/g,
404-
(_, varName) => varName,
405-
);
452+
return content
453+
.replaceAll('"baseMetadata"', 'baseMetadata')
454+
.replaceAll('"baseDestination"', 'baseDestination');
406455
}

0 commit comments

Comments
 (0)