@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.
- package/README.md +112 -584
- package/dist/agent/api_catalog.d.ts +11 -0
- package/dist/agent/api_catalog.d.ts.map +1 -0
- package/dist/agent/api_catalog.js +210 -0
- package/dist/agent/llm_agents_flow.d.ts +15 -0
- package/dist/agent/llm_agents_flow.d.ts.map +1 -0
- package/dist/agent/llm_agents_flow.js +434 -0
- package/dist/agent/native_flow.d.ts +6 -0
- package/dist/agent/native_flow.d.ts.map +1 -0
- package/dist/agent/native_flow.js +179 -0
- package/dist/agent/pipeline.d.ts +2 -25
- package/dist/agent/pipeline.d.ts.map +1 -1
- package/dist/agent/pipeline.js +30 -1329
- package/dist/agent/pipeline_types.d.ts +54 -0
- package/dist/agent/pipeline_types.d.ts.map +1 -0
- package/dist/agent/pipeline_types.js +4 -0
- package/dist/agent/pipeline_utils.d.ts +12 -0
- package/dist/agent/pipeline_utils.d.ts.map +1 -0
- package/dist/agent/pipeline_utils.js +156 -0
- package/dist/agent/process_runner.d.ts +10 -0
- package/dist/agent/process_runner.d.ts.map +1 -0
- package/dist/agent/process_runner.js +92 -0
- package/dist/agent/spec_generator.d.ts +5 -0
- package/dist/agent/spec_generator.d.ts.map +1 -0
- package/dist/agent/spec_generator.js +253 -0
- package/dist/agent/validation_runner.d.ts +5 -0
- package/dist/agent/validation_runner.d.ts.map +1 -0
- package/dist/agent/validation_runner.js +77 -0
- package/dist/agentic/playwright_runner.js +1 -1
- package/dist/cli/commands/analyze.d.ts +3 -0
- package/dist/cli/commands/analyze.d.ts.map +1 -0
- package/dist/cli/commands/analyze.js +77 -0
- package/dist/cli/commands/feedback.d.ts +3 -0
- package/dist/cli/commands/feedback.d.ts.map +1 -0
- package/dist/cli/commands/feedback.js +39 -0
- package/dist/cli/commands/finalize.d.ts +3 -0
- package/dist/cli/commands/finalize.d.ts.map +1 -0
- package/dist/cli/commands/finalize.js +41 -0
- package/dist/cli/commands/generate.d.ts +4 -0
- package/dist/cli/commands/generate.d.ts.map +1 -0
- package/dist/cli/commands/generate.js +108 -0
- package/dist/cli/commands/heal.d.ts +3 -0
- package/dist/cli/commands/heal.d.ts.map +1 -0
- package/dist/cli/commands/heal.js +60 -0
- package/dist/cli/commands/impact.d.ts +4 -0
- package/dist/cli/commands/impact.d.ts.map +1 -0
- package/dist/cli/commands/impact.js +26 -0
- package/dist/cli/commands/llm_health.d.ts +2 -0
- package/dist/cli/commands/llm_health.d.ts.map +1 -0
- package/dist/cli/commands/llm_health.js +38 -0
- package/dist/cli/commands/plan.d.ts +4 -0
- package/dist/cli/commands/plan.d.ts.map +1 -0
- package/dist/cli/commands/plan.js +83 -0
- package/dist/cli/commands/traceability.d.ts +4 -0
- package/dist/cli/commands/traceability.d.ts.map +1 -0
- package/dist/cli/commands/traceability.js +77 -0
- package/dist/cli/parse_args.d.ts +6 -0
- package/dist/cli/parse_args.d.ts.map +1 -0
- package/dist/cli/parse_args.js +216 -0
- package/dist/cli/types.d.ts +70 -0
- package/dist/cli/types.d.ts.map +1 -0
- package/dist/cli/types.js +4 -0
- package/dist/cli/usage.d.ts +2 -0
- package/dist/cli/usage.d.ts.map +1 -0
- package/dist/cli/usage.js +86 -0
- package/dist/cli.js +26 -1060
- package/dist/esm/agent/api_catalog.js +199 -0
- package/dist/esm/agent/llm_agents_flow.js +421 -0
- package/dist/esm/agent/native_flow.js +175 -0
- package/dist/esm/agent/pipeline.js +8 -1307
- package/dist/esm/agent/pipeline_types.js +3 -0
- package/dist/esm/agent/pipeline_utils.js +146 -0
- package/dist/esm/agent/process_runner.js +83 -0
- package/dist/esm/agent/spec_generator.js +249 -0
- package/dist/esm/agent/validation_runner.js +73 -0
- package/dist/esm/agentic/playwright_runner.js +1 -1
- package/dist/esm/cli/commands/analyze.js +74 -0
- package/dist/esm/cli/commands/feedback.js +36 -0
- package/dist/esm/cli/commands/finalize.js +38 -0
- package/dist/esm/cli/commands/generate.js +105 -0
- package/dist/esm/cli/commands/heal.js +57 -0
- package/dist/esm/cli/commands/impact.js +23 -0
- package/dist/esm/cli/commands/llm_health.js +35 -0
- package/dist/esm/cli/commands/plan.js +80 -0
- package/dist/esm/cli/commands/traceability.js +73 -0
- package/dist/esm/cli/parse_args.js +210 -0
- package/dist/esm/cli/types.js +3 -0
- package/dist/esm/cli/usage.js +83 -0
- package/dist/esm/cli.js +20 -1054
- package/dist/esm/mcp-server.js +18 -1
- package/dist/mcp-server.d.ts.map +1 -1
- package/dist/mcp-server.js +17 -0
- package/package.json +2 -4
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
2
|
+
// See LICENSE.txt for license information.
|
|
3
|
+
import { existsSync, readFileSync, readdirSync } from 'fs';
|
|
4
|
+
import { join } from 'path';
|
|
5
|
+
export function createDefaultApiSurfaceCatalog() {
|
|
6
|
+
const pwNestedMethods = new Map();
|
|
7
|
+
pwNestedMethods.set('apiClient', new Set([
|
|
8
|
+
'createPost',
|
|
9
|
+
'createDirectChannel',
|
|
10
|
+
'createChannel',
|
|
11
|
+
'getChannels',
|
|
12
|
+
'getChannelByName',
|
|
13
|
+
'getPostsSince',
|
|
14
|
+
]));
|
|
15
|
+
return {
|
|
16
|
+
pwProps: new Set([
|
|
17
|
+
'initSetup',
|
|
18
|
+
'testBrowser',
|
|
19
|
+
'apiInitSetup',
|
|
20
|
+
'apiAdminSetup',
|
|
21
|
+
'apiCreateChannel',
|
|
22
|
+
'apiCreateUser',
|
|
23
|
+
'apiLogin',
|
|
24
|
+
'apiClient',
|
|
25
|
+
]),
|
|
26
|
+
pwNestedMethods,
|
|
27
|
+
initSetupKeys: new Set([
|
|
28
|
+
'user',
|
|
29
|
+
'team',
|
|
30
|
+
'adminClient',
|
|
31
|
+
'adminUser',
|
|
32
|
+
'adminConfig',
|
|
33
|
+
'userClient',
|
|
34
|
+
'offTopicUrl',
|
|
35
|
+
'townSquareUrl',
|
|
36
|
+
]),
|
|
37
|
+
initSetupVariableMethods: new Map(),
|
|
38
|
+
testBrowserMethods: new Set([
|
|
39
|
+
'login',
|
|
40
|
+
'openNewBrowserContext',
|
|
41
|
+
'newContext',
|
|
42
|
+
]),
|
|
43
|
+
channelsPageMembers: new Set([
|
|
44
|
+
'goto',
|
|
45
|
+
'page',
|
|
46
|
+
'postMessage',
|
|
47
|
+
'getLastPost',
|
|
48
|
+
'sidebarRight',
|
|
49
|
+
'openChannelSettings',
|
|
50
|
+
'newChannel',
|
|
51
|
+
'globalHeader',
|
|
52
|
+
'searchBox',
|
|
53
|
+
]),
|
|
54
|
+
sidebarRightMembers: new Set([
|
|
55
|
+
'openThreadForPost',
|
|
56
|
+
'postMessage',
|
|
57
|
+
'getLastPost',
|
|
58
|
+
]),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function collectMatches(content, pattern) {
|
|
62
|
+
const out = new Set();
|
|
63
|
+
for (const match of content.matchAll(pattern)) {
|
|
64
|
+
const value = match[1];
|
|
65
|
+
if (value) {
|
|
66
|
+
out.add(value);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
export function addNestedMethod(catalog, objectName, methodName) {
|
|
72
|
+
const methods = catalog.pwNestedMethods.get(objectName) || new Set();
|
|
73
|
+
methods.add(methodName);
|
|
74
|
+
catalog.pwNestedMethods.set(objectName, methods);
|
|
75
|
+
}
|
|
76
|
+
export function escapeRegExp(value) {
|
|
77
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
78
|
+
}
|
|
79
|
+
export function parseInitSetupBindings(content) {
|
|
80
|
+
const bindings = [];
|
|
81
|
+
for (const match of content.matchAll(/(?:const|let|var)\s*\{\s*([^}]+)\s*\}\s*=\s*await\s+pw\.initSetup\s*\(/g)) {
|
|
82
|
+
const raw = match[1];
|
|
83
|
+
if (!raw) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
for (const part of raw.split(',')) {
|
|
87
|
+
const cleaned = part.trim();
|
|
88
|
+
if (!cleaned) {
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const [leftRaw, rightRaw] = cleaned.split(':');
|
|
92
|
+
const key = (leftRaw || '').trim();
|
|
93
|
+
const variableCandidate = (rightRaw || leftRaw || '').trim().split('=')[0]?.trim();
|
|
94
|
+
if (!key || !variableCandidate) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
bindings.push({ key, variable: variableCandidate });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return bindings;
|
|
101
|
+
}
|
|
102
|
+
export function collectDestructuredInitSetupKeys(content) {
|
|
103
|
+
return new Set(parseInitSetupBindings(content).map((binding) => binding.key));
|
|
104
|
+
}
|
|
105
|
+
export function addInitSetupVariableMethod(catalog, variable, methodName) {
|
|
106
|
+
const methods = catalog.initSetupVariableMethods.get(variable) || new Set();
|
|
107
|
+
methods.add(methodName);
|
|
108
|
+
catalog.initSetupVariableMethods.set(variable, methods);
|
|
109
|
+
}
|
|
110
|
+
export function collectApiSurfaceFromContent(content, catalog) {
|
|
111
|
+
for (const prop of collectMatches(content, /\bpw\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
|
|
112
|
+
catalog.pwProps.add(prop);
|
|
113
|
+
}
|
|
114
|
+
for (const match of content.matchAll(/\bpw\.([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
|
|
115
|
+
const objectName = match[1];
|
|
116
|
+
const methodName = match[2];
|
|
117
|
+
if (!objectName || !methodName) {
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
addNestedMethod(catalog, objectName, methodName);
|
|
121
|
+
}
|
|
122
|
+
for (const method of collectMatches(content, /\bpw\.testBrowser\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
|
|
123
|
+
catalog.testBrowserMethods.add(method);
|
|
124
|
+
}
|
|
125
|
+
for (const member of collectMatches(content, /\bchannelsPage\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
|
|
126
|
+
catalog.channelsPageMembers.add(member);
|
|
127
|
+
}
|
|
128
|
+
for (const member of collectMatches(content, /\bchannelsPage\.sidebarRight\.([A-Za-z_][A-Za-z0-9_]*)\b/g)) {
|
|
129
|
+
catalog.sidebarRightMembers.add(member);
|
|
130
|
+
}
|
|
131
|
+
for (const binding of parseInitSetupBindings(content)) {
|
|
132
|
+
catalog.initSetupKeys.add(binding.key);
|
|
133
|
+
const methodPattern = new RegExp(`\\b${escapeRegExp(binding.variable)}\\.([A-Za-z_][A-Za-z0-9_]*)\\b`, 'g');
|
|
134
|
+
for (const method of collectMatches(content, methodPattern)) {
|
|
135
|
+
addInitSetupVariableMethod(catalog, binding.variable, method);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export function buildApiSurfaceCatalog(testsRoot, seedFile) {
|
|
140
|
+
const catalog = createDefaultApiSurfaceCatalog();
|
|
141
|
+
const candidateRoots = [
|
|
142
|
+
join(testsRoot, 'specs'),
|
|
143
|
+
join(testsRoot, 'tests'),
|
|
144
|
+
];
|
|
145
|
+
const files = [];
|
|
146
|
+
for (const root of candidateRoots) {
|
|
147
|
+
if (!existsSync(root)) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
const stack = [root];
|
|
151
|
+
while (stack.length > 0) {
|
|
152
|
+
const current = stack.pop();
|
|
153
|
+
let entries;
|
|
154
|
+
try {
|
|
155
|
+
entries = readdirSync(current, { withFileTypes: true });
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
for (const entry of entries) {
|
|
161
|
+
const full = join(current, entry.name);
|
|
162
|
+
if (entry.isDirectory()) {
|
|
163
|
+
if (entry.name === 'node_modules' || entry.name === '.git' || entry.name === 'dist') {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
stack.push(full);
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (!entry.isFile()) {
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (!/\.(spec|test)\.[jt]sx?$/.test(entry.name)) {
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
files.push(full);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const uniqueFiles = Array.from(new Set(files)).slice(0, 2500);
|
|
180
|
+
for (const filePath of uniqueFiles) {
|
|
181
|
+
try {
|
|
182
|
+
const content = readFileSync(filePath, 'utf-8');
|
|
183
|
+
collectApiSurfaceFromContent(content, catalog);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
const absoluteSeed = join(testsRoot, seedFile);
|
|
190
|
+
if (existsSync(absoluteSeed)) {
|
|
191
|
+
try {
|
|
192
|
+
collectApiSurfaceFromContent(readFileSync(absoluteSeed, 'utf-8'), catalog);
|
|
193
|
+
}
|
|
194
|
+
catch {
|
|
195
|
+
// ignore seed read failures; defaults + catalog scan still apply
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return catalog;
|
|
199
|
+
}
|
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
2
|
+
// See LICENSE.txt for license information.
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'fs';
|
|
4
|
+
import { join, relative, resolve } from 'path';
|
|
5
|
+
import { isPathWithinRoot, normalizePath } from './utils.js';
|
|
6
|
+
import { createMcpStatus, firstFlowFiles, toSafeSlug } from './pipeline_utils.js';
|
|
7
|
+
import { validateGeneratedSpecContent } from './spec_generator.js';
|
|
8
|
+
import { resolvePlaywrightBinary, runCommand, runCommandWithRetries, summarizeCommandOutput, resolveMcpCommandTimeoutMs, resolveMcpRetries } from './process_runner.js';
|
|
9
|
+
import { runPlaywrightListValidation, runPlaywrightRuntimeValidation } from './validation_runner.js';
|
|
10
|
+
import { buildApiSurfaceCatalog } from './api_catalog.js';
|
|
11
|
+
export function findSpecFiles(root) {
|
|
12
|
+
if (!existsSync(root)) {
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
const entries = readdirSync(root, { withFileTypes: true });
|
|
16
|
+
const files = [];
|
|
17
|
+
for (const entry of entries) {
|
|
18
|
+
const fullPath = join(root, entry.name);
|
|
19
|
+
if (entry.isDirectory()) {
|
|
20
|
+
files.push(...findSpecFiles(fullPath));
|
|
21
|
+
}
|
|
22
|
+
else if (entry.isFile() && entry.name.endsWith('.spec.ts')) {
|
|
23
|
+
files.push(fullPath);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return files;
|
|
27
|
+
}
|
|
28
|
+
export function findDisallowedDescribeFiles(root) {
|
|
29
|
+
const files = findSpecFiles(root);
|
|
30
|
+
return files.filter((file) => /\btest\.describe\s*\(/.test(readFileSync(file, 'utf-8')));
|
|
31
|
+
}
|
|
32
|
+
export function hasCommand(command, cwd) {
|
|
33
|
+
const result = runCommand(command, ['--version'], cwd);
|
|
34
|
+
return result.status === 0;
|
|
35
|
+
}
|
|
36
|
+
export function hasPlaywrightAgentDefinitions(testsRoot) {
|
|
37
|
+
const required = [
|
|
38
|
+
'.mcp.json',
|
|
39
|
+
'.claude/agents/playwright-test-planner.md',
|
|
40
|
+
'.claude/agents/playwright-test-generator.md',
|
|
41
|
+
'.claude/agents/playwright-test-healer.md',
|
|
42
|
+
];
|
|
43
|
+
return required.every((path) => existsSync(join(testsRoot, path)));
|
|
44
|
+
}
|
|
45
|
+
export function hasPlaywrightConfig(testsRoot) {
|
|
46
|
+
const candidates = [
|
|
47
|
+
'playwright.config.ts',
|
|
48
|
+
'playwright.config.js',
|
|
49
|
+
'playwright.config.mts',
|
|
50
|
+
'playwright.config.mjs',
|
|
51
|
+
'playwright.config.cts',
|
|
52
|
+
'playwright.config.cjs',
|
|
53
|
+
];
|
|
54
|
+
return candidates.some((candidate) => existsSync(join(testsRoot, candidate)));
|
|
55
|
+
}
|
|
56
|
+
export function bootstrapPlaywrightAgentDefinitions(testsRoot, pipeline, timeoutMs) {
|
|
57
|
+
const args = ['playwright', 'init-agents', '--loop=claude', '--prompts'];
|
|
58
|
+
if (pipeline.project) {
|
|
59
|
+
args.push('--project', pipeline.project);
|
|
60
|
+
}
|
|
61
|
+
return runCommand('npx', args, testsRoot, timeoutMs);
|
|
62
|
+
}
|
|
63
|
+
export function resolveAgentSeedSpec(testsRoot) {
|
|
64
|
+
const preferred = join(testsRoot, 'specs', 'seed.spec.ts');
|
|
65
|
+
const specsRoot = join(testsRoot, 'specs');
|
|
66
|
+
const specFiles = findSpecFiles(specsRoot).filter((file) => !normalizePath(file).includes('/functional/ai-assisted/'));
|
|
67
|
+
const scored = specFiles
|
|
68
|
+
.map((file) => {
|
|
69
|
+
const rel = normalizePath(relative(testsRoot, file));
|
|
70
|
+
const content = readFileSync(file, 'utf-8');
|
|
71
|
+
let score = 0;
|
|
72
|
+
if (rel.endsWith('/seed.spec.ts')) {
|
|
73
|
+
// Generated default seed from init-agents is often a placeholder; prefer real tests.
|
|
74
|
+
if (!/generate code here/i.test(content)) {
|
|
75
|
+
score += 2;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (content.includes('@mattermost/playwright-lib')) {
|
|
79
|
+
score += 8;
|
|
80
|
+
}
|
|
81
|
+
if (content.includes('pw.initSetup(')) {
|
|
82
|
+
score += 6;
|
|
83
|
+
}
|
|
84
|
+
if (content.includes('testBrowser.login(')) {
|
|
85
|
+
score += 4;
|
|
86
|
+
}
|
|
87
|
+
if (content.includes('channelsPage')) {
|
|
88
|
+
score += 2;
|
|
89
|
+
}
|
|
90
|
+
if (rel.includes('/functional/channels/')) {
|
|
91
|
+
score += 1;
|
|
92
|
+
}
|
|
93
|
+
return { rel, score };
|
|
94
|
+
})
|
|
95
|
+
.sort((a, b) => b.score - a.score);
|
|
96
|
+
if (scored.length > 0 && scored[0].score > 0) {
|
|
97
|
+
return scored[0].rel;
|
|
98
|
+
}
|
|
99
|
+
if (existsSync(preferred)) {
|
|
100
|
+
return normalizePath(relative(testsRoot, preferred));
|
|
101
|
+
}
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
export function buildPlaywrightAgentsPrompt(flow, seedFile, planFile, testFile, includeHealer) {
|
|
105
|
+
const linkedFiles = firstFlowFiles(flow).join(', ') || 'N/A';
|
|
106
|
+
const reasons = (flow.reasons || []).slice(0, 5).join(' | ') || 'N/A';
|
|
107
|
+
return [
|
|
108
|
+
'Use official Playwright Test agents (planner, generator, healer) to implement exactly one high-quality test for this flow.',
|
|
109
|
+
'',
|
|
110
|
+
`Flow ID: ${flow.id}`,
|
|
111
|
+
`Flow Name: ${flow.name}`,
|
|
112
|
+
`Priority: ${flow.priority}`,
|
|
113
|
+
`Linked files: ${linkedFiles}`,
|
|
114
|
+
`Risk reasons: ${reasons}`,
|
|
115
|
+
'',
|
|
116
|
+
'Workflow requirements:',
|
|
117
|
+
'1) Use #playwright-test-planner to explore and save a focused test plan.',
|
|
118
|
+
'2) Use #playwright-test-generator to generate one test from that plan.',
|
|
119
|
+
includeHealer
|
|
120
|
+
? '3) Use #playwright-test-healer to run and fix that generated test.'
|
|
121
|
+
: '3) Skip runtime healing and focus on producing compile-ready test code.',
|
|
122
|
+
'',
|
|
123
|
+
`Seed file: ${seedFile}`,
|
|
124
|
+
`Plan file to save: ${planFile}`,
|
|
125
|
+
`Generated test file path (must be exact): ${testFile}`,
|
|
126
|
+
'',
|
|
127
|
+
'Quality constraints (must follow):',
|
|
128
|
+
'- The generated file must contain a standalone test() and must not use test.describe or test.only.',
|
|
129
|
+
'- Do not mark the test with test.fixme unless user explicitly requests skipping.',
|
|
130
|
+
"- The generated test must include a single tag string '@ai-assisted'.",
|
|
131
|
+
'- Match fixture/import style from the seed file. Prefer existing page-object APIs over raw brittle selectors.',
|
|
132
|
+
'- Only use `pw` and page-object methods that already exist in the seed/current specs (for example, do not invent APIs like `pw.mainClient.*`).',
|
|
133
|
+
'- For system-console/admin flows, avoid `systemConsolePage.toBeVisible()` and brittle class selectors (`.backstage-navbar`, `.admin-console__wrapper`, `.left-panel`, `.panel-card`).',
|
|
134
|
+
'- Prefer stable assertions using URL patterns, test IDs, roles, labels, and established page-object methods.',
|
|
135
|
+
'- Keep the scenario strictly aligned to the flow and linked files, not broad unrelated flows.',
|
|
136
|
+
'',
|
|
137
|
+
'At the end, return a short summary that includes the generated test file path and whether healing succeeded.',
|
|
138
|
+
].join('\n');
|
|
139
|
+
}
|
|
140
|
+
export function buildPlaywrightHealerPrompt(testFile, extra) {
|
|
141
|
+
const lines = [
|
|
142
|
+
'Heal this specific Playwright test file and keep edits minimal.',
|
|
143
|
+
`Target test file: ${testFile}`,
|
|
144
|
+
'Constraints:',
|
|
145
|
+
'- Do not use test.describe or test.only.',
|
|
146
|
+
"- Keep a single tag string '@ai-assisted'.",
|
|
147
|
+
'- Use only existing Mattermost Playwright fixture/page-object APIs; do not invent new `pw.*` clients or methods.',
|
|
148
|
+
'- Avoid `systemConsolePage.toBeVisible()` and brittle class selectors (`.backstage-navbar`, `.admin-console__wrapper`, `.left-panel`, `.panel-card`).',
|
|
149
|
+
'- Prefer stable checks with URL/test IDs/roles/page-object methods.',
|
|
150
|
+
'- Keep the test intent unchanged and focused.',
|
|
151
|
+
'',
|
|
152
|
+
'Run and fix this test until it compiles/passes, or mark test.fixme with a clear comment when behavior is truly broken.',
|
|
153
|
+
];
|
|
154
|
+
if (extra) {
|
|
155
|
+
lines.push('', `Context: ${extra}`);
|
|
156
|
+
}
|
|
157
|
+
return lines.join('\n');
|
|
158
|
+
}
|
|
159
|
+
export function runPlaywrightAgentsFlow(testsRoot, flow, pipeline, outputDir, preferredTestFile, seedFile, apiSurface, playwrightBinary, mcpTimeoutMs, mcpRetries) {
|
|
160
|
+
mkdirSync(outputDir, { recursive: true });
|
|
161
|
+
const slug = toSafeSlug(flow.id);
|
|
162
|
+
const planFile = normalizePath(relative(testsRoot, join(outputDir, `${slug}.plan.md`)));
|
|
163
|
+
const absolutePlanFile = join(testsRoot, planFile);
|
|
164
|
+
const targetTestFile = normalizePath(relative(testsRoot, preferredTestFile));
|
|
165
|
+
const existingSpecFiles = findSpecFiles(outputDir);
|
|
166
|
+
const existingSpecSnapshots = new Map();
|
|
167
|
+
for (const specFile of existingSpecFiles) {
|
|
168
|
+
try {
|
|
169
|
+
existingSpecSnapshots.set(specFile, readFileSync(specFile, 'utf-8'));
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const originalPlanContent = existsSync(absolutePlanFile) ? readFileSync(absolutePlanFile, 'utf-8') : null;
|
|
176
|
+
const restoreArtifactsOnFailure = () => {
|
|
177
|
+
for (const currentSpecFile of findSpecFiles(outputDir)) {
|
|
178
|
+
const originalSpecContent = existingSpecSnapshots.get(currentSpecFile);
|
|
179
|
+
if (originalSpecContent === undefined) {
|
|
180
|
+
rmSync(currentSpecFile, { force: true });
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
if (readFileSync(currentSpecFile, 'utf-8') !== originalSpecContent) {
|
|
185
|
+
writeFileSync(currentSpecFile, originalSpecContent, 'utf-8');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
// best-effort restore only
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
for (const [specFile, originalSpecContent] of existingSpecSnapshots.entries()) {
|
|
193
|
+
if (!existsSync(specFile)) {
|
|
194
|
+
writeFileSync(specFile, originalSpecContent, 'utf-8');
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (originalPlanContent === null) {
|
|
198
|
+
rmSync(absolutePlanFile, { force: true });
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
try {
|
|
202
|
+
if (!existsSync(absolutePlanFile) || readFileSync(absolutePlanFile, 'utf-8') !== originalPlanContent) {
|
|
203
|
+
writeFileSync(absolutePlanFile, originalPlanContent, 'utf-8');
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
catch {
|
|
207
|
+
// best-effort restore only
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
const failFlow = (error) => {
|
|
212
|
+
restoreArtifactsOnFailure();
|
|
213
|
+
return {
|
|
214
|
+
flowId: flow.id,
|
|
215
|
+
flowName: flow.name,
|
|
216
|
+
generatedDir: outputDir,
|
|
217
|
+
generateStatus: 'failed',
|
|
218
|
+
healStatus: pipeline.heal ? 'failed' : undefined,
|
|
219
|
+
error,
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
if (pipeline.dryRun) {
|
|
223
|
+
return {
|
|
224
|
+
flowId: flow.id,
|
|
225
|
+
flowName: flow.name,
|
|
226
|
+
generatedDir: outputDir,
|
|
227
|
+
generateStatus: 'skipped',
|
|
228
|
+
healStatus: pipeline.heal ? 'skipped' : undefined,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
const prompt = buildPlaywrightAgentsPrompt(flow, seedFile, planFile, targetTestFile, Boolean(pipeline.heal));
|
|
232
|
+
const runArgs = [
|
|
233
|
+
'-p',
|
|
234
|
+
'--permission-mode',
|
|
235
|
+
'bypassPermissions',
|
|
236
|
+
'--setting-sources',
|
|
237
|
+
'project,local',
|
|
238
|
+
'--strict-mcp-config',
|
|
239
|
+
'--mcp-config',
|
|
240
|
+
'.mcp.json',
|
|
241
|
+
'--add-dir',
|
|
242
|
+
testsRoot,
|
|
243
|
+
'--',
|
|
244
|
+
prompt,
|
|
245
|
+
];
|
|
246
|
+
const runResult = runCommandWithRetries('claude', runArgs, testsRoot, mcpTimeoutMs, mcpRetries);
|
|
247
|
+
if (runResult.status !== 0) {
|
|
248
|
+
return failFlow(summarizeCommandOutput(runResult.stdout, runResult.stderr) || runResult.error || 'Playwright agents run failed');
|
|
249
|
+
}
|
|
250
|
+
let actualTestFile = preferredTestFile;
|
|
251
|
+
if (!existsSync(actualTestFile)) {
|
|
252
|
+
const candidates = findSpecFiles(outputDir);
|
|
253
|
+
if (candidates.length === 1) {
|
|
254
|
+
actualTestFile = candidates[0];
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (!existsSync(actualTestFile)) {
|
|
258
|
+
return failFlow(`Playwright agents did not produce expected test file: ${targetTestFile}`);
|
|
259
|
+
}
|
|
260
|
+
const relativeActualTestFile = normalizePath(relative(testsRoot, actualTestFile));
|
|
261
|
+
let qualityIssues = validateGeneratedSpecContent(readFileSync(actualTestFile, 'utf-8'), apiSurface);
|
|
262
|
+
if (qualityIssues.length > 0 && pipeline.heal) {
|
|
263
|
+
const healResult = runCommandWithRetries('claude', [
|
|
264
|
+
'-p',
|
|
265
|
+
'--permission-mode',
|
|
266
|
+
'bypassPermissions',
|
|
267
|
+
'--setting-sources',
|
|
268
|
+
'project,local',
|
|
269
|
+
'--strict-mcp-config',
|
|
270
|
+
'--agent',
|
|
271
|
+
'playwright-test-healer',
|
|
272
|
+
'--mcp-config',
|
|
273
|
+
'.mcp.json',
|
|
274
|
+
'--add-dir',
|
|
275
|
+
testsRoot,
|
|
276
|
+
'--',
|
|
277
|
+
buildPlaywrightHealerPrompt(relativeActualTestFile, qualityIssues.map((issue) => issue.message).join(' | ')),
|
|
278
|
+
], testsRoot, mcpTimeoutMs, mcpRetries);
|
|
279
|
+
if (healResult.status === 0 && existsSync(actualTestFile)) {
|
|
280
|
+
qualityIssues = validateGeneratedSpecContent(readFileSync(actualTestFile, 'utf-8'), apiSurface);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (qualityIssues.length > 0) {
|
|
284
|
+
return failFlow(`Playwright agents produced invalid test content: ${qualityIssues.map((issue) => issue.message).join(' | ')}`);
|
|
285
|
+
}
|
|
286
|
+
if (pipeline.heal) {
|
|
287
|
+
let compileValidation = runPlaywrightListValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
|
|
288
|
+
if (compileValidation.status === 'failed') {
|
|
289
|
+
const healResult = runCommandWithRetries('claude', [
|
|
290
|
+
'-p',
|
|
291
|
+
'--permission-mode',
|
|
292
|
+
'bypassPermissions',
|
|
293
|
+
'--setting-sources',
|
|
294
|
+
'project,local',
|
|
295
|
+
'--strict-mcp-config',
|
|
296
|
+
'--agent',
|
|
297
|
+
'playwright-test-healer',
|
|
298
|
+
'--mcp-config',
|
|
299
|
+
'.mcp.json',
|
|
300
|
+
'--add-dir',
|
|
301
|
+
testsRoot,
|
|
302
|
+
'--',
|
|
303
|
+
buildPlaywrightHealerPrompt(relativeActualTestFile, compileValidation.detail || 'playwright --list failed'),
|
|
304
|
+
], testsRoot, mcpTimeoutMs, mcpRetries);
|
|
305
|
+
if (healResult.status === 0 && existsSync(actualTestFile)) {
|
|
306
|
+
compileValidation = runPlaywrightListValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
|
|
307
|
+
}
|
|
308
|
+
if (compileValidation.status === 'failed') {
|
|
309
|
+
return failFlow(`Playwright agents compile validation failed: ${compileValidation.detail || 'playwright --list failed'}`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
let runtimeValidation = runPlaywrightRuntimeValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
|
|
313
|
+
if (runtimeValidation.status === 'failed') {
|
|
314
|
+
const healResult = runCommandWithRetries('claude', [
|
|
315
|
+
'-p',
|
|
316
|
+
'--permission-mode',
|
|
317
|
+
'bypassPermissions',
|
|
318
|
+
'--setting-sources',
|
|
319
|
+
'project,local',
|
|
320
|
+
'--strict-mcp-config',
|
|
321
|
+
'--agent',
|
|
322
|
+
'playwright-test-healer',
|
|
323
|
+
'--mcp-config',
|
|
324
|
+
'.mcp.json',
|
|
325
|
+
'--add-dir',
|
|
326
|
+
testsRoot,
|
|
327
|
+
'--',
|
|
328
|
+
buildPlaywrightHealerPrompt(relativeActualTestFile, runtimeValidation.detail || 'playwright runtime failed'),
|
|
329
|
+
], testsRoot, mcpTimeoutMs, mcpRetries);
|
|
330
|
+
if (healResult.status === 0 && existsSync(actualTestFile)) {
|
|
331
|
+
runtimeValidation = runPlaywrightRuntimeValidation(testsRoot, actualTestFile, pipeline, playwrightBinary);
|
|
332
|
+
}
|
|
333
|
+
if (runtimeValidation.status === 'failed') {
|
|
334
|
+
return failFlow(`Playwright agents runtime validation failed: ${runtimeValidation.detail || 'playwright test failed'}`);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return {
|
|
339
|
+
flowId: flow.id,
|
|
340
|
+
flowName: flow.name,
|
|
341
|
+
generatedDir: outputDir,
|
|
342
|
+
generateStatus: 'success',
|
|
343
|
+
healStatus: pipeline.heal ? 'success' : undefined,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
export function runPlaywrightAgentsPipeline(testsRoot, flows, pipeline) {
|
|
347
|
+
const warnings = [];
|
|
348
|
+
const results = [];
|
|
349
|
+
const mcpTimeoutMs = resolveMcpCommandTimeoutMs(pipeline);
|
|
350
|
+
const mcpRetries = resolveMcpRetries(pipeline);
|
|
351
|
+
if (!hasCommand('claude', testsRoot)) {
|
|
352
|
+
warnings.push('Claude CLI is required for official Playwright planner/generator/healer execution but was not found.');
|
|
353
|
+
return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
|
|
354
|
+
}
|
|
355
|
+
if (!hasPlaywrightConfig(testsRoot)) {
|
|
356
|
+
warnings.push('Playwright config file not found in testsRoot; skipping official Playwright agents backend.');
|
|
357
|
+
return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
|
|
358
|
+
}
|
|
359
|
+
if (!hasPlaywrightAgentDefinitions(testsRoot)) {
|
|
360
|
+
const bootstrap = bootstrapPlaywrightAgentDefinitions(testsRoot, pipeline, mcpTimeoutMs);
|
|
361
|
+
if (bootstrap.status !== 0) {
|
|
362
|
+
warnings.push(summarizeCommandOutput(bootstrap.stdout, bootstrap.stderr) ||
|
|
363
|
+
bootstrap.error ||
|
|
364
|
+
'Failed to initialize Playwright agents via `npx playwright init-agents`.');
|
|
365
|
+
return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (!hasPlaywrightAgentDefinitions(testsRoot)) {
|
|
369
|
+
warnings.push('Playwright agent definitions are missing after bootstrap.');
|
|
370
|
+
return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
|
|
371
|
+
}
|
|
372
|
+
const seedFile = resolveAgentSeedSpec(testsRoot);
|
|
373
|
+
if (!seedFile) {
|
|
374
|
+
warnings.push('No seed spec file found under specs/. Playwright planner cannot be initialized.');
|
|
375
|
+
return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
|
|
376
|
+
}
|
|
377
|
+
const playwrightBinary = pipeline.heal ? resolvePlaywrightBinary(testsRoot) : null;
|
|
378
|
+
const apiSurface = buildApiSurfaceCatalog(testsRoot, seedFile);
|
|
379
|
+
if (pipeline.heal && !playwrightBinary) {
|
|
380
|
+
warnings.push('Playwright binary was not found. Healer runtime validation may be limited.');
|
|
381
|
+
}
|
|
382
|
+
const outputBase = resolve(testsRoot, pipeline.outputDir || 'specs/functional/ai-assisted');
|
|
383
|
+
if (!isPathWithinRoot(testsRoot, outputBase)) {
|
|
384
|
+
warnings.push(`Pipeline outputDir resolves outside testsRoot and was blocked: ${pipeline.outputDir}`);
|
|
385
|
+
return { runner: 'unknown', results, warnings, mcp: createMcpStatus('unknown', true) };
|
|
386
|
+
}
|
|
387
|
+
for (const flow of flows) {
|
|
388
|
+
if (flow.priority !== 'P0' && flow.priority !== 'P1') {
|
|
389
|
+
continue;
|
|
390
|
+
}
|
|
391
|
+
const slug = toSafeSlug(flow.id);
|
|
392
|
+
const outputDir = normalizePath(join(outputBase, slug));
|
|
393
|
+
if (!isPathWithinRoot(testsRoot, outputDir)) {
|
|
394
|
+
results.push({
|
|
395
|
+
flowId: flow.id,
|
|
396
|
+
flowName: flow.name,
|
|
397
|
+
generatedDir: outputDir,
|
|
398
|
+
generateStatus: 'failed',
|
|
399
|
+
error: 'output directory resolves outside testsRoot',
|
|
400
|
+
});
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
const testFile = normalizePath(join(outputDir, `${slug}.spec.ts`));
|
|
404
|
+
if (!isPathWithinRoot(testsRoot, testFile)) {
|
|
405
|
+
results.push({
|
|
406
|
+
flowId: flow.id,
|
|
407
|
+
flowName: flow.name,
|
|
408
|
+
generatedDir: outputDir,
|
|
409
|
+
generateStatus: 'failed',
|
|
410
|
+
error: 'generated test path resolves outside testsRoot',
|
|
411
|
+
});
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
results.push(runPlaywrightAgentsFlow(testsRoot, flow, pipeline, outputDir, testFile, seedFile, apiSurface, playwrightBinary, mcpTimeoutMs, mcpRetries));
|
|
415
|
+
if (pipeline.mcpOnly && results[results.length - 1].generateStatus === 'failed') {
|
|
416
|
+
warnings.push(`MCP-only mode: stopping after first failed flow (${flow.id}).`);
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
return { runner: 'playwright-agents', results, warnings, mcp: createMcpStatus('playwright-agents', true) };
|
|
421
|
+
}
|