testchimp-runner-core 0.0.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.
Files changed (74) hide show
  1. package/dist/auth-config.d.ts +33 -0
  2. package/dist/auth-config.d.ts.map +1 -0
  3. package/dist/auth-config.js +69 -0
  4. package/dist/auth-config.js.map +1 -0
  5. package/dist/env-loader.d.ts +20 -0
  6. package/dist/env-loader.d.ts.map +1 -0
  7. package/dist/env-loader.js +83 -0
  8. package/dist/env-loader.js.map +1 -0
  9. package/dist/execution-service.d.ts +61 -0
  10. package/dist/execution-service.d.ts.map +1 -0
  11. package/dist/execution-service.js +822 -0
  12. package/dist/execution-service.js.map +1 -0
  13. package/dist/file-handler.d.ts +59 -0
  14. package/dist/file-handler.d.ts.map +1 -0
  15. package/dist/file-handler.js +75 -0
  16. package/dist/file-handler.js.map +1 -0
  17. package/dist/index.d.ts +46 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +196 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/llm-facade.d.ts +101 -0
  22. package/dist/llm-facade.d.ts.map +1 -0
  23. package/dist/llm-facade.js +289 -0
  24. package/dist/llm-facade.js.map +1 -0
  25. package/dist/playwright-mcp-service.d.ts +42 -0
  26. package/dist/playwright-mcp-service.d.ts.map +1 -0
  27. package/dist/playwright-mcp-service.js +167 -0
  28. package/dist/playwright-mcp-service.js.map +1 -0
  29. package/dist/prompts.d.ts +34 -0
  30. package/dist/prompts.d.ts.map +1 -0
  31. package/dist/prompts.js +237 -0
  32. package/dist/prompts.js.map +1 -0
  33. package/dist/scenario-service.d.ts +25 -0
  34. package/dist/scenario-service.d.ts.map +1 -0
  35. package/dist/scenario-service.js +119 -0
  36. package/dist/scenario-service.js.map +1 -0
  37. package/dist/scenario-worker-class.d.ts +30 -0
  38. package/dist/scenario-worker-class.d.ts.map +1 -0
  39. package/dist/scenario-worker-class.js +263 -0
  40. package/dist/scenario-worker-class.js.map +1 -0
  41. package/dist/script-utils.d.ts +44 -0
  42. package/dist/script-utils.d.ts.map +1 -0
  43. package/dist/script-utils.js +100 -0
  44. package/dist/script-utils.js.map +1 -0
  45. package/dist/types.d.ts +171 -0
  46. package/dist/types.d.ts.map +1 -0
  47. package/dist/types.js +28 -0
  48. package/dist/types.js.map +1 -0
  49. package/dist/utils/browser-utils.d.ts +13 -0
  50. package/dist/utils/browser-utils.d.ts.map +1 -0
  51. package/dist/utils/browser-utils.js +269 -0
  52. package/dist/utils/browser-utils.js.map +1 -0
  53. package/dist/utils/page-info-utils.d.ts +16 -0
  54. package/dist/utils/page-info-utils.d.ts.map +1 -0
  55. package/dist/utils/page-info-utils.js +77 -0
  56. package/dist/utils/page-info-utils.js.map +1 -0
  57. package/env.prod +1 -0
  58. package/env.staging +1 -0
  59. package/package.json +38 -0
  60. package/src/auth-config.ts +84 -0
  61. package/src/env-loader.ts +91 -0
  62. package/src/execution-service.ts +999 -0
  63. package/src/file-handler.ts +104 -0
  64. package/src/index.ts +205 -0
  65. package/src/llm-facade.ts +413 -0
  66. package/src/playwright-mcp-service.ts +203 -0
  67. package/src/prompts.ts +247 -0
  68. package/src/scenario-service.ts +138 -0
  69. package/src/scenario-worker-class.ts +330 -0
  70. package/src/script-utils.ts +109 -0
  71. package/src/types.ts +202 -0
  72. package/src/utils/browser-utils.ts +272 -0
  73. package/src/utils/page-info-utils.ts +93 -0
  74. package/tsconfig.json +19 -0
