@ondc/automation-mock-runner 1.3.48 → 1.3.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -205,27 +205,52 @@ class CodeValidator {
|
|
|
205
205
|
warnings.push(`Function should return an object with properties: ${Object.keys(expectedProperties).join(", ")}`);
|
|
206
206
|
return warnings;
|
|
207
207
|
}
|
|
208
|
-
//
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
208
|
+
// Flatten conditional/logical/sequence/parenthesized wrappers so that
|
|
209
|
+
// minifier output like `return cond ? {a:1} : {a:2}` or `return x && {...}`
|
|
210
|
+
// is treated as the set of object-literal branches it actually returns.
|
|
211
|
+
const objectBranches = [];
|
|
212
|
+
const stack = [...foundReturns];
|
|
213
|
+
let nonObjectFound = false;
|
|
214
|
+
while (stack.length) {
|
|
215
|
+
const node = stack.pop();
|
|
216
|
+
switch (node.type) {
|
|
217
|
+
case "ObjectExpression":
|
|
218
|
+
objectBranches.push(node);
|
|
219
|
+
break;
|
|
220
|
+
case "ConditionalExpression":
|
|
221
|
+
stack.push(node.consequent, node.alternate);
|
|
222
|
+
break;
|
|
223
|
+
case "LogicalExpression":
|
|
224
|
+
stack.push(node.left, node.right);
|
|
225
|
+
break;
|
|
226
|
+
case "SequenceExpression":
|
|
227
|
+
stack.push(node.expressions[node.expressions.length - 1]);
|
|
228
|
+
break;
|
|
229
|
+
case "ParenthesizedExpression":
|
|
230
|
+
stack.push(node.expression);
|
|
231
|
+
break;
|
|
232
|
+
default:
|
|
233
|
+
nonObjectFound = true;
|
|
228
234
|
}
|
|
235
|
+
}
|
|
236
|
+
if (objectBranches.length === 0 || nonObjectFound) {
|
|
237
|
+
warnings.push(`Function should return an object literal with properties: ${Object.keys(expectedProperties).join(", ")}`);
|
|
238
|
+
}
|
|
239
|
+
objectBranches.forEach((returnArg) => {
|
|
240
|
+
const returnedProps = new Set(returnArg.properties.map((p) => p.key.name));
|
|
241
|
+
const expectedProps = Object.keys(expectedProperties);
|
|
242
|
+
// Check for missing properties
|
|
243
|
+
expectedProps.forEach((prop) => {
|
|
244
|
+
if (!returnedProps.has(prop)) {
|
|
245
|
+
warnings.push(`Return object is missing property '${prop}' (expected: ${expectedProperties[prop].type})`);
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
// Check for extra properties
|
|
249
|
+
returnedProps.forEach((prop) => {
|
|
250
|
+
if (!expectedProps.includes(prop)) {
|
|
251
|
+
warnings.push(`Return object has unexpected property '${prop}'`);
|
|
252
|
+
}
|
|
253
|
+
});
|
|
229
254
|
});
|
|
230
255
|
return warnings;
|
|
231
256
|
}
|
|
@@ -57,6 +57,12 @@ describe("CodeValidator.validate — return structure", () => {
|
|
|
57
57
|
expect(result.isValid).toBe(false);
|
|
58
58
|
expect(result.errors.some((e) => e.includes("Function should return an object literal"))).toBe(true);
|
|
59
59
|
});
|
|
60
|
+
it("accepts a minified conditional return: return i ? {...} : {...}", () => {
|
|
61
|
+
const code = `function validate(i,d){return i?{valid:!0,code:200,description:"Valid request"}:{valid:!1,code:200,description:"oh no"}}`;
|
|
62
|
+
const result = code_validator_1.CodeValidator.validate(code, validateSchema);
|
|
63
|
+
expect(result.errors).toEqual([]);
|
|
64
|
+
expect(result.isValid).toBe(true);
|
|
65
|
+
});
|
|
60
66
|
it("warns when only a nested helper returns and the outer function has no return", () => {
|
|
61
67
|
const code = `
|
|
62
68
|
function validate(targetPayload, sessionData) {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const MockRunner_1 = require("../lib/MockRunner");
|
|
4
|
+
describe("scratch: user-supplied validate fn", () => {
|
|
5
|
+
afterEach(async () => {
|
|
6
|
+
const shared = MockRunner_1.MockRunner.sharedRunner;
|
|
7
|
+
if (shared?.terminate)
|
|
8
|
+
await shared.terminate();
|
|
9
|
+
MockRunner_1.MockRunner.sharedRunner = undefined;
|
|
10
|
+
});
|
|
11
|
+
it("runs the user's validate() against a payload and a null payload", async () => {
|
|
12
|
+
const validateSrc = `function validate(targetPayload, sessionData) {
|
|
13
|
+
if(!targetPayload){
|
|
14
|
+
return {valid: false, code: 200, description: "oh no"};
|
|
15
|
+
}
|
|
16
|
+
return { valid: true, code: 200, description: "Valid request" };
|
|
17
|
+
}`;
|
|
18
|
+
const config = {
|
|
19
|
+
meta: { domain: "ONDC:TRV14", version: "2.0.0", flowId: "testing" },
|
|
20
|
+
transaction_data: {
|
|
21
|
+
transaction_id: "e9e0b5cb-3f15-48a1-9d86-d4d643f0909d",
|
|
22
|
+
latest_timestamp: "1970-01-01T00:00:00.000Z",
|
|
23
|
+
},
|
|
24
|
+
steps: [],
|
|
25
|
+
transaction_history: [],
|
|
26
|
+
validationLib: "",
|
|
27
|
+
helperLib: "",
|
|
28
|
+
};
|
|
29
|
+
const runner = new MockRunner_1.MockRunner(config);
|
|
30
|
+
const step = runner.getDefaultStep("search", "search_0");
|
|
31
|
+
step.mock.validate = MockRunner_1.MockRunner.encodeBase64(validateSrc);
|
|
32
|
+
runner.getConfig().steps.push(step);
|
|
33
|
+
const r1 = await runner.runValidatePayloadWithSession("search_0", { context: {} }, {});
|
|
34
|
+
console.log("WITH_PAYLOAD:", JSON.stringify(r1, null, 2));
|
|
35
|
+
const r2 = await runner.runValidatePayloadWithSession("search_0", null, {});
|
|
36
|
+
console.log("WITH_NULL:", JSON.stringify(r2, null, 2));
|
|
37
|
+
expect(r1.success).toBe(true);
|
|
38
|
+
expect(r2.success).toBe(true);
|
|
39
|
+
}, 30000);
|
|
40
|
+
});
|