@ondc/automation-mock-runner 0.0.9 → 0.1.1

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.
@@ -12,10 +12,13 @@ export declare class MockRunner {
12
12
  errors?: import("zod/v4/core").$ZodIssue[];
13
13
  };
14
14
  runGeneratePayload(actionId: string, inputs?: any): Promise<ExecutionResult>;
15
+ runGeneratePayloadWithSession(actionId: string, sessionData: any): Promise<ExecutionResult>;
15
16
  runValidatePayload(actionId: string, targetPayload: any): Promise<ExecutionResult>;
16
- runMeetRequirements(actionId: string, targetPayload: any): Promise<ExecutionResult>;
17
+ runValidatePayloadWithSession(actionId: string, targetPayload: any, sessionData: any): Promise<ExecutionResult>;
18
+ runMeetRequirements(actionId: string): Promise<ExecutionResult>;
19
+ runMeetRequirementsWithSession(actionId: string, sessionData: any): Promise<ExecutionResult>;
17
20
  getDefaultStep(api: string, actionId: string): MockPlaygroundConfigType["steps"][0];
18
- generateContext(actionId: string, action: string): any;
21
+ generateContext(actionId: string, action: string, sessionData?: any): any;
19
22
  getSessionDataUpToStep(index: number): Record<string, any>;
20
23
  static encodeBase64(input: string): string;
21
24
  static decodeBase64(encoded: string): string;
@@ -103,6 +103,53 @@ class MockRunner {
103
103
  };
104
104
  }
105
105
  }
106
+ async runGeneratePayloadWithSession(actionId, sessionData) {
107
+ const executionId = this.logger.createExecutionContext(actionId);
108
+ const startTime = Date.now();
109
+ try {
110
+ this.logger.logExecution(executionId, "Starting payload generation with session data", {
111
+ actionId,
112
+ sessionKeys: Object.keys(sessionData),
113
+ });
114
+ const step = this.config.steps.find((s) => s.action_id === actionId);
115
+ if (!step) {
116
+ const availableActions = this.config.steps.map((s) => s.action_id);
117
+ throw new errors_1.ActionNotFoundError(actionId, availableActions);
118
+ }
119
+ const index = this.config.steps.findIndex((s) => s.action_id === actionId);
120
+ // Deep clone to avoid mutations
121
+ const defaultPayload = JSON.parse(JSON.stringify(step.mock.defaultPayload));
122
+ const context = this.generateContext(step.action_id, step.api, sessionData);
123
+ defaultPayload.context = context;
124
+ const schema = (0, function_registry_1.getFunctionSchema)("generate");
125
+ const result = await this.getRunnerInstance().execute(MockRunner.decodeBase64(step.mock.generate), schema, [defaultPayload, sessionData]);
126
+ const executionTime = Date.now() - startTime;
127
+ this.logger.logExecution(executionId, "Payload generation with session data completed", {
128
+ actionId,
129
+ success: result.success,
130
+ output: result.result,
131
+ executionTime,
132
+ });
133
+ return result;
134
+ }
135
+ catch (error) {
136
+ const executionTime = Date.now() - startTime;
137
+ this.logger.error("Payload generation with session data failed", { actionId, executionTime }, error);
138
+ return {
139
+ timestamp: new Date().toISOString(),
140
+ success: false,
141
+ error: {
142
+ name: error instanceof Error
143
+ ? error.constructor.name
144
+ : "PayloadGenerationError",
145
+ message: error.message || "Unknown error",
146
+ },
147
+ logs: [],
148
+ executionTime,
149
+ validation: { isValid: false, errors: [], warnings: [] },
150
+ };
151
+ }
152
+ }
106
153
  async runValidatePayload(actionId, targetPayload) {
107
154
  try {
108
155
  const step = this.config.steps.find((s) => s.action_id === actionId);
@@ -129,7 +176,31 @@ class MockRunner {
129
176
  };
130
177
  }
131
178
  }
