@ondc/automation-mock-runner 1.3.19 → 1.3.21
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.
package/dist/lib/MockRunner.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare class MockRunner {
|
|
|
6
6
|
private config;
|
|
7
7
|
private static sharedRunner;
|
|
8
8
|
logger: Logger;
|
|
9
|
+
private static getSharedRunner;
|
|
9
10
|
constructor(config: MockPlaygroundConfigType, skipValidation?: boolean);
|
|
10
11
|
getRunnerInstance(): BaseCodeRunner;
|
|
11
12
|
getConfig(): {
|
package/dist/lib/MockRunner.js
CHANGED
|
@@ -12,6 +12,12 @@ const jsonpath_1 = __importDefault(require("jsonpath"));
|
|
|
12
12
|
const function_registry_1 = require("./constants/function-registry");
|
|
13
13
|
const uuid_1 = require("uuid");
|
|
14
14
|
class MockRunner {
|
|
15
|
+
static getSharedRunner(logger) {
|
|
16
|
+
if (!MockRunner.sharedRunner) {
|
|
17
|
+
MockRunner.sharedRunner = runner_factory_1.RunnerFactory.createRunner({}, logger);
|
|
18
|
+
}
|
|
19
|
+
return MockRunner.sharedRunner;
|
|
20
|
+
}
|
|
15
21
|
constructor(config, skipValidation = false) {
|
|
16
22
|
this.logger = logger_1.Logger.getInstance();
|
|
17
23
|
if (!skipValidation) {
|
|
@@ -38,12 +44,9 @@ class MockRunner {
|
|
|
38
44
|
}
|
|
39
45
|
getRunnerInstance() {
|
|
40
46
|
this.logger.debug("Getting code runner instance");
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.logger.debug("Code runner instance obtained successfully: " +
|
|
45
|
-
MockRunner.sharedRunner.toString());
|
|
46
|
-
return MockRunner.sharedRunner;
|
|
47
|
+
const runner = MockRunner.getSharedRunner(this.logger);
|
|
48
|
+
this.logger.debug("Code runner instance obtained successfully: " + runner.toString());
|
|
49
|
+
return runner;
|
|
47
50
|
}
|
|
48
51
|
getConfig() {
|
|
49
52
|
return this.config;
|
|
@@ -287,10 +290,7 @@ class MockRunner {
|
|
|
287
290
|
}
|
|
288
291
|
}
|
|
289
292
|
getDefaultStep(api, actionId, formType) {
|
|
290
|
-
if (formType === "html_form") {
|
|
291
|
-
throw new Error("HTML form generation is not implemented yet");
|
|
292
|
-
}
|
|
293
|
-
if (formType === "dynamic_form") {
|
|
293
|
+
if (formType === "dynamic_form" || formType === "html_form") {
|
|
294
294
|
return {
|
|
295
295
|
api: api,
|
|
296
296
|
action_id: actionId,
|
|
@@ -567,7 +567,7 @@ class MockRunner {
|
|
|
567
567
|
}
|
|
568
568
|
static async runGetSave(payload, expression) {
|
|
569
569
|
const evalExpression = MockRunner.decodeBase64(expression);
|
|
570
|
-
const runner =
|
|
570
|
+
const runner = MockRunner.getSharedRunner();
|
|
571
571
|
const schema = (0, function_registry_1.getFunctionSchema)("getSave");
|
|
572
572
|
return await runner.execute(evalExpression, schema, [payload]);
|
|
573
573
|
}
|
package/dist/lib/configHelper.js
CHANGED
|
@@ -197,7 +197,6 @@ async function getMinifiedCode(base64Code) {
|
|
|
197
197
|
*/
|
|
198
198
|
async function generatePlaygroundConfigFromFlowConfig(payloads, flowConfig) {
|
|
199
199
|
flowConfig = JSON.parse(JSON.stringify(flowConfig));
|
|
200
|
-
flowConfig.sequence = flowConfig.sequence.filter((step) => step.type !== "HTML_FORM" && step.type !== "DYNAMIC_FORM");
|
|
201
200
|
payloads = payloads.sort((a, b) => new Date(a.context.timestamp).getTime() -
|
|
202
201
|
new Date(b.context.timestamp).getTime());
|
|
203
202
|
const domain = payloads[0].context.domain;
|
|
@@ -206,33 +205,37 @@ async function generatePlaygroundConfigFromFlowConfig(payloads, flowConfig) {
|
|
|
206
205
|
const mockRunner = new MockRunner_1.MockRunner(config);
|
|
207
206
|
let index = 0;
|
|
208
207
|
for (const step of flowConfig.sequence) {
|
|
209
|
-
|
|
208
|
+
const isFormStep = step.type === "HTML_FORM" ||
|
|
210
209
|
step.type === "DYNAMIC_FORM" ||
|
|
211
|
-
step.type === "FORM"
|
|
212
|
-
|
|
210
|
+
step.type === "FORM";
|
|
211
|
+
let stepConfig;
|
|
212
|
+
if (isFormStep) {
|
|
213
|
+
// HTML_FORM is not yet fully implemented — fall back to dynamic_form default
|
|
214
|
+
stepConfig = mockRunner.getDefaultStep(step.type, step.key, "dynamic_form");
|
|
213
215
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
const stepConfig = mockRunner.getDefaultStep(step.type, step.key);
|
|
220
|
-
if (index === 0) {
|
|
221
|
-
stepConfig.mock.generate = MockRunner_1.MockRunner.encodeBase64(`async function generate(defaultPayload, sessionData) {
|
|
216
|
+
else {
|
|
217
|
+
stepConfig = mockRunner.getDefaultStep(step.type, step.key);
|
|
218
|
+
if (index === 0) {
|
|
219
|
+
stepConfig.mock.generate = MockRunner_1.MockRunner.encodeBase64(`async function generate(defaultPayload, sessionData) {
|
|
222
220
|
setCityFromInputs(defaultPayload, sessionData.user_inputs);
|
|
223
221
|
return defaultPayload;
|
|
224
222
|
}`);
|
|
225
|
-
|
|
223
|
+
stepConfig.mock.inputs = cityInputs;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
stepConfig.mock.inputs = {};
|
|
227
|
+
}
|
|
228
|
+
const stepPayloadIndex = payloads.findIndex((p) => p.context.action === step.type);
|
|
229
|
+
if (stepPayloadIndex !== -1) {
|
|
230
|
+
stepConfig.mock.defaultPayload = payloads[stepPayloadIndex];
|
|
231
|
+
payloads.splice(stepPayloadIndex, 1); // remove used payload
|
|
232
|
+
}
|
|
233
|
+
index++;
|
|
226
234
|
}
|
|
227
|
-
else {
|
|
228
|
-
stepConfig.mock.inputs = {};
|
|
229
|
-
}
|
|
230
|
-
stepConfig.mock.defaultPayload = payload;
|
|
231
235
|
const findResponseFor = flowConfig.sequence.find((s) => s.pair === step.key);
|
|
232
236
|
stepConfig.responseFor = findResponseFor ? findResponseFor.key : null;
|
|
233
237
|
stepConfig.unsolicited = step.unsolicited;
|
|
234
238
|
config.steps.push(stepConfig);
|
|
235
|
-
index++;
|
|
236
239
|
}
|
|
237
240
|
return config;
|
|
238
241
|
}
|
|
@@ -37,7 +37,7 @@ exports.NodeRunner = void 0;
|
|
|
37
37
|
const worker_threads_1 = require("worker_threads");
|
|
38
38
|
const code_validator_1 = require("../validators/code-validator");
|
|
39
39
|
const path = __importStar(require("path"));
|
|
40
|
-
const DEFAULT_POOL_SIZE =
|
|
40
|
+
const DEFAULT_POOL_SIZE = 2;
|
|
41
41
|
const MAX_EXECUTIONS_PER_WORKER = 100;
|
|
42
42
|
const MAX_WORKER_AGE_MS = 10 * 60 * 1000; // 10 minutes
|
|
43
43
|
class NodeRunner {
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const configHelper_1 = require("../lib/configHelper");
|
|
7
7
|
const MockRunner_1 = require("../lib/MockRunner");
|
|
8
|
+
const runner_factory_1 = require("../lib/runners/runner-factory");
|
|
8
9
|
describe("MockRunner", () => {
|
|
9
10
|
let mockRunner;
|
|
10
11
|
let mockConfig;
|
|
@@ -35,7 +36,12 @@ describe("MockRunner", () => {
|
|
|
35
36
|
throw error;
|
|
36
37
|
}
|
|
37
38
|
});
|
|
38
|
-
afterEach(() => {
|
|
39
|
+
afterEach(async () => {
|
|
40
|
+
const sharedRunner = MockRunner_1.MockRunner.sharedRunner;
|
|
41
|
+
if (sharedRunner?.terminate) {
|
|
42
|
+
await sharedRunner.terminate();
|
|
43
|
+
}
|
|
44
|
+
MockRunner_1.MockRunner.sharedRunner = undefined;
|
|
39
45
|
jest.clearAllMocks();
|
|
40
46
|
});
|
|
41
47
|
describe("Constructor", () => {
|
|
@@ -68,6 +74,16 @@ describe("MockRunner", () => {
|
|
|
68
74
|
const mockRunnerB = new MockRunner_1.MockRunner(optimizedConfig, true);
|
|
69
75
|
expect(mockRunnerA.getRunnerInstance()).toBe(mockRunnerB.getRunnerInstance());
|
|
70
76
|
});
|
|
77
|
+
it("should reuse singleton runner for runGetSave", async () => {
|
|
78
|
+
MockRunner_1.MockRunner.sharedRunner = undefined;
|
|
79
|
+
const createRunnerSpy = jest.spyOn(runner_factory_1.RunnerFactory, "createRunner");
|
|
80
|
+
mockRunner.getRunnerInstance();
|
|
81
|
+
const getSaveFunction = MockRunner_1.MockRunner.encodeBase64(`async function getSave(payload){ return payload.value; }`);
|
|
82
|
+
const result = await MockRunner_1.MockRunner.runGetSave({ value: "singleton-check" }, getSaveFunction);
|
|
83
|
+
expect(result.success).toBe(true);
|
|
84
|
+
expect(result.result).toBe("singleton-check");
|
|
85
|
+
expect(createRunnerSpy).toHaveBeenCalledTimes(1);
|
|
86
|
+
});
|
|
71
87
|
});
|
|
72
88
|
describe("Config Validation", () => {
|
|
73
89
|
it("should validate correct config successfully", () => {
|
|
@@ -686,71 +686,6 @@ describe("configHelper", () => {
|
|
|
686
686
|
expect(onSearchStepConfig.unsolicited).toBe(false);
|
|
687
687
|
expect(onSearchStepConfig.mock.defaultPayload).toBe(onSearchPayload);
|
|
688
688
|
});
|
|
689
|
-
it("should ignore HTML_FORM and DYNAMIC_FORM steps and preserve unsolicited flag", async () => {
|
|
690
|
-
const payloads = [
|
|
691
|
-
{
|
|
692
|
-
context: {
|
|
693
|
-
action: "search",
|
|
694
|
-
timestamp: "2025-01-01T09:00:00.000Z",
|
|
695
|
-
domain: "ONDC:RET10",
|
|
696
|
-
version: "2.0.0",
|
|
697
|
-
},
|
|
698
|
-
},
|
|
699
|
-
{
|
|
700
|
-
context: {
|
|
701
|
-
action: "on_status",
|
|
702
|
-
timestamp: "2025-01-01T10:00:00.000Z",
|
|
703
|
-
domain: "ONDC:RET10",
|
|
704
|
-
version: "2.0.0",
|
|
705
|
-
},
|
|
706
|
-
},
|
|
707
|
-
];
|
|
708
|
-
const flowConfig = {
|
|
709
|
-
id: "flow_with_forms",
|
|
710
|
-
sequence: [
|
|
711
|
-
{
|
|
712
|
-
key: "search_step",
|
|
713
|
-
type: "search",
|
|
714
|
-
unsolicited: false,
|
|
715
|
-
description: "Search step",
|
|
716
|
-
pair: null,
|
|
717
|
-
owner: "BAP",
|
|
718
|
-
},
|
|
719
|
-
{
|
|
720
|
-
key: "html_form_step",
|
|
721
|
-
type: "HTML_FORM",
|
|
722
|
-
unsolicited: false,
|
|
723
|
-
description: "HTML form step",
|
|
724
|
-
pair: null,
|
|
725
|
-
owner: "BAP",
|
|
726
|
-
},
|
|
727
|
-
{
|
|
728
|
-
key: "dynamic_form_step",
|
|
729
|
-
type: "DYNAMIC_FORM",
|
|
730
|
-
unsolicited: false,
|
|
731
|
-
description: "Dynamic form step",
|
|
732
|
-
pair: null,
|
|
733
|
-
owner: "BAP",
|
|
734
|
-
},
|
|
735
|
-
{
|
|
736
|
-
key: "on_status_step",
|
|
737
|
-
type: "on_status",
|
|
738
|
-
unsolicited: true,
|
|
739
|
-
description: "Unsolicited status",
|
|
740
|
-
pair: null,
|
|
741
|
-
owner: "BPP",
|
|
742
|
-
},
|
|
743
|
-
],
|
|
744
|
-
};
|
|
745
|
-
const config = await (0, configHelper_1.generatePlaygroundConfigFromFlowConfig)(payloads, flowConfig);
|
|
746
|
-
// Only non-form steps should be present
|
|
747
|
-
expect(config.steps.map((s) => s.action_id)).toEqual([
|
|
748
|
-
"search_step",
|
|
749
|
-
"on_status_step",
|
|
750
|
-
]);
|
|
751
|
-
const onStatusStep = config.steps.find((s) => s.action_id === "on_status_step");
|
|
752
|
-
expect(onStatusStep?.unsolicited).toBe(true);
|
|
753
|
-
});
|
|
754
689
|
it("should derive version from core_version when version is missing", async () => {
|
|
755
690
|
const payloads = [
|
|
756
691
|
{
|