@ondc/automation-mock-runner 0.1.2 → 0.2.0

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.
@@ -1,10 +1,11 @@
1
1
  import { BaseCodeRunner } from "./runners/base-runner";
2
2
  import { MockPlaygroundConfigType } from "./types/mock-config";
3
+ import { Logger } from "./utils/logger";
3
4
  import { ExecutionResult } from "./types/execution-results";
4
5
  export declare class MockRunner {
5
6
  private config;
6
7
  private runner;
7
- private logger;
8
+ logger: Logger;
8
9
  constructor(config: MockPlaygroundConfigType, skipValidation?: boolean);
9
10
  getRunnerInstance(): BaseCodeRunner;
10
11
  validateConfig(): {
@@ -37,9 +37,11 @@ class MockRunner {
37
37
  });
38
38
  }
39
39
  getRunnerInstance() {
40
+ this.logger.debug("Getting code runner instance");
40
41
  if (!this.runner) {
41
- this.runner = runner_factory_1.RunnerFactory.createRunner();
42
+ this.runner = runner_factory_1.RunnerFactory.createRunner({}, this.logger);
42
43
  }
44
+ this.logger.debug("Code runner instance obtained successfully: " + this.runner.toString());
43
45
  return this.runner;
44
46
  }
45
47
  validateConfig() {
@@ -21,5 +21,5 @@ export declare class RunnerFactory {
21
21
  /**
22
22
  * Creates appropriate runner based on environment
23
23
  */
24
- static createRunner(options?: any): BrowserRunner | NodeRunner;
24
+ static createRunner(options?: any, logger?: any): BrowserRunner | NodeRunner;
25
25
  }
@@ -99,8 +99,9 @@ class RunnerFactory {
99
99
  /**
100
100
  * Creates appropriate runner based on environment
101
101
  */
102
- static createRunner(options) {
102
+ static createRunner(options, logger) {
103
103
  const env = RunnerFactory.getEnvironment();
104
+ logger?.debug(`Creating runner for environment: ${env}`);
104
105
  if (env === "browser") {
105
106
  return new browser_runner_1.BrowserRunner();
106
107
  }
@@ -79,120 +79,103 @@ describe("BrowserRunner", () => {
79
79
  expect(result.result.valid).toBe(true);
80
80
  });
81
81
  });
82
- // describe("Error Handling", () => {
83
- // it("should handle syntax errors", async () => {
84
- // const invalidCode = `
85
- // function generate(defaultPayload, sessionData) {
86
- // // Missing closing brace and return
87
- // defaultPayload.message = { invalid: syntax
88
- // `;
89
- // const schema = getFunctionSchema("generate");
90
- // const args = [{}, {}];
91
- // const result = await browserRunner.execute(invalidCode, schema, args);
92
- // expect(result.success).toBe(false);
93
- // expect(result.error).toBeDefined();
94
- // expect(result.error!.name).toBe("ValidationError");
95
- // });
96
- // it("should handle runtime errors", async () => {
97
- // const errorCode = `
98
- // async function generate(defaultPayload, sessionData) {
99
- // throw new Error('Test runtime error');
100
- // }
101
- // `;
102
- // const schema = getFunctionSchema("generate");
103
- // const args = [{}, {}];
104
- // const result = await browserRunner.execute(errorCode, schema, args);
105
- // expect(result.success).toBe(false);
106
- // expect(result.error).toBeDefined();
107
- // expect(result.error!.message).toContain("Test runtime error");
108
- // });
109
- // it("should handle function not found error", async () => {
110
- // const wrongFunctionCode = `
111
- // async function wrongName(defaultPayload, sessionData) {
112
- // return defaultPayload;
113
- // }
114
- // `;
115
- // const schema = getFunctionSchema("generate");
116
- // const args = [{}, {}];
117
- // const result = await browserRunner.execute(
118
- // wrongFunctionCode,
119
- // schema,
120
- // args,
121
- // );
122
- // expect(result.success).toBe(false);
123
- // expect(result.error).toBeDefined();
124
- // expect(result.error!.message).toContain("generate is not a function");
125
- // });
126
- // });
127
- // describe("Security Features", () => {
128
- // it("should block dangerous function calls", async () => {
129
- // const dangerousCode = `
130
- // function validate(targetPayload, sessionData) {
131
- // eval('console.log("dangerous code")');
132
- // return { valid: true, code: 200, description: 'Should not reach here' };
133
- // }
134
- // `;
135
- // const schema = getFunctionSchema("validate");
136
- // const args = [{}, {}];
137
- // const result = await browserRunner.execute(dangerousCode, schema, args);
138
- // expect(result.success).toBe(false);
139
- // expect(result.error).toBeDefined();
140
- // expect(result.validation.errors.length).toBeGreaterThan(0);
141
- // });
142
- // it("should block access to forbidden properties", async () => {
143
- // const dangerousCode = `
144
- // function validate(targetPayload, sessionData) {
145
- // localStorage.setItem('test', 'value');
146
- // return { valid: true, code: 200, description: 'Should not reach here' };
147
- // }
148
- // `;
149
- // const schema = getFunctionSchema("validate");
150
- // const args = [{}, {}];
151
- // const result = await browserRunner.execute(dangerousCode, schema, args);
152
- // expect(result.success).toBe(false);
153
- // expect(result.validation.errors.length).toBeGreaterThan(0);
154
- // });
155
- // });
156
- // describe("Console Logging", () => {
157
- // it("should capture console logs", async () => {
158
- // const functionWithLogs = `
159
- // async function generate(defaultPayload, sessionData) {
160
- // console.log('Test log message');
161
- // console.warn('Test warning');
162
- // defaultPayload.message = { logged: true };
163
- // return defaultPayload;
164
- // }
165
- // `;
166
- // const schema = getFunctionSchema("generate");
167
- // const args = [{}, {}];
168
- // const result = await browserRunner.execute(
169
- // functionWithLogs,
170
- // schema,
171
- // args,
172
- // );
173
- // expect(result.success).toBe(true);
174
- // expect(result.logs).toBeDefined();
175
- // expect(result.logs.length).toBeGreaterThan(0);
176
- // });
177
- // });
178
- // describe("Timeout Handling", () => {
179
- // it("should timeout long-running functions", async () => {
180
- // const longRunningCode = `
181
- // async function generate(defaultPayload, sessionData) {
182
- // // Simulate long-running operation
183
- // const start = Date.now();
184
- // while (Date.now() - start < 10000) {
185
- // // Busy wait
186
- // }
187
- // return defaultPayload;
188
- // }
189
- // `;
190
- // const schema = { ...getFunctionSchema("generate"), timeout: 100 };
191
- // const args = [{}, {}];
192
- // const result = await browserRunner.execute(longRunningCode, schema, args);
193
- // expect(result.success).toBe(false);
194
- // expect(result.error).toBeDefined();
195
- // expect(result.error!.name).toBe("TimeoutError");
196
- // }, 1000); // Test timeout
197
- // });
82
+ describe("Error Handling", () => {
83
+ it("should handle syntax errors", async () => {
84
+ const invalidCode = `
85
+ function generate(defaultPayload, sessionData) {
86
+ // Missing closing brace and return
87
+ defaultPayload.message = { invalid: syntax
88
+ `;
89
+ const schema = (0, function_registry_1.getFunctionSchema)("generate");
90
+ const args = [{}, {}];
91
+ const result = await browserRunner.execute(invalidCode, schema, args);
92
+ expect(result.success).toBe(false);
93
+ expect(result.error).toBeDefined();
94
+ expect(result.error.name).toBe("ValidationError");
95
+ });
96
+ it("should handle runtime errors", async () => {
97
+ const errorCode = `
98
+ async function generate(defaultPayload, sessionData) {
99
+ throw new Error('Test runtime error');
100
+ }
101
+ `;
102
+ const schema = (0, function_registry_1.getFunctionSchema)("generate");
103
+ const args = [{}, {}];
104
+ const result = await browserRunner.execute(errorCode, schema, args);
105
+ expect(result.success).toBe(false);
106
+ expect(result.error).toBeDefined();
107
+ expect(result.error.message).toContain("Test runtime error");
108
+ });
109
+ it("should handle function not found error", async () => {
110
+ const wrongFunctionCode = `
111
+ async function wrongName(defaultPayload, sessionData) {
112
+ return defaultPayload;
113
+ }
114
+ `;
115
+ const schema = (0, function_registry_1.getFunctionSchema)("generate");
116
+ const args = [{}, {}];
117
+ const result = await browserRunner.execute(wrongFunctionCode, schema, args);
118
+ expect(result.success).toBe(false);
119
+ expect(result.error).toBeDefined();
120
+ expect(result.error.message).toContain("generate is not defined");
121
+ });
122
+ it("should handle runtime errors in functions", async () => {
123
+ const errorCode = `
124
+ function validate(targetPayload, sessionData) {
125
+ throw new Error('Test runtime error in validate');
126
+ return { valid: true, code: 200, description: 'Should not reach here' };
127
+ }
128
+ `;
129
+ const schema = (0, function_registry_1.getFunctionSchema)("validate");
130
+ const args = [{}, {}];
131
+ const result = await browserRunner.execute(errorCode, schema, args);
132
+ expect(result.success).toBe(false);
133
+ expect(result.error).toBeDefined();
134
+ expect(result.error.message).toContain("Test runtime error in validate");
135
+ });
136
+ });
137
+ describe("Security Features", () => {
138
+ it("should block dangerous function calls", async () => {
139
+ const dangerousCode = `
140
+ function validate(targetPayload, sessionData) {
141
+ eval('console.log("dangerous code")');
142
+ return { valid: true, code: 200, description: 'Should not reach here' };
143
+ }
144
+ `;
145
+ const schema = (0, function_registry_1.getFunctionSchema)("validate");
146
+ const args = [{}, {}];
147
+ const result = await browserRunner.execute(dangerousCode, schema, args);
148
+ expect(result.success).toBe(false);
149
+ expect(result.error).toBeDefined();
150
+ });
151
+ it("should block access to forbidden properties", async () => {
152
+ const dangerousCode = `
153
+ function validate(targetPayload, sessionData) {
154
+ localStorage.setItem('test', 'value');
155
+ return { valid: true, code: 200, description: 'Should not reach here' };
156
+ }
157
+ `;
158
+ const schema = (0, function_registry_1.getFunctionSchema)("validate");
159
+ const args = [{}, {}];
160
+ const result = await browserRunner.execute(dangerousCode, schema, args);
161
+ expect(result.success).toBe(false);
162
+ });
163
+ });
164
+ describe("Console Logging", () => {
165
+ it("should capture console logs", async () => {
166
+ const functionWithLogs = `
167
+ async function generate(defaultPayload, sessionData) {
168
+ console.log('Test log message');
169
+ console.warn('Test warning');
170
+ defaultPayload.message = { logged: true };
171
+ return defaultPayload;
172
+ }
173
+ `;
174
+ const schema = (0, function_registry_1.getFunctionSchema)("generate");
175
+ const args = [{}, {}];
176
+ const result = await browserRunner.execute(functionWithLogs, schema, args);
177
+ expect(result.success).toBe(true);
178
+ expect(result.logs).toBeDefined();
179
+ });
180
+ });
198
181
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ondc/automation-mock-runner",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "A TypeScript library for ONDC automation mock runner",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",