generatorTools.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 generatorTools_exports = {};
  30. __export(generatorTools_exports, {
  31. generatorReadLog: () => generatorReadLog,
  32. generatorWriteTest: () => generatorWriteTest,
  33. setupPage: () => setupPage
  34. });
  35. module.exports = __toCommonJS(generatorTools_exports);
  36. var import_fs = __toESM(require("fs"));
  37. var import_path = __toESM(require("path"));
  38. var import_bundle = require("../sdk/bundle");
  39. var import_testTool = require("./testTool");
  40. var import_testContext = require("./testContext");
  41. const setupPage = (0, import_testTool.defineTestTool)({
  42. schema: {
  43. name: "generator_setup_page",
  44. title: "Setup generator page",
  45. description: "Setup the page for test.",
  46. inputSchema: import_bundle.z.object({
  47. plan: import_bundle.z.string().describe("The plan for the test. This should be the actual test plan with all the steps."),
  48. project: import_bundle.z.string().optional().describe('Project to use for setup. For example: "chromium", if no project is provided uses the first project in the config.'),
  49. seedFile: import_bundle.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.')
  50. }),
  51. type: "readOnly"
  52. },
  53. handle: async (context, params) => {
  54. const seed = await context.getOrCreateSeedFile(params.seedFile, params.project);
  55. context.generatorJournal = new import_testContext.GeneratorJournal(context.rootPath, params.plan, seed);
  56. const { output, status } = await context.runSeedTest(seed.file, seed.projectName);
  57. return { content: [{ type: "text", text: output }], isError: status !== "paused" };
  58. }
  59. });
  60. const generatorReadLog = (0, import_testTool.defineTestTool)({
  61. schema: {
  62. name: "generator_read_log",
  63. title: "Retrieve test log",
  64. description: "Retrieve the performed test log",
  65. inputSchema: import_bundle.z.object({}),
  66. type: "readOnly"
  67. },
  68. handle: async (context) => {
  69. if (!context.generatorJournal)
  70. throw new Error(`Please setup page using "${setupPage.schema.name}" first.`);
  71. const result = context.generatorJournal.journal();
  72. return { content: [{
  73. type: "text",
  74. text: result
  75. }] };
  76. }
  77. });
  78. const generatorWriteTest = (0, import_testTool.defineTestTool)({
  79. schema: {
  80. name: "generator_write_test",
  81. title: "Write test",
  82. description: "Write the generated test to the test file",
  83. inputSchema: import_bundle.z.object({
  84. fileName: import_bundle.z.string().describe("The file to write the test to"),
  85. code: import_bundle.z.string().describe("The generated test code")
  86. }),
  87. type: "readOnly"
  88. },
  89. handle: async (context, params) => {
  90. if (!context.generatorJournal)
  91. throw new Error(`Please setup page using "${setupPage.schema.name}" first.`);
  92. const testRunner = context.existingTestRunner();
  93. if (!testRunner)
  94. throw new Error("No test runner found, please setup page and perform actions first.");
  95. const config = await testRunner.loadConfig();
  96. const dirs = [];
  97. for (const project of config.projects) {
  98. const testDir = import_path.default.relative(context.rootPath, project.project.testDir).replace(/\\/g, "/");
  99. const fileName = params.fileName.replace(/\\/g, "/");
  100. if (fileName.startsWith(testDir)) {
  101. const resolvedFile = import_path.default.resolve(context.rootPath, fileName);
  102. await import_fs.default.promises.mkdir(import_path.default.dirname(resolvedFile), { recursive: true });
  103. await import_fs.default.promises.writeFile(resolvedFile, params.code);
  104. return {
  105. content: [{
  106. type: "text",
  107. text: `### Result
  108. Test written to ${params.fileName}`
  109. }]
  110. };
  111. }
  112. dirs.push(testDir);
  113. }
  114. throw new Error(`Test file did not match any of the test dirs: ${dirs.join(", ")}`);
  115. }
  116. });
  117. // Annotate the CommonJS export names for ESM import in node:
  118. 0 && (module.exports = {
  119. generatorReadLog,
  120. generatorWriteTest,
  121. setupPage
  122. });