132
- async runMeetRequirements(actionId, targetPayload) {
179
+ async runValidatePayloadWithSession(actionId, targetPayload, sessionData) {
180
+ try {
181
+ const step = this.config.steps.find((s) => s.action_id === actionId);
182
+ if (!step) {
183
+ throw new Error(`Action step with ID ${actionId} not found.`);
184
+ }
185
+ const schema = (0, function_registry_1.getFunctionSchema)("validate");
186
+ const result = await this.getRunnerInstance().execute(MockRunner.decodeBase64(step.mock.validate), schema, [targetPayload, sessionData]);
187
+ return result;
188
+ }
189
+ catch (error) {
190
+ return {
191
+ timestamp: new Date().toISOString(),
192
+ success: false,
193
+ error: {
194
+ name: "PayloadValidationError",
195
+ message: error.message || "Unknown error",
196
+ },
197
+ logs: [],
198
+ executionTime: 0,
199
+ validation: { isValid: false, errors: [], warnings: [] },
200
+ };
201
+ }
202
+ }
203
+ async runMeetRequirements(actionId) {
133
204
  try {
134
205
  const step = this.config.steps.find((s) => s.action_id === actionId);
135
206
  if (!step) {
@@ -138,7 +209,31 @@ class MockRunner {
138
209
  const index = this.config.steps.findIndex((s) => s.action_id === actionId);
139
210
  const schema = (0, function_registry_1.getFunctionSchema)("meetsRequirements");
140
211
  const sessionData = this.getSessionDataUpToStep(index);
141
- const result = await this.getRunnerInstance().execute(MockRunner.decodeBase64(step.mock.requirements), schema, [targetPayload, sessionData]);
212
+ const result = await this.getRunnerInstance().execute(MockRunner.decodeBase64(step.mock.requirements), schema, [sessionData]);
213
+ return result;
214
+ }
215
+ catch (error) {
216
+ return {
217
+ timestamp: new Date().toISOString(),
218
+ success: false,
219
+ error: {
220
+ name: "MeetRequirementsError",
221
+ message: error.message || "Unknown error",
222
+ },
223
+ logs: [],
224
+ executionTime: 0,
225
+ validation: { isValid: false, errors: [], warnings: [] },
226
+ };
227
+ }
228
+ }
229
+ async runMeetRequirementsWithSession(actionId, sessionData) {
230
+ try {
231
+ const step = this.config.steps.find((s) => s.action_id === actionId);
232
+ if (!step) {
233
+ throw new Error(`Action step with ID ${actionId} not found.`);
234
+ }
235
+ const schema = (0, function_registry_1.getFunctionSchema)("meetsRequirements");
236
+ const result = await this.getRunnerInstance().execute(MockRunner.decodeBase64(step.mock.requirements), schema, [sessionData]);
142
237
  return result;
143
238
  }
144
239
  catch (error) {
@@ -172,6 +267,7 @@ class MockRunner {
172
267
  message: {},
173
268
  },
174
269
  saveData: {
270
+ transactionId: "$.context.transaction_id",
175
271
  latestMessage_id: "$.context.message_id",
176
272
  latestTimestamp: "$.context.timestamp",
177
273
  bapId: "$.context.bap_id",
@@ -220,7 +316,7 @@ class MockRunner {
220
316
  },
221
317
  };
222
318
  }
223
- generateContext(actionId, action) {
319
+ generateContext(actionId, action, sessionData) {
224
320
  let step = this.config.steps.find((s) => s.action_id === actionId);
225
321
  if (!step) {
226
322
  step = undefined;
@@ -228,16 +324,22 @@ class MockRunner {
228
324
  const responseFor = step?.responseFor;
229
325
  let messageId = (0, uuid_1.v4)();
230
326
  if (responseFor) {
231
- const responsePayload = this.config.transaction_history.find((item) => item.action_id === responseFor)?.payload;
232
- if (responsePayload.context?.message_id) {
233
- messageId = responsePayload.context.message_id;
327
+ if (sessionData?.latestMessage_id) {
328
+ messageId = sessionData.latestMessage_id;
329
+ }
330
+ else {
331
+ const responsePayload = this.config.transaction_history.find((item) => item.action_id === responseFor)?.payload;
332
+ if (responsePayload.context?.message_id) {
333
+ messageId = responsePayload.context.message_id;
334
+ }
234
335
  }
235
336
  }
236
337
  const baseContext = {
237
338
  domain: this.config.meta.domain,
238
339
  action: action,
239
340
  timestamp: new Date().toISOString(),
240
- transaction_id: this.config.transaction_data.transaction_id,
341
+ transaction_id: sessionData?.transaction_id ||
342
+ this.config.transaction_data.transaction_id,
241
343
  message_id: messageId,
242
344
  bap_id: this.config.transaction_data.bap_id || "",
243
345
  bap_uri: this.config.transaction_data.bap_uri || "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ondc/automation-mock-runner",
3
- "version": "0.0.9",
3
+ "version": "0.1.1",
4
4
  "description": "A TypeScript library for ONDC automation mock runner",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",