@yasserkhanorg/e2e-agents 0.10.0 → 0.11.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.
Files changed (93) hide show
  1. package/README.md +112 -584
  2. package/dist/agent/api_catalog.d.ts +11 -0
  3. package/dist/agent/api_catalog.d.ts.map +1 -0
  4. package/dist/agent/api_catalog.js +210 -0
  5. package/dist/agent/llm_agents_flow.d.ts +15 -0
  6. package/dist/agent/llm_agents_flow.d.ts.map +1 -0
  7. package/dist/agent/llm_agents_flow.js +434 -0
  8. package/dist/agent/native_flow.d.ts +6 -0
  9. package/dist/agent/native_flow.d.ts.map +1 -0
  10. package/dist/agent/native_flow.js +179 -0
  11. package/dist/agent/pipeline.d.ts +2 -25
  12. package/dist/agent/pipeline.d.ts.map +1 -1
  13. package/dist/agent/pipeline.js +30 -1329
  14. package/dist/agent/pipeline_types.d.ts +54 -0
  15. package/dist/agent/pipeline_types.d.ts.map +1 -0
  16. package/dist/agent/pipeline_types.js +4 -0
  17. package/dist/agent/pipeline_utils.d.ts +12 -0
  18. package/dist/agent/pipeline_utils.d.ts.map +1 -0
  19. package/dist/agent/pipeline_utils.js +156 -0
  20. package/dist/agent/process_runner.d.ts +10 -0
  21. package/dist/agent/process_runner.d.ts.map +1 -0
  22. package/dist/agent/process_runner.js +92 -0
  23. package/dist/agent/spec_generator.d.ts +5 -0
  24. package/dist/agent/spec_generator.d.ts.map +1 -0
  25. package/dist/agent/spec_generator.js +253 -0
  26. package/dist/agent/validation_runner.d.ts +5 -0
  27. package/dist/agent/validation_runner.d.ts.map +1 -0
  28. package/dist/agent/validation_runner.js +77 -0
  29. package/dist/agentic/playwright_runner.js +1 -1
  30. package/dist/cli/commands/analyze.d.ts +3 -0
  31. package/dist/cli/commands/analyze.d.ts.map +1 -0
  32. package/dist/cli/commands/analyze.js +77 -0
  33. package/dist/cli/commands/feedback.d.ts +3 -0
  34. package/dist/cli/commands/feedback.d.ts.map +1 -0
  35. package/dist/cli/commands/feedback.js +39 -0
  36. package/dist/cli/commands/finalize.d.ts +3 -0
  37. package/dist/cli/commands/finalize.d.ts.map +1 -0
  38. package/dist/cli/commands/finalize.js +41 -0
  39. package/dist/cli/commands/generate.d.ts +4 -0
  40. package/dist/cli/commands/generate.d.ts.map +1 -0
  41. package/dist/cli/commands/generate.js +108 -0
  42. package/dist/cli/commands/heal.d.ts +3 -0
  43. package/dist/cli/commands/heal.d.ts.map +1 -0
  44. package/dist/cli/commands/heal.js +60 -0
  45. package/dist/cli/commands/impact.d.ts +4 -0
  46. package/dist/cli/commands/impact.d.ts.map +1 -0
  47. package/dist/cli/commands/impact.js +26 -0
  48. package/dist/cli/commands/llm_health.d.ts +2 -0
  49. package/dist/cli/commands/llm_health.d.ts.map +1 -0
  50. package/dist/cli/commands/llm_health.js +38 -0
  51. package/dist/cli/commands/plan.d.ts +4 -0
  52. package/dist/cli/commands/plan.d.ts.map +1 -0
  53. package/dist/cli/commands/plan.js +83 -0
  54. package/dist/cli/commands/traceability.d.ts +4 -0
  55. package/dist/cli/commands/traceability.d.ts.map +1 -0
  56. package/dist/cli/commands/traceability.js +77 -0
  57. package/dist/cli/parse_args.d.ts +6 -0
  58. package/dist/cli/parse_args.d.ts.map +1 -0
  59. package/dist/cli/parse_args.js +216 -0
  60. package/dist/cli/types.d.ts +70 -0
  61. package/dist/cli/types.d.ts.map +1 -0
  62. package/dist/cli/types.js +4 -0
  63. package/dist/cli/usage.d.ts +2 -0
  64. package/dist/cli/usage.d.ts.map +1 -0
  65. package/dist/cli/usage.js +86 -0
  66. package/dist/cli.js +26 -1060
  67. package/dist/esm/agent/api_catalog.js +199 -0
  68. package/dist/esm/agent/llm_agents_flow.js +421 -0
  69. package/dist/esm/agent/native_flow.js +175 -0
  70. package/dist/esm/agent/pipeline.js +8 -1307
  71. package/dist/esm/agent/pipeline_types.js +3 -0
  72. package/dist/esm/agent/pipeline_utils.js +146 -0
  73. package/dist/esm/agent/process_runner.js +83 -0
  74. package/dist/esm/agent/spec_generator.js +249 -0
  75. package/dist/esm/agent/validation_runner.js +73 -0
  76. package/dist/esm/agentic/playwright_runner.js +1 -1
  77. package/dist/esm/cli/commands/analyze.js +74 -0
  78. package/dist/esm/cli/commands/feedback.js +36 -0
  79. package/dist/esm/cli/commands/finalize.js +38 -0
  80. package/dist/esm/cli/commands/generate.js +105 -0
  81. package/dist/esm/cli/commands/heal.js +57 -0
  82. package/dist/esm/cli/commands/impact.js +23 -0
  83. package/dist/esm/cli/commands/llm_health.js +35 -0
  84. package/dist/esm/cli/commands/plan.js +80 -0
  85. package/dist/esm/cli/commands/traceability.js +73 -0
  86. package/dist/esm/cli/parse_args.js +210 -0
  87. package/dist/esm/cli/types.js +3 -0
  88. package/dist/esm/cli/usage.js +83 -0
  89. package/dist/esm/cli.js +20 -1054
  90. package/dist/esm/mcp-server.js +18 -1
  91. package/dist/mcp-server.d.ts.map +1 -1
  92. package/dist/mcp-server.js +17 -0
  93. package/package.json +2 -4
@@ -1,905 +1,17 @@
1
1
  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2
2
  // See LICENSE.txt for license information.
3
- import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'fs';
4
- import { basename, dirname, join, relative, resolve } from 'path';
5
- import { spawnSync } from 'child_process';
6
- import { baseNameWithoutExt, isPathWithinRoot, normalizePath, titleCase, tokenize, uniqueTokens } from './utils.js';
7
- function createMcpStatus(backend, requested) {
8
- return {
9
- requested,
10
- active: requested && (backend === 'e2e-test-gen' || backend === 'playwright-agents'),
11
- backend,
12
- };
13
- }
14
- function classifyPipelineFailure(result) {
15
- if (result.failureCategory || result.failureCode) {
16
- return result;
17
- }
18
- if (!result.error) {
19
- return result;
20
- }
21
- const errorText = result.error.toLowerCase();
22
- if (errorText.includes('etimedout') || errorText.includes('timed out')) {
23
- return { ...result, failureCategory: 'environment', failureCode: 'mcp_timeout' };
24
- }
25
- if (errorText.includes('outside testsroot')) {
26
- return { ...result, failureCategory: 'path-safety', failureCode: 'path_outside_tests_root' };
27
- }
28
- if (errorText.includes('playwright binary') || errorText.includes('not found')) {
29
- return { ...result, failureCategory: 'environment', failureCode: 'dependency_missing' };
30
- }
31
- if (errorText.includes('compile validation')) {
32
- return { ...result, failureCategory: 'validation', failureCode: 'compile_validation_failed' };
33
- }
34
- if (errorText.includes('runtime validation') || errorText.includes('playwright test failed')) {
35
- return { ...result, failureCategory: 'runtime', failureCode: 'runtime_validation_failed' };
36
- }
37
- if (errorText.includes('quality checks failed') || errorText.includes('invalid test content')) {
38
- return { ...result, failureCategory: 'quality', failureCode: 'quality_guard_failed' };
39
- }
40
- if (errorText.includes('generate failed') || errorText.includes('did not produce expected test file')) {
41
- return { ...result, failureCategory: 'generation', failureCode: 'generation_failed' };
42
- }
43
- return { ...result, failureCategory: 'unknown', failureCode: 'unknown' };
44
- }
45
- function finalizePipelineSummary(summary) {
46
- return {
47
- ...summary,
48
- results: summary.results.map(classifyPipelineFailure),
49
- };
50
- }
3
+ import { existsSync } from 'fs';
4
+ import { dirname, join, relative, resolve } from 'path';
5
+ import { isPathWithinRoot, normalizePath } from './utils.js';
6
+ import { createMcpStatus, finalizePipelineSummary, buildSyntheticFlowFromSpecTarget } from './pipeline_utils.js';
7
+ import { buildApiSurfaceCatalog } from './api_catalog.js';
8
+ import { resolvePlaywrightBinary, runCommand, summarizeCommandOutput } from './process_runner.js';
9
+ import { runPackageNativeFlow, runPackageNativePipeline } from './native_flow.js';
10
+ import { findDisallowedDescribeFiles, resolveAgentSeedSpec, runPlaywrightAgentsPipeline } from './llm_agents_flow.js';
51
11
  function hasE2eTestGenCLI(testsRoot) {
52
12
  const cliPath = join(testsRoot, 'e2e-test-gen-cli.ts');
53
13
  return existsSync(cliPath) ? cliPath : null;
54
14
  }