@@ -0,0 +1,822 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ExecutionService = void 0;
4
+ const playwright_mcp_service_1 = require("./playwright-mcp-service");
5
+ const types_1 = require("./types");
6
+ const test_1 = require("@playwright/test");
7
+ const page_info_utils_1 = require("./utils/page-info-utils");
8
+ const browser_utils_1 = require("./utils/browser-utils");
9
+ const llm_facade_1 = require("./llm-facade");
10
+ const script_utils_1 = require("./script-utils");
11
+ /**
12
+ * Service for orchestrating Playwright script execution
13
+ */
14
+ class ExecutionService {
15
+ constructor(authConfig) {
16
+ this.playwrightService = new playwright_mcp_service_1.PlaywrightMCPService();
17
+ this.llmFacade = new llm_facade_1.LLMFacade(authConfig);
18
+ }
19
+ /**
20
+ * Initialize the execution service
21
+ */
22
+ async initialize() {
23
+ await this.playwrightService.initialize();
24
+ }
25
+ /**
26
+ * Execute a script with optional AI repair capabilities
27
+ */
28
+ async executeScript(request) {
29
+ const startTime = Date.now();
30
+ const model = request.model || 'gpt-4.1-mini';
31
+ try {
32
+ if (request.mode === types_1.ExecutionMode.RUN_EXACTLY) {
33
+ return await this.runExactly(request, startTime);
34
+ }
35
+ else {
36
+ return await this.runWithAIRepair(request, startTime, model);
37
+ }
38
+ }
39
+ catch (error) {
40
+ return {
41
+ run_status: 'failed',
42
+ executionTime: Date.now() - startTime,
43
+ error: error instanceof Error ? error.message : 'Unknown error'
44
+ };
45
+ }
46
+ }
47
+ /**
48
+ * Execute a complete Playwright test suite as a single job
49
+ */
50
+ async executeTestSuite(request) {
51
+ try {
52
+ // Parse Playwright configuration
53
+ const config = this.parsePlaywrightConfig(request.playwrightConfig);
54
+ // Execute the entire job (prescript + script + postscript) as one unit
55
+ const jobResult = await this.playwrightService.executeJob(request.prescript, request.script, request.postscript, config);
56
+ return {
57
+ success: jobResult.success,
58
+ results: jobResult.results,
59
+ executionTime: jobResult.executionTime,
60
+ error: jobResult.error
61
+ };
62
+ }
63
+ catch (error) {
64
+ return {
65
+ success: false,
66
+ results: {
67
+ script: { success: false, output: '', error: '', executionTime: 0 }
68
+ },
69
+ executionTime: 0,
70
+ error: error instanceof Error ? error.message : 'Unknown error occurred'
71
+ };
72
+ }
73
+ }
74
+ /**
75
+ * Parse Playwright configuration from string
76
+ */
77
+ parsePlaywrightConfig(configString) {
78
+ try {
79
+ // Try to parse as JSON first
80
+ const config = JSON.parse(configString);
81
+ return {
82
+ browserType: config.browserType || 'chromium',
83
+ headless: config.headless === true,
84
+ viewport: config.viewport || { width: 1280, height: 720 },
85
+ options: config.options || {}
86
+ };
87
+ }
88
+ catch {
89
+ // If not JSON, try to extract basic config from JavaScript
90
+ try {
91
+ // Simple regex-based extraction for common config patterns
92
+ const headlessMatch = configString.match(/headless:\s*(true|false)/);
93
+ const viewportMatch = configString.match(/viewport:\s*\{\s*width:\s*(\d+),\s*height:\s*(\d+)\s*\}/);
94
+ const browserMatch = configString.match(/browserType:\s*['"`](chromium|firefox|webkit)['"`]/);
95
+ return {
96
+ browserType: browserMatch ? browserMatch[1] : 'chromium',
97
+ headless: headlessMatch ? headlessMatch[1] === 'true' : true,
98
+ viewport: viewportMatch ?
99
+ { width: parseInt(viewportMatch[1]), height: parseInt(viewportMatch[2]) } :
100
+ { width: 1280, height: 720 },
101
+ options: {}
102
+ };
103
+ }
104
+ catch {
105
+ // Return default config if parsing fails
106
+ return {
107
+ browserType: 'chromium',
108
+ headless: false,
109
+ viewport: { width: 1280, height: 720 },
110
+ options: {}
111
+ };
112
+ }
113
+ }
114
+ }
115
+ /**
116
+ * Close the execution service
117
+ */
118
+ async close() {
119
+ await this.playwrightService.close();
120
+ }
121
+ /**
122
+ * Check if the service is ready
123
+ */
124
+ isReady() {
125
+ return this.playwrightService.isReady();
126
+ }
127
+ async runExactly(request, startTime, model) {
128
+ const deflakeRunCount = request.deflake_run_count !== undefined ? request.deflake_run_count : 1;
129
+ const totalAttempts = deflakeRunCount + 1; // Original run + deflake attempts
130
+ let lastError = null;
131
+ console.log(`runExactly: deflake_run_count = ${request.deflake_run_count}, totalAttempts = ${totalAttempts}`);
132
+ // Script content should be provided by the caller (TestChimpService)
133
+ // The TestChimpService handles file reading through the appropriate FileHandler
134
+ if (!request.script) {
135
+ throw new Error('Script content is required for execution. The TestChimpService should read the file and provide script content.');
136
+ }
137
+ for (let attempt = 1; attempt <= totalAttempts; attempt++) {
138
+ console.log(`Attempting deflake run ${attempt}/${totalAttempts}`);
139
+ const { browser, context, page } = await this.initializeBrowser(request.playwrightConfig, request.headless, request.playwrightConfigFilePath);
140
+ try {
141
+ // Execute the script as-is
142
+ await this.executeScriptContent(request.script, page);
143
+ await browser.close();
144
+ // Success! Return immediately
145
+ return {
146
+ run_status: 'success',
147
+ num_deflake_runs: attempt - 1, // Count only deflaking runs (exclude original run)
148
+ executionTime: Date.now() - startTime
149
+ };
150
+ }
151
+ catch (error) {
152
+ lastError = error instanceof Error ? error : new Error('Script execution failed');
153
+ console.log(`Initial run failed: ${lastError.message}`);
154
+ try {
155
+ await browser.close();
156
+ }
157
+ catch (closeError) {
158
+ // Browser might already be closed
159
+ }
160
+ // If this is not the last attempt, continue to next attempt
161
+ if (attempt < totalAttempts) {
162
+ console.log(`Deflaking attempt ${attempt} failed, trying again... (${attempt + 1}/${totalAttempts})`);
163
+ continue;
164
+ }
165
+ }
166
+ }
167
+ // All attempts failed
168
+ return {
169
+ run_status: 'failed',
170
+ num_deflake_runs: deflakeRunCount, // Count only deflaking runs (exclude original run)
171
+ executionTime: Date.now() - startTime,
172
+ error: lastError?.message || 'All deflaking attempts failed'
173
+ };
174
+ }
175
+ async runWithAIRepair(request, startTime, model) {
176
+ const repairFlexibility = request.repair_flexibility || 3;
177
+ // Script content should be provided by the caller (TestChimpService)
178
+ // The TestChimpService handles file reading through the appropriate FileHandler
179
+ if (!request.script) {
180
+ throw new Error('Script content is required for AI repair. The TestChimpService should read the file and provide script content.');
181
+ }
182
+ // First, try runExactly (which includes deflaking if configured)
183
+ console.log('Attempting runExactly first (with deflaking if configured)...');
184
+ const runExactlyResult = await this.runExactly(request, startTime, model);
185
+ // If runExactly succeeded, return that result
186
+ if (runExactlyResult.run_status === 'success') {
187
+ return runExactlyResult;
188
+ }
189
+ // runExactly failed, start AI repair
190
+ console.log('runExactly failed, starting AI repair process...');
191
+ try {
192
+ // Start browser initialization and script parsing in parallel for faster startup
193
+ console.log('Initializing repair browser and parsing script...');
194
+ const [steps, { browser: repairBrowser, context: repairContext, page: repairPage }] = await Promise.all([
195
+ this.parseScriptIntoSteps(request.script, model),
196
+ this.initializeBrowser(request.playwrightConfig, request.headless, request.playwrightConfigFilePath) // Use request.headless (defaults to false/headed)
197
+ ]);
198
+ console.log('Starting AI repair with parsed steps...');
199
+ const updatedSteps = await this.repairStepsWithAI(steps, repairPage, repairFlexibility, model);
200
+ // Always generate the updated script
201
+ const updatedScript = this.generateUpdatedScript(updatedSteps);
202
+ // Check if repair was successful by seeing if we completed all steps
203
+ const allStepsSuccessful = updatedSteps.length > 0 && updatedSteps.every(step => step.success);
204
+ // Check if we have any successful repairs (partial success)
205
+ const hasSuccessfulRepairs = updatedSteps.some(step => step.success);
206
+ // Debug: Log step success status
207
+ console.log('Step success status:', updatedSteps.map((step, index) => `Step ${index + 1}: ${step.success ? 'SUCCESS' : 'FAILED'}`));
208
+ console.log('All steps successful:', allStepsSuccessful);
209
+ console.log('Has successful repairs:', hasSuccessfulRepairs);
210
+ // Debug: Log individual step details
211
+ updatedSteps.forEach((step, index) => {
212
+ console.log(`Step ${index + 1} details: success=${step.success}, description="${step.description}"`);
213
+ });
214
+ // Update file if we have any successful repairs (partial or complete)
215
+ if (hasSuccessfulRepairs) {
216
+ const confidenceResponse = await this.llmFacade.assessRepairConfidence(request.script, updatedScript, model);
217
+ const finalScript = await this.llmFacade.generateFinalScript(request.script, updatedScript, confidenceResponse.advice, model);
218
+ // Ensure the final script has the correct TestChimp comment format with repair advice
219
+ const scriptWithRepairAdvice = (0, script_utils_1.addTestChimpComment)(finalScript, confidenceResponse.advice);
220
+ await repairBrowser.close();
221
+ return {
222
+ run_status: 'failed', // Original script failed
223
+ repair_status: allStepsSuccessful ? 'success' : 'partial', // Complete or partial repair success
224
+ repair_confidence: confidenceResponse.confidence,
225
+ repair_advice: confidenceResponse.advice,
226
+ updated_script: scriptWithRepairAdvice, // Return the drop-in replacement script with proper TestChimp comment
227
+ num_deflake_runs: runExactlyResult.num_deflake_runs, // All deflaking attempts failed
228
+ executionTime: Date.now() - startTime
229
+ };
230
+ }
231
+ else {
232
+ // No successful repairs at all
233
+ await repairBrowser.close();
234
+ return {
235
+ run_status: 'failed', // Original script failed
236
+ repair_status: 'failed',
237
+ repair_confidence: 0,
238
+ repair_advice: 'AI repair could not fix any steps',
239
+ updated_script: request.script, // Return original script since no repairs were successful
240
+ num_deflake_runs: runExactlyResult.num_deflake_runs, // All deflaking attempts failed
241
+ executionTime: Date.now() - startTime,
242
+ error: 'AI repair could not fix any steps'
243
+ };
244
+ }
245
+ }
246
+ catch (error) {
247
+ return {
248
+ run_status: 'failed',
249
+ repair_status: 'failed',
250
+ num_deflake_runs: runExactlyResult.num_deflake_runs,
251
+ executionTime: Date.now() - startTime,
252
+ error: error instanceof Error ? error.message : 'Script execution failed'
253
+ };
254
+ }
255
+ }
256
+ async parseScriptIntoSteps(script, model) {
257
+ // First try LLM-based parsing
258
+ try {
259
+ console.log('Attempting LLM-based script parsing...');
260
+ const result = await this.llmFacade.parseScriptIntoSteps(script, model);
261
+ console.log('LLM parsing successful, got', result.length, 'steps');
262
+ return result;
263
+ }
264
+ catch (error) {
265
+ console.log('LLM parsing failed, falling back to code parsing:', error);
266
+ const fallbackResult = this.parseScriptIntoStepsFallback(script);
267
+ console.log('Fallback parsing successful, got', fallbackResult.length, 'steps');
268
+ return fallbackResult;
269
+ }
270
+ }
271
+ parseScriptIntoStepsFallback(script) {
272
+ const lines = script.split('\n');
273
+ const steps = [];
274
+ let currentStep = null;
275
+ let currentCode = [];
276
+ for (const line of lines) {
277
+ const trimmedLine = line.trim();
278
+ // Check for step comment
279
+ if (trimmedLine.startsWith('// Step ')) {
280
+ // Save previous step if exists and has code
281
+ if (currentStep) {
282
+ const code = currentCode.join('\n').trim();
283
+ const cleanedCode = this.cleanStepCode(code);
284
+ if (cleanedCode) {
285
+ currentStep.code = cleanedCode;
286
+ steps.push(currentStep);
287
+ }
288
+ }
289
+ // Start new step
290
+ const description = trimmedLine.replace(/^\/\/\s*Step\s*\d+:\s*/, '').replace(/\s*\[FAILED\]\s*$/, '').trim();
291
+ currentStep = { description, code: '' };
292
+ currentCode = [];
293
+ }
294
+ else if (trimmedLine && !trimmedLine.startsWith('import') && !trimmedLine.startsWith('test(') && !trimmedLine.startsWith('});')) {
295
+ // Add code line to current step
296
+ if (currentStep) {
297
+ currentCode.push(line);
298
+ }
299
+ }
300
+ }
301
+ // Add the last step if it has code
302
+ if (currentStep) {
303
+ const code = currentCode.join('\n').trim();
304
+ const cleanedCode = this.cleanStepCode(code);
305
+ if (cleanedCode) {
306
+ currentStep.code = cleanedCode;
307
+ steps.push(currentStep);
308
+ }
309
+ }
310
+ return steps;
311
+ }
312
+ async repairStepsWithAI(steps, page, repairFlexibility, model) {
313
+ let updatedSteps = [...steps];
314
+ const maxTries = 3;
315
+ const recentRepairs = [];
316
+ // Create a shared execution context that accumulates all executed code for variable tracking
317
+ let executionContext = '';
318
+ const contextVariables = new Map();
319
+ let i = 0;
320
+ while (i < updatedSteps.length) {
321
+ const step = updatedSteps[i];
322
+ console.log(`Loop iteration: i=${i}, step description="${step.description}", total steps=${updatedSteps.length}`);
323
+ try {
324
+ // Try to execute the step directly without context replay
325
+ console.log(`Attempting Step ${i + 1}: ${step.description}`);
326
+ console.log(` Code: ${step.code}`);
327
+ await this.executeStepCode(step.code, page);
328
+ step.success = true;
329
+ console.log(`Step ${i + 1} executed successfully: ${step.description}`);
330
+ console.log(`Step ${i + 1} success status set to: ${step.success}`);
331
+ // Add this step's code to the execution context for future steps (for variable tracking)
332
+ executionContext += step.code + '\n';
333
+ i++; // Move to next step
334
+ }
335
+ catch (error) {
336
+ console.log(`Step ${i + 1} failed: ${step.description}`);
337
+ console.log(` Failed code: ${step.code}`);
338
+ console.log(` Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
339
+ if (error instanceof Error && error.stack) {
340
+ console.log(` Stack trace: ${error.stack}`);
341
+ }
342
+ step.success = false;
343
+ step.error = this.safeSerializeError(error);
344
+ // Try multiple repair attempts
345
+ const repairHistory = [];
346
+ let repairSuccess = false;
347
+ const originalDescription = step.description;
348
+ const originalCode = step.code;
349
+ for (let attempt = 1; attempt <= maxTries; attempt++) {
350
+ console.log(`Step ${i + 1} repair attempt ${attempt}/${maxTries}`);
351
+ // Get current page state for AI repair
352
+ const pageInfo = await this.getEnhancedPageInfo(page);
353
+ // Build failure history for LLM context
354
+ const failureHistory = this.buildFailureHistory(repairHistory, step, error);
355
+ // Build recent repairs context for LLM
356
+ const recentRepairsContext = this.buildRecentRepairsContext(recentRepairs);
357
+ // Ask AI for repair suggestion with failure history and recent repairs
358
+ const repairSuggestion = await this.llmFacade.getRepairSuggestion(step.description, step.code, step.error || 'Unknown error', pageInfo, failureHistory, recentRepairsContext, model);
359
+ if (!repairSuggestion.shouldContinue) {
360
+ console.log(`AI decided to stop repair at attempt ${attempt}: ${repairSuggestion.reason}`);
361
+ break;
362
+ }
363
+ // Apply the repair action
364
+ try {
365
+ // Set the step index and insertAfterIndex on the client side based on current step being processed
366
+ const repairAction = {
367
+ ...repairSuggestion.action,
368
+ stepIndex: i, // Client-side step index management
369
+ insertAfterIndex: repairSuggestion.action.operation === types_1.StepOperation.INSERT ? i - 1 : undefined // For INSERT, insert before current step
370
+ };
371
+ console.log(`🔧 Applying repair action:`, {
372
+ operation: repairAction.operation,
373
+ stepIndex: repairAction.stepIndex,
374
+ insertAfterIndex: repairAction.insertAfterIndex,
375
+ newStepDescription: repairAction.newStep?.description,
376
+ newStepCode: repairAction.newStep?.code
377
+ });
378
+ console.log(`🔧 Steps array before repair:`, updatedSteps.map((s, idx) => `${idx}: "${s.description}" (success: ${s.success})`));
379
+ const result = await this.applyRepairActionInContext(repairAction, updatedSteps, i, page, executionContext, contextVariables);
380
+ if (result.success) {
381
+ repairSuccess = true;
382
+ console.log(`🔧 Steps array after repair:`, updatedSteps.map((s, idx) => `${idx}: "${s.description}" (success: ${s.success})`));
383
+ // Mark the appropriate step(s) as successful based on operation type
384
+ if (repairAction.operation === types_1.StepOperation.MODIFY) {
385
+ // For MODIFY: mark the modified step as successful
386
+ step.success = true;
387
+ step.error = undefined;
388
+ updatedSteps[i].success = true;
389
+ updatedSteps[i].error = undefined;
390
+ console.log(`Step ${i + 1} marked as successful after MODIFY repair`);
391
+ }
392
+ else if (repairAction.operation === types_1.StepOperation.INSERT) {
393
+ // For INSERT: mark the newly inserted step as successful
394
+ const insertIndex = repairAction.insertAfterIndex !== undefined ? repairAction.insertAfterIndex + 1 : i + 1;
395
+ if (updatedSteps[insertIndex]) {
396
+ updatedSteps[insertIndex].success = true;
397
+ updatedSteps[insertIndex].error = undefined;
398
+ }
399
+ }
400
+ else if (repairAction.operation === types_1.StepOperation.REMOVE) {
401
+ // For REMOVE: no step to mark as successful since we removed it
402
+ // The step is already removed from the array
403
+ }
404
+ const commandInfo = repairAction.operation === types_1.StepOperation.MODIFY ?
405
+ `MODIFY: "${repairAction.newStep?.code || 'N/A'}"` :
406
+ repairAction.operation === types_1.StepOperation.INSERT ?
407
+ `INSERT: "${repairAction.newStep?.code || 'N/A'}"` :
408
+ repairAction.operation === types_1.StepOperation.REMOVE ?
409
+ `REMOVE: step at index ${repairAction.stepIndex}` :
410
+ repairAction.operation;
411
+ console.log(`Step ${i + 1} repair action ${commandInfo} executed successfully on attempt ${attempt}`);
412
+ // Update execution context based on the repair action
413
+ if (repairAction.operation === types_1.StepOperation.MODIFY && repairAction.newStep) {
414
+ // Update the step in the execution context for variable tracking
415
+ executionContext = executionContext.replace(originalCode, repairAction.newStep.code);
416
+ }
417
+ else if (repairAction.operation === types_1.StepOperation.INSERT && repairAction.newStep) {
418
+ // Insert the new step code into execution context for variable tracking
419
+ executionContext += repairAction.newStep.code + '\n';
420
+ }
421
+ else if (repairAction.operation === types_1.StepOperation.REMOVE) {
422
+ // Remove the step code from execution context for variable tracking
423
+ executionContext = executionContext.replace(originalCode, '');
424
+ }
425
+ // Record this successful repair
426
+ recentRepairs.push({
427
+ stepNumber: i + 1,
428
+ operation: repairAction.operation,
429
+ originalDescription: repairAction.operation === types_1.StepOperation.REMOVE ? originalDescription : undefined,
430
+ newDescription: repairAction.newStep?.description,
431
+ originalCode: repairAction.operation === types_1.StepOperation.REMOVE ? originalCode : undefined,
432
+ newCode: repairAction.newStep?.code
433
+ });
434
+ // Keep only the last 3 repairs for context
435
+ if (recentRepairs.length > 3) {
436
+ recentRepairs.shift();
437
+ }
438
+ // Update step index based on operation
439
+ if (repairAction.operation === types_1.StepOperation.INSERT) {
440
+ // For INSERT: inserted step is already executed
441
+ console.log(`INSERT operation: current i=${i}, insertAfterIndex=${repairAction.insertAfterIndex}`);
442
+ console.log(`INSERT: Steps array length before: ${updatedSteps.length}`);
443
+ console.log(`INSERT: Steps before operation:`, updatedSteps.map((s, idx) => `${idx}: "${s.description}" (success: ${s.success})`));
444
+ if (repairAction.insertAfterIndex !== undefined && repairAction.insertAfterIndex < i) {
445
+ // If inserting before current position, current step moved down by 1
446
+ console.log(`INSERT before current position: incrementing i from ${i} to ${i + 1}`);
447
+ i++; // Move to the original step that was pushed to the next position
448
+ }
449
+ else {
450
+ // If inserting at or after current position, stay at current step
451
+ console.log(`INSERT at/after current position: keeping i at ${i}`);
452
+ }
453
+ console.log(`INSERT: Steps array length after: ${updatedSteps.length}`);
454
+ console.log(`INSERT: Steps after operation:`, updatedSteps.map((s, idx) => `${idx}: "${s.description}" (success: ${s.success})`));
455
+ }
456
+ else if (repairAction.operation === types_1.StepOperation.REMOVE) {
457
+ // For REMOVE: stay at same index since the next step moved to current position
458
+ // Don't increment i because the array shifted left
459
+ }
460
+ else {
461
+ // For MODIFY: move to next step since modified step was executed
462
+ i++; // Move to next step for MODIFY
463
+ }
464
+ // Add the repaired step's code to execution context for variable tracking
465
+ executionContext += step.code + '\n';
466
+ break;
467
+ }
468
+ else {
469
+ throw new Error(result.error || 'Repair action failed');
470
+ }
471
+ }
472
+ catch (repairError) {
473
+ const repairErrorMessage = repairError instanceof Error ? repairError.message : 'Repair failed';
474
+ const commandInfo = repairSuggestion.action.operation === types_1.StepOperation.MODIFY ?
475
+ `MODIFY: "${repairSuggestion.action.newStep?.code || 'N/A'}"` :
476
+ repairSuggestion.action.operation === types_1.StepOperation.INSERT ?
477
+ `INSERT: "${repairSuggestion.action.newStep?.code || 'N/A'}"` :
478
+ repairSuggestion.action.operation === types_1.StepOperation.REMOVE ?
479
+ `REMOVE: step at index ${repairSuggestion.action.stepIndex}` :
480
+ repairSuggestion.action.operation;
481
+ console.log(`Step ${i + 1} repair attempt ${attempt} failed (${commandInfo}): ${repairErrorMessage}`);
482
+ if (repairError instanceof Error && repairError.stack) {
483
+ console.log(` Repair stack trace: ${repairError.stack}`);
484
+ }
485
+ // Record this attempt in history
486
+ repairHistory.push({
487
+ attempt,
488
+ action: repairSuggestion.action,
489
+ error: repairErrorMessage,
490
+ pageInfo
491
+ });
492
+ step.error = repairErrorMessage;
493
+ }
494
+ }
495
+ if (!repairSuccess) {
496
+ console.log(`Step ${i + 1} failed after ${maxTries} repair attempts`);
497
+ break;
498
+ }
499
+ }
500
+ }
501
+ return updatedSteps;
502
+ }
503
+ async executeStepCode(code, page) {
504
+ // Set timeout for individual step execution
505
+ page.setDefaultTimeout(5000); // 5 seconds for individual commands
506
+ try {
507
+ // Clean and validate the code before execution
508
+ const cleanedCode = this.cleanStepCode(code);
509
+ if (!cleanedCode || cleanedCode.trim().length === 0) {
510
+ throw new Error('Step code is empty or contains only comments');
511
+ }
512
+ // Create an async function that has access to page, expect, and other Playwright globals
513
+ const executeCode = new Function('page', 'expect', `return (async () => { ${cleanedCode} })()`);
514
+ const result = executeCode(page, test_1.expect);
515
+ await result;
516
+ }
517
+ finally {
518
+ // Restore to reasonable default timeout
519
+ page.setDefaultTimeout(10000);
520
+ }
521
+ }
522
+ /**
523
+ * Validate step code has executable content (preserves comments)
524
+ */
525
+ cleanStepCode(code) {
526
+ if (!code || code.trim().length === 0) {
527
+ return '';
528
+ }
529
+ // Check if there are any executable statements (including those with comments)
530
+ const hasExecutableCode = /[a-zA-Z_$][a-zA-Z0-9_$]*\s*\(|await\s+|return\s+|if\s*\(|for\s*\(|while\s*\(|switch\s*\(|try\s*\{|catch\s*\(/.test(code);
531
+ if (!hasExecutableCode) {
532
+ return '';
533
+ }
534
+ return code; // Return the original code without removing comments
535
+ }
536
+ async executeStepInContext(code, page, executionContext, contextVariables) {
537
+ // Set timeout for individual step execution
538
+ page.setDefaultTimeout(5000); // 5 seconds for individual commands
539
+ try {
540
+ // Execute only the current step code, but make context variables available
541
+ const fullCode = code;
542
+ // Create a function that has access to page, expect, and the context variables
543
+ const executeCode = new Function('page', 'expect', 'contextVariables', `return (async () => {
544
+ // Make context variables available in the execution scope
545
+ for (const [key, value] of contextVariables) {
546
+ globalThis[key] = value;
547
+ }
548
+
549
+ ${fullCode}
550
+
551
+ // Capture any new variables that might have been created
552
+ const newVars = {};
553
+ for (const key in globalThis) {
554
+ if (!contextVariables.has(key) && typeof globalThis[key] !== 'function' && key !== 'page' && key !== 'expect') {
555
+ newVars[key] = globalThis[key];
556
+ }
557
+ }
558
+ return newVars;
559
+ })()`);
560
+ const newVars = await executeCode(page, test_1.expect, contextVariables);
561
+ // Update the context variables with any new variables created
562
+ for (const [key, value] of Object.entries(newVars)) {
563
+ contextVariables.set(key, value);
564
+ }
565
+ }
566
+ finally {
567
+ // Restore to reasonable default timeout
568
+ page.setDefaultTimeout(5000);
569
+ }
570
+ }
571
+ async executeScriptContent(script, page) {
572
+ // Extract the test function content
573
+ const testMatch = script.match(/test\([^,]+,\s*async\s*\(\s*\{\s*page[^}]*\}\s*\)\s*=>\s*\{([\s\S]*)\}\s*\);/);
574
+ if (!testMatch) {
575
+ throw new Error('Could not extract test function from script');
576
+ }
577
+ const testBody = testMatch[1];
578
+ // Execute the entire test body as one async function
579
+ const executeTest = new Function('page', 'expect', `return (async () => { ${testBody} })()`);
580
+ await executeTest(page, test_1.expect);
581
+ }
582
+ async getEnhancedPageInfo(page) {
583
+ try {
584
+ return await (0, page_info_utils_1.getEnhancedPageInfo)(page);
585
+ }
586
+ catch (error) {
587
+ return {
588
+ url: page.url(),
589
+ title: 'Unknown',
590
+ elements: 'Unable to extract',
591
+ formFields: 'Unable to extract',
592
+ interactiveElements: 'Unable to extract',
593
+ pageStructure: 'Unable to extract'
594
+ };
595
+ }
596
+ }
597
+ buildFailureHistory(repairHistory, originalStep, originalError) {
598
+ if (repairHistory.length === 0) {
599
+ return `Original failure: ${this.safeSerializeError(originalError)}`;
600
+ }
601
+ let history = `Original failure: ${this.safeSerializeError(originalError)}\n\n`;
602
+ history += `Previous repair attempts:\n`;
603
+ repairHistory.forEach((attempt, index) => {
604
+ history += `Attempt ${attempt.attempt}:\n`;
605
+ history += ` Operation: ${attempt.action.operation}\n`;
606
+ if (attempt.action.newStep) {
607
+ history += ` Description: ${attempt.action.newStep.description}\n`;
608
+ history += ` Code: ${attempt.action.newStep.code}\n`;
609
+ }
610
+ history += ` Error: ${attempt.error}\n`;
611
+ if (index < repairHistory.length - 1) {
612
+ history += `\n`;
613
+ }
614
+ });
615
+ return history;
616
+ }
617
+ buildRecentRepairsContext(recentRepairs) {
618
+ if (recentRepairs.length === 0) {
619
+ return 'No recent repairs to consider.';
620
+ }
621
+ let context = 'Recent successful repairs that may affect this step:\n\n';
622
+ recentRepairs.forEach((repair, index) => {
623
+ context += `Step ${repair.stepNumber} was successfully repaired:\n`;
624
+ context += ` Operation: ${repair.operation}\n`;
625
+ if (repair.operation === 'REMOVE') {
626
+ context += ` Removed: "${repair.originalDescription}"\n`;
627
+ context += ` Code removed:\n ${repair.originalCode?.replace(/\n/g, '\n ')}\n`;
628
+ }
629
+ else if (repair.operation === 'INSERT') {
630
+ context += ` Inserted: "${repair.newDescription}"\n`;
631
+ context += ` Code inserted:\n ${repair.newCode?.replace(/\n/g, '\n ')}\n`;
632
+ }
633
+ else {
634
+ context += ` Original: "${repair.originalDescription}"\n`;
635
+ context += ` Repaired: "${repair.newDescription}"\n`;
636
+ context += ` Code changed from:\n ${repair.originalCode?.replace(/\n/g, '\n ')}\n`;
637
+ context += ` To:\n ${repair.newCode?.replace(/\n/g, '\n ')}\n`;
638
+ }
639
+ if (index < recentRepairs.length - 1) {
640
+ context += `\n`;
641
+ }
642
+ });
643
+ context += '\nConsider how these changes might affect the current step and adjust accordingly.';
644
+ return context;
645
+ }
646
+ async applyRepairActionInContext(action, steps, currentIndex, page, executionContext, contextVariables) {
647
+ try {
648
+ switch (action.operation) {
649
+ case types_1.StepOperation.MODIFY:
650
+ if (action.newStep && action.stepIndex !== undefined) {
651
+ // Modify existing step
652
+ steps[action.stepIndex] = {
653
+ ...action.newStep,
654
+ success: false,
655
+ error: undefined
656
+ };
657
+ // Test the modified step with current page state and variables
658
+ await this.executeStepCode(action.newStep.code, page);
659
+ return { success: true, updatedContext: executionContext + action.newStep.code };
660
+ }
661
+ break;
662
+ case types_1.StepOperation.INSERT:
663
+ if (action.newStep && action.insertAfterIndex !== undefined) {
664
+ // Insert new step after specified index
665
+ const insertIndex = action.insertAfterIndex + 1;
666
+ const newStep = {
667
+ ...action.newStep,
668
+ success: false,
669
+ error: undefined
670
+ };
671
+ console.log(`INSERT: Inserting step at index ${insertIndex} with description "${newStep.description}"`);
672
+ console.log(`INSERT: Steps before insertion:`, steps.map((s, i) => `${i}: "${s.description}" (success: ${s.success})`));
673
+ // Preserve success status of existing steps before insertion
674
+ const successStatusMap = new Map(steps.map((step, index) => [index, { success: step.success, error: step.error }]));
675
+ steps.splice(insertIndex, 0, newStep);
676
+ // Restore success status for steps that were shifted by the insertion
677
+ // Steps at insertIndex and before keep their original status
678
+ // Steps after insertIndex need to be shifted to their new positions
679
+ for (let i = insertIndex + 1; i < steps.length; i++) {
680
+ const originalIndex = i - 1; // The step that was originally at this position
681
+ if (successStatusMap.has(originalIndex)) {
682
+ const status = successStatusMap.get(originalIndex);
683
+ steps[i].success = status.success;
684
+ steps[i].error = status.error;
685
+ }
686
+ }
687
+ // CRITICAL FIX: Ensure the inserted step doesn't overwrite existing step data
688
+ // The new step should only have its own description, not inherit from existing steps
689
+ console.log(`INSERT: Final step array after restoration:`, steps.map((s, i) => `${i}: "${s.description}" (success: ${s.success})`));
690
+ console.log(`INSERT: Steps after insertion:`, steps.map((s, i) => `${i}: "${s.description}" (success: ${s.success})`));
691
+ // Test the new step with current page state
692
+ await this.executeStepCode(action.newStep.code, page);
693
+ return { success: true, updatedContext: executionContext + action.newStep.code };
694
+ }
695
+ break;
696
+ case types_1.StepOperation.REMOVE:
697
+ if (action.stepIndex !== undefined) {
698
+ // Remove step
699
+ steps.splice(action.stepIndex, 1);
700
+ return { success: true, updatedContext: executionContext };
701
+ }
702
+ break;
703
+ }
704
+ return { success: false, error: 'Invalid repair action' };
705
+ }
706
+ catch (error) {
707
+ return {
708
+ success: false,
709
+ error: error instanceof Error ? error.message : 'Unknown error during repair action'
710
+ };
711
+ }
712
+ }
713
+ async applyRepairAction(action, steps, currentIndex, page) {
714
+ try {
715
+ switch (action.operation) {
716
+ case types_1.StepOperation.MODIFY:
717
+ if (action.newStep && action.stepIndex !== undefined) {
718
+ // Modify existing step
719
+ steps[action.stepIndex] = {
720
+ ...action.newStep,
721
+ success: false,
722
+ error: undefined
723
+ };
724
+ // Test the modified step
725
+ await this.executeStepCode(action.newStep.code, page);
726
+ return { success: true };
727
+ }
728
+ break;
729
+ case types_1.StepOperation.INSERT:
730
+ if (action.newStep && action.insertAfterIndex !== undefined) {
731
+ // Insert new step after specified index
732
+ const insertIndex = action.insertAfterIndex + 1;
733
+ const newStep = {
734
+ ...action.newStep,
735
+ success: false,
736
+ error: undefined
737
+ };
738
+ steps.splice(insertIndex, 0, newStep);
739
+ // Test the inserted step
740
+ await this.executeStepCode(action.newStep.code, page);
741
+ return { success: true };
742
+ }
743
+ break;
744
+ case types_1.StepOperation.REMOVE:
745
+ if (action.stepIndex !== undefined) {
746
+ // Remove the step
747
+ steps.splice(action.stepIndex, 1);
748
+ return { success: true };
749
+ }
750
+ break;
751
+ }
752
+ return { success: false, error: 'Invalid repair action' };
753
+ }
754
+ catch (error) {
755
+ return {
756
+ success: false,
757
+ error: error instanceof Error ? error.message : 'Repair action execution failed'
758
+ };
759
+ }
760
+ }
761
+ generateUpdatedScript(steps, repairAdvice) {
762
+ const scriptLines = [
763
+ "import { test, expect } from '@playwright/test';",
764
+ `test('repairedTest', async ({ page, browser, context }) => {`
765
+ ];
766
+ steps.forEach((step, index) => {
767
+ scriptLines.push(` // Step ${index + 1}: ${step.description}`);
768
+ const codeLines = step.code.split('\n');
769
+ codeLines.forEach(line => {
770
+ scriptLines.push(` ${line}`);
771
+ });
772
+ });
773
+ scriptLines.push('});');
774
+ const script = scriptLines.join('\n');
775
+ // Add TestChimp comment to the repaired script with repair advice
776
+ return (0, script_utils_1.addTestChimpComment)(script, repairAdvice);
777
+ }
778
+ /**
779
+ * Initialize browser with configuration (delegates to utility function)
780
+ */
781
+ async initializeBrowser(playwrightConfig, headless, playwrightConfigFilePath) {
782
+ return (0, browser_utils_1.initializeBrowser)(playwrightConfig, headless, playwrightConfigFilePath);
783
+ }
784
+ /**
785
+ * Safely serialize error information, filtering out non-serializable values
786
+ */
787
+ safeSerializeError(error) {
788
+ try {
789
+ if (error instanceof Error) {
790
+ return error.message;
791
+ }
792
+ if (typeof error === 'string') {
793
+ return error;
794
+ }
795
+ if (typeof error === 'object' && error !== null) {
796
+ // Try to extract meaningful information without serializing the entire object
797
+ const safeError = {};
798
+ // Copy safe properties
799
+ if (error.message)
800
+ safeError.message = error.message;
801
+ if (error.name)
802
+ safeError.name = error.name;
803
+ if (error.code)
804
+ safeError.code = error.code;
805
+ if (error.status)
806
+ safeError.status = error.status;
807
+ // Try to get stack trace safely
808
+ if (error.stack && typeof error.stack === 'string') {
809
+ safeError.stack = error.stack;
810
+ }
811
+ return JSON.stringify(safeError);
812
+ }
813
+ return String(error);
814
+ }
815
+ catch (serializationError) {
816
+ // If even safe serialization fails, return a basic string representation
817
+ return `Error: ${String(error)}`;
818
+ }
819
+ }
820
+ }
821
+ exports.ExecutionService = ExecutionService;
822
+ //# sourceMappingURL=execution-service.js.map