File size: 11,540 Bytes
92319bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | "use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var testContext_exports = {};
__export(testContext_exports, {
GeneratorJournal: () => GeneratorJournal,
TestContext: () => TestContext,
createScreen: () => createScreen
});
module.exports = __toCommonJS(testContext_exports);
var import_fs = __toESM(require("fs"));
var import_os = __toESM(require("os"));
var import_path = __toESM(require("path"));
var import_coreBundle = require("playwright-core/lib/coreBundle");
var import_runner = require("../../runner");
var import_streams = require("./streams");
var import_util = require("../../util");
var import_seed = require("./seed");
var import_common = require("../../common");
const debug = require("playwright-core/lib/utilsBundle").debug;
const { noColors } = require("playwright-core/lib/coreBundle").iso;
const { ManualPromise, signalToPromise } = require("playwright-core/lib/coreBundle").iso;
const { escapeRegExp } = require("playwright-core/lib/coreBundle").iso;
const { toPosixPath } = require("playwright-core/lib/coreBundle").utils;
class GeneratorJournal {
constructor(rootPath, plan, seed) {
this._rootPath = rootPath;
this._plan = plan;
this._seed = seed;
this._steps = [];
}
logStep(title, code) {
if (title)
this._steps.push({ title, code });
}
journal() {
const result = [];
result.push(`# Plan`);
result.push(this._plan);
result.push(`# Seed file: ${toPosixPath(import_path.default.relative(this._rootPath, this._seed.file))}`);
result.push("```ts");
result.push(this._seed.content);
result.push("```");
result.push(`# Steps`);
result.push(this._steps.map((step) => `### ${step.title}
\`\`\`ts
${step.code}
\`\`\``).join("\n\n"));
result.push(bestPracticesMarkdown);
return result.join("\n\n");
}
}
class TestContext {
constructor(clientInfo, configPath, options) {
this._testOpQueue = Promise.resolve();
this._clientInfo = clientInfo;
this._configLocation = import_common.configLoader.resolveConfigLocation(configPath || clientInfo.cwd);
this.rootPath = clientInfo.cwd || this._configLocation.configDir;
if (options?.headless !== void 0)
this.computedHeaded = !options.headless;
else
this.computedHeaded = !process.env.CI && !(import_os.default.platform() === "linux" && !process.env.DISPLAY);
}
async _enqueue(fn) {
const next = this._testOpQueue.then(fn);
this._testOpQueue = next.then(() => {
});
return await next;
}
existingTestRunner() {
return this._testRunnerAndScreen?.testRunner;
}
async _cleanupTestRunner() {
if (!this._testRunnerAndScreen)
return;
await this._testRunnerAndScreen.testRunner.stopTests();
this._testRunnerAndScreen.claimStdio();
try {
await this._testRunnerAndScreen.testRunner.runGlobalTeardown();
} finally {
this._testRunnerAndScreen.releaseStdio();
this._testRunnerAndScreen = void 0;
}
}
async createTestRunner() {
await this._cleanupTestRunner();
const runner = new import_runner.testRunner.TestRunner(this._configLocation, {});
await runner.initialize({});
const testPaused = new ManualPromise();
const testRunnerAndScreen = {
...createScreen(),
testRunner: runner,
waitForTestPaused: () => testPaused
};
this._testRunnerAndScreen = testRunnerAndScreen;
runner.on(import_runner.testRunner.TestRunnerEvent.TestPaused, (params) => {
testRunnerAndScreen.sendMessageToPausedTest = params.sendMessage;
testPaused.resolve();
});
return testRunnerAndScreen;
}
async getOrCreateSeedFile(seedFile, projectName) {
const configDir = this._configLocation.configDir;
const { testRunner: runner } = await this.createTestRunner();
const config = await runner.loadConfig();
const project = (0, import_seed.seedProject)(config, projectName);
if (!seedFile) {
seedFile = await (0, import_seed.ensureSeedFile)(project);
} else {
const candidateFiles = [];
const testDir = project.project.testDir;
candidateFiles.push(import_path.default.resolve(testDir, seedFile));
candidateFiles.push(import_path.default.resolve(configDir, seedFile));
candidateFiles.push(import_path.default.resolve(this.rootPath, seedFile));
let resolvedSeedFile;
for (const candidateFile of candidateFiles) {
if (await (0, import_util.fileExistsAsync)(candidateFile)) {
resolvedSeedFile = candidateFile;
break;
}
}
if (!resolvedSeedFile)
throw new Error("seed test not found.");
seedFile = resolvedSeedFile;
}
const seedFileContent = await import_fs.default.promises.readFile(seedFile, "utf8");
return {
file: seedFile,
content: seedFileContent,
projectName: project.project.name
};
}
async runSeedTest(seedFile, projectName, signal) {
const result = await this.runTestsWithGlobalSetupAndPossiblePause({
headed: this.computedHeaded,
locations: ["/" + escapeRegExp(seedFile) + "/"],
projects: [projectName],
timeout: 0,
workers: 1,
pauseAtEnd: true,
disableConfigReporters: true,
failOnLoadErrors: true
}, signal);
if (result.status === "passed")
result.output += "\nError: seed test not found.";
else if (result.status !== "paused")
result.output += "\nError while running the seed test.";
return result;
}
async runTestsWithGlobalSetupAndPossiblePause(params, signal) {
return this._enqueue(() => this._runTestsImpl(params, signal));
}
async _runTestsImpl(params, signal) {
const configDir = this._configLocation.configDir;
const testRunnerAndScreen = await this.createTestRunner();
const { testRunner: runner, screen, claimStdio, releaseStdio } = testRunnerAndScreen;
claimStdio();
try {
const setupReporter = new MCPListReporter({ configDir, screen, includeTestId: true });
const { status: status2 } = await runner.runGlobalSetup([setupReporter]);
if (status2 !== "passed")
return { output: testRunnerAndScreen.output.join("\n"), status: status2 };
} finally {
releaseStdio();
}
let status = "passed";
const cleanup = async () => {
claimStdio();
try {
const result = await runner.runGlobalTeardown();
if (status === "passed")
status = result.status;
} finally {
releaseStdio();
}
};
const abortPromise = signal ? signalToPromise(signal).promise.then(() => "interrupted") : new Promise(() => {
});
try {
const reporter = new MCPListReporter({ configDir, screen, includeTestId: true });
status = await Promise.race([
runner.runTests(reporter, params).then((result) => result.status),
testRunnerAndScreen.waitForTestPaused().then(() => "paused"),
abortPromise
]);
if (status === "interrupted") {
await runner.stopTests();
await cleanup();
return { output: testRunnerAndScreen.output.join("\n"), status };
}
if (status === "paused") {
const response = await testRunnerAndScreen.sendMessageToPausedTest({ request: { initialize: { clientInfo: this._clientInfo } } });
if (response.error)
throw new Error(response.error.message);
testRunnerAndScreen.output.push(response.response.initialize.pausedMessage);
return { output: testRunnerAndScreen.output.join("\n"), status };
}
} catch (e) {
status = "failed";
testRunnerAndScreen.output.push(String(e));
await cleanup();
return { output: testRunnerAndScreen.output.join("\n"), status };
}
await cleanup();
return { output: testRunnerAndScreen.output.join("\n"), status };
}
async close() {
await this._enqueue(() => this._cleanupTestRunner()).catch((e) => debug("pw:mcp:error")(e));
}
async sendMessageToPausedTest(request) {
const sendMessage = this._testRunnerAndScreen?.sendMessageToPausedTest;
if (!sendMessage)
throw new Error("Must setup test before interacting with the page");
const result = await sendMessage({ request });
if (result.error)
throw new Error(result.error.message);
if (typeof request?.callTool?.arguments?.["intent"] === "string") {
const response = import_coreBundle.tools.parseResponse(result.response.callTool);
if (response && !response.isError && response.code)
this.generatorJournal?.logStep(request.callTool.arguments["intent"], response.code);
}
return result.response;
}
}
function createScreen() {
const output = [];
const stdout = new import_streams.StringWriteStream(output, "stdout");
const stderr = new import_streams.StringWriteStream(output, "stderr");
const screen = {
...import_runner.base.terminalScreen,
isTTY: false,
colors: noColors,
stdout,
stderr
};
const originalStdoutWrite = process.stdout.write;
const originalStderrWrite = process.stderr.write;
const claimStdio = () => {
process.stdout.write = (chunk) => {
stdout.write(chunk);
return true;
};
process.stderr.write = (chunk) => {
stderr.write(chunk);
return true;
};
};
const releaseStdio = () => {
process.stdout.write = originalStdoutWrite;
process.stderr.write = originalStderrWrite;
};
return { screen, claimStdio, releaseStdio, output };
}
const bestPracticesMarkdown = `
# Best practices
- Do not improvise, do not add directives that were not asked for
- Use clear, descriptive assertions to validate the expected behavior
- Use reliable locators from this log
- Use local variables for locators that are used multiple times
- Use Playwright waiting assertions and best practices from this log
- NEVER! use page.waitForLoadState()
- NEVER! use page.waitForNavigation()
- NEVER! use page.waitForTimeout()
- NEVER! use page.evaluate()
`;
class MCPListReporter extends import_runner.ListReporter {
async onTestPaused() {
await new Promise(() => {
});
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
GeneratorJournal,
TestContext,
createScreen
});
|