55
- function toSafeSlug(value) {
56
- return value.replace(/[^a-zA-Z0-9._-]+/g, '-').replace(/^-+|-+$/g, '') || 'flow';
57
- }
58
- function stripSpecSuffix(value) {
59
- return value.replace(/\.(spec|test)\.[^.]+$/i, '').replace(/\.[^.]+$/, '');
60
- }
61
- function buildSyntheticFlowFromSpecTarget(relativeSpecPath, target) {
62
- const normalizedSpecPath = normalizePath(relativeSpecPath);
63
- const noSuffix = stripSpecSuffix(normalizedSpecPath);
64
- const flowId = toSafeSlug(noSuffix.replace(/\//g, '.'));
65
- const base = baseNameWithoutExt(stripSpecSuffix(basename(normalizedSpecPath)));
66
- const flowName = titleCase(base.replace(/[._-]+/g, ' ')) || 'Recovered Spec';
67
- const keywords = uniqueTokens(tokenize(noSuffix.replace(/[/.]/g, ' ')));
68
- const reasons = [
69
- `Playwright report marked this spec as ${target.status || 'unstable'}.`,
70
- target.reason || `Auto-heal target: ${normalizedSpecPath}`,
71
- ];
72
- return {
73
- id: flowId,
74
- name: flowName,
75
- kind: 'flow',
76
- score: target.status === 'failed' ? 12 : 9,
77
- priority: target.status === 'failed' ? 'P0' : 'P1',
78
- reasons,
79
- keywords,
80
- files: [normalizedSpecPath],
81
- };
82
- }
83
- function firstFlowFiles(flow) {
84
- return (flow.files || []).filter(Boolean).slice(0, 5);
85
- }
86
- function buildNativeStrategyOrder(flow) {
87
- const flowId = (flow.id || '').toLowerCase();
88
- const haystack = [
89
- flow.id,
90
- flow.name,
91
- ...(flow.files || []),
92
- ...(flow.reasons || []),
93
- ...(flow.keywords || []),
94
- ].join(' ').toLowerCase();
95
- const strategies = [];
96
- if (flowId.includes('search')) {
97
- strategies.push('search-baseline');
98
- }
99
- if (flowId.includes('threads') || flowId.includes('thread')) {
100
- strategies.push('thread-reply');
101
- }
102
- if (flowId.includes('channels.lifecycle')) {
103
- strategies.push('lifecycle-channel');
104
- }
105
- if (flowId.includes('channels.settings')) {
106
- strategies.push('channel-settings');
107
- }
108
- if (flowId.includes('channels.switch')) {
109
- strategies.push('channel-switch');
110
- }
111
- if (flowId.includes('messaging.markdown')) {
112
- strategies.push('markdown-post');
113
- }
114
- if (flowId.includes('messaging.mentions')) {
115
- strategies.push('mentions-post');
116
- }
117
- if (flowId.includes('messaging.realtime')) {
118
- strategies.push('realtime-post');
119
- }
120
- if (/(thread|reply|rhs|sidebar[_-]?right)/.test(haystack)) {
121
- strategies.push('thread-reply');
122
- }
123
- if (/(create|join|leave|invite)/.test(haystack)) {
124
- strategies.push('lifecycle-channel');
125
- }
126
- if (/(settings|preferences)/.test(haystack)) {
127
- strategies.push('channel-settings');
128
- }
129
- if (/(switch|quick\\s*switch)/.test(haystack)) {
130
- strategies.push('channel-switch');
131
- }
132
- if (/(markdown|format)/.test(haystack)) {
133
- strategies.push('markdown-post');
134
- }
135
- if (/(mention|@)/.test(haystack)) {
136
- strategies.push('mentions-post');
137
- }
138
- if (/(realtime|websocket|presence)/.test(haystack)) {
139
- strategies.push('realtime-post');
140
- }
141
- if (/(search|find|spotlight)/.test(haystack)) {
142
- strategies.push('search-baseline');
143
- }
144
- if (/(message|post|realtime|websocket|chat)/.test(haystack)) {
145
- strategies.push('message-post');
146
- }
147
- if (/(channel|navigation|sidebar|switch)/.test(haystack)) {
148
- strategies.push('channel-baseline');
149
- }
150
- strategies.push('generic-baseline');
151
- return Array.from(new Set(strategies));
152
- }
153
- function createDefaultApiSurfaceCatalog() {
154
- const pwNestedMethods = new Map();
155
- pwNestedMethods.set('apiClient', new Set([
156
- 'createPost',
157
- 'createDirectChannel',
158
- 'createChannel',
159
- 'getChannels',
160
- 'getChannelByName',
161
- 'getPostsSince',
162
- ]));
163
- return {
164
- pwProps: new Set([
165
- 'initSetup',
166
- 'testBrowser',
167
- 'apiInitSetup',
168
- 'apiAdminSetup',
169
- 'apiCreateChannel',
170
- 'apiCreateUser',
171
- 'apiLogin',
172
- 'apiClient',
173
- ]),
174
- pwNestedMethods,
175
- initSetupKeys: new Set([
176
- 'user',
177
- 'team',
178
- 'adminClient',
179
- 'adminUser',
180
- 'adminConfig',
181
- 'userClient',
182
- 'offTopicUrl',
183
- 'townSquareUrl',
184
- ]),
185
- initSetupVariableMethods: new Map(),
186
- testBrowserMethods: new Set([
187
- 'login',
188
- 'openNewBrowserContext',
189
- 'newContext',
190
- ]),
191
- channelsPageMembers: new Set([
192
- 'goto',
193
- 'page',
194
- 'postMessage',
195
- 'getLastPost',
196
- 'sidebarRight',
197
- 'openChannelSettings',
198
- 'newChannel',
199
- 'globalHeader',
200
- 'searchBox',
201
- ]),
202
- sidebarRightMembers: new Set([
203
- 'openThreadForPost',
204
- 'postMessage',
205
- 'getLastPost',
206
- ]),
207
- };
208
- }
209
- function collectMatches(content, pattern) {
210
- const out = new Set();
211
- for (const match of content.matchAll(pattern)) {
212
- const value = match[1];
213
- if (value) {
214
- out.add(value);
215
- }
216
- }
217
- return out;
218
- }
219
- function addNestedMethod(catalog, objectName, methodName) {
220
- const methods = catalog.pwNestedMethods.get(objectName) || new Set();
221
- methods.add(methodName);
222
- catalog.pwNestedMethods.set(objectName, methods);
223
- }
224
- function escapeRegExp(value) {
225
- return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
226
- }
227
- function parseInitSetupBindings(content) {
228
- const bindings = [];
229
- for (const match of content.matchAll(/(?:const|let|var)\s*\{\s*([^}]+)\s*\}\s*=\s*await\s+pw\.initSetup\s*\(/g)) {
230
- const raw = match[1];
231
- if (!raw) {
232
- continue;
233
- }
234
- for (const part of raw.split(',')) {
235
- const cleaned = part.trim();
236
- if (!cleaned) {
237
- continue;
238
- }
239
- const [leftRaw, rightRaw] = cleaned.split(':');
240
- const key = (leftRaw || '').trim();
241
- const variableCandidate = (rightRaw || leftRaw || '').trim().split('=')[0]?.trim();
242
- if (!key || !variableCandidate) {
243
- continue;
244
- }
245
- bindings.push({ key, variable: variableCandidate });
246
- }
247
- }
248
- return bindings;
249
- }
250
- function collectDestructuredInitSetupKeys(content) {
251
- return new Set(parseInitSetupBindings(content).map((binding) => binding.key));
252
- }
253
- function addInitSetupVariableMethod(catalog, variable, methodName) {
254
- const methods = catalog.initSetupVariableMethods.get(variable) || new Set();
255
- methods.add(methodName);
256
- catalog.initSetupVariableMethods.set(variable, methods);
257
- }
258
- function collectApiSurfaceFromContent(content, catalog) {
259
- for (const prop of collectMatches(content, /\bpw\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
260
- catalog.pwProps.add(prop);
261
- }
262
- for (const match of content.matchAll(/\bpw\.([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
263
- const objectName = match[1];
264
- const methodName = match[2];
265
- if (!objectName || !methodName) {
266
- continue;
267
- }
268
- addNestedMethod(catalog, objectName, methodName);
269
- }
270
- for (const method of collectMatches(content, /\bpw\.testBrowser\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
271
- catalog.testBrowserMethods.add(method);
272
- }
273
- for (const member of collectMatches(content, /\bchannelsPage\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
274
- catalog.channelsPageMembers.add(member);
275
- }
276
- for (const member of collectMatches(content, /\bchannelsPage\.sidebarRight\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
277
- catalog.sidebarRightMembers.add(member);
278
- }
279
- for (const binding of parseInitSetupBindings(content)) {
280
- catalog.initSetupKeys.add(binding.key);
281
- const methodPattern = new RegExp(`\\b${escapeRegExp(binding.variable)}\\.([A-Za-z_][A-Za-z0-9_]*)\\b`, 'g');
282
- for (const method of collectMatches(content, methodPattern)) {
283
- addInitSetupVariableMethod(catalog, binding.variable, method);
284
- }
285
- }
286
- }
287
- function buildApiSurfaceCatalog(testsRoot, seedFile) {
288
- const catalog = createDefaultApiSurfaceCatalog();
289
- const candidateRoots = [
290
- join(testsRoot, 'specs'),
291
- join(testsRoot, 'tests'),
292
- ];
293
- const files = [];
294
- for (const root of candidateRoots) {
295
- if (!existsSync(root)) {
296
- continue;
297
- }
298
- const stack = [root];
299
- while (stack.length > 0) {
300
- const current = stack.pop();
301
- let entries;
302
- try {
303
- entries = readdirSync(current, { withFileTypes: true });
304
- }
305
- catch {
306
- continue;
307
- }
308
- for (const entry of entries) {
309
- const full = join(current, entry.name);
310
- if (entry.isDirectory()) {
311
- if (entry.name === 'node_modules' || entry.name === '.git' || entry.name === 'dist') {
312
- continue;
313
- }
314
- stack.push(full);
315
- continue;
316
- }
317
- if (!entry.isFile()) {
318
- continue;
319
- }
320
- if (!/\.(spec|test)\.[jt]sx?$/.test(entry.name)) {
321
- continue;
322
- }
323
- files.push(full);
324
- }
325
- }
326
- }
327
- const uniqueFiles = Array.from(new Set(files)).slice(0, 2500);
328
- for (const filePath of uniqueFiles) {
329
- try {
330
- const content = readFileSync(filePath, 'utf-8');
331
- collectApiSurfaceFromContent(content, catalog);
332
- }
333
- catch {
334
- continue;
335
- }
336
- }
337
- const absoluteSeed = join(testsRoot, seedFile);
338
- if (existsSync(absoluteSeed)) {
339
- try {
340
- collectApiSurfaceFromContent(readFileSync(absoluteSeed, 'utf-8'), catalog);
341
- }
342
- catch {
343
- // ignore seed read failures; defaults + catalog scan still apply
344
- }
345
- }
346
- return catalog;
347
- }
348
- function validateGeneratedSpecContent(content, apiSurface) {
349
- const issues = [];
350
- if (/\btest\.describe\s*\(/.test(content)) {
351
- issues.push({
352
- code: 'disallowed-describe',
353
- message: 'Generated tests must not use test.describe.',
354
- });
355
- }
356
- if (/\btest\.only\s*\(/.test(content)) {
357
- issues.push({
358
- code: 'disallowed-only',
359
- message: 'Generated tests must not use test.only.',
360
- });
361
- }
362
- if (!/\btest\s*\(/.test(content)) {
363
- issues.push({
364
- code: 'missing-test',
365
- message: 'Generated file does not include a test() declaration.',
366
- });
367
- }
368
- if (/\btag\s*:\s*\[/.test(content)) {
369
- issues.push({
370
- code: 'tag-array-disallowed',
371
- message: 'Generated tests must use a single tag string, not a tag array.',
372
- });
373
- }
374
- const hasTagOption = /\btag\s*:\s*['"][^'"]+['"]/.test(content);
375
- const hasTagInTitle = /\btest(?:\.\w+)?\s*\(\s*['"][^'"]*@ai-assisted[^'"]*['"]/.test(content);
376
- if (!(hasTagOption || hasTagInTitle) || !/@ai-assisted/.test(content)) {
377
- issues.push({
378
- code: 'missing-tag',
379
- message: "Generated tests must include '@ai-assisted' either as tag option or in test title.",
380
- });
381
- }
382
- if (/\bsystemConsolePage\.toBeVisible\s*\(/.test(content)) {
383
- issues.push({
384
- code: 'fragile-system-console-visibility',
385
- message: 'Avoid systemConsolePage.toBeVisible(); it relies on legacy backstage navigation that may be absent.',
386
- });
387
- }
388
- const fragileSelectors = [
389
- '.backstage-navbar',
390
- '.admin-console__wrapper',
391
- '.left-panel',
392
- '.panel-card',
393
- ].filter((selector) => content.includes(selector));
394
- if (fragileSelectors.length > 0) {
395
- issues.push({
396
- code: 'fragile-selector',
397
- message: `Avoid brittle class selectors in generated tests: ${Array.from(new Set(fragileSelectors)).join(', ')}`,
398
- });
399
- }
400
- if (apiSurface) {
401
- const unknownPwProps = Array.from(collectMatches(content, /\bpw\.([A-Za-z_][A-Za-z0-9_]*)\b/g)).filter((prop) => !apiSurface.pwProps.has(prop));
402
- const unknownBrowserMethods = Array.from(collectMatches(content, /\bpw\.testBrowser\.([A-Za-z_][A-Za-z0-9_]*)\b/g)).filter((method) => !apiSurface.testBrowserMethods.has(method));
403
- const unknownNestedPwMembers = [];
404
- for (const match of content.matchAll(/\bpw\.([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
405
- const objectName = match[1];
406
- const methodName = match[2];
407
- if (!objectName || !methodName || objectName === 'testBrowser') {
408
- continue;
409
- }
410
- const knownMethods = apiSurface.pwNestedMethods.get(objectName);
411
- if (!knownMethods || !knownMethods.has(methodName)) {
412
- unknownNestedPwMembers.push(`pw.${objectName}.${methodName}`);
413
- }
414
- }
415
- const unknownChannelMembers = Array.from(collectMatches(content, /\bchannelsPage\.([A-Za-z_][A-Za-z0-9_]*)\b/g)).filter((member) => !apiSurface.channelsPageMembers.has(member));
416
- const unknownSidebarMembers = Array.from(collectMatches(content, /\bchannelsPage\.sidebarRight\.([A-Za-z_][A-Za-z0-9_]*)\b/g)).filter((member) => !apiSurface.sidebarRightMembers.has(member));
417
- const initSetupBindings = parseInitSetupBindings(content);
418
- const unknownInitSetupKeys = initSetupBindings
419
- .map((binding) => binding.key)
420
- .filter((key) => !apiSurface.initSetupKeys.has(key));
421
- const unknownInitSetupVariableMethods = [];
422
- for (const binding of initSetupBindings) {
423
- const knownMethods = apiSurface.initSetupVariableMethods.get(binding.variable);
424
- if (!knownMethods || knownMethods.size === 0) {
425
- continue;
426
- }
427
- const methodPattern = new RegExp(`\\b${escapeRegExp(binding.variable)}\\.([A-Za-z_][A-Za-z0-9_]*)\\b`, 'g');
428
- for (const method of collectMatches(content, methodPattern)) {
429
- if (!knownMethods.has(method)) {
430
- unknownInitSetupVariableMethods.push(`${binding.variable}.${method}`);
431
- }
432
- }
433
- }
434
- const unknown = [
435
- ...unknownPwProps.map((value) => `pw.${value}`),
436
- ...unknownBrowserMethods.map((value) => `pw.testBrowser.${value}`),
437
- ...unknownNestedPwMembers,
438
- ...unknownChannelMembers.map((value) => `channelsPage.${value}`),
439
- ...unknownSidebarMembers.map((value) => `channelsPage.sidebarRight.${value}`),
440
- ...unknownInitSetupKeys.map((value) => `pw.initSetup.{${value}}`),
441
- ...unknownInitSetupVariableMethods,
442
- ];
443
- if (unknown.length > 0) {
444
- issues.push({
445
- code: 'unknown-api-surface',
446
- message: `Generated test uses unknown API/page-object members: ${Array.from(new Set(unknown)).join(', ')}`,
447
- });
448
- }
449
- }
450
- return issues;
451
- }
452
- function createNativePlaywrightSpec(flow, slug, strategy) {
453
- const linkedFiles = firstFlowFiles(flow).join(', ') || 'N/A';
454
- const header = [
455
- "import {test, expect} from '@mattermost/playwright-lib';",
456
- '',
457
- '/**',
458
- ` * Auto-generated by @yasserkhanorg/e2e-agents`,
459
- ` * Flow: ${flow.id} (${flow.name})`,
460
- ` * Strategy: ${strategy}`,
461
- ` * Linked files: ${linkedFiles}`,
462
- ' */',
463
- ];
464
- const start = [
465
- `test('${flow.priority}: ${flow.name} generated coverage', {tag: '@ai-assisted'}, async ({pw}) => {`,
466
- ' const {user, team} = await pw.initSetup();',
467
- ' const {channelsPage} = await pw.testBrowser.login(user);',
468
- ' await channelsPage.goto(team.name);',
469
- ];
470
- const end = [
471
- '});',
472
- '',
473
- ];
474
- if (strategy === 'thread-reply') {
475
- return [
476
- ...header,
477
- ...start,
478
- ` const parentMessage = \`ai-${slug}-parent-\${Date.now()}\`;`,
479
- ' await channelsPage.postMessage(parentMessage);',
480
- ' const rootPost = await channelsPage.getLastPost();',
481
- ' await rootPost.openAThread();',
482
- ` const replyMessage = \`ai-${slug}-reply-\${Date.now()}\`;`,
483
- ' await channelsPage.sidebarRight.postMessage(replyMessage);',
484
- ' const lastReply = await channelsPage.sidebarRight.getLastPost();',
485
- ' await expect(lastReply.container).toContainText(replyMessage);',
486
- ...end,
487
- ].join('\n');
488
- }
489
- if (strategy === 'lifecycle-channel') {
490
- return [
491
- ...header,
492
- ...start,
493
- ` const channelName = \`ai-${slug}-\${Date.now().toString().slice(-6)}\`;`,
494
- " await channelsPage.newChannel(channelName, 'O');",
495
- ' await expect(channelsPage.page).toHaveURL(new RegExp(`/channels/${channelName}$`));',
496
- ...end,
497
- ].join('\n');
498
- }
499
- if (strategy === 'channel-settings') {
500
- return [
501
- ...header,
502
- ...start,
503
- ' await channelsPage.openChannelSettings();',
504
- " await expect(channelsPage.page.getByRole('dialog', {name: 'Channel Settings'})).toBeVisible();",
505
- " await channelsPage.page.keyboard.press('Escape');",
506
- ...end,
507
- ].join('\n');
508
- }
509
- if (strategy === 'channel-switch') {
510
- return [
511
- ...header,
512
- ...start,
513
- " await channelsPage.goto(team.name, 'off-topic');",
514
- " await expect(channelsPage.page).toHaveURL(/\\/channels\\/off-topic$/);",
515
- " await expect(channelsPage.page.locator('#channelHeaderTitle')).toContainText(/off-topic/i);",
516
- ...end,
517
- ].join('\n');
518
- }
519
- if (strategy === 'markdown-post') {
520
- return [
521
- ...header,
522
- ...start,
523
- ` const message = '**ai-${slug}-bold** _italic_';`,
524
- ' await channelsPage.postMessage(message);',
525
- ' const lastPost = await channelsPage.getLastPost();',
526
- " await expect(lastPost.container.locator('strong')).toBeVisible();",
527
- ...end,
528
- ].join('\n');
529
- }
530
- if (strategy === 'mentions-post') {
531
- return [
532
- ...header,
533
- ...start,
534
- ' const mention = `@${user.username}`;',
535
- ' await channelsPage.postMessage(`Ping ${mention}`);',
536
- ' const lastPost = await channelsPage.getLastPost();',
537
- ' await expect(lastPost.container).toContainText(mention);',
538
- ...end,
539
- ].join('\n');
540
- }
541
- if (strategy === 'realtime-post') {
542
- return [
543
- ...header,
544
- ...start,
545
- ` const message = \`ai-${slug}-realtime-\${Date.now()}\`;`,
546
- ' await channelsPage.postMessage(message);',
547
- ' const lastPost = await channelsPage.getLastPost();',
548
- ' await expect(lastPost.container).toContainText(message);',
549
- " await expect(channelsPage.page.locator('#channel_view')).toBeVisible();",
550
- ...end,
551
- ].join('\n');
552
- }
553
- if (strategy === 'message-post') {
554
- return [
555
- ...header,
556
- ...start,
557
- ` const message = \`ai-${slug}-message-\${Date.now()}\`;`,
558
- ' await channelsPage.postMessage(message);',
559
- ' await expect(channelsPage.getLastPost()).toContainText(message);',
560
- ...end,
561
- ].join('\n');
562
- }
563
- if (strategy === 'channel-baseline') {
564
- return [
565
- ...header,
566
- ...start,
567
- " await expect(channelsPage.page.locator('#channelHeaderTitle')).toBeVisible();",
568
- " await expect(channelsPage.page.locator('#SidebarContainer')).toBeVisible();",
569
- ...end,
570
- ].join('\n');
571
- }
572
- if (strategy === 'search-baseline') {
573
- return [
574
- ...header,
575
- ...start,
576
- ` const searchTerm = \`ai-${slug}-\${Date.now().toString().slice(-6)}\`;`,
577
- ' await channelsPage.postMessage(searchTerm);',
578
- ' await channelsPage.globalHeader.openSearch();',
579
- ' await channelsPage.searchBox.searchInput.fill(searchTerm);',
580
- " await channelsPage.page.keyboard.press('Enter');",
581
- " await expect(channelsPage.page.locator('#searchContainer')).toBeVisible();",
582
- ...end,
583
- ].join('\n');
584
- }
585
- return [
586
- ...header,
587
- ...start,
588
- ' await expect(channelsPage.page).toHaveURL(/\\/channels\\//);',
589
- " await expect(channelsPage.page.locator('#channelHeaderTitle')).toBeVisible();",
590
- ...end,
591
- ].join('\n');
592
- }
593
- function resolvePlaywrightBinary(testsRoot) {
594
- const unixPath = join(testsRoot, 'node_modules', '.bin', 'playwright');
595
- const windowsPath = join(testsRoot, 'node_modules', '.bin', 'playwright.cmd');
596
- if (existsSync(unixPath)) {
597
- return unixPath;
598
- }
599
- if (existsSync(windowsPath)) {
600
- return windowsPath;
601
- }
602
- return null;
603
- }
604
- function summarizeCommandOutput(stdout, stderr) {
605
- const combined = [stdout, stderr].filter(Boolean).join('\n').trim();
606
- if (!combined) {
607
- return '';
608
- }
609
- const lines = combined.split('\n').slice(-20);
610
- return lines.join('\n').slice(0, 2000);
611
- }
612
- function runCommand(command, args, cwd, timeoutMs = 60 * 60 * 1000) {
613
- // When spawning `claude`, unset CLAUDECODE so nested invocations are allowed.
614
- // Claude Code sets this variable to block nested sessions; child processes
615
- // that spawn their own claude instance must run without it.
616
- let env;
617
- if (command === 'claude') {
618
- const { CLAUDECODE: _, ...rest } = process.env;
619
- env = rest;
620
- }
621
- const result = spawnSync(command, args, {
622
- cwd,
623
- encoding: 'utf-8',
624
- timeout: timeoutMs,
625
- stdio: 'pipe',
626
- ...(env ? { env } : {}),
627
- });
628
- return {
629
- status: result.status ?? 1,
630
- stdout: result.stdout || '',
631
- stderr: result.stderr || '',
632
- error: result.error ? result.error.message : undefined,
633
- };
634
- }
635
- function resolveMcpCommandTimeoutMs(pipeline) {
636
- const value = pipeline.mcpCommandTimeoutMs;
637
- if (typeof value !== 'number' || !Number.isFinite(value)) {
638
- return 180000;
639
- }
640
- return Math.max(60000, Math.min(15 * 60 * 1000, Math.round(value)));
641
- }
642
- function resolveMcpRetries(pipeline) {
643
- const value = pipeline.mcpRetries;
644
- if (typeof value !== 'number' || !Number.isFinite(value)) {
645
- return 1;
646
- }
647
- return Math.max(0, Math.min(5, Math.round(value)));
648
- }
649
- function isRetryableMcpFailure(result) {
650
- const haystack = [result.error || '', result.stderr || '', result.stdout || ''].join('\n').toLowerCase();
651
- return haystack.includes('etimedout') ||
652
- haystack.includes('timed out') ||
653
- haystack.includes('econnreset') ||
654
- haystack.includes('429') ||
655
- haystack.includes('rate limit') ||
656
- haystack.includes('temporar');
657
- }
658
- function runCommandWithRetries(command, args, cwd, timeoutMs, retries) {
659
- let result = runCommand(command, args, cwd, timeoutMs);
660
- for (let attempt = 1; attempt <= retries; attempt += 1) {
661
- if (result.status === 0) {
662
- return result;
663
- }
664
- if (!isRetryableMcpFailure(result)) {
665
- return result;
666
- }
667
- result = runCommand(command, args, cwd, timeoutMs);
668
- }
669
- return result;
670
- }
671
- function runPlaywrightRuntimeValidation(testsRoot, testFile, pipeline, playwrightBinary) {
672
- if (!playwrightBinary) {
673
- return {
674
- status: 'failed',
675
- detail: 'Playwright binary not found; cannot execute runtime validation.',
676
- };
677
- }
678
- const relativeSpecPath = normalizePath(relative(testsRoot, testFile));
679
- if (relativeSpecPath.startsWith('../') || relativeSpecPath.startsWith('..\\')) {
680
- return {
681
- status: 'failed',
682
- detail: 'Generated spec path resolved outside testsRoot during runtime validation.',
683
- };
684
- }
685
- const args = ['test', relativeSpecPath, '--workers', '1', '--retries', '0', '--max-failures', '1', '--reporter', 'line'];
686
- if (pipeline.headless === false) {
687
- args.push('--headed');
688
- }
689
- if (pipeline.project) {
690
- args.push('--project', pipeline.project);
691
- }
692
- const commandResult = runCommand(playwrightBinary, args, testsRoot, 10 * 60 * 1000);
693
- if (commandResult.status === 0) {
694
- return { status: 'passed' };
695
- }
696
- const summary = summarizeCommandOutput(commandResult.stdout, commandResult.stderr);
697
- return {
698
- status: 'failed',
699
- detail: summary || commandResult.error || `playwright test failed with status ${commandResult.status}`,
700
- };
701
- }
702
- function runPlaywrightListValidation(testsRoot, testFile, pipeline, playwrightBinary) {
703
- if (!playwrightBinary) {
704
- return {
705
- status: 'skipped',
706
- detail: 'Playwright binary not found under testsRoot/node_modules/.bin; runtime compile validation skipped.',
707
- };
708
- }
709
- const relativeSpecPath = normalizePath(relative(testsRoot, testFile));
710
- if (relativeSpecPath.startsWith('../') || relativeSpecPath.startsWith('..\\')) {
711
- return {
712
- status: 'failed',
713
- detail: 'Generated spec path resolved outside testsRoot during validation.',
714
- };
715
- }
716
- const args = ['test', '--list', relativeSpecPath];
717
- if (pipeline.headless === false) {
718
- args.push('--headed');
719
- }
720
- if (pipeline.project) {
721
- args.push('--project', pipeline.project);
722
- }
723
- const commandResult = runCommand(playwrightBinary, args, testsRoot);
724
- if (commandResult.error && /ENOENT/.test(commandResult.error)) {
725
- return {
726
- status: 'skipped',
727
- detail: 'Playwright binary was not executable; runtime compile validation skipped.',
728
- };
729
- }
730
- if (commandResult.status === 0) {
731
- return { status: 'passed' };
732
- }
733
- const summary = summarizeCommandOutput(commandResult.stdout, commandResult.stderr);
734
- return {
735
- status: 'failed',
736
- detail: summary || commandResult.error || `playwright --list failed with status ${commandResult.status}`,
737
- };
738
- }
739
- function runPackageNativeFlow(testsRoot, flow, pipeline, outputDir, testFile, playwrightBinary, apiSurface) {
740
- const flowId = flow.id;
741
- const flowName = flow.name;
742
- const existingFile = existsSync(testFile);
743
- const originalContent = existingFile ? readFileSync(testFile, 'utf-8') : null;
744
- if (existingFile && !pipeline.heal) {
745
- return {
746
- flowId,
747
- flowName,
748
- generatedDir: outputDir,
749
- generateStatus: 'skipped',
750
- };
751
- }
752
- const slug = toSafeSlug(flow.id);
753
- const strategies = buildNativeStrategyOrder(flow);
754
- const attempts = [];
755
- const candidates = [];
756
- if (pipeline.heal && originalContent !== null) {
757
- candidates.push({
758
- label: 'existing',
759
- content: originalContent,
760
- write: false,
761
- });
762
- }
763
- for (const strategy of strategies) {
764
- candidates.push({
765
- label: strategy,
766
- strategy,
767
- content: createNativePlaywrightSpec(flow, slug, strategy),
768
- write: true,
769
- });
770
- }
771
- mkdirSync(outputDir, { recursive: true });
772
- let wroteNewFile = false;
773
- for (let i = 0; i < candidates.length; i += 1) {
774
- const candidate = candidates[i];
775
- if (candidate.write) {
776
- writeFileSync(testFile, candidate.content, 'utf-8');
777
- wroteNewFile = true;
778
- }
779
- const currentContent = candidate.write ? candidate.content : (originalContent || '');
780
- const qualityIssues = validateGeneratedSpecContent(currentContent, apiSurface);
781
- if (qualityIssues.length > 0) {
782
- attempts.push(`${candidate.label}: ${qualityIssues.map((issue) => issue.message).join(' ')}`);
783
- if (pipeline.heal && i < candidates.length - 1) {
784
- continue;
785
- }
786
- if (originalContent !== null) {
787
- writeFileSync(testFile, originalContent, 'utf-8');
788
- }
789
- else if (wroteNewFile && existsSync(testFile)) {
790
- rmSync(testFile, { force: true });
791
- }
792
- return {
793
- flowId,
794
- flowName,
795
- generatedDir: outputDir,
796
- generateStatus: 'failed',
797
- healStatus: pipeline.heal ? 'failed' : undefined,
798
- error: `Quality checks failed. Attempts: ${attempts.join(' | ')}`,
799
- };
800
- }
801
- if (pipeline.heal) {
802
- const validation = runPlaywrightListValidation(testsRoot, testFile, pipeline, playwrightBinary);
803
- if (validation.status === 'failed') {
804
- attempts.push(`${candidate.label}: ${validation.detail || 'playwright validation failed'}`);
805
- if (i < candidates.length - 1) {
806
- continue;
807
- }
808
- if (originalContent !== null) {
809
- writeFileSync(testFile, originalContent, 'utf-8');
810
- }
811
- else if (wroteNewFile && existsSync(testFile)) {
812
- rmSync(testFile, { force: true });
813
- }
814
- return {
815
- flowId,
816
- flowName,
817
- generatedDir: outputDir,
818
- generateStatus: 'failed',
819
- healStatus: 'failed',
820
- error: `Heal validation failed. Attempts: ${attempts.join(' | ')}`,
821
- };
822
- }
823
- }
824
- return {
825
- flowId,
826
- flowName,
827
- generatedDir: outputDir,
828
- generateStatus: candidate.write ? 'success' : 'skipped',
829
- healStatus: pipeline.heal ? 'success' : undefined,
830
- };
831
- }
832
- if (originalContent !== null) {
833
- writeFileSync(testFile, originalContent, 'utf-8');
834
- }
835
- else if (wroteNewFile && existsSync(testFile)) {
836
- rmSync(testFile, { force: true });
837
- }
838
- return {
839
- flowId,
840
- flowName,
841
- generatedDir: outputDir,
842
- generateStatus: 'failed',
843
- healStatus: pipeline.heal ? 'failed' : undefined,
844
- error: attempts.length > 0 ? attempts.join(' | ') : 'No generation candidates were available.',
845
- };
846
- }
847
- function runPackageNativePipeline(testsRoot, flows, pipeline, baseWarnings = []) {
848
- const warningSet = new Set(baseWarnings);
849
- const mcp = createMcpStatus('package-native', Boolean(pipeline.mcp));
850
- const playwrightBinary = pipeline.heal ? resolvePlaywrightBinary(testsRoot) : null;
851
- const seedFile = resolveAgentSeedSpec(testsRoot) || 'specs/seed.spec.ts';
852
- const apiSurface = buildApiSurfaceCatalog(testsRoot, seedFile);
853
- if (pipeline.heal && !playwrightBinary) {
854
- warningSet.add('Playwright binary was not found. Heal uses static quality checks without runtime compile validation.');
855
- }
856
- const results = [];
857
- const outputBase = resolve(testsRoot, pipeline.outputDir || 'specs/functional/ai-assisted');
858
- if (!isPathWithinRoot(testsRoot, outputBase)) {
859
- warningSet.add(`Pipeline outputDir resolves outside testsRoot and was blocked: ${pipeline.outputDir}`);
860
- return { runner: 'unknown', results, warnings: Array.from(warningSet), mcp: createMcpStatus('unknown', Boolean(pipeline.mcp)) };
861
- }
862
- for (const flow of flows) {
863
- if (flow.priority !== 'P0' && flow.priority !== 'P1') {
864
- continue;
865
- }
866
- const slug = toSafeSlug(flow.id);
867
- const outputDir = normalizePath(join(outputBase, slug));
868
- if (!isPathWithinRoot(testsRoot, outputDir)) {
869
- results.push({
870
- flowId: flow.id,
871
- flowName: flow.name,
872
- generatedDir: outputDir,
873
- generateStatus: 'failed',
874
- error: 'output directory resolves outside testsRoot',
875
- });
876
- continue;
877
- }
878
- if (pipeline.dryRun) {
879
- results.push({
880
- flowId: flow.id,
881
- flowName: flow.name,
882
- generatedDir: outputDir,
883
- generateStatus: 'skipped',
884
- healStatus: pipeline.heal ? 'skipped' : undefined,
885
- });
886
- continue;
887
- }
888
- const testFile = normalizePath(join(outputDir, `${slug}.spec.ts`));
889
- if (!isPathWithinRoot(testsRoot, testFile)) {
890
- results.push({
891
- flowId: flow.id,
892
- flowName: flow.name,
893
- generatedDir: outputDir,
894
- generateStatus: 'failed',
895
- error: 'generated test path resolves outside testsRoot',
896
- });
897
- continue;
898
- }
899
- results.push(runPackageNativeFlow(testsRoot, flow, pipeline, outputDir, testFile, playwrightBinary, apiSurface));
900
- }
901
- return { runner: 'package-native', results, warnings: Array.from(warningSet), mcp };
902
- }
903
15
  export function runTargetedSpecHeal(testsRoot, targets, pipeline) {
904
16
  const warnings = new Set();
905
17
  const results = [];
@@ -976,417 +88,6 @@ export function runTargetedSpecHeal(testsRoot, targets, pipeline) {
976
88
  mcp,
977
89
  });
978
90
  }
979
- function findSpecFiles(root) {
980
- if (!existsSync(root)) {
981
- return [];
982
- }
983
- const entries = readdirSync(root, { withFileTypes: true });
984
- const files = [];
985
- for (const entry of entries) {
986
- const fullPath = join(root, entry.name);
987
- if (entry.isDirectory()) {
988
- files.push(...findSpecFiles(fullPath));
989
- }
990
- else if (entry.isFile() && entry.name.endsWith('.spec.ts')) {
991
- files.push(fullPath);
992
- }
993
- }
994
- return files;
995
- }
996
- function findDisallowedDescribeFiles(root) {
997
- const files = findSpecFiles(root);
998
- return files.filter((file) => /\btest\.describe\s*\(/.test(readFileSync(file, 'utf-8')));
999
- }
1000
- function hasCommand(command, cwd) {
1001
- const result = runCommand(command, ['--version'], cwd);
1002
- return result.status === 0;
1003
- }
1004
- function hasPlaywrightAgentDefinitions(testsRoot) {
1005
- const required = [
1006
- '.mcp.json',
1007
- '.claude/agents/playwright-test-planner.md',
1008
- '.claude/agents/playwright-test-generator.md',
1009
- '.claude/agents/playwright-test-healer.md',
1010
- ];
1011
- return required.every((path) => existsSync(join(testsRoot, path)));
1012
- }
1013
- function hasPlaywrightConfig(testsRoot) {
1014
- const candidates = [
1015
- 'playwright.config.ts',
1016
- 'playwright.config.js',
1017
- 'playwright.config.mts',
1018
- 'playwright.config.mjs',
1019
- 'playwright.config.cts',
1020
- 'playwright.config.cjs',
1021
- ];
1022
- return candidates.some((candidate) => existsSync(join(testsRoot, candidate)));
1023
- }
1024
- function bootstrapPlaywrightAgentDefinitions(testsRoot, pipeline, timeoutMs) {
1025
- const args = ['playwright', 'init-agents', '--loop=claude', '--prompts'];
1026
- if (pipeline.project) {
1027
- args.push('--project', pipeline.project);
1028
- }
1029
- return runCommand('npx', args, testsRoot, timeoutMs);
1030
- }
1031
- function resolveAgentSeedSpec(testsRoot) {
1032
- const preferred = join(testsRoot, 'specs', 'seed.spec.ts');
1033
- const specsRoot = join(testsRoot, 'specs');
1034
- const specFiles = findSpecFiles(specsRoot).filter((file) => !normalizePath(file).includes('/functional/ai-assisted/'));
1035
- const scored = specFiles
1036
- .map((file) => {
1037
- const rel = normalizePath(relative(testsRoot, file));
1038
- const content = readFileSync(file, 'utf-8');
1039
- let score = 0;
1040
- if (rel.endsWith('/seed.spec.ts')) {
1041
- // Generated default seed from init-agents is often a placeholder; prefer real tests.
1042
- if (!/generate code here/i.test(content)) {
1043
- score += 2;
1044
- }
1045
- }
1046
- if (content.includes('@mattermost/playwright-lib')) {
1047
- score += 8;
1048
- }
1049
- if (content.includes('pw.initSetup(')) {
1050
- score += 6;
1051
- }
1052
- if (content.includes('testBrowser.login(')) {
1053
- score += 4;
1054
- }
1055
- if (content.includes('channelsPage')) {
1056
- score += 2;
1057
- }
1058
- if (rel.includes('/functional/channels/')) {
1059
- score += 1;
1060
- }
1061
- return { rel, score };
1062
- })
1063
- .sort((a, b) => b.score - a.score);
1064
- if (scored.length > 0 && scored[0].score > 0) {
1065
- return scored[0].rel;
1066
- }
1067
- if (existsSync(preferred)) {
1068
- return normalizePath(relative(testsRoot, preferred));
1069
- }
1070
- return null;
1071
- }
1072
- function buildPlaywrightAgentsPrompt(flow, seedFile, planFile, testFile, includeHealer) {
1073
- const linkedFiles = firstFlowFiles(flow).join(', ') || 'N/A';
1074
- const reasons = (flow.reasons || []).slice(0, 5).join(' | ') || 'N/A';
1075
- return [
1076
- 'Use official Playwright Test agents (planner, generator, healer) to implement exactly one high-quality test for this flow.',
1077
- '',
1078
- `Flow ID: ${flow.id}`,
1079
- `Flow Name: ${flow.name}`,
1080
- `Priority: ${flow.priority}`,
1081
- `Linked files: ${linkedFiles}`,
1082
- `Risk reasons: ${reasons}`,
1083
- '',
1084
- 'Workflow requirements:',
1085
- '1) Use #playwright-test-planner to explore and save a focused test plan.',
1086
- '2) Use #playwright-test-generator to generate one test from that plan.',
1087
- includeHealer
1088
- ? '3) Use #playwright-test-healer to run and fix that generated test.'
1089
- : '3) Skip runtime healing and focus on producing compile-ready test code.',
1090
- '',
1091
- `Seed file: ${seedFile}`,
1092
- `Plan file to save: ${planFile}`,
1093
- `Generated test file path (must be exact): ${testFile}`,
1094
- '',
1095
- 'Quality constraints (must follow):',
1096
- '- The generated file must contain a standalone test() and must not use test.describe or test.only.',
1097
- '- Do not mark the test with test.fixme unless user explicitly requests skipping.',
1098
- "- The generated test must include a single tag string '@ai-assisted'.",
1099
- '- Match fixture/import style from the seed file. Prefer existing page-object APIs over raw brittle selectors.',
1100
- '- Only use `pw` and page-object methods that already exist in the seed/current specs (for example, do not invent APIs like `pw.mainClient.*`).',
1101
- '- For system-console/admin flows, avoid `systemConsolePage.toBeVisible()` and brittle class selectors (`.backstage-navbar`, `.admin-console__wrapper`, `.left-panel`, `.panel-card`).',
1102
- '- Prefer stable assertions using URL patterns, test IDs, roles, labels, and established page-object methods.',
1103
- '- Keep the scenario strictly aligned to the flow and linked files, not broad unrelated flows.',
1104
- '',
1105
- 'At the end, return a short summary that includes the generated test file path and whether healing succeeded.',
1106
- ].join('\n');
1107
- }
1108
- function buildPlaywrightHealerPrompt(testFile, extra) {
1109
- const lines = [
1110
- 'Heal this specific Playwright test file and keep edits minimal.',
1111
- `Target test file: ${testFile}`,
1112
- 'Constraints:',
1113
- '- Do not use test.describe or test.only.',
1114
- "- Keep a single tag string '@ai-assisted'.",
1115
- '- Use only existing Mattermost Playwright fixture/page-object APIs; do not invent new `pw.*` clients or methods.',
1116
- '- Avoid `systemConsolePage.toBeVisible()` and brittle class selectors (`.backstage-navbar`, `.admin-console__wrapper`, `.left-panel`, `.panel-card`).',
1117
- '- Prefer stable checks with URL/test IDs/roles/page-object methods.',
1118
- '- Keep the test intent unchanged and focused.',
1119
- '',
1120
- 'Run and fix this test until it compiles/passes, or mark test.fixme with a clear comment when behavior is truly broken.',
1121
- ];
1122
- if (extra) {
1123
- lines.push('', `Context: ${extra}`);
1124
- }
1125
- return lines.join('\n');
1126
- }
1127
- function runPlaywrightAgentsFlow(testsRoot, flow, pipeline, outputDir, preferredTestFile, seedFile, apiSurface, playwrightBinary, mcpTimeoutMs, mcpRetries) {
1128
- mkdirSync(outputDir, { recursive: true });
1129
- const slug = toSafeSlug(flow.id);
1130
- const planFile = normalizePath(relative(testsRoot, join(outputDir, `${slug}.plan.md`)));
1131
- const absolutePlanFile = join(testsRoot, planFile);
1132
- const targetTestFile = normalizePath(relative(testsRoot, preferredTestFile));
1133
- const existingSpecFiles = findSpecFiles(outputDir);
1134
- const existingSpecSnapshots = new Map();
1135
- for (const specFile of existingSpecFiles) {
1136
- try {
1137
- existingSpecSnapshots.set(specFile, readFileSync(specFile, 'utf-8'));
1138
- }
1139
- catch {
1140
- continue;
1141
- }
1142
- }
1143
- const originalPlanContent = existsSync(absolutePlanFile) ? readFileSync(absolutePlanFile, 'utf-8') : null;
1144
- const restoreArtifactsOnFailure = () => {
1145
- for (const currentSpecFile of findSpecFiles(outputDir)) {
1146
- const originalSpecContent = existingSpecSnapshots.get(currentSpecFile);
1147
- if (originalSpecContent === undefined) {
1148
- rmSync(currentSpecFile, { force: true });
1149
- continue;
1150
- }
1151
- try {
1152
- if (readFileSync(currentSpecFile, 'utf-8') !== originalSpecContent) {
1153
- writeFileSync(currentSpecFile, originalSpecContent, 'utf-8');
1154
- }
1155
- }
1156
- catch {
1157
- // best-effort restore only
1158
- }
1159
- }
1160
- for (const [specFile, originalSpecContent] of existingSpecSnapshots.entries()) {
1161
- if (!existsSync(specFile)) {
1162
- writeFileSync(specFile, originalSpecContent, 'utf-8');
1163
- }
1164
- }
1165
- if (originalPlanContent === null) {
1166
- rmSync(absolutePlanFile, { force: true });
1167
- }
1168
- else {
1169
- try {
1170
- if (!existsSync(absolutePlanFile) || readFileSync(absolutePlanFile, 'utf-8') !== originalPlanContent) {
1171
- writeFileSync(absolutePlanFile, originalPlanContent, 'utf-8');
1172
- }
1173
- }
1174
- catch {
1175
- // best-effort restore only
1176
- }
1177
- }
1178
- };
1179
- const failFlow = (error) => {
1180
- restoreArtifactsOnFailure();
1181
- return {
1182
- flowId: flow.id,
1183
- flowName: flow.name,
1184
- generatedDir: outputDir,
1185
- generateStatus: 'failed',
1186
- healStatus: pipeline.heal ? 'failed' : undefined,
1187
- error,
1188
- };
1189
- };
1190
- if (pipeline.dryRun) {
1191
- return {
1192
- flowId: flow.id,
1193
- flowName: flow.name,
1194
- generatedDir: outputDir,
1195
- generateStatus: 'skipped',
1196
- healStatus: pipeline.heal ? 'skipped' : undefined,
1197
- };
1198
- }
1199
- const prompt = buildPlaywrightAgentsPrompt(flow, seedFile, planFile, targetTestFile, Boolean(pipeline.heal));
1200
- const runArgs = [
1201
- '-p',
1202
- '--permission-mode',
1203
- 'bypassPermissions',
1204
- '--setting-sources',
1205
- 'project,local',
1206
- '--strict-mcp-config',
1207
- '--mcp-config',
1208
- '.mcp.json',
1209
- '--add-dir',
1210
- testsRoot,
1211
- '--',
1212
- prompt,
1213
- ];
1214
- const runResult = runCommandWithRetries('claude', runArgs, testsRoot, mcpTimeoutMs, mcpRetries);
1215
- if (runResult.status !== 0) {
1216
- return failFlow(summarizeCommandOutput(runResult.stdout, runResult.stderr) || runResult.error || 'Playwright agents run failed');
1217
- }
1218
- let actualTestFile = preferredTestFile;
1219
- if (!existsSync(actualTestFile)) {
1220
- const candidates = findSpecFiles(outputDir);
1221
- if (candidates.length === 1) {
1222
- actualTestFile = candidates[0];
1223
- }
1224
- }
1225
- if (!existsSync(actualTestFile)) {
1226
- return failFlow(`Playwright agents did not produce expected test file: ${targetTestFile}`);
1227
- }
1228
- const relativeActualTestFile = normalizePath(relative(testsRoot, actualTestFile));
1229
- let qualityIssues = validateGeneratedSpecContent(readFileSync(actualTestFile, 'utf-8'), apiSurface);
1230
- if (qualityIssues.length > 0 && pipeline.heal) {
1231
- const healResult = runCommandWithRetries('claude', [
1232
- '-p',
1233
- '--permission-mode',
1234
- 'bypassPermissions',
1235
- '--setting-sources',
1236
- 'project,local',
1237
- '--strict-mcp-config',
1238
- '--agent',
1239
- 'playwright-test-healer',
1240
- '--mcp-config',
1241
- '.mcp.json',
1242
- '--add-dir',
1243
- testsRoot,
1244
- '--',
1245
- buildPlaywrightHealerPrompt(relativeActualTestFile, qualityIssues.map((issue) => issue.message).join(' | ')),
1246
- ], testsRoot, mcpTimeoutMs, mcpRetries);
1247
- if (healResult.status === 0 && existsSync(actualTestFile)) {
1248
- qualityIssues = validateGeneratedSpecContent(readFileSync(actualTestFile, 'utf-8'), apiSurface);
1249
- }
1250
- }
1251
- if (qualityIssues.length > 0) {
1252
- return failFlow(`Playwright agents produced invalid test content: ${qualityIssues.map((issue) => issue.message).join(' | ')}`);
1253
- }
1254
- if (pipeline.heal) {
1255
- let compileValidation = runPlaywrightListValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
1256
- if (compileValidation.status === 'failed') {
1257
- const healResult = runCommandWithRetries('claude', [
1258
- '-p',
1259
- '--permission-mode',
1260
- 'bypassPermissions',
1261
- '--setting-sources',
1262
- 'project,local',
1263
- '--strict-mcp-config',
1264
- '--agent',
1265
- 'playwright-test-healer',
1266
- '--mcp-config',
1267
- '.mcp.json',
1268
- '--add-dir',
1269
- testsRoot,
1270
- '--',
1271
- buildPlaywrightHealerPrompt(relativeActualTestFile, compileValidation.detail || 'playwright --list failed'),
1272
- ], testsRoot, mcpTimeoutMs, mcpRetries);
1273
- if (healResult.status === 0 && existsSync(actualTestFile)) {
1274
- compileValidation = runPlaywrightListValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
1275
- }
1276
- if (compileValidation.status === 'failed') {
1277
- return failFlow(`Playwright agents compile validation failed: ${compileValidation.detail || 'playwright --list failed'}`);
1278
- }
1279
- }
1280
- let runtimeValidation = runPlaywrightRuntimeValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
1281
- if (runtimeValidation.status === 'failed') {
1282
- const healResult = runCommandWithRetries('claude', [
1283
- '-p',
1284
- '--permission-mode',
1285
- 'bypassPermissions',
1286
- '--setting-sources',
1287
- 'project,local',
1288
- '--strict-mcp-config',
1289
- '--agent',
1290
- 'playwright-test-healer',
1291
- '--mcp-config',
1292
- '.mcp.json',
1293
- '--add-dir',
1294
- testsRoot,
1295
- '--',
1296
- buildPlaywrightHealerPrompt(relativeActualTestFile, runtimeValidation.detail || 'playwright runtime failed'),
1297
- ], testsRoot, mcpTimeoutMs, mcpRetries);
1298
- if (healResult.status === 0 && existsSync(actualTestFile)) {
1299
- runtimeValidation = runPlaywrightRuntimeValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
1300
- }
1301
- if (runtimeValidation.status === 'failed') {
1302
- return failFlow(`Playwright agents runtime validation failed: ${runtimeValidation.detail || 'playwright test failed'}`);
1303
- }
1304
- }
1305
- }
1306
- return {
1307
- flowId: flow.id,
1308
- flowName: flow.name,
1309
- generatedDir: outputDir,
1310
- generateStatus: 'success',
1311
- healStatus: pipeline.heal ? 'success' : undefined,
1312
- };
1313
- }
1314
- function runPlaywrightAgentsPipeline(testsRoot, flows, pipeline) {
1315
- const warnings = [];
1316
- const results = [];
1317
- const mcpTimeoutMs = resolveMcpCommandTimeoutMs(pipeline);
1318
- const mcpRetries = resolveMcpRetries(pipeline);
1319
- if (!hasCommand('claude', testsRoot)) {
1320
- warnings.push('Claude CLI is required for official Playwright planner/generator/healer execution but was not found.');
1321
- return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
1322
- }
1323
- if (!hasPlaywrightConfig(testsRoot)) {
1324
- warnings.push('Playwright config file not found in testsRoot; skipping official Playwright agents backend.');
1325
- return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
1326
- }
1327
- if (!hasPlaywrightAgentDefinitions(testsRoot)) {
1328
- const bootstrap = bootstrapPlaywrightAgentDefinitions(testsRoot, pipeline, mcpTimeoutMs);
1329
- if (bootstrap.status !== 0) {
1330
- warnings.push(summarizeCommandOutput(bootstrap.stdout, bootstrap.stderr) ||
1331
- bootstrap.error ||
1332
- 'Failed to initialize Playwright agents via `npx playwright init-agents`.');
1333
- return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
1334
- }
1335
- }
1336
- if (!hasPlaywrightAgentDefinitions(testsRoot)) {
1337
- warnings.push('Playwright agent definitions are missing after bootstrap.');
1338
- return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
1339
- }
1340
- const seedFile = resolveAgentSeedSpec(testsRoot);
1341
- if (!seedFile) {
1342
- warnings.push('No seed spec file found under specs/. Playwright planner cannot be initialized.');
1343
- return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
1344
- }
1345
- const playwrightBinary = pipeline.heal ? resolvePlaywrightBinary(testsRoot) : null;
1346
- const apiSurface = buildApiSurfaceCatalog(testsRoot, seedFile);
1347
- if (pipeline.heal && !playwrightBinary) {
1348
- warnings.push('Playwright binary was not found. Healer runtime validation may be limited.');
1349
- }
1350
- const outputBase = resolve(testsRoot, pipeline.outputDir || 'specs/functional/ai-assisted');
1351
- if (!isPathWithinRoot(testsRoot, outputBase)) {
1352
- warnings.push(`Pipeline outputDir resolves outside testsRoot and was blocked: ${pipeline.outputDir}`);
1353
- return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
1354
- }
1355
- for (const flow of flows) {
1356
- if (flow.priority !== 'P0' && flow.priority !== 'P1') {
1357
- continue;
1358
- }
1359
- const slug = toSafeSlug(flow.id);
1360
- const outputDir = normalizePath(join(outputBase, slug));
1361
- if (!isPathWithinRoot(testsRoot, outputDir)) {
1362
- results.push({
1363
- flowId: flow.id,
1364
- flowName: flow.name,
1365
- generatedDir: outputDir,
1366
- generateStatus: 'failed',
1367
- error: 'output directory resolves outside testsRoot',
1368
- });
1369
- continue;
1370
- }
1371
- const testFile = normalizePath(join(outputDir, `${slug}.spec.ts`));
1372
- if (!isPathWithinRoot(testsRoot, testFile)) {
1373
- results.push({
1374
- flowId: flow.id,
1375
- flowName: flow.name,
1376
- generatedDir: outputDir,
1377
- generateStatus: 'failed',
1378
- error: 'generated test path resolves outside testsRoot',
1379
- });
1380
- continue;
1381
- }
1382
- results.push(runPlaywrightAgentsFlow(testsRoot, flow, pipeline, outputDir, testFile, seedFile, apiSurface, playwrightBinary, mcpTimeoutMs, mcpRetries));
1383
- if (pipeline.mcpOnly && results[results.length - 1].generateStatus === 'failed') {
1384
- warnings.push(`MCP-only mode: stopping after first failed flow (${flow.id}).`);
1385
- break;
1386
- }
1387
- }
1388
- return { runner: 'playwright-agents', results, warnings, mcp: createMcpStatus('playwright-agents', true) };
1389
- }
1390
91
  export function runPlaywrightPipeline(testsRoot, flows, pipeline) {
1391
92
  const mcpFallbackWarnings = [];
1392
93
  // MCP-only mode requires MCP to be enabled