@ondc/automation-mock-runner 1.0.5 → 1.0.7

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.
@@ -319,49 +319,84 @@ class MockRunner {
319
319
  };
320
320
  }
321
321
  generateContext(actionId, action, sessionData) {
322
- let step = this.config.steps.find((s) => s.action_id === actionId);
323
- if (!step) {
324
- step = undefined;
325
- }
326
- const responseFor = step?.responseFor;
322
+ // Find the step configuration for this action
323
+ const step = this.config.steps.find((s) => s.action_id === actionId);
324
+ // Determine the message_id based on responseFor logic
327
325
  let messageId = (0, uuid_1.v4)();
328
- if (responseFor) {
329
- if (sessionData?.latestMessage_id) {
330
- messageId = sessionData.latestMessage_id;
326
+ if (step?.responseFor) {
327
+ // Priority 1: Get from sessionData if available
328
+ if (sessionData?.latestMessage_id &&
329
+ Array.isArray(sessionData.latestMessage_id) &&
330
+ sessionData.latestMessage_id.length > 0) {
331
+ messageId = sessionData.latestMessage_id[0];
331
332
  }
333
+ // Priority 2: Fall back to transaction history
332
334
  else {
333
- const responsePayload = this.config.transaction_history.find((item) => item.action_id === responseFor)?.payload;
334
- if (responsePayload.context?.message_id) {
335
- messageId = responsePayload.context.message_id;
335
+ const historyItem = this.config.transaction_history?.find((item) => item.action_id === step.responseFor);
336
+ if (historyItem?.payload?.context?.message_id) {
337
+ messageId = historyItem.payload.context.message_id;
336
338
  }
337
339
  }
338
340
  }
341
+ // Safely extract transaction_id
342
+ const transactionId = (() => {
343
+ // Priority 1: Get from sessionData
344
+ if (sessionData?.transaction_id) {
345
+ const sessionTxnId = Array.isArray(sessionData.transaction_id)
346
+ ? sessionData.transaction_id[0]
347
+ : sessionData.transaction_id;
348
+ // Only return if we got a valid non-empty value
349
+ if (sessionTxnId && sessionTxnId.trim().length > 0) {
350
+ return sessionTxnId;
351
+ }
352
+ }
353
+ // Priority 2: Get from transaction history (most recent)
354
+ if (this.config.transaction_history?.length > 0) {
355
+ const mostRecentHistory = this.config.transaction_history[this.config.transaction_history.length - 1];
356
+ const historyTxnId = mostRecentHistory?.payload?.context?.transaction_id;
357
+ if (historyTxnId && historyTxnId.trim().length > 0) {
358
+ return historyTxnId;
359
+ }
360
+ }
361
+ // Priority 3: Get from transaction_data
362
+ const configTxnId = this.config.transaction_data?.transaction_id;
363
+ if (configTxnId && configTxnId.trim().length > 0) {
364
+ return configTxnId;
365
+ }
366
+ // Priority 4: Generate new UUID as last resort
367
+ return (0, uuid_1.v4)();
368
+ })();
369
+ // Build base context
339
370
  const baseContext = {
340
- domain: this.config.meta.domain,
371
+ domain: this.config.meta?.domain || "",
341
372
  action: action,
342
373
  timestamp: new Date().toISOString(),
343
- transaction_id: sessionData?.transaction_id ||
344
- this.config.transaction_data.transaction_id,
374
+ transaction_id: transactionId,
345
375
  message_id: messageId,
346
- bap_id: this.config.transaction_data.bap_id || "",
347
- bap_uri: this.config.transaction_data.bap_uri || "",
376
+ bap_id: this.config.transaction_data?.bap_id || "",
377
+ bap_uri: this.config.transaction_data?.bap_uri || "",
348
378
  ttl: "PT30S",
349
379
  };
350
- if (action != "search") {
351
- baseContext.bpp_id = this.config.transaction_data.bpp_id || "";
352
- baseContext.bpp_uri = this.config.transaction_data.bpp_uri || "";
380
+ // Add BPP details for non-search actions
381
+ if (action !== "search") {
382
+ baseContext.bpp_id = this.config.transaction_data?.bpp_id || "";
383
+ baseContext.bpp_uri = this.config.transaction_data?.bpp_uri || "";
353
384
  }
354
- if (this.config.meta.version.split(".")[0] === "1") {
385
+ // Version-specific context structure
386
+ const version = this.config.meta?.version || "2.0.0";
387
+ const majorVersion = parseInt(version.split(".")[0], 10);
388
+ if (majorVersion === 1) {
355
389
  return {
356
390
  ...baseContext,
357
391
  country: "IND",
358
392
  city: "*",
359
- core_version: this.config.meta.version,
393
+ core_version: version,
360
394
  };
361
395
  }
396
+ // Version 2+ format
362
397
  return {
363
398
  ...baseContext,
364
- version: this.config.meta.version,
399
+ version: version,
365
400
  location: {
366
401
  country: {
367
402
  code: "IND",
@@ -161,5 +161,29 @@ describe("MockRunner", () => {
161
161
  console.log(Object.keys(ob).length);
162
162
  expect(res.result).toBe(ob.a + ob.b);
163
163
  });
164
+ it("should timeout for long-running getSave functions", async () => {
165
+ const longRunningFunction = MockRunner_1.MockRunner.encodeBase64(`async function getSave(payload){
166
+ console.log('Starting long-running operation...');
167
+ // Simulate a long-running operation that takes more than 3 seconds
168
+ await new Promise(resolve => setTimeout(resolve, 4000));
169
+ console.log('Long-running operation completed');
170
+ return payload.result;
171
+ }`);
172
+ const payload = {
173
+ result: "This should timeout",
174
+ };
175
+ // This should timeout and return an error result
176
+ const res = await MockRunner_1.MockRunner.runGetSave(payload, longRunningFunction);
177
+ console.log("Timeout test result:", JSON.stringify(res, null, 2));
178
+ // Expect the operation to fail due to timeout
179
+ expect(res.success).toBe(false);
180
+ expect(res.error).toBeDefined();
181
+ if (res.error) {
182
+ expect(res.error.message.toLowerCase().includes("timeout") ||
183
+ res.error.message.includes("Timeout") ||
184
+ res.error.message.includes("TIMEOUT") ||
185
+ res.error.name.toLowerCase().includes("timeout")).toBe(true);
186
+ }
187
+ }, 10000); // Set Jest timeout to 10 seconds to allow for the timeout to occur
164
188
  });
165
189
  });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,472 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const MockRunner_1 = require("../lib/MockRunner");
4
+ const uuid_1 = require("uuid");
5
+ // Mock UUID to get predictable test results
6
+ jest.mock("uuid", () => ({
7
+ v4: jest.fn(() => "test-uuid-1234"),
8
+ }));
9
+ describe("MockRunner - generateContext", () => {
10
+ let mockRunner;
11
+ let mockConfig;
12
+ beforeEach(() => {
13
+ // Reset UUID mock
14
+ uuid_1.v4.mockReturnValue("test-uuid-1234");
15
+ mockConfig = {
16
+ meta: {
17
+ domain: "ONDC:TRV14",
18
+ version: "2.0.0",
19
+ flowId: "test-flow",
20
+ },
21
+ transaction_data: {
22
+ transaction_id: "txn-123",
23
+ latest_timestamp: "2025-11-11T10:00:00.000Z",
24
+ bap_id: "test-bap-id",
25
+ bap_uri: "https://test-bap.com",
26
+ bpp_id: "test-bpp-id",
27
+ bpp_uri: "https://test-bpp.com",
28
+ },
29
+ steps: [
30
+ {
31
+ api: "search",
32
+ action_id: "search_1",
33
+ owner: "BAP",
34
+ responseFor: null,
35
+ unsolicited: false,
36
+ description: "Search step",
37
+ mock: {
38
+ generate: "base64-encoded-generate",
39
+ validate: "base64-encoded-validate",
40
+ requirements: "base64-encoded-requirements",
41
+ defaultPayload: { context: {}, message: {} },
42
+ saveData: {},
43
+ inputs: {},
44
+ },
45
+ },
46
+ {
47
+ api: "on_search",
48
+ action_id: "on_search_1",
49
+ owner: "BPP",
50
+ responseFor: "search_1",
51
+ unsolicited: false,
52
+ description: "On search response",
53
+ mock: {
54
+ generate: "base64-encoded-generate",
55
+ validate: "base64-encoded-validate",
56
+ requirements: "base64-encoded-requirements",
57
+ defaultPayload: { context: {}, message: {} },
58
+ saveData: {},
59
+ inputs: {},
60
+ },
61
+ },
62
+ {
63
+ api: "select",
64
+ action_id: "select_1",
65
+ owner: "BAP",
66
+ responseFor: null,
67
+ unsolicited: false,
68
+ description: "Select step",
69
+ mock: {
70
+ generate: "base64-encoded-generate",
71
+ validate: "base64-encoded-validate",
72
+ requirements: "base64-encoded-requirements",
73
+ defaultPayload: { context: {}, message: {} },
74
+ saveData: {},
75
+ inputs: {},
76
+ },
77
+ },
78
+ ],
79
+ transaction_history: [
80
+ {
81
+ action_id: "search_1",
82
+ payload: {
83
+ context: {
84
+ message_id: "search-msg-123",
85
+ timestamp: "2025-11-11T10:00:00.000Z",
86
+ },
87
+ message: {},
88
+ },
89
+ saved_info: {},
90
+ },
91
+ ],
92
+ validationLib: "",
93
+ helperLib: "",
94
+ };
95
+ mockRunner = new MockRunner_1.MockRunner(mockConfig, true); // Skip validation for tests
96
+ });
97
+ describe("Basic Context Generation", () => {
98
+ it("should generate context with basic required fields", () => {
99
+ const context = mockRunner.generateContext("search_1", "search");
100
+ expect(context).toHaveProperty("domain", "ONDC:TRV14");
101
+ expect(context).toHaveProperty("action", "search");
102
+ expect(context).toHaveProperty("timestamp");
103
+ expect(context).toHaveProperty("transaction_id", "txn-123");
104
+ expect(context).toHaveProperty("message_id");
105
+ expect(context).toHaveProperty("bap_id", "test-bap-id");
106
+ expect(context).toHaveProperty("bap_uri", "https://test-bap.com");
107
+ expect(context).toHaveProperty("ttl", "PT30S");
108
+ });
109
+ it("should generate timestamp in ISO format", () => {
110
+ const context = mockRunner.generateContext("search_1", "search");
111
+ expect(context.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
112
+ expect(new Date(context.timestamp).toISOString()).toBe(context.timestamp);
113
+ });
114
+ it("should use transaction_id from config", () => {
115
+ const context = mockRunner.generateContext("search_1", "search");
116
+ expect(context.transaction_id).toBe("txn-123");
117
+ });
118
+ it("should generate new UUID for message_id when no responseFor", () => {
119
+ const context = mockRunner.generateContext("search_1", "search");
120
+ expect(context.message_id).toBe("test-uuid-1234");
121
+ });
122
+ });
123
+ describe("Version-based Context Structure", () => {
124
+ it("should generate v2.0.0 context structure", () => {
125
+ const context = mockRunner.generateContext("search_1", "search");
126
+ expect(context).toHaveProperty("version", "2.0.0");
127
+ expect(context).toHaveProperty("location");
128
+ expect(context.location).toEqual({
129
+ country: { code: "IND" },
130
+ city: { code: "*" },
131
+ });
132
+ expect(context).not.toHaveProperty("country");
133
+ expect(context).not.toHaveProperty("city");
134
+ expect(context).not.toHaveProperty("core_version");
135
+ });
136
+ it("should generate v1.x.x context structure", () => {
137
+ const v1Config = {
138
+ ...mockConfig,
139
+ meta: { ...mockConfig.meta, version: "1.2.0" },
140
+ };
141
+ const v1Runner = new MockRunner_1.MockRunner(v1Config, true);
142
+ const context = v1Runner.generateContext("search_1", "search");
143
+ expect(context).toHaveProperty("core_version", "1.2.0");
144
+ expect(context).toHaveProperty("country", "IND");
145
+ expect(context).toHaveProperty("city", "*");
146
+ expect(context).not.toHaveProperty("version");
147
+ expect(context).not.toHaveProperty("location");
148
+ });
149
+ it("should handle version 1.0.0", () => {
150
+ const v1Config = {
151
+ ...mockConfig,
152
+ meta: { ...mockConfig.meta, version: "1.0.0" },
153
+ };
154
+ const v1Runner = new MockRunner_1.MockRunner(v1Config, true);
155
+ const context = v1Runner.generateContext("search_1", "search");
156
+ expect(context.core_version).toBe("1.0.0");
157
+ expect(context.country).toBe("IND");
158
+ expect(context.city).toBe("*");
159
+ });
160
+ it("should handle version 1.10.5", () => {
161
+ const v1Config = {
162
+ ...mockConfig,
163
+ meta: { ...mockConfig.meta, version: "1.10.5" },
164
+ };
165
+ const v1Runner = new MockRunner_1.MockRunner(v1Config, true);
166
+ const context = v1Runner.generateContext("search_1", "search");
167
+ expect(context.core_version).toBe("1.10.5");
168
+ });
169
+ });
170
+ describe("BPP Fields Handling", () => {
171
+ it("should not include bpp fields for search action", () => {
172
+ const context = mockRunner.generateContext("search_1", "search");
173
+ expect(context).not.toHaveProperty("bpp_id");
174
+ expect(context).not.toHaveProperty("bpp_uri");
175
+ });
176
+ it("should include bpp fields for non-search actions", () => {
177
+ const context = mockRunner.generateContext("select_1", "select");
178
+ expect(context).toHaveProperty("bpp_id", "test-bpp-id");
179
+ expect(context).toHaveProperty("bpp_uri", "https://test-bpp.com");
180
+ });
181
+ it("should include bpp fields for on_search action", () => {
182
+ const context = mockRunner.generateContext("on_search_1", "on_search");
183
+ expect(context).toHaveProperty("bpp_id", "test-bpp-id");
184
+ expect(context).toHaveProperty("bpp_uri", "https://test-bpp.com");
185
+ });
186
+ it("should handle empty bpp_id and bpp_uri gracefully", () => {
187
+ const configWithEmptyBpp = {
188
+ ...mockConfig,
189
+ transaction_data: {
190
+ ...mockConfig.transaction_data,
191
+ bpp_id: "",
192
+ bpp_uri: "",
193
+ },
194
+ };
195
+ const runnerWithEmptyBpp = new MockRunner_1.MockRunner(configWithEmptyBpp, true);
196
+ const context = runnerWithEmptyBpp.generateContext("select_1", "select");
197
+ expect(context.bpp_id).toBe("");
198
+ expect(context.bpp_uri).toBe("");
199
+ });
200
+ });
201
+ describe("Response Message ID Handling", () => {
202
+ it("should use message_id from responseFor step in transaction history", () => {
203
+ const context = mockRunner.generateContext("on_search_1", "on_search");
204
+ expect(context.message_id).toBe("search-msg-123");
205
+ });
206
+ it("should generate new UUID when responseFor not found in transaction history", () => {
207
+ const configWithMissingHistory = {
208
+ ...mockConfig,
209
+ transaction_history: [], // Empty history
210
+ };
211
+ const runnerWithMissingHistory = new MockRunner_1.MockRunner(configWithMissingHistory, true);
212
+ // This will throw because the function tries to access responsePayload.context on undefined
213
+ expect(() => {
214
+ runnerWithMissingHistory.generateContext("on_search_1", "on_search");
215
+ }).not.toThrow("Cannot read properties of undefined (reading 'context')");
216
+ });
217
+ it("should generate new UUID when responseFor payload has no message_id", () => {
218
+ const configWithIncompleteHistory = {
219
+ ...mockConfig,
220
+ transaction_history: [
221
+ {
222
+ action_id: "search_1",
223
+ payload: {
224
+ context: {}, // No message_id
225
+ message: {},
226
+ },
227
+ saved_info: {},
228
+ },
229
+ ],
230
+ };
231
+ const runnerWithIncompleteHistory = new MockRunner_1.MockRunner(configWithIncompleteHistory, true);
232
+ const context = runnerWithIncompleteHistory.generateContext("on_search_1", "on_search");
233
+ expect(context.message_id).toBe("test-uuid-1234");
234
+ });
235
+ });
236
+ describe("Session Data Integration", () => {
237
+ it("should use transaction_id from sessionData when provided", () => {
238
+ const sessionData = {
239
+ transaction_id: ["session-txn-456"],
240
+ };
241
+ const context = mockRunner.generateContext("search_1", "search", sessionData);
242
+ expect(context.transaction_id).toBe("session-txn-456");
243
+ });
244
+ it("should use latestMessage_id from sessionData for response steps", () => {
245
+ const sessionData = {
246
+ transaction_id: ["session-txn-456"], // Need to provide transaction_id array
247
+ latestMessage_id: ["session-msg-789"],
248
+ };
249
+ const context = mockRunner.generateContext("on_search_1", "on_search", sessionData);
250
+ expect(context.message_id).toBe("session-msg-789");
251
+ });
252
+ it("should fallback to config transaction_id when sessionData has empty transaction_id", () => {
253
+ const sessionData = {
254
+ transaction_id: [],
255
+ };
256
+ const context = mockRunner.generateContext("search_1", "search", sessionData);
257
+ expect(context.transaction_id).toBe("txn-123");
258
+ });
259
+ it("should fallback to transaction history when sessionData has empty latestMessage_id", () => {
260
+ const sessionData = {
261
+ transaction_id: ["session-txn"], // Provide transaction_id array
262
+ latestMessage_id: [],
263
+ };
264
+ const context = mockRunner.generateContext("on_search_1", "on_search", sessionData);
265
+ expect(context.transaction_id).toBe("session-txn");
266
+ expect(context.message_id).toBe("search-msg-123");
267
+ });
268
+ it("should handle sessionData with null values", () => {
269
+ const sessionData = {
270
+ transaction_id: null,
271
+ latestMessage_id: null,
272
+ };
273
+ // This will throw because the function tries to access transaction_id[0] on null
274
+ expect(() => {
275
+ mockRunner.generateContext("on_search_1", "on_search", sessionData);
276
+ }).not.toThrow("Cannot read properties of null (reading '0')");
277
+ });
278
+ });
279
+ describe("Step Resolution", () => {
280
+ it("should handle non-existent actionId gracefully", () => {
281
+ const context = mockRunner.generateContext("non_existent", "unknown");
282
+ expect(context.message_id).toBe("test-uuid-1234"); // Should generate new UUID
283
+ expect(context.domain).toBe("ONDC:TRV14");
284
+ expect(context.action).toBe("unknown");
285
+ });
286
+ it("should handle step without responseFor", () => {
287
+ const context = mockRunner.generateContext("search_1", "search");
288
+ expect(context.message_id).toBe("test-uuid-1234");
289
+ });
290
+ });
291
+ describe("Empty and Default Values", () => {
292
+ it("should handle empty bap_id and bap_uri", () => {
293
+ const configWithEmptyBap = {
294
+ ...mockConfig,
295
+ transaction_data: {
296
+ ...mockConfig.transaction_data,
297
+ bap_id: "",
298
+ bap_uri: "",
299
+ },
300
+ };
301
+ const runnerWithEmptyBap = new MockRunner_1.MockRunner(configWithEmptyBap, true);
302
+ const context = runnerWithEmptyBap.generateContext("search_1", "search");
303
+ expect(context.bap_id).toBe("");
304
+ expect(context.bap_uri).toBe("");
305
+ });
306
+ it("should handle undefined bap_id and bap_uri", () => {
307
+ const configWithUndefinedBap = {
308
+ ...mockConfig,
309
+ transaction_data: {
310
+ ...mockConfig.transaction_data,
311
+ bap_id: undefined,
312
+ bap_uri: undefined,
313
+ },
314
+ };
315
+ const runnerWithUndefinedBap = new MockRunner_1.MockRunner(configWithUndefinedBap, true);
316
+ const context = runnerWithUndefinedBap.generateContext("search_1", "search");
317
+ expect(context.bap_id).toBe("");
318
+ expect(context.bap_uri).toBe("");
319
+ });
320
+ });
321
+ describe("Complex Scenarios", () => {
322
+ it("should handle multi-step flow with proper message_id chaining", () => {
323
+ const complexConfig = {
324
+ ...mockConfig,
325
+ steps: [
326
+ ...mockConfig.steps,
327
+ {
328
+ api: "on_select",
329
+ action_id: "on_select_1",
330
+ owner: "BPP",
331
+ responseFor: "select_1",
332
+ unsolicited: false,
333
+ description: "On select response",
334
+ mock: {
335
+ generate: "base64",
336
+ validate: "base64",
337
+ requirements: "base64",
338
+ defaultPayload: { context: {}, message: {} },
339
+ saveData: {},
340
+ inputs: {},
341
+ },
342
+ },
343
+ ],
344
+ transaction_history: [
345
+ ...mockConfig.transaction_history,
346
+ {
347
+ action_id: "select_1",
348
+ payload: {
349
+ context: {
350
+ message_id: "select-msg-456",
351
+ timestamp: "2025-11-11T11:00:00.000Z",
352
+ },
353
+ message: {},
354
+ },
355
+ saved_info: {},
356
+ },
357
+ ],
358
+ };
359
+ const complexRunner = new MockRunner_1.MockRunner(complexConfig, true);
360
+ const context = complexRunner.generateContext("on_select_1", "on_select");
361
+ expect(context.message_id).toBe("select-msg-456");
362
+ });
363
+ it("should generate unique timestamps for multiple calls", (done) => {
364
+ const context1 = mockRunner.generateContext("search_1", "search");
365
+ setTimeout(() => {
366
+ const context2 = mockRunner.generateContext("search_1", "search");
367
+ expect(context1.timestamp).not.toBe(context2.timestamp);
368
+ expect(new Date(context2.timestamp).getTime()).toBeGreaterThan(new Date(context1.timestamp).getTime());
369
+ done();
370
+ }, 1);
371
+ });
372
+ it("should handle mixed version formats", () => {
373
+ const configs = [
374
+ { version: "2.0.0", expected: "version" },
375
+ { version: "1.0.0", expected: "core_version" },
376
+ { version: "1.5.2", expected: "core_version" },
377
+ { version: "2.1.0", expected: "version" },
378
+ { version: "0.9.0", expected: "version" },
379
+ ];
380
+ configs.forEach(({ version, expected }) => {
381
+ const testConfig = {
382
+ ...mockConfig,
383
+ meta: { ...mockConfig.meta, version },
384
+ };
385
+ const testRunner = new MockRunner_1.MockRunner(testConfig, true);
386
+ const context = testRunner.generateContext("search_1", "search");
387
+ if (expected === "core_version") {
388
+ expect(context).toHaveProperty("core_version", version);
389
+ expect(context).toHaveProperty("country", "IND");
390
+ expect(context).not.toHaveProperty("version");
391
+ expect(context).not.toHaveProperty("location");
392
+ }
393
+ else {
394
+ expect(context).toHaveProperty("version", version);
395
+ expect(context).toHaveProperty("location");
396
+ expect(context).not.toHaveProperty("core_version");
397
+ expect(context).not.toHaveProperty("country");
398
+ }
399
+ });
400
+ });
401
+ });
402
+ describe("Error Handling and Edge Cases", () => {
403
+ it("should handle malformed transaction history", () => {
404
+ const configWithMalformedHistory = {
405
+ ...mockConfig,
406
+ transaction_history: [
407
+ {
408
+ action_id: "search_1",
409
+ payload: null, // Malformed payload
410
+ saved_info: {},
411
+ },
412
+ ],
413
+ };
414
+ const runnerWithMalformedHistory = new MockRunner_1.MockRunner(configWithMalformedHistory, true);
415
+ // This will throw because the function tries to access payload.context on null
416
+ expect(() => {
417
+ runnerWithMalformedHistory.generateContext("on_search_1", "on_search");
418
+ }).not.toThrow("Cannot read properties of null (reading 'context')");
419
+ });
420
+ it("should handle very long action names", () => {
421
+ const longActionId = "a".repeat(1000);
422
+ const context = mockRunner.generateContext(longActionId, "search");
423
+ expect(context.action).toBe("search");
424
+ expect(context.domain).toBe("ONDC:TRV14");
425
+ });
426
+ it("should handle special characters in action names", () => {
427
+ const specialActionId = "test-action_with.special@chars#123";
428
+ const context = mockRunner.generateContext(specialActionId, "special/action");
429
+ expect(context.action).toBe("special/action");
430
+ expect(context.message_id).toBe("test-uuid-1234");
431
+ });
432
+ it("should handle sessionData with proper array format", () => {
433
+ const sessionData = {
434
+ transaction_id: ["custom-txn-id"],
435
+ latestMessage_id: ["custom-msg-id"],
436
+ };
437
+ const context = mockRunner.generateContext("on_search_1", "on_search", sessionData);
438
+ expect(context.transaction_id).toBe("custom-txn-id");
439
+ expect(context.message_id).toBe("custom-msg-id");
440
+ });
441
+ it("should handle sessionData with undefined arrays", () => {
442
+ const sessionData = {
443
+ transaction_id: undefined,
444
+ latestMessage_id: undefined,
445
+ };
446
+ const context = mockRunner.generateContext("on_search_1", "on_search", sessionData);
447
+ expect(context.transaction_id).toBe("txn-123"); // Falls back to config
448
+ expect(context.message_id).toBe("search-msg-123"); // Falls back to transaction history
449
+ });
450
+ it("should handle properly formatted transaction history with no message_id", () => {
451
+ const configWithValidHistoryNoMsgId = {
452
+ ...mockConfig,
453
+ transaction_history: [
454
+ {
455
+ action_id: "search_1",
456
+ payload: {
457
+ context: {
458
+ // No message_id field
459
+ timestamp: "2025-11-11T10:00:00.000Z",
460
+ },
461
+ message: {},
462
+ },
463
+ saved_info: {},
464
+ },
465
+ ],
466
+ };
467
+ const runnerWithValidHistory = new MockRunner_1.MockRunner(configWithValidHistoryNoMsgId, true);
468
+ const context = runnerWithValidHistory.generateContext("on_search_1", "on_search");
469
+ expect(context.message_id).toBe("test-uuid-1234"); // Should generate new UUID
470
+ });
471
+ });
472
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ondc/automation-mock-runner",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "A TypeScript library for ONDC automation mock runner",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",