ScuttleBot

scuttlebot / tests / e2e / node_modules / playwright / lib / mcp / test / browserBackend.js
Source Blame History 98 lines
f7eb47b… lmata 1 "use strict";
f7eb47b… lmata 2 var __defProp = Object.defineProperty;
f7eb47b… lmata 3 var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
f7eb47b… lmata 4 var __getOwnPropNames = Object.getOwnPropertyNames;
f7eb47b… lmata 5 var __hasOwnProp = Object.prototype.hasOwnProperty;
f7eb47b… lmata 6 var __export = (target, all) => {
f7eb47b… lmata 7 for (var name in all)
f7eb47b… lmata 8 __defProp(target, name, { get: all[name], enumerable: true });
f7eb47b… lmata 9 };
f7eb47b… lmata 10 var __copyProps = (to, from, except, desc) => {
f7eb47b… lmata 11 if (from && typeof from === "object" || typeof from === "function") {
f7eb47b… lmata 12 for (let key of __getOwnPropNames(from))
f7eb47b… lmata 13 if (!__hasOwnProp.call(to, key) && key !== except)
f7eb47b… lmata 14 __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
f7eb47b… lmata 15 }
f7eb47b… lmata 16 return to;
f7eb47b… lmata 17 };
f7eb47b… lmata 18 var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
f7eb47b… lmata 19 var browserBackend_exports = {};
f7eb47b… lmata 20 __export(browserBackend_exports, {
f7eb47b… lmata 21 createCustomMessageHandler: () => createCustomMessageHandler
f7eb47b… lmata 22 });
f7eb47b… lmata 23 module.exports = __toCommonJS(browserBackend_exports);
f7eb47b… lmata 24 var import_config = require("../browser/config");
f7eb47b… lmata 25 var import_browserServerBackend = require("../browser/browserServerBackend");
f7eb47b… lmata 26 var import_tab = require("../browser/tab");
f7eb47b… lmata 27 var import_util = require("../../util");
f7eb47b… lmata 28 var import_browserContextFactory = require("../browser/browserContextFactory");
f7eb47b… lmata 29 function createCustomMessageHandler(testInfo, context) {
f7eb47b… lmata 30 let backend;
f7eb47b… lmata 31 return async (data) => {
f7eb47b… lmata 32 if (data.initialize) {
f7eb47b… lmata 33 if (backend)
f7eb47b… lmata 34 throw new Error("MCP backend is already initialized");
f7eb47b… lmata 35 backend = new import_browserServerBackend.BrowserServerBackend({ ...import_config.defaultConfig, capabilities: ["testing"] }, (0, import_browserContextFactory.identityBrowserContextFactory)(context));
f7eb47b… lmata 36 await backend.initialize(data.initialize.clientInfo);
f7eb47b… lmata 37 const pausedMessage = await generatePausedMessage(testInfo, context);
f7eb47b… lmata 38 return { initialize: { pausedMessage } };
f7eb47b… lmata 39 }
f7eb47b… lmata 40 if (data.listTools) {
f7eb47b… lmata 41 if (!backend)
f7eb47b… lmata 42 throw new Error("MCP backend is not initialized");
f7eb47b… lmata 43 return { listTools: await backend.listTools() };
f7eb47b… lmata 44 }
f7eb47b… lmata 45 if (data.callTool) {
f7eb47b… lmata 46 if (!backend)
f7eb47b… lmata 47 throw new Error("MCP backend is not initialized");
f7eb47b… lmata 48 return { callTool: await backend.callTool(data.callTool.name, data.callTool.arguments) };
f7eb47b… lmata 49 }
f7eb47b… lmata 50 if (data.close) {
f7eb47b… lmata 51 backend?.serverClosed();
f7eb47b… lmata 52 backend = void 0;
f7eb47b… lmata 53 return { close: {} };
f7eb47b… lmata 54 }
f7eb47b… lmata 55 throw new Error("Unknown MCP request");
f7eb47b… lmata 56 };
f7eb47b… lmata 57 }
f7eb47b… lmata 58 async function generatePausedMessage(testInfo, context) {
f7eb47b… lmata 59 const lines = [];
f7eb47b… lmata 60 if (testInfo.errors.length) {
f7eb47b… lmata 61 lines.push(`### Paused on error:`);
f7eb47b… lmata 62 for (const error of testInfo.errors)
f7eb47b… lmata 63 lines.push((0, import_util.stripAnsiEscapes)(error.message || ""));
f7eb47b… lmata 64 } else {
f7eb47b… lmata 65 lines.push(`### Paused at end of test. ready for interaction`);
f7eb47b… lmata 66 }
f7eb47b… lmata 67 for (let i = 0; i < context.pages().length; i++) {
f7eb47b… lmata 68 const page = context.pages()[i];
f7eb47b… lmata 69 const stateSuffix = context.pages().length > 1 ? i + 1 + " of " + context.pages().length : "state";
f7eb47b… lmata 70 lines.push(
f7eb47b… lmata 71 "",
f7eb47b… lmata 72 `### Page ${stateSuffix}`,
f7eb47b… lmata 73 `- Page URL: ${page.url()}`,
f7eb47b… lmata 74 `- Page Title: ${await page.title()}`.trim()
f7eb47b… lmata 75 );
f7eb47b… lmata 76 let console = testInfo.errors.length ? await import_tab.Tab.collectConsoleMessages(page) : [];
f7eb47b… lmata 77 console = console.filter((msg) => msg.type === "error");
f7eb47b… lmata 78 if (console.length) {
f7eb47b… lmata 79 lines.push("- Console Messages:");
f7eb47b… lmata 80 for (const message of console)
f7eb47b… lmata 81 lines.push(` - ${message.toString()}`);
f7eb47b… lmata 82 }
f7eb47b… lmata 83 lines.push(
f7eb47b… lmata 84 `- Page Snapshot:`,
f7eb47b… lmata 85 "```yaml",
f7eb47b… lmata 86 (await page._snapshotForAI()).full,
f7eb47b… lmata 87 "```"
f7eb47b… lmata 88 );
f7eb47b… lmata 89 }
f7eb47b… lmata 90 lines.push("");
f7eb47b… lmata 91 if (testInfo.errors.length)
f7eb47b… lmata 92 lines.push(`### Task`, `Try recovering from the error prior to continuing`);
f7eb47b… lmata 93 return lines.join("\n");
f7eb47b… lmata 94 }
f7eb47b… lmata 95 // Annotate the CommonJS export names for ESM import in node:
f7eb47b… lmata 96 0 && (module.exports = {
f7eb47b… lmata 97 createCustomMessageHandler
f7eb47b… lmata 98 });

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button