@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.
package/dist/lib/MockRunner.d.ts
CHANGED
|
@@ -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
|
-
|
|
8
|
+
logger: Logger;
|
|
8
9
|
constructor(config: MockPlaygroundConfigType, skipValidation?: boolean);
|
|
9
10
|
getRunnerInstance(): BaseCodeRunner;
|
|
10
11
|
validateConfig(): {
|
package/dist/lib/MockRunner.js
CHANGED
|
@@ -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() {
|
|
@@ -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
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
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
|
});
|