explorbot 0.1.10 → 0.1.12

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 (90) hide show
  1. package/README.md +37 -1
  2. package/bin/explorbot-cli.ts +27 -18
  3. package/dist/bin/explorbot-cli.js +26 -18
  4. package/dist/package.json +3 -3
  5. package/dist/rules/navigator/output.md +9 -0
  6. package/dist/rules/navigator/verification-actions.md +2 -0
  7. package/dist/src/action-result.js +23 -1
  8. package/dist/src/action.js +51 -42
  9. package/dist/src/ai/bosun.js +11 -1
  10. package/dist/src/ai/conversation.js +39 -0
  11. package/dist/src/ai/historian/codeceptjs.js +109 -0
  12. package/dist/src/ai/historian/experience.js +321 -0
  13. package/dist/src/ai/historian/mixin.js +2 -0
  14. package/dist/src/ai/historian/playwright.js +145 -0
  15. package/dist/src/ai/historian/screencast.js +121 -0
  16. package/dist/src/ai/historian/utils.js +18 -0
  17. package/dist/src/ai/historian.js +21 -405
  18. package/dist/src/ai/navigator.js +82 -29
  19. package/dist/src/ai/pilot.js +232 -13
  20. package/dist/src/ai/planner.js +29 -9
  21. package/dist/src/ai/provider.js +54 -17
  22. package/dist/src/ai/researcher.js +41 -32
  23. package/dist/src/ai/rules.js +26 -14
  24. package/dist/src/ai/tester.js +90 -26
  25. package/dist/src/ai/tools.js +13 -7
  26. package/dist/src/browser-server.js +16 -3
  27. package/dist/src/commands/add-rule-command.js +11 -8
  28. package/dist/src/commands/clean-command.js +2 -1
  29. package/dist/src/commands/explore-command.js +43 -15
  30. package/dist/src/commands/init-command.js +9 -8
  31. package/dist/src/commands/plan-command.js +32 -0
  32. package/dist/src/commands/plan-save-command.js +19 -7
  33. package/dist/src/commands/rerun-command.js +4 -0
  34. package/dist/src/components/App.js +15 -5
  35. package/dist/src/execution-controller.js +13 -2
  36. package/dist/src/experience-tracker.js +20 -64
  37. package/dist/src/explorbot.js +8 -8
  38. package/dist/src/explorer.js +11 -3
  39. package/dist/src/observability.js +50 -99
  40. package/dist/src/playwright-recorder.js +309 -0
  41. package/dist/src/reporter.js +4 -1
  42. package/dist/src/test-plan.js +12 -0
  43. package/dist/src/utils/aria.js +37 -1
  44. package/dist/src/utils/error-page.js +20 -7
  45. package/dist/src/utils/next-steps.js +37 -0
  46. package/dist/src/utils/strings.js +15 -0
  47. package/package.json +3 -3
  48. package/rules/navigator/output.md +9 -0
  49. package/rules/navigator/verification-actions.md +2 -0
  50. package/src/action-result.ts +26 -1
  51. package/src/action.ts +49 -41
  52. package/src/ai/bosun.ts +11 -1
  53. package/src/ai/conversation.ts +37 -0
  54. package/src/ai/historian/codeceptjs.ts +130 -0
  55. package/src/ai/historian/experience.ts +384 -0
  56. package/src/ai/historian/mixin.ts +4 -0
  57. package/src/ai/historian/playwright.ts +169 -0
  58. package/src/ai/historian/screencast.ts +133 -0
  59. package/src/ai/historian/utils.ts +23 -0
  60. package/src/ai/historian.ts +37 -473
  61. package/src/ai/navigator.ts +82 -29
  62. package/src/ai/pilot.ts +237 -14
  63. package/src/ai/planner.ts +29 -9
  64. package/src/ai/provider.ts +51 -17
  65. package/src/ai/researcher.ts +45 -33
  66. package/src/ai/rules.ts +27 -14
  67. package/src/ai/tester.ts +94 -26
  68. package/src/ai/tools.ts +47 -25
  69. package/src/browser-server.ts +17 -3
  70. package/src/commands/add-rule-command.ts +11 -7
  71. package/src/commands/clean-command.ts +2 -1
  72. package/src/commands/explore-command.ts +46 -14
  73. package/src/commands/init-command.ts +9 -8
  74. package/src/commands/plan-command.ts +35 -0
  75. package/src/commands/plan-save-command.ts +18 -7
  76. package/src/commands/rerun-command.ts +5 -0
  77. package/src/components/App.tsx +16 -5
  78. package/src/config.ts +12 -1
  79. package/src/execution-controller.ts +14 -3
  80. package/src/experience-tracker.ts +21 -72
  81. package/src/explorbot.ts +8 -8
  82. package/src/explorer.ts +13 -3
  83. package/src/observability.ts +50 -109
  84. package/src/playwright-recorder.ts +305 -0
  85. package/src/reporter.ts +4 -1
  86. package/src/test-plan.ts +12 -0
  87. package/src/utils/aria.ts +38 -1
  88. package/src/utils/error-page.ts +22 -7
  89. package/src/utils/next-steps.ts +51 -0
  90. package/src/utils/strings.ts +17 -0
@@ -0,0 +1,121 @@
1
+ import { mkdirSync } from 'node:fs';
2
+ import { join } from 'node:path';
3
+ // @ts-ignore
4
+ import * as codeceptjs from 'codeceptjs';
5
+ import { outputPath } from "../../config.js";
6
+ import { tag } from "../../utils/logger.js";
7
+ import { relativeToCwd } from "../../utils/next-steps.js";
8
+ import { safeFilename } from "../../utils/strings.js";
9
+ import { debugLog } from "./mixin.js";
10
+ export function WithScreencast(Base) {
11
+ return class extends Base {
12
+ screencastPage = null;
13
+ screencastActive = false;
14
+ screencastPath = null;
15
+ screencastListenersInstalled = false;
16
+ screencastTask = null;
17
+ screencastLastChapter = null;
18
+ onTestBefore;
19
+ onStepPassed;
20
+ onTestAfter;
21
+ isScreencastActive() {
22
+ return this.screencastActive;
23
+ }
24
+ attachScreencast() {
25
+ if (this.screencastListenersInstalled)
26
+ return;
27
+ if (!this.config?.ai?.agents?.historian?.screencast)
28
+ return;
29
+ if (!this.playwright?.helper)
30
+ return;
31
+ this.onTestBefore = (test) => {
32
+ void this.startScreencast(test);
33
+ };
34
+ this.onStepPassed = (step) => {
35
+ void this.emitChapter(step);
36
+ };
37
+ this.onTestAfter = () => {
38
+ void this.stopScreencast();
39
+ };
40
+ codeceptjs.event.dispatcher.on('test.before', this.onTestBefore);
41
+ codeceptjs.event.dispatcher.on('step.passed', this.onStepPassed);
42
+ codeceptjs.event.dispatcher.on('test.after', this.onTestAfter);
43
+ this.screencastListenersInstalled = true;
44
+ }
45
+ async startScreencast(test) {
46
+ if (this.screencastActive)
47
+ return;
48
+ const page = this.playwright?.helper?.page;
49
+ if (!page?.screencast?.start)
50
+ return;
51
+ const task = test?._explorbotTest;
52
+ const scenarioName = task?.scenario || test?.title || 'scenario';
53
+ const planTitle = task?.plan?.title;
54
+ const planTests = task?.plan?.tests;
55
+ const index = planTests && task ? planTests.indexOf(task) + 1 : 0;
56
+ const parts = [];
57
+ if (planTitle)
58
+ parts.push(safeFilename(planTitle));
59
+ if (index > 0)
60
+ parts.push(String(index));
61
+ parts.push(safeFilename(scenarioName));
62
+ const dir = outputPath('screencasts');
63
+ mkdirSync(dir, { recursive: true });
64
+ const filePath = join(dir, `${parts.join('-')}.webm`);
65
+ const screencastConfig = this.config?.ai?.agents?.historian?.screencast;
66
+ const screencastOpts = typeof screencastConfig === 'object' ? screencastConfig : {};
67
+ const size = screencastOpts.size ?? page.viewportSize?.() ?? undefined;
68
+ const quality = screencastOpts.quality ?? 95;
69
+ try {
70
+ await page.screencast.start({ path: filePath, quality, size });
71
+ await page.screencast.showActions({ position: 'top-left' });
72
+ this.screencastPage = page;
73
+ this.screencastPath = filePath;
74
+ this.screencastActive = true;
75
+ this.screencastTask = test?._explorbotTest || null;
76
+ this.screencastLastChapter = null;
77
+ }
78
+ catch (err) {
79
+ tag('substep').log(`Screencast start failed: ${err.message}`);
80
+ }
81
+ }
82
+ async emitChapter(_step) {
83
+ if (!this.screencastActive)
84
+ return;
85
+ const explanation = this.screencastTask?.activeNote?.getMessage?.();
86
+ if (!explanation)
87
+ return;
88
+ if (explanation === this.screencastLastChapter)
89
+ return;
90
+ this.screencastLastChapter = explanation;
91
+ try {
92
+ await this.screencastPage.screencast.showChapter(explanation);
93
+ }
94
+ catch (err) {
95
+ debugLog('screencast.showChapter failed:', err);
96
+ }
97
+ }
98
+ async stopScreencast() {
99
+ if (!this.screencastActive)
100
+ return;
101
+ const path = this.screencastPath;
102
+ const task = this.screencastTask;
103
+ try {
104
+ await this.screencastPage.screencast.stop();
105
+ }
106
+ catch (err) {
107
+ tag('substep').log(`Screencast stop failed: ${err.message}`);
108
+ }
109
+ this.screencastActive = false;
110
+ this.screencastPage = null;
111
+ this.screencastPath = null;
112
+ this.screencastTask = null;
113
+ this.screencastLastChapter = null;
114
+ if (path) {
115
+ this.savedFiles.add(path);
116
+ task?.addArtifact?.(path);
117
+ tag('substep').log(`Saved screencast: ${relativeToCwd(path)}`);
118
+ }
119
+ }
120
+ };
121
+ }
@@ -0,0 +1,18 @@
1
+ export function isNonReusableCode(code) {
2
+ return /\bI\.clickXY\s*\(/.test(code);
3
+ }
4
+ export function escapeString(str) {
5
+ return str.replace(/'/g, "\\'").replace(/\n/g, ' ');
6
+ }
7
+ export function stripComments(code) {
8
+ return code
9
+ .split('\n')
10
+ .filter((line) => {
11
+ const trimmed = line.trim();
12
+ return trimmed && !trimmed.startsWith('//') && !trimmed.startsWith('/*') && !trimmed.startsWith('*');
13
+ })
14
+ .join('\n');
15
+ }
16
+ export function getExecutionLabel(exec, fallback) {
17
+ return exec.input?.explanation || exec.input?.assertion || exec.input?.note || fallback || '';
18
+ }
@@ -1,379 +1,33 @@
1
- import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
2
- import { join } from 'node:path';
3
- import dedent from 'dedent';
4
- import { z } from 'zod';
5
- import { ActionResult } from "../action-result.js";
6
- import { ConfigParser } from "../config.js";
1
+ import { readFileSync, writeFileSync } from 'node:fs';
7
2
  import { ExperienceTracker } from "../experience-tracker.js";
8
- import { KnowledgeTracker } from "../knowledge-tracker.js";
9
- import { Test } from "../test-plan.js";
10
- import { createDebug, tag } from "../utils/logger.js";
11
- import { extractStatePath } from "../utils/url-matcher.js";
12
- import { ASSERTION_TOOLS, CODECEPT_TOOLS } from "./tools.js";
13
- const debugLog = createDebug('explorbot:historian');
14
- export class Historian {
15
- provider;
16
- experienceTracker;
17
- reporter;
18
- stateManager;
19
- savedFiles = new Set();
20
- constructor(provider, experienceTracker, reporter, stateManager) {
3
+ import { tag } from "../utils/logger.js";
4
+ import { relativeToCwd } from "../utils/next-steps.js";
5
+ import { WithCodeceptJS } from "./historian/codeceptjs.js";
6
+ import { WithExperience } from "./historian/experience.js";
7
+ import { WithPlaywright } from "./historian/playwright.js";
8
+ import { WithScreencast } from "./historian/screencast.js";
9
+ export { isNonReusableCode } from "./historian/utils.js";
10
+ const HistorianBase = WithScreencast(WithPlaywright(WithCodeceptJS(WithExperience(Object))));
11
+ export class Historian extends HistorianBase {
12
+ constructor(provider, experienceTracker, reporter, stateManager, config, playwright) {
13
+ super();
21
14
  this.provider = provider;
22
15
  this.experienceTracker = experienceTracker || new ExperienceTracker();
23
16
  this.reporter = reporter;
24
17
  this.stateManager = stateManager;
18
+ this.config = config;
19
+ this.playwright = playwright;
20
+ this.savedFiles = new Set();
21
+ this.attachScreencast();
22
+ }
23
+ isPlaywrightFramework() {
24
+ return this.config?.ai?.agents?.historian?.framework === 'playwright';
25
25
  }
26
26
  getSavedFiles() {
27
27
  return [...this.savedFiles];
28
28
  }
29
- async saveSession(task, initialState, conversation) {
30
- debugLog('Saving session experience');
31
- const result = this.determineResult(task);
32
- const toolExecutions = conversation.getToolExecutions();
33
- if (task instanceof Test) {
34
- task.generatedCode = this.toCode(conversation, task.description);
35
- }
36
- const steps = await this.extractSteps(toolExecutions);
37
- await this.detectRetryPatterns(toolExecutions, initialState);
38
- const verifiedSteps = await this.verifySteps(steps, initialState);
39
- if (verifiedSteps.length > 0) {
40
- const relatedUrls = this.extractVisitedUrls(toolExecutions, initialState.url || '');
41
- this.experienceTracker.writeFlow(initialState, {
42
- scenario: task.description,
43
- steps: verifiedSteps,
44
- relatedUrls,
45
- });
46
- }
47
- if (task instanceof Test && result !== 'failed') {
48
- await this.reportSession(task, steps);
49
- }
50
- tag('substep').log(`Historian saved session for: ${task.description}`);
51
- }
52
- async reportSession(test, steps) {
53
- if (!this.reporter)
54
- return;
55
- const reporterSteps = steps.map((step) => ({
56
- title: step.message,
57
- status: step.status === 'passed' ? 'passed' : 'failed',
58
- code: step.code ? step.code.split('\n').filter((l) => l.trim()) : [],
59
- discovery: step.discovery,
60
- }));
61
- await this.reporter.reportSteps(test, reporterSteps);
62
- }
63
- async extractSteps(toolExecutions) {
64
- const stepsWithDiffs = [];
65
- for (const exec of toolExecutions) {
66
- if (!CODECEPT_TOOLS.includes(exec.toolName))
67
- continue;
68
- if (!exec.output?.code)
69
- continue;
70
- if (!exec.wasSuccessful)
71
- continue;
72
- if (isNonReusableCode(exec.output.code))
73
- continue;
74
- const message = this.getExecutionLabel(exec, `Executed ${exec.toolName}`);
75
- const ariaDiff = exec.output?.pageDiff?.ariaChanges || null;
76
- const urlChanged = exec.output?.pageDiff?.urlChanged || false;
77
- const step = {
78
- message,
79
- status: 'passed',
80
- tool: exec.toolName,
81
- code: this.stripComments(exec.output.code),
82
- };
83
- stepsWithDiffs.push({ step, ariaDiff, urlChanged });
84
- }
85
- await this.analyzeDiscoveries(stepsWithDiffs);
86
- return stepsWithDiffs.map((s) => s.step);
87
- }
88
- async verifySteps(steps, initialState) {
89
- if (steps.length === 0)
90
- return [];
91
- const existingExperience = this.experienceTracker
92
- .getRelevantExperience(initialState)
93
- .map((e) => e.content)
94
- .filter(Boolean)
95
- .join('\n');
96
- const existingSummary = existingExperience.length > 2000 ? existingExperience.substring(0, 2000) : existingExperience;
97
- const stepsList = steps.map((s, i) => `${i}. ${s.message}\n Code: ${s.code || 'none'}`).join('\n');
98
- const prompt = dedent `
99
- Review these test steps and determine which are valuable to save as experience
100
- for future test executions on this page.
101
-
102
- <steps>
103
- ${stepsList}
104
- </steps>
105
-
106
- ${existingSummary ? `<existing_experience>\n${existingSummary}\n</existing_experience>` : ''}
107
-
108
- For each step, determine if it is useful:
109
- - NOT useful if it uses auto-generated or unstable locators (ember IDs, numeric data-testid, random IDs)
110
- - NOT useful if it is already documented in existing experience
111
- - NOT useful if it requires an unclear precondition that would not be reproducible
112
- - NOT useful if it is trivial navigation (I.amOnPage) without meaningful context
113
- - USEFUL if it demonstrates how to interact with a specific UI component (expand dropdown, fill form, etc)
114
- - USEFUL if it shows a working approach for a common task on this page
115
- `;
116
- const schema = z.object({
117
- steps: z.array(z.object({
118
- stepIndex: z.number(),
119
- useful: z.boolean(),
120
- })),
121
- });
122
- try {
123
- const response = await this.provider.generateObject([
124
- { role: 'system', content: 'Evaluate test steps for experience value. Be selective — only keep steps that teach something reusable.' },
125
- { role: 'user', content: prompt },
126
- ], schema, undefined, { telemetryFunctionId: 'historian.verifySteps' });
127
- const usefulIndices = new Set((response?.object?.steps || []).filter((s) => s.useful).map((s) => s.stepIndex));
128
- const verified = steps.filter((_, i) => usefulIndices.has(i));
129
- debugLog('Verified %d/%d steps as useful', verified.length, steps.length);
130
- return verified;
131
- }
132
- catch (error) {
133
- debugLog('Step verification failed, keeping all steps: %s', error.message);
134
- return steps;
135
- }
136
- }
137
- async detectRetryPatterns(toolExecutions, initialState) {
138
- if (!this.experienceTracker || !this.stateManager)
139
- return;
140
- const failedByTool = new Map();
141
- const candidates = [];
142
- for (const exec of toolExecutions) {
143
- if (!CODECEPT_TOOLS.includes(exec.toolName))
144
- continue;
145
- if (!exec.output?.code)
146
- continue;
147
- if (!exec.wasSuccessful) {
148
- const bucket = failedByTool.get(exec.toolName) || [];
149
- bucket.push(exec);
150
- failedByTool.set(exec.toolName, bucket);
151
- continue;
152
- }
153
- const failed = failedByTool.get(exec.toolName);
154
- if (failed?.length) {
155
- candidates.push({ failed: [...failed], success: exec });
156
- failedByTool.set(exec.toolName, []);
157
- }
158
- }
159
- if (candidates.length === 0)
160
- return;
161
- const prompt = dedent `
162
- Analyze these retry patterns where a tool failed multiple times before succeeding.
163
- For each candidate, determine which failed attempts were trying to do the same thing as the success.
164
-
165
- ${candidates
166
- .map((c, i) => dedent `
167
- Candidate ${i}:
168
- Failed attempts:
169
- ${c.failed.map((f, j) => ` ${j}: ${this.getExecutionLabel(f, f.toolName)} → code: ${f.output?.code}`).join('\n')}
170
- Succeeded:
171
- ${this.getExecutionLabel(c.success, c.success.toolName)} → code: ${c.success.output.code}
172
- `)
173
- .join('\n\n')}
174
-
175
- For each candidate where failures share the same intent as the success:
176
- - candidateIndex: index of the candidate
177
- - failedIndices: which failed attempts share the same intent
178
- - intent: business-focused description of what was being done
179
- - explanation: actionable tip explaining which element works and what to avoid
180
- `;
181
- const schema = z.object({
182
- retryPatterns: z.array(z.object({
183
- candidateIndex: z.number(),
184
- failedIndices: z.array(z.number()),
185
- intent: z.string(),
186
- explanation: z.string(),
187
- })),
188
- });
189
- try {
190
- const response = await this.provider.generateObject([
191
- { role: 'system', content: 'Analyze retry patterns in web testing tool executions. Identify when failed attempts share the same intent as a successful one.' },
192
- { role: 'user', content: prompt },
193
- ], schema);
194
- for (const pattern of response?.object?.retryPatterns || []) {
195
- const candidate = candidates[pattern.candidateIndex];
196
- if (!candidate)
197
- continue;
198
- const url = candidate.success.output?.pageDiff?.currentUrl;
199
- let state = initialState;
200
- if (url && url !== initialState.url) {
201
- const transition = this.stateManager.getLastVisitToPath(url);
202
- if (transition) {
203
- state = ActionResult.fromState(transition.toState);
204
- }
205
- }
206
- if (isNonReusableCode(candidate.success.output.code))
207
- continue;
208
- this.experienceTracker.writeAction(state, { title: pattern.intent, code: candidate.success.output.code, explanation: pattern.explanation });
209
- }
210
- debugLog('Detected %d retry patterns', response?.object?.retryPatterns?.length || 0);
211
- }
212
- catch (error) {
213
- debugLog('Failed to detect retry patterns: %s', error.message);
214
- }
215
- }
216
- async analyzeDiscoveries(stepsWithDiffs) {
217
- if (!stepsWithDiffs.some((s) => s.ariaDiff))
218
- return;
219
- const prompt = this.buildDiscoveryPrompt(stepsWithDiffs);
220
- const schema = z.object({
221
- discoveries: z.array(z.object({
222
- stepNumber: z.number(),
223
- discoveries: z.array(z.string()),
224
- })),
225
- });
226
- try {
227
- const response = await this.provider.generateObject([
228
- { role: 'system', content: 'Analyze test execution steps and identify valuable UI discoveries. Return multiple discoveries per step when multiple new elements appear. Return no discoveries for steps with no meaningful changes.' },
229
- { role: 'user', content: prompt },
230
- ], schema);
231
- for (const { stepNumber, discoveries } of response?.object?.discoveries || []) {
232
- const stepIndex = stepNumber - 1;
233
- if (!stepsWithDiffs[stepIndex])
234
- continue;
235
- if (discoveries.length === 0)
236
- continue;
237
- stepsWithDiffs[stepIndex].step.discovery = discoveries.join('\n');
238
- }
239
- }
240
- catch (error) {
241
- debugLog('Failed to analyze discoveries: %s', error.message);
242
- }
243
- }
244
- buildDiscoveryPrompt(stepsWithDiffs) {
245
- let prompt = dedent `
246
- Review these test steps and their ARIA diffs. Identify new UI elements that appeared
247
- which could be valuable for:
248
- - Deeper testing of this feature
249
- - Related features that can be triggered from this flow
250
-
251
- IMPORTANT:
252
- - Return MULTIPLE discoveries per step when multiple new elements appear (e.g., if 3 buttons appeared, return an array with 3 discoveries for that step)
253
- - Return NO discoveries (empty array) for a step if nothing new appeared or if elements were already discovered in previous steps
254
- - Only include steps that have discoveries
255
-
256
- Steps:
257
- `;
258
- for (let i = 0; i < stepsWithDiffs.length; i++) {
259
- const { step, ariaDiff, urlChanged } = stepsWithDiffs[i];
260
- prompt += `\n\nStep ${i + 1}: ${step.message}`;
261
- if (ariaDiff) {
262
- prompt += `\n${ariaDiff}`;
263
- }
264
- }
265
- prompt += dedent `
266
-
267
- Return discoveries in format:
268
- - stepNumber: which step revealed these elements
269
- - discoveries: array of brief descriptions like ["A new button appeared: Publish To Twitter", "A new input field appeared: Description"]
270
-
271
- Only return elements that are actionable and could lead to new test scenarios.
272
- Ignore generic UI changes (loading spinners, timestamps, etc).
273
- If errors or warnings appeared in the step, include them in the discoveries array.
274
- If multiple buttons, inputs, links, or other actionable elements appeared in the same step, include all of them in the discoveries array.
275
- `;
276
- return prompt;
277
- }
278
- determineResult(task) {
279
- if ('isSuccessful' in task && task.isSuccessful)
280
- return 'success';
281
- if ('hasAchievedAny' in task && task.hasAchievedAny())
282
- return 'partial';
283
- const hasPassedNotes = Object.values(task.notes).some((n) => n.status === 'passed');
284
- if (hasPassedNotes)
285
- return 'partial';
286
- return 'failed';
287
- }
288
- extractVisitedUrls(toolExecutions, initialUrl) {
289
- const urls = new Set();
290
- const initialPath = extractStatePath(initialUrl);
291
- for (const exec of toolExecutions) {
292
- const currentUrl = exec.output?.pageDiff?.currentUrl;
293
- if (!currentUrl)
294
- continue;
295
- const relativePath = extractStatePath(currentUrl);
296
- if (relativePath && relativePath !== initialPath) {
297
- urls.add(relativePath);
298
- }
299
- }
300
- return [...urls];
301
- }
302
- toCode(conversation, scenario) {
303
- const toolExecutions = conversation.getToolExecutions();
304
- const TRACKABLE_TOOLS = [...CODECEPT_TOOLS, ...ASSERTION_TOOLS];
305
- const successfulSteps = toolExecutions.filter((exec) => exec.wasSuccessful && TRACKABLE_TOOLS.includes(exec.toolName) && exec.output?.code);
306
- if (successfulSteps.length === 0) {
307
- return '';
308
- }
309
- const lines = [];
310
- lines.push(`Scenario('${this.escapeString(scenario)}', ({ I }) => {`);
311
- for (const exec of successfulSteps) {
312
- if (isNonReusableCode(exec.output.code))
313
- continue;
314
- const explanation = this.getExecutionLabel(exec);
315
- if (explanation) {
316
- lines.push('');
317
- lines.push(` Section('${this.escapeString(explanation)}');`);
318
- }
319
- const code = this.stripComments(exec.output.code);
320
- const codeLines = code.includes('\n') ? code.split('\n') : code.split('; ');
321
- for (const codeLine of codeLines) {
322
- const trimmed = codeLine.trim();
323
- if (trimmed) {
324
- lines.push(` ${trimmed}`);
325
- }
326
- }
327
- }
328
- lines.push('});');
329
- return lines.join('\n');
330
- }
331
29
  savePlanToFile(plan) {
332
- const lines = [];
333
- lines.push(`import step, { Section } from 'codeceptjs/steps';`);
334
- lines.push('');
335
- lines.push(`Feature('${this.escapeString(plan.title)}')`);
336
- lines.push('');
337
- const startUrl = plan.url || plan.tests[0]?.startUrl;
338
- if (startUrl) {
339
- lines.push('Before(({ I }) => {');
340
- lines.push(` I.amOnPage('${this.escapeString(startUrl)}');`);
341
- lines.push(...this.getKnowledgeLines(startUrl));
342
- lines.push('});');
343
- lines.push('');
344
- }
345
- for (const test of plan.tests) {
346
- if (test.generatedCode) {
347
- if (test.isSuccessful) {
348
- lines.push(test.generatedCode);
349
- }
350
- else {
351
- lines.push(`// FAILED: ${test.scenario}`);
352
- lines.push(test.generatedCode.replace(/Scenario\(/, 'Scenario.skip('));
353
- }
354
- lines.push('');
355
- continue;
356
- }
357
- lines.push(`Scenario.todo('${this.escapeString(test.scenario)}', ({ I }) => {`);
358
- if (test.plannedSteps.length > 0) {
359
- for (const step of test.plannedSteps) {
360
- lines.push(` // ${step}`);
361
- }
362
- }
363
- else {
364
- lines.push(` // ${test.scenario}`);
365
- }
366
- lines.push('});');
367
- lines.push('');
368
- }
369
- const testsDir = ConfigParser.getInstance().getTestsDir();
370
- mkdirSync(testsDir, { recursive: true });
371
- const filename = plan.title.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase();
372
- const filePath = join(testsDir, `${filename}.js`);
373
- writeFileSync(filePath, lines.join('\n'));
374
- this.savedFiles.add(filePath);
375
- tag('substep').log(`Saved plan tests to: ${filePath}`);
376
- return filePath;
30
+ return this.isPlaywrightFramework() ? this.savePlaywrightPlanToFile(plan) : this.saveCodeceptPlanToFile(plan);
377
31
  }
378
32
  rewriteScenarioInFile(filePath, healedSteps) {
379
33
  let content = readFileSync(filePath, 'utf-8');
@@ -384,44 +38,6 @@ export class Historian {
384
38
  }
385
39
  writeFileSync(filePath, content);
386
40
  this.savedFiles.add(filePath);
387
- tag('substep').log(`Updated test file with healed steps: ${filePath}`);
41
+ tag('substep').log(`Updated test file with healed steps: ${relativeToCwd(filePath)}`);
388
42
  }
389
- getExecutionLabel(exec, fallback) {
390
- return exec.input?.explanation || exec.input?.assertion || exec.input?.note || fallback || '';
391
- }
392
- escapeString(str) {
393
- return str.replace(/'/g, "\\'").replace(/\n/g, ' ');
394
- }
395
- getKnowledgeLines(url, indent = ' ') {
396
- const knowledgeTracker = new KnowledgeTracker();
397
- const state = new ActionResult({ url });
398
- const { wait, waitForElement, code } = knowledgeTracker.getStateParameters(state, ['wait', 'waitForElement', 'code']);
399
- const lines = [];
400
- if (wait !== undefined) {
401
- lines.push(`${indent}I.wait(${wait});`);
402
- }
403
- if (waitForElement) {
404
- lines.push(`${indent}I.waitForElement(${JSON.stringify(waitForElement)});`);
405
- }
406
- if (code) {
407
- for (const codeLine of code.split('\n')) {
408
- const trimmed = codeLine.trim();
409
- if (trimmed)
410
- lines.push(`${indent}${trimmed}`);
411
- }
412
- }
413
- return lines;
414
- }
415
- stripComments(code) {
416
- return code
417
- .split('\n')
418
- .filter((line) => {
419
- const trimmed = line.trim();
420
- return trimmed && !trimmed.startsWith('//') && !trimmed.startsWith('/*') && !trimmed.startsWith('*');
421
- })
422
- .join('\n');
423
- }
424
- }
425
- export function isNonReusableCode(code) {
426
- return /\bI\.clickXY\s*\(/.test(code);
427
43
  }