@cotestdev/mcp_playwright 0.0.14 → 0.0.15

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 (47) hide show
  1. package/lib/mcp/browser/browserContextFactory.js +49 -13
  2. package/lib/mcp/browser/browserServerBackend.js +5 -2
  3. package/lib/mcp/browser/config.js +95 -23
  4. package/lib/mcp/browser/context.js +28 -3
  5. package/lib/mcp/browser/response.js +240 -57
  6. package/lib/mcp/browser/sessionLog.js +1 -1
  7. package/lib/mcp/browser/tab.js +96 -69
  8. package/lib/mcp/browser/tools/common.js +8 -8
  9. package/lib/mcp/browser/tools/console.js +6 -3
  10. package/lib/mcp/browser/tools/dialogs.js +13 -13
  11. package/lib/mcp/browser/tools/evaluate.js +9 -20
  12. package/lib/mcp/browser/tools/files.js +10 -5
  13. package/lib/mcp/browser/tools/form.js +11 -22
  14. package/lib/mcp/browser/tools/install.js +3 -3
  15. package/lib/mcp/browser/tools/keyboard.js +12 -12
  16. package/lib/mcp/browser/tools/mouse.js +14 -14
  17. package/lib/mcp/browser/tools/navigate.js +5 -5
  18. package/lib/mcp/browser/tools/network.js +16 -5
  19. package/lib/mcp/browser/tools/pdf.js +7 -18
  20. package/lib/mcp/browser/tools/runCode.js +77 -0
  21. package/lib/mcp/browser/tools/screenshot.js +44 -33
  22. package/lib/mcp/browser/tools/snapshot.js +42 -33
  23. package/lib/mcp/browser/tools/tabs.js +7 -10
  24. package/lib/mcp/browser/tools/tool.js +8 -7
  25. package/lib/mcp/browser/tools/tracing.js +4 -4
  26. package/lib/mcp/browser/tools/utils.js +50 -52
  27. package/lib/mcp/browser/tools/verify.js +23 -34
  28. package/lib/mcp/browser/tools/wait.js +6 -6
  29. package/lib/mcp/browser/tools.js +4 -3
  30. package/lib/mcp/extension/cdpRelay.js +1 -1
  31. package/lib/mcp/extension/extensionContextFactory.js +4 -3
  32. package/lib/mcp/log.js +2 -2
  33. package/lib/mcp/program.js +21 -29
  34. package/lib/mcp/sdk/exports.js +1 -5
  35. package/lib/mcp/sdk/http.js +37 -50
  36. package/lib/mcp/sdk/server.js +61 -9
  37. package/lib/mcp/sdk/tool.js +5 -4
  38. package/lib/mcp/test/browserBackend.js +67 -61
  39. package/lib/mcp/test/generatorTools.js +122 -0
  40. package/lib/mcp/test/plannerTools.js +144 -0
  41. package/lib/mcp/test/seed.js +82 -0
  42. package/lib/mcp/test/streams.js +10 -7
  43. package/lib/mcp/test/testBackend.js +44 -24
  44. package/lib/mcp/test/testContext.js +243 -14
  45. package/lib/mcp/test/testTools.js +23 -109
  46. package/lib/util.js +12 -6
  47. package/package.json +1 -1
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var plannerTools_exports = {};
30
+ __export(plannerTools_exports, {
31
+ saveTestPlan: () => saveTestPlan,
32
+ setupPage: () => setupPage,
33
+ submitTestPlan: () => submitTestPlan
34
+ });
35
+ module.exports = __toCommonJS(plannerTools_exports);
36
+ var import_fs = __toESM(require("fs"));
37
+ var import_path = __toESM(require("path"));
38
+ var import_mcpBundle = require("playwright-core/lib/mcpBundle");
39
+ var import_testTool = require("./testTool");
40
+ const setupPage = (0, import_testTool.defineTestTool)({
41
+ schema: {
42
+ name: "planner_setup_page",
43
+ title: "Setup planner page",
44
+ description: "Setup the page for test planning",
45
+ inputSchema: import_mcpBundle.z.object({
46
+ project: import_mcpBundle.z.string().optional().describe('Project to use for setup. For example: "chromium", if no project is provided uses the first project in the config.'),
47
+ seedFile: import_mcpBundle.z.string().optional().describe('A seed file contains a single test that is used to setup the page for testing, for example: "tests/seed.spec.ts". If no seed file is provided, a default seed file is created.')
48
+ }),
49
+ type: "readOnly"
50
+ },
51
+ handle: async (context, params) => {
52
+ const seed = await context.getOrCreateSeedFile(params.seedFile, params.project);
53
+ const { output, status } = await context.runSeedTest(seed.file, seed.projectName);
54
+ return { content: [{ type: "text", text: output }], isError: status !== "paused" };
55
+ }
56
+ });
57
+ const planSchema = import_mcpBundle.z.object({
58
+ overview: import_mcpBundle.z.string().describe("A brief overview of the application to be tested"),
59
+ suites: import_mcpBundle.z.array(import_mcpBundle.z.object({
60
+ name: import_mcpBundle.z.string().describe("The name of the suite"),
61
+ seedFile: import_mcpBundle.z.string().describe("A seed file that was used to setup the page for testing."),
62
+ tests: import_mcpBundle.z.array(import_mcpBundle.z.object({
63
+ name: import_mcpBundle.z.string().describe("The name of the test"),
64
+ file: import_mcpBundle.z.string().describe('The file the test should be saved to, for example: "tests/<suite-name>/<test-name>.spec.ts".'),
65
+ steps: import_mcpBundle.z.array(import_mcpBundle.z.string().describe(`The steps to be executed to perform the test. For example: 'Click on the "Submit" button'`)),
66
+ expectedResults: import_mcpBundle.z.array(import_mcpBundle.z.string().describe("The expected results of the steps for test to verify."))
67
+ }))
68
+ }))
69
+ });
70
+ const submitTestPlan = (0, import_testTool.defineTestTool)({
71
+ schema: {
72
+ name: "planner_submit_plan",
73
+ title: "Submit test plan",
74
+ description: "Submit the test plan to the test planner",
75
+ inputSchema: planSchema,
76
+ type: "readOnly"
77
+ },
78
+ handle: async (context, params) => {
79
+ return {
80
+ content: [{
81
+ type: "text",
82
+ text: JSON.stringify(params, null, 2)
83
+ }]
84
+ };
85
+ }
86
+ });
87
+ const saveTestPlan = (0, import_testTool.defineTestTool)({
88
+ schema: {
89
+ name: "planner_save_plan",
90
+ title: "Save test plan as markdown file",
91
+ description: "Save the test plan as a markdown file",
92
+ inputSchema: planSchema.extend({
93
+ name: import_mcpBundle.z.string().describe('The name of the test plan, for example: "Test Plan".'),
94
+ fileName: import_mcpBundle.z.string().describe('The file to save the test plan to, for example: "spec/test.plan.md". Relative to the workspace root.')
95
+ }),
96
+ type: "readOnly"
97
+ },
98
+ handle: async (context, params) => {
99
+ const lines = [];
100
+ lines.push(`# ${params.name}`);
101
+ lines.push(``);
102
+ lines.push(`## Application Overview`);
103
+ lines.push(``);
104
+ lines.push(params.overview);
105
+ lines.push(``);
106
+ lines.push(`## Test Scenarios`);
107
+ for (let i = 0; i < params.suites.length; i++) {
108
+ lines.push(``);
109
+ const suite = params.suites[i];
110
+ lines.push(`### ${i + 1}. ${suite.name}`);
111
+ lines.push(``);
112
+ lines.push(`**Seed:** \`${suite.seedFile}\``);
113
+ for (let j = 0; j < suite.tests.length; j++) {
114
+ lines.push(``);
115
+ const test = suite.tests[j];
116
+ lines.push(`#### ${i + 1}.${j + 1}. ${test.name}`);
117
+ lines.push(``);
118
+ lines.push(`**File:** \`${test.file}\``);
119
+ lines.push(``);
120
+ lines.push(`**Steps:**`);
121
+ for (let k = 0; k < test.steps.length; k++)
122
+ lines.push(` ${k + 1}. ${test.steps[k]}`);
123
+ lines.push(``);
124
+ lines.push(`**Expected Results:**`);
125
+ for (const result of test.expectedResults)
126
+ lines.push(` - ${result}`);
127
+ }
128
+ }
129
+ lines.push(``);
130
+ await import_fs.default.promises.writeFile(import_path.default.resolve(context.rootPath, params.fileName), lines.join("\n"));
131
+ return {
132
+ content: [{
133
+ type: "text",
134
+ text: `Test plan saved to ${params.fileName}`
135
+ }]
136
+ };
137
+ }
138
+ });
139
+ // Annotate the CommonJS export names for ESM import in node:
140
+ 0 && (module.exports = {
141
+ saveTestPlan,
142
+ setupPage,
143
+ submitTestPlan
144
+ });
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var seed_exports = {};
30
+ __export(seed_exports, {
31
+ defaultSeedFile: () => defaultSeedFile,
32
+ ensureSeedFile: () => ensureSeedFile,
33
+ findSeedFile: () => findSeedFile,
34
+ seedFileContent: () => seedFileContent,
35
+ seedProject: () => seedProject
36
+ });
37
+ module.exports = __toCommonJS(seed_exports);
38
+ var import_fs = __toESM(require("fs"));
39
+ var import_path = __toESM(require("path"));
40
+ var import_utils = require("playwright-core/lib/utils");
41
+ var import_projectUtils = require("../../runner/projectUtils");
42
+ function seedProject(config, projectName) {
43
+ if (!projectName)
44
+ return (0, import_projectUtils.findTopLevelProjects)(config)[0];
45
+ const project = config.projects.find((p) => p.project.name === projectName);
46
+ if (!project)
47
+ throw new Error(`Project ${projectName} not found`);
48
+ return project;
49
+ }
50
+ async function findSeedFile(project) {
51
+ const files = await (0, import_projectUtils.collectFilesForProject)(project);
52
+ return files.find((file) => import_path.default.basename(file).includes("seed"));
53
+ }
54
+ function defaultSeedFile(project) {
55
+ const testDir = project.project.testDir;
56
+ return import_path.default.resolve(testDir, "seed.spec.ts");
57
+ }
58
+ async function ensureSeedFile(project) {
59
+ const seedFile = await findSeedFile(project);
60
+ if (seedFile)
61
+ return seedFile;
62
+ const seedFilePath = defaultSeedFile(project);
63
+ await (0, import_utils.mkdirIfNeeded)(seedFilePath);
64
+ await import_fs.default.promises.writeFile(seedFilePath, seedFileContent);
65
+ return seedFilePath;
66
+ }
67
+ const seedFileContent = `import { test, expect } from '@playwright/test';
68
+
69
+ test.describe('Test group', () => {
70
+ test('seed', async ({ page }) => {
71
+ // generate code here.
72
+ });
73
+ });
74
+ `;
75
+ // Annotate the CommonJS export names for ESM import in node:
76
+ 0 && (module.exports = {
77
+ defaultSeedFile,
78
+ ensureSeedFile,
79
+ findSeedFile,
80
+ seedFileContent,
81
+ seedProject
82
+ });
@@ -22,18 +22,21 @@ __export(streams_exports, {
22
22
  });
23
23
  module.exports = __toCommonJS(streams_exports);
24
24
  var import_stream = require("stream");
25
+ var import_util = require("../../util");
25
26
  class StringWriteStream extends import_stream.Writable {
26
- constructor() {
27
- super(...arguments);
28
- this._chunks = [];
27
+ constructor(output, stdio) {
28
+ super();
29
+ this._output = output;
30
+ this._prefix = stdio === "stdout" ? "" : "[err] ";
29
31
  }
30
32
  _write(chunk, encoding, callback) {
31
- this._chunks.push(chunk.toString());
33
+ let text = (0, import_util.stripAnsiEscapes)(chunk.toString());
34
+ if (text.endsWith("\n"))
35
+ text = text.slice(0, -1);
36
+ if (text)
37
+ this._output.push(this._prefix + text);
32
38
  callback();
33
39
  }
34
- content() {
35
- return this._chunks.join("");
36
- }
37
40
  }
38
41
  // Annotate the CommonJS export names for ESM import in node:
39
42
  0 && (module.exports = {
@@ -31,48 +31,68 @@ __export(testBackend_exports, {
31
31
  TestServerBackend: () => TestServerBackend
32
32
  });
33
33
  module.exports = __toCommonJS(testBackend_exports);
34
+ var import_mcpBundle = require("playwright-core/lib/mcpBundle");
34
35
  var mcp = __toESM(require("../sdk/exports"));
35
36
  var import_testContext = require("./testContext");
36
- var import_testTools = require("./testTools.js");
37
+ var testTools = __toESM(require("./testTools.js"));
38
+ var generatorTools = __toESM(require("./generatorTools.js"));
39
+ var plannerTools = __toESM(require("./plannerTools.js"));
37
40
  var import_tools = require("../browser/tools");
38
- var import_configLoader = require("../../common/configLoader");
39
41
  class TestServerBackend {
40
- constructor(configOption, options) {
42
+ constructor(configPath, options) {
41
43
  this.name = "Playwright";
42
44
  this.version = "0.0.1";
43
- this._tools = [import_testTools.listTests, import_testTools.runTests, import_testTools.debugTest, import_testTools.setupPage];
44
- this._context = new import_testContext.TestContext(options);
45
- this._configOption = configOption;
45
+ this._tools = [
46
+ plannerTools.saveTestPlan,
47
+ plannerTools.setupPage,
48
+ plannerTools.submitTestPlan,
49
+ generatorTools.setupPage,
50
+ generatorTools.generatorReadLog,
51
+ generatorTools.generatorWriteTest,
52
+ testTools.listTests,
53
+ testTools.runTests,
54
+ testTools.debugTest,
55
+ ...import_tools.browserTools.map((tool) => wrapBrowserTool(tool))
56
+ ];
57
+ this._options = options || {};
58
+ this._configPath = configPath;
46
59
  }
47
- async initialize(server, clientInfo) {
48
- if (this._configOption) {
49
- this._context.setConfigLocation((0, import_configLoader.resolveConfigLocation)(this._configOption));
50
- return;
51
- }
52
- const rootPath = mcp.firstRootPath(clientInfo);
53
- if (rootPath) {
54
- this._context.setConfigLocation((0, import_configLoader.resolveConfigLocation)(rootPath));
55
- return;
56
- }
57
- throw new Error("No config option or MCP root path provided");
60
+ async initialize(clientInfo) {
61
+ this._context = new import_testContext.TestContext(clientInfo, this._configPath, this._options);
58
62
  }
59
63
  async listTools() {
60
- return [
61
- ...this._tools.map((tool) => mcp.toMcpTool(tool.schema)),
62
- ...import_tools.browserTools.map((tool) => mcp.toMcpTool(tool.schema))
63
- ];
64
+ return this._tools.map((tool) => mcp.toMcpTool(tool.schema));
64
65
  }
65
66
  async callTool(name, args) {
66
67
  const tool = this._tools.find((tool2) => tool2.schema.name === name);
67
68
  if (!tool)
68
69
  throw new Error(`Tool not found: ${name}. Available tools: ${this._tools.map((tool2) => tool2.schema.name).join(", ")}`);
69
- const parsedArguments = tool.schema.inputSchema.parse(args || {});
70
- return await tool.handle(this._context, parsedArguments);
70
+ try {
71
+ return await tool.handle(this._context, tool.schema.inputSchema.parse(args || {}));
72
+ } catch (e) {
73
+ return { content: [{ type: "text", text: String(e) }], isError: true };
74
+ }
71
75
  }
72
76
  serverClosed() {
73
- void this._context.close();
77
+ void this._context?.close();
74
78
  }
75
79
  }
80
+ const typesWithIntent = ["action", "assertion", "input"];
81
+ function wrapBrowserTool(tool) {
82
+ const inputSchema = typesWithIntent.includes(tool.schema.type) ? tool.schema.inputSchema.extend({
83
+ intent: import_mcpBundle.z.string().describe("The intent of the call, for example the test step description plan idea")
84
+ }) : tool.schema.inputSchema;
85
+ return {
86
+ schema: {
87
+ ...tool.schema,
88
+ inputSchema
89
+ },
90
+ handle: async (context, params) => {
91
+ const response = await context.sendMessageToPausedTest({ callTool: { name: tool.schema.name, arguments: params } });
92
+ return response.callTool;
93
+ }
94
+ };
95
+ }
76
96
  // Annotate the CommonJS export names for ESM import in node:
77
97
  0 && (module.exports = {
78
98
  TestServerBackend
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,36 +17,263 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
  var testContext_exports = {};
20
30
  __export(testContext_exports, {
21
- TestContext: () => TestContext
31
+ GeneratorJournal: () => GeneratorJournal,
32
+ TestContext: () => TestContext,
33
+ createScreen: () => createScreen
22
34
  });
23
35
  module.exports = __toCommonJS(testContext_exports);
36
+ var import_fs = __toESM(require("fs"));
37
+ var import_os = __toESM(require("os"));
38
+ var import_path = __toESM(require("path"));
39
+ var import_utils = require("playwright-core/lib/utils");
40
+ var import_base = require("../../reporters/base");
41
+ var import_list = __toESM(require("../../reporters/list"));
42
+ var import_streams = require("./streams");
43
+ var import_util = require("../../util");
24
44
  var import_testRunner = require("../../runner/testRunner");
45
+ var import_seed = require("./seed");
46
+ var import_exports = require("../sdk/exports");
47
+ var import_configLoader = require("../../common/configLoader");
48
+ var import_response = require("../browser/response");
49
+ var import_log = require("../log");
50
+ class GeneratorJournal {
51
+ constructor(rootPath, plan, seed) {
52
+ this._rootPath = rootPath;
53
+ this._plan = plan;
54
+ this._seed = seed;
55
+ this._steps = [];
56
+ }
57
+ logStep(title, code) {
58
+ if (title)
59
+ this._steps.push({ title, code });
60
+ }
61
+ journal() {
62
+ const result = [];
63
+ result.push(`# Plan`);
64
+ result.push(this._plan);
65
+ result.push(`# Seed file: ${(0, import_utils.toPosixPath)(import_path.default.relative(this._rootPath, this._seed.file))}`);
66
+ result.push("```ts");
67
+ result.push(this._seed.content);
68
+ result.push("```");
69
+ result.push(`# Steps`);
70
+ result.push(this._steps.map((step) => `### ${step.title}
71
+ \`\`\`ts
72
+ ${step.code}
73
+ \`\`\``).join("\n\n"));
74
+ result.push(bestPracticesMarkdown);
75
+ return result.join("\n\n");
76
+ }
77
+ }
25
78
  class TestContext {
26
- constructor(options) {
27
- this.options = options;
79
+ constructor(clientInfo, configPath, options) {
80
+ this._clientInfo = clientInfo;
81
+ const rootPath = (0, import_exports.firstRootPath)(clientInfo);
82
+ this._configLocation = (0, import_configLoader.resolveConfigLocation)(configPath || rootPath);
83
+ this.rootPath = rootPath || this._configLocation.configDir;
84
+ if (options?.headless !== void 0)
85
+ this.computedHeaded = !options.headless;
86
+ else
87
+ this.computedHeaded = !process.env.CI && !(import_os.default.platform() === "linux" && !process.env.DISPLAY);
88
+ }
89
+ existingTestRunner() {
90
+ return this._testRunnerAndScreen?.testRunner;
28
91
  }
29
- setConfigLocation(configLocation) {
30
- this.configLocation = configLocation;
92
+ async _cleanupTestRunner() {
93
+ if (!this._testRunnerAndScreen)
94
+ return;
95
+ await this._testRunnerAndScreen.testRunner.stopTests();
96
+ this._testRunnerAndScreen.claimStdio();
97
+ try {
98
+ await this._testRunnerAndScreen.testRunner.runGlobalTeardown();
99
+ } finally {
100
+ this._testRunnerAndScreen.releaseStdio();
101
+ this._testRunnerAndScreen = void 0;
102
+ }
31
103
  }
32
104
  async createTestRunner() {
33
- if (this._testRunner)
34
- await this._testRunner.stopTests();
35
- const testRunner = new import_testRunner.TestRunner(this.configLocation, {});
105
+ await this._cleanupTestRunner();
106
+ const testRunner = new import_testRunner.TestRunner(this._configLocation, {});
36
107
  await testRunner.initialize({});
37
- this._testRunner = testRunner;
38
- testRunner.on(import_testRunner.TestRunnerEvent.TestFilesChanged, (testFiles) => {
39
- this._testRunner?.emit(import_testRunner.TestRunnerEvent.TestFilesChanged, testFiles);
108
+ const testPaused = new import_utils.ManualPromise();
109
+ const testRunnerAndScreen = {
110
+ ...createScreen(),
111
+ testRunner,
112
+ waitForTestPaused: () => testPaused
113
+ };
114
+ this._testRunnerAndScreen = testRunnerAndScreen;
115
+ testRunner.on(import_testRunner.TestRunnerEvent.TestPaused, (params) => {
116
+ testRunnerAndScreen.sendMessageToPausedTest = params.sendMessage;
117
+ testPaused.resolve();
40
118
  });
41
- this._testRunner = testRunner;
42
- return testRunner;
119
+ return testRunnerAndScreen;
120
+ }
121
+ async getOrCreateSeedFile(seedFile, projectName) {
122
+ const configDir = this._configLocation.configDir;
123
+ const { testRunner } = await this.createTestRunner();
124
+ const config = await testRunner.loadConfig();
125
+ const project = (0, import_seed.seedProject)(config, projectName);
126
+ if (!seedFile) {
127
+ seedFile = await (0, import_seed.ensureSeedFile)(project);
128
+ } else {
129
+ const candidateFiles = [];
130
+ const testDir = project.project.testDir;
131
+ candidateFiles.push(import_path.default.resolve(testDir, seedFile));
132
+ candidateFiles.push(import_path.default.resolve(configDir, seedFile));
133
+ candidateFiles.push(import_path.default.resolve(this.rootPath, seedFile));
134
+ let resolvedSeedFile;
135
+ for (const candidateFile of candidateFiles) {
136
+ if (await (0, import_util.fileExistsAsync)(candidateFile)) {
137
+ resolvedSeedFile = candidateFile;
138
+ break;
139
+ }
140
+ }
141
+ if (!resolvedSeedFile)
142
+ throw new Error("seed test not found.");
143
+ seedFile = resolvedSeedFile;
144
+ }
145
+ const seedFileContent = await import_fs.default.promises.readFile(seedFile, "utf8");
146
+ return {
147
+ file: seedFile,
148
+ content: seedFileContent,
149
+ projectName: project.project.name
150
+ };
151
+ }
152
+ async runSeedTest(seedFile, projectName) {
153
+ const result = await this.runTestsWithGlobalSetupAndPossiblePause({
154
+ headed: this.computedHeaded,
155
+ locations: ["/" + (0, import_utils.escapeRegExp)(seedFile) + "/"],
156
+ projects: [projectName],
157
+ timeout: 0,
158
+ workers: 1,
159
+ pauseAtEnd: true,
160
+ disableConfigReporters: true,
161
+ failOnLoadErrors: true
162
+ });
163
+ if (result.status === "passed")
164
+ result.output += "\nError: seed test not found.";
165
+ else if (result.status !== "paused")
166
+ result.output += "\nError while running the seed test.";
167
+ return result;
168
+ }
169
+ async runTestsWithGlobalSetupAndPossiblePause(params) {
170
+ const configDir = this._configLocation.configDir;
171
+ const testRunnerAndScreen = await this.createTestRunner();
172
+ const { testRunner, screen, claimStdio, releaseStdio } = testRunnerAndScreen;
173
+ claimStdio();
174
+ try {
175
+ const setupReporter = new import_list.default({ configDir, screen, includeTestId: true });
176
+ const { status: status2 } = await testRunner.runGlobalSetup([setupReporter]);
177
+ if (status2 !== "passed")
178
+ return { output: testRunnerAndScreen.output.join("\n"), status: status2 };
179
+ } finally {
180
+ releaseStdio();
181
+ }
182
+ let status = "passed";
183
+ const cleanup = async () => {
184
+ claimStdio();
185
+ try {
186
+ const result = await testRunner.runGlobalTeardown();
187
+ if (status === "passed")
188
+ status = result.status;
189
+ } finally {
190
+ releaseStdio();
191
+ }
192
+ };
193
+ try {
194
+ const reporter = new import_list.default({ configDir, screen, includeTestId: true });
195
+ status = await Promise.race([
196
+ testRunner.runTests(reporter, params).then((result) => result.status),
197
+ testRunnerAndScreen.waitForTestPaused().then(() => "paused")
198
+ ]);
199
+ if (status === "paused") {
200
+ const response = await testRunnerAndScreen.sendMessageToPausedTest({ request: { initialize: { clientInfo: this._clientInfo } } });
201
+ if (response.error)
202
+ throw new Error(response.error.message);
203
+ testRunnerAndScreen.output.push(response.response.initialize.pausedMessage);
204
+ return { output: testRunnerAndScreen.output.join("\n"), status };
205
+ }
206
+ } catch (e) {
207
+ status = "failed";
208
+ testRunnerAndScreen.output.push(String(e));
209
+ await cleanup();
210
+ return { output: testRunnerAndScreen.output.join("\n"), status };
211
+ }
212
+ await cleanup();
213
+ return { output: testRunnerAndScreen.output.join("\n"), status };
43
214
  }
44
215
  async close() {
216
+ await this._cleanupTestRunner().catch(import_log.logUnhandledError);
45
217
  }
218
+ async sendMessageToPausedTest(request) {
219
+ const sendMessage = this._testRunnerAndScreen?.sendMessageToPausedTest;
220
+ if (!sendMessage)
221
+ throw new Error("Must setup test before interacting with the page");
222
+ const result = await sendMessage({ request });
223
+ if (result.error)
224
+ throw new Error(result.error.message);
225
+ if (typeof request?.callTool?.arguments?.["intent"] === "string") {
226
+ const response = (0, import_response.parseResponse)(result.response.callTool);
227
+ if (response && !response.isError && response.code)
228
+ this.generatorJournal?.logStep(request.callTool.arguments["intent"], response.code);
229
+ }
230
+ return result.response;
231
+ }
232
+ }
233
+ function createScreen() {
234
+ const output = [];
235
+ const stdout = new import_streams.StringWriteStream(output, "stdout");
236
+ const stderr = new import_streams.StringWriteStream(output, "stderr");
237
+ const screen = {
238
+ ...import_base.terminalScreen,
239
+ isTTY: false,
240
+ colors: import_utils.noColors,
241
+ stdout,
242
+ stderr
243
+ };
244
+ const originalStdoutWrite = process.stdout.write;
245
+ const originalStderrWrite = process.stderr.write;
246
+ const claimStdio = () => {
247
+ process.stdout.write = (chunk) => {
248
+ stdout.write(chunk);
249
+ return true;
250
+ };
251
+ process.stderr.write = (chunk) => {
252
+ stderr.write(chunk);
253
+ return true;
254
+ };
255
+ };
256
+ const releaseStdio = () => {
257
+ process.stdout.write = originalStdoutWrite;
258
+ process.stderr.write = originalStderrWrite;
259
+ };
260
+ return { screen, claimStdio, releaseStdio, output };
46
261
  }
262
+ const bestPracticesMarkdown = `
263
+ # Best practices
264
+ - Do not improvise, do not add directives that were not asked for
265
+ - Use clear, descriptive assertions to validate the expected behavior
266
+ - Use reliable locators from this log
267
+ - Use local variables for locators that are used multiple times
268
+ - Use Playwright waiting assertions and best practices from this log
269
+ - NEVER! use page.waitForLoadState()
270
+ - NEVER! use page.waitForNavigation()
271
+ - NEVER! use page.waitForTimeout()
272
+ - NEVER! use page.evaluate()
273
+ `;
47
274
  // Annotate the CommonJS export names for ESM import in node:
48
275
  0 && (module.exports = {
49
- TestContext
276
+ GeneratorJournal,
277
+ TestContext,
278
+ createScreen
50
279
  });