@midscene/core 0.2.2 → 0.2.3-beta-20240815085544.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.
@@ -4433,7 +4433,6 @@ import { Buffer as Buffer4 } from "buffer";
4433
4433
  import assert3 from "assert";
4434
4434
  import { randomUUID } from "crypto";
4435
4435
  import {
4436
- copyFileSync,
4437
4436
  existsSync,
4438
4437
  mkdirSync,
4439
4438
  readFileSync as readFileSync2,
package/dist/es/image.js CHANGED
@@ -124,7 +124,6 @@ import { Buffer as Buffer3 } from "buffer";
124
124
  import assert2 from "assert";
125
125
  import { randomUUID } from "crypto";
126
126
  import {
127
- copyFileSync,
128
127
  existsSync,
129
128
  mkdirSync,
130
129
  readFileSync as readFileSync2,
@@ -138,19 +137,26 @@ function getPkgInfo() {
138
137
  return pkg;
139
138
  }
140
139
  let pkgJsonFile = "";
140
+ let pkgDir = "";
141
141
  if (existsSync(join(__dirname, "../package.json"))) {
142
142
  pkgJsonFile = join(__dirname, "../package.json");
143
+ pkgDir = join(__dirname, "../");
144
+ } else if (existsSync(join(__dirname, "../../package.json"))) {
145
+ pkgJsonFile = join(__dirname, "../../package.json");
146
+ pkgDir = join(__dirname, "../../");
143
147
  } else if (existsSync(join(__dirname, "../../../package.json"))) {
144
148
  pkgJsonFile = join(__dirname, "../../../package.json");
149
+ pkgDir = join(__dirname, "../../../");
145
150
  }
146
151
  if (pkgJsonFile) {
147
152
  const { name, version } = JSON.parse(readFileSync2(pkgJsonFile, "utf-8"));
148
- pkg = { name, version };
153
+ pkg = { name, version, dir: pkgDir };
149
154
  return pkg;
150
155
  }
151
156
  return {
152
157
  name: "midscene-unknown-page-name",
153
- version: "0.0.0"
158
+ version: "0.0.0",
159
+ dir: pkgDir
154
160
  };
155
161
  }
156
162
  var logDir = join(process.cwd(), "./midscene_run/");
package/dist/es/index.js CHANGED
@@ -1015,7 +1015,6 @@ import assert2 from "assert";
1015
1015
  import assert from "assert";
1016
1016
  import { randomUUID } from "crypto";
1017
1017
  import {
1018
- copyFileSync,
1019
1018
  existsSync,
1020
1019
  mkdirSync,
1021
1020
  readFileSync,
@@ -1029,39 +1028,71 @@ function getPkgInfo() {
1029
1028
  return pkg;
1030
1029
  }
1031
1030
  let pkgJsonFile = "";
1031
+ let pkgDir = "";
1032
1032
  if (existsSync(join(__dirname, "../package.json"))) {
1033
1033
  pkgJsonFile = join(__dirname, "../package.json");
1034
+ pkgDir = join(__dirname, "../");
1035
+ } else if (existsSync(join(__dirname, "../../package.json"))) {
1036
+ pkgJsonFile = join(__dirname, "../../package.json");
1037
+ pkgDir = join(__dirname, "../../");
1034
1038
  } else if (existsSync(join(__dirname, "../../../package.json"))) {
1035
1039
  pkgJsonFile = join(__dirname, "../../../package.json");
1040
+ pkgDir = join(__dirname, "../../../");
1036
1041
  }
1037
1042
  if (pkgJsonFile) {
1038
1043
  const { name, version } = JSON.parse(readFileSync(pkgJsonFile, "utf-8"));
1039
- pkg = { name, version };
1044
+ pkg = { name, version, dir: pkgDir };
1040
1045
  return pkg;
1041
1046
  }
1042
1047
  return {
1043
1048
  name: "midscene-unknown-page-name",
1044
- version: "0.0.0"
1049
+ version: "0.0.0",
1050
+ dir: pkgDir
1045
1051
  };
1046
1052
  }
1047
1053
  var logDir = join(process.cwd(), "./midscene_run/");
1048
1054
  var logEnvReady = false;
1049
1055
  var insightDumpFileExt = "insight-dump.json";
1050
- function getDumpDir() {
1056
+ function getLogDir() {
1051
1057
  return logDir;
1052
1058
  }
1053
- function setDumpDir(dir) {
1059
+ function setLogDir(dir) {
1054
1060
  logDir = dir;
1055
1061
  }
1056
- function getDumpDirPath(type) {
1057
- return join(getDumpDir(), type);
1062
+ function getLogDirByType(type) {
1063
+ const dir = join(getLogDir(), type);
1064
+ if (!existsSync(dir)) {
1065
+ mkdirSync(dir, { recursive: true });
1066
+ }
1067
+ return dir;
1058
1068
  }
1059
- function writeDumpFile(opts) {
1060
- const { fileName, fileExt, fileContent, type = "dump" } = opts;
1061
- const targetDir = getDumpDirPath(type);
1062
- if (!existsSync(targetDir)) {
1063
- mkdirSync(targetDir, { recursive: true });
1069
+ function writeDumpReport(fileName, dumpData) {
1070
+ const { dir } = getPkgInfo();
1071
+ const reportTplPath = join(dir, "./report/index.html");
1072
+ existsSync(reportTplPath) || assert(false, `report template not found: ${reportTplPath}`);
1073
+ const reportPath = join(getLogDirByType("report"), `${fileName}.html`);
1074
+ const tpl = readFileSync(reportTplPath, "utf-8");
1075
+ let reportContent;
1076
+ if (typeof dumpData === "string") {
1077
+ reportContent = tpl.replace(
1078
+ "{{dump}}",
1079
+ `<script type="midscene_web_dump" type="application/json">${dumpData}</script>`
1080
+ );
1081
+ } else {
1082
+ const dumps = dumpData.map(({ dumpString, attributes }) => {
1083
+ const attributesArr = Object.keys(attributes || {}).map((key) => {
1084
+ return `${key}="${encodeURIComponent(attributes[key])}"`;
1085
+ });
1086
+ return `<script type="midscene_web_dump" type="application/json" ${attributesArr.join(" ")}>${dumpString}</script>`;
1087
+ });
1088
+ reportContent = tpl.replace("{{dump}}", dumps.join("\n"));
1064
1089
  }
1090
+ writeFileSync(reportPath, reportContent);
1091
+ return reportPath;
1092
+ }
1093
+ function writeLogFile(opts) {
1094
+ const { fileName, fileExt, fileContent, type = "dump" } = opts;
1095
+ const targetDir = getLogDirByType(type);
1065
1096
  if (!logEnvReady) {
1066
1097
  assert(targetDir, "logDir should be set before writing dump file");
1067
1098
  const gitIgnorePath = join(targetDir, "../../.gitignore");
@@ -1085,8 +1116,8 @@ ${logDirName}/dump
1085
1116
  }
1086
1117
  const filePath = join(targetDir, `${fileName}.${fileExt}`);
1087
1118
  writeFileSync(filePath, fileContent);
1088
- if (type === "dump") {
1089
- copyFileSync(filePath, join(targetDir, `latest.${fileExt}`));
1119
+ if (opts == null ? void 0 : opts.generateReport) {
1120
+ return writeDumpReport(fileName, fileContent);
1090
1121
  }
1091
1122
  return filePath;
1092
1123
  }
@@ -1203,7 +1234,7 @@ var Executor = class {
1203
1234
  successfullyCompleted = false;
1204
1235
  task.error = (e == null ? void 0 : e.message) || "error-without-message";
1205
1236
  task.errorStack = e.stack;
1206
- task.status = "fail";
1237
+ task.status = "failed";
1207
1238
  task.timing.end = Date.now();
1208
1239
  task.timing.cost = task.timing.end - task.timing.start;
1209
1240
  break;
@@ -1229,7 +1260,7 @@ var Executor = class {
1229
1260
  return null;
1230
1261
  }
1231
1262
  const errorTaskIndex = this.tasks.findIndex(
1232
- (task) => task.status === "fail"
1263
+ (task) => task.status === "failed"
1233
1264
  );
1234
1265
  if (errorTaskIndex >= 0) {
1235
1266
  return this.tasks[errorTaskIndex];
@@ -5250,7 +5281,7 @@ var logIdIndexMap = {};
5250
5281
  var { pid } = process;
5251
5282
  var logFileExt = insightDumpFileExt;
5252
5283
  function writeInsightDump(data, logId, dumpSubscriber) {
5253
- const logDir2 = getDumpDir();
5284
+ const logDir2 = getLogDir();
5254
5285
  assert9(logDir2, "logDir should be set before writing dump file");
5255
5286
  const id = logId || randomUUID2();
5256
5287
  const baseData = {
@@ -5274,12 +5305,13 @@ function writeInsightDump(data, logId, dumpSubscriber) {
5274
5305
  const length = logContent.push(dataString);
5275
5306
  logIdIndexMap[id] = length - 1;
5276
5307
  }
5277
- writeDumpFile({
5308
+ writeLogFile({
5278
5309
  fileName: logFileName,
5279
5310
  fileExt: logFileExt,
5280
5311
  fileContent: `[
5281
5312
  ${logContent.join(",\n")}
5282
- ]`
5313
+ ]`,
5314
+ type: "dump"
5283
5315
  });
5284
5316
  return id;
5285
5317
  }
@@ -5576,5 +5608,5 @@ export {
5576
5608
  getElement,
5577
5609
  getSection,
5578
5610
  plan,
5579
- setDumpDir
5611
+ setLogDir
5580
5612
  };
package/dist/es/utils.js CHANGED
@@ -2,7 +2,6 @@
2
2
  import assert from "assert";
3
3
  import { randomUUID } from "crypto";
4
4
  import {
5
- copyFileSync,
6
5
  existsSync,
7
6
  mkdirSync,
8
7
  readFileSync,
@@ -16,40 +15,72 @@ function getPkgInfo() {
16
15
  return pkg;
17
16
  }
18
17
  let pkgJsonFile = "";
18
+ let pkgDir = "";
19
19
  if (existsSync(join(__dirname, "../package.json"))) {
20
20
  pkgJsonFile = join(__dirname, "../package.json");
21
+ pkgDir = join(__dirname, "../");
22
+ } else if (existsSync(join(__dirname, "../../package.json"))) {
23
+ pkgJsonFile = join(__dirname, "../../package.json");
24
+ pkgDir = join(__dirname, "../../");
21
25
  } else if (existsSync(join(__dirname, "../../../package.json"))) {
22
26
  pkgJsonFile = join(__dirname, "../../../package.json");
27
+ pkgDir = join(__dirname, "../../../");
23
28
  }
24
29
  if (pkgJsonFile) {
25
30
  const { name, version } = JSON.parse(readFileSync(pkgJsonFile, "utf-8"));
26
- pkg = { name, version };
31
+ pkg = { name, version, dir: pkgDir };
27
32
  return pkg;
28
33
  }
29
34
  return {
30
35
  name: "midscene-unknown-page-name",
31
- version: "0.0.0"
36
+ version: "0.0.0",
37
+ dir: pkgDir
32
38
  };
33
39
  }
34
40
  var logDir = join(process.cwd(), "./midscene_run/");
35
41
  var logEnvReady = false;
36
42
  var insightDumpFileExt = "insight-dump.json";
37
43
  var groupedActionDumpFileExt = "web-dump.json";
38
- function getDumpDir() {
44
+ function getLogDir() {
39
45
  return logDir;
40
46
  }
41
- function setDumpDir(dir) {
47
+ function setLogDir(dir) {
42
48
  logDir = dir;
43
49
  }
44
- function getDumpDirPath(type) {
45
- return join(getDumpDir(), type);
50
+ function getLogDirByType(type) {
51
+ const dir = join(getLogDir(), type);
52
+ if (!existsSync(dir)) {
53
+ mkdirSync(dir, { recursive: true });
54
+ }
55
+ return dir;
46
56
  }
47
- function writeDumpFile(opts) {
48
- const { fileName, fileExt, fileContent, type = "dump" } = opts;
49
- const targetDir = getDumpDirPath(type);
50
- if (!existsSync(targetDir)) {
51
- mkdirSync(targetDir, { recursive: true });
57
+ function writeDumpReport(fileName, dumpData) {
58
+ const { dir } = getPkgInfo();
59
+ const reportTplPath = join(dir, "./report/index.html");
60
+ existsSync(reportTplPath) || assert(false, `report template not found: ${reportTplPath}`);
61
+ const reportPath = join(getLogDirByType("report"), `${fileName}.html`);
62
+ const tpl = readFileSync(reportTplPath, "utf-8");
63
+ let reportContent;
64
+ if (typeof dumpData === "string") {
65
+ reportContent = tpl.replace(
66
+ "{{dump}}",
67
+ `<script type="midscene_web_dump" type="application/json">${dumpData}</script>`
68
+ );
69
+ } else {
70
+ const dumps = dumpData.map(({ dumpString, attributes }) => {
71
+ const attributesArr = Object.keys(attributes || {}).map((key) => {
72
+ return `${key}="${encodeURIComponent(attributes[key])}"`;
73
+ });
74
+ return `<script type="midscene_web_dump" type="application/json" ${attributesArr.join(" ")}>${dumpString}</script>`;
75
+ });
76
+ reportContent = tpl.replace("{{dump}}", dumps.join("\n"));
52
77
  }
78
+ writeFileSync(reportPath, reportContent);
79
+ return reportPath;
80
+ }
81
+ function writeLogFile(opts) {
82
+ const { fileName, fileExt, fileContent, type = "dump" } = opts;
83
+ const targetDir = getLogDirByType(type);
53
84
  if (!logEnvReady) {
54
85
  assert(targetDir, "logDir should be set before writing dump file");
55
86
  const gitIgnorePath = join(targetDir, "../../.gitignore");
@@ -73,8 +104,8 @@ ${logDirName}/dump
73
104
  }
74
105
  const filePath = join(targetDir, `${fileName}.${fileExt}`);
75
106
  writeFileSync(filePath, fileContent);
76
- if (type === "dump") {
77
- copyFileSync(filePath, join(targetDir, `latest.${fileExt}`));
107
+ if (opts == null ? void 0 : opts.generateReport) {
108
+ return writeDumpReport(fileName, fileContent);
78
109
  }
79
110
  return filePath;
80
111
  }
@@ -109,8 +140,8 @@ function stringifyDumpData(data, indents) {
109
140
  }
110
141
  export {
111
142
  commonScreenshotParam,
112
- getDumpDir,
113
- getDumpDirPath,
143
+ getLogDir,
144
+ getLogDirByType,
114
145
  getPkgInfo,
115
146
  getTmpDir,
116
147
  getTmpFile,
@@ -118,8 +149,9 @@ export {
118
149
  insightDumpFileExt,
119
150
  overlapped,
120
151
  replacerForPageObject,
121
- setDumpDir,
152
+ setLogDir,
122
153
  sleep,
123
154
  stringifyDumpData,
124
- writeDumpFile
155
+ writeDumpReport,
156
+ writeLogFile
125
157
  };
package/dist/lib/image.js CHANGED
@@ -177,19 +177,26 @@ function getPkgInfo() {
177
177
  return pkg;
178
178
  }
179
179
  let pkgJsonFile = "";
180
+ let pkgDir = "";
180
181
  if ((0, import_node_fs2.existsSync)((0, import_node_path.join)(__dirname, "../package.json"))) {
181
182
  pkgJsonFile = (0, import_node_path.join)(__dirname, "../package.json");
183
+ pkgDir = (0, import_node_path.join)(__dirname, "../");
184
+ } else if ((0, import_node_fs2.existsSync)((0, import_node_path.join)(__dirname, "../../package.json"))) {
185
+ pkgJsonFile = (0, import_node_path.join)(__dirname, "../../package.json");
186
+ pkgDir = (0, import_node_path.join)(__dirname, "../../");
182
187
  } else if ((0, import_node_fs2.existsSync)((0, import_node_path.join)(__dirname, "../../../package.json"))) {
183
188
  pkgJsonFile = (0, import_node_path.join)(__dirname, "../../../package.json");
189
+ pkgDir = (0, import_node_path.join)(__dirname, "../../../");
184
190
  }
185
191
  if (pkgJsonFile) {
186
192
  const { name, version } = JSON.parse((0, import_node_fs2.readFileSync)(pkgJsonFile, "utf-8"));
187
- pkg = { name, version };
193
+ pkg = { name, version, dir: pkgDir };
188
194
  return pkg;
189
195
  }
190
196
  return {
191
197
  name: "midscene-unknown-page-name",
192
- version: "0.0.0"
198
+ version: "0.0.0",
199
+ dir: pkgDir
193
200
  };
194
201
  }
195
202
  var logDir = (0, import_node_path.join)(process.cwd(), "./midscene_run/");
package/dist/lib/index.js CHANGED
@@ -1025,7 +1025,7 @@ __export(src_exports, {
1025
1025
  getElement: () => getElement,
1026
1026
  getSection: () => getSection,
1027
1027
  plan: () => plan,
1028
- setDumpDir: () => setDumpDir
1028
+ setLogDir: () => setLogDir
1029
1029
  });
1030
1030
  module.exports = __toCommonJS(src_exports);
1031
1031
 
@@ -1044,39 +1044,71 @@ function getPkgInfo() {
1044
1044
  return pkg;
1045
1045
  }
1046
1046
  let pkgJsonFile = "";
1047
+ let pkgDir = "";
1047
1048
  if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "../package.json"))) {
1048
1049
  pkgJsonFile = (0, import_node_path.join)(__dirname, "../package.json");
1050
+ pkgDir = (0, import_node_path.join)(__dirname, "../");
1051
+ } else if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "../../package.json"))) {
1052
+ pkgJsonFile = (0, import_node_path.join)(__dirname, "../../package.json");
1053
+ pkgDir = (0, import_node_path.join)(__dirname, "../../");
1049
1054
  } else if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "../../../package.json"))) {
1050
1055
  pkgJsonFile = (0, import_node_path.join)(__dirname, "../../../package.json");
1056
+ pkgDir = (0, import_node_path.join)(__dirname, "../../../");
1051
1057
  }
1052
1058
  if (pkgJsonFile) {
1053
1059
  const { name, version } = JSON.parse((0, import_node_fs.readFileSync)(pkgJsonFile, "utf-8"));
1054
- pkg = { name, version };
1060
+ pkg = { name, version, dir: pkgDir };
1055
1061
  return pkg;
1056
1062
  }
1057
1063
  return {
1058
1064
  name: "midscene-unknown-page-name",
1059
- version: "0.0.0"
1065
+ version: "0.0.0",
1066
+ dir: pkgDir
1060
1067
  };
1061
1068
  }
1062
1069
  var logDir = (0, import_node_path.join)(process.cwd(), "./midscene_run/");
1063
1070
  var logEnvReady = false;
1064
1071
  var insightDumpFileExt = "insight-dump.json";
1065
- function getDumpDir() {
1072
+ function getLogDir() {
1066
1073
  return logDir;
1067
1074
  }
1068
- function setDumpDir(dir) {
1075
+ function setLogDir(dir) {
1069
1076
  logDir = dir;
1070
1077
  }
1071
- function getDumpDirPath(type) {
1072
- return (0, import_node_path.join)(getDumpDir(), type);
1078
+ function getLogDirByType(type) {
1079
+ const dir = (0, import_node_path.join)(getLogDir(), type);
1080
+ if (!(0, import_node_fs.existsSync)(dir)) {
1081
+ (0, import_node_fs.mkdirSync)(dir, { recursive: true });
1082
+ }
1083
+ return dir;
1073
1084
  }
1074
- function writeDumpFile(opts) {
1075
- const { fileName, fileExt, fileContent, type = "dump" } = opts;
1076
- const targetDir = getDumpDirPath(type);
1077
- if (!(0, import_node_fs.existsSync)(targetDir)) {
1078
- (0, import_node_fs.mkdirSync)(targetDir, { recursive: true });
1085
+ function writeDumpReport(fileName, dumpData) {
1086
+ const { dir } = getPkgInfo();
1087
+ const reportTplPath = (0, import_node_path.join)(dir, "./report/index.html");
1088
+ (0, import_node_fs.existsSync)(reportTplPath) || (0, import_node_assert.default)(false, `report template not found: ${reportTplPath}`);
1089
+ const reportPath = (0, import_node_path.join)(getLogDirByType("report"), `${fileName}.html`);
1090
+ const tpl = (0, import_node_fs.readFileSync)(reportTplPath, "utf-8");
1091
+ let reportContent;
1092
+ if (typeof dumpData === "string") {
1093
+ reportContent = tpl.replace(
1094
+ "{{dump}}",
1095
+ `<script type="midscene_web_dump" type="application/json">${dumpData}</script>`
1096
+ );
1097
+ } else {
1098
+ const dumps = dumpData.map(({ dumpString, attributes }) => {
1099
+ const attributesArr = Object.keys(attributes || {}).map((key) => {
1100
+ return `${key}="${encodeURIComponent(attributes[key])}"`;
1101
+ });
1102
+ return `<script type="midscene_web_dump" type="application/json" ${attributesArr.join(" ")}>${dumpString}</script>`;
1103
+ });
1104
+ reportContent = tpl.replace("{{dump}}", dumps.join("\n"));
1079
1105
  }
1106
+ (0, import_node_fs.writeFileSync)(reportPath, reportContent);
1107
+ return reportPath;
1108
+ }
1109
+ function writeLogFile(opts) {
1110
+ const { fileName, fileExt, fileContent, type = "dump" } = opts;
1111
+ const targetDir = getLogDirByType(type);
1080
1112
  if (!logEnvReady) {
1081
1113
  (0, import_node_assert.default)(targetDir, "logDir should be set before writing dump file");
1082
1114
  const gitIgnorePath = (0, import_node_path.join)(targetDir, "../../.gitignore");
@@ -1100,8 +1132,8 @@ ${logDirName}/dump
1100
1132
  }
1101
1133
  const filePath = (0, import_node_path.join)(targetDir, `${fileName}.${fileExt}`);
1102
1134
  (0, import_node_fs.writeFileSync)(filePath, fileContent);
1103
- if (type === "dump") {
1104
- (0, import_node_fs.copyFileSync)(filePath, (0, import_node_path.join)(targetDir, `latest.${fileExt}`));
1135
+ if (opts == null ? void 0 : opts.generateReport) {
1136
+ return writeDumpReport(fileName, fileContent);
1105
1137
  }
1106
1138
  return filePath;
1107
1139
  }
@@ -1218,7 +1250,7 @@ var Executor = class {
1218
1250
  successfullyCompleted = false;
1219
1251
  task.error = (e == null ? void 0 : e.message) || "error-without-message";
1220
1252
  task.errorStack = e.stack;
1221
- task.status = "fail";
1253
+ task.status = "failed";
1222
1254
  task.timing.end = Date.now();
1223
1255
  task.timing.cost = task.timing.end - task.timing.start;
1224
1256
  break;
@@ -1244,7 +1276,7 @@ var Executor = class {
1244
1276
  return null;
1245
1277
  }
1246
1278
  const errorTaskIndex = this.tasks.findIndex(
1247
- (task) => task.status === "fail"
1279
+ (task) => task.status === "failed"
1248
1280
  );
1249
1281
  if (errorTaskIndex >= 0) {
1250
1282
  return this.tasks[errorTaskIndex];
@@ -5265,7 +5297,7 @@ var logIdIndexMap = {};
5265
5297
  var { pid } = process;
5266
5298
  var logFileExt = insightDumpFileExt;
5267
5299
  function writeInsightDump(data, logId, dumpSubscriber) {
5268
- const logDir2 = getDumpDir();
5300
+ const logDir2 = getLogDir();
5269
5301
  (0, import_node_assert9.default)(logDir2, "logDir should be set before writing dump file");
5270
5302
  const id = logId || (0, import_node_crypto2.randomUUID)();
5271
5303
  const baseData = {
@@ -5289,12 +5321,13 @@ function writeInsightDump(data, logId, dumpSubscriber) {
5289
5321
  const length = logContent.push(dataString);
5290
5322
  logIdIndexMap[id] = length - 1;
5291
5323
  }
5292
- writeDumpFile({
5324
+ writeLogFile({
5293
5325
  fileName: logFileName,
5294
5326
  fileExt: logFileExt,
5295
5327
  fileContent: `[
5296
5328
  ${logContent.join(",\n")}
5297
- ]`
5329
+ ]`,
5330
+ type: "dump"
5298
5331
  });
5299
5332
  return id;
5300
5333
  }
@@ -5591,5 +5624,5 @@ var src_default = Insight;
5591
5624
  getElement,
5592
5625
  getSection,
5593
5626
  plan,
5594
- setDumpDir
5627
+ setLogDir
5595
5628
  });
package/dist/lib/utils.js CHANGED
@@ -31,8 +31,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
31
31
  var utils_exports = {};
32
32
  __export(utils_exports, {
33
33
  commonScreenshotParam: () => commonScreenshotParam,
34
- getDumpDir: () => getDumpDir,
35
- getDumpDirPath: () => getDumpDirPath,
34
+ getLogDir: () => getLogDir,
35
+ getLogDirByType: () => getLogDirByType,
36
36
  getPkgInfo: () => getPkgInfo,
37
37
  getTmpDir: () => getTmpDir,
38
38
  getTmpFile: () => getTmpFile,
@@ -40,10 +40,11 @@ __export(utils_exports, {
40
40
  insightDumpFileExt: () => insightDumpFileExt,
41
41
  overlapped: () => overlapped,
42
42
  replacerForPageObject: () => replacerForPageObject,
43
- setDumpDir: () => setDumpDir,
43
+ setLogDir: () => setLogDir,
44
44
  sleep: () => sleep,
45
45
  stringifyDumpData: () => stringifyDumpData,
46
- writeDumpFile: () => writeDumpFile
46
+ writeDumpReport: () => writeDumpReport,
47
+ writeLogFile: () => writeLogFile
47
48
  });
48
49
  module.exports = __toCommonJS(utils_exports);
49
50
  var import_node_assert = __toESM(require("assert"));
@@ -57,40 +58,72 @@ function getPkgInfo() {
57
58
  return pkg;
58
59
  }
59
60
  let pkgJsonFile = "";
61
+ let pkgDir = "";
60
62
  if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "../package.json"))) {
61
63
  pkgJsonFile = (0, import_node_path.join)(__dirname, "../package.json");
64
+ pkgDir = (0, import_node_path.join)(__dirname, "../");
65
+ } else if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "../../package.json"))) {
66
+ pkgJsonFile = (0, import_node_path.join)(__dirname, "../../package.json");
67
+ pkgDir = (0, import_node_path.join)(__dirname, "../../");
62
68
  } else if ((0, import_node_fs.existsSync)((0, import_node_path.join)(__dirname, "../../../package.json"))) {
63
69
  pkgJsonFile = (0, import_node_path.join)(__dirname, "../../../package.json");
70
+ pkgDir = (0, import_node_path.join)(__dirname, "../../../");
64
71
  }
65
72
  if (pkgJsonFile) {
66
73
  const { name, version } = JSON.parse((0, import_node_fs.readFileSync)(pkgJsonFile, "utf-8"));
67
- pkg = { name, version };
74
+ pkg = { name, version, dir: pkgDir };
68
75
  return pkg;
69
76
  }
70
77
  return {
71
78
  name: "midscene-unknown-page-name",
72
- version: "0.0.0"
79
+ version: "0.0.0",
80
+ dir: pkgDir
73
81
  };
74
82
  }
75
83
  var logDir = (0, import_node_path.join)(process.cwd(), "./midscene_run/");
76
84
  var logEnvReady = false;
77
85
  var insightDumpFileExt = "insight-dump.json";
78
86
  var groupedActionDumpFileExt = "web-dump.json";
79
- function getDumpDir() {
87
+ function getLogDir() {
80
88
  return logDir;
81
89
  }
82
- function setDumpDir(dir) {
90
+ function setLogDir(dir) {
83
91
  logDir = dir;
84
92
  }
85
- function getDumpDirPath(type) {
86
- return (0, import_node_path.join)(getDumpDir(), type);
93
+ function getLogDirByType(type) {
94
+ const dir = (0, import_node_path.join)(getLogDir(), type);
95
+ if (!(0, import_node_fs.existsSync)(dir)) {
96
+ (0, import_node_fs.mkdirSync)(dir, { recursive: true });
97
+ }
98
+ return dir;
87
99
  }
88
- function writeDumpFile(opts) {
89
- const { fileName, fileExt, fileContent, type = "dump" } = opts;
90
- const targetDir = getDumpDirPath(type);
91
- if (!(0, import_node_fs.existsSync)(targetDir)) {
92
- (0, import_node_fs.mkdirSync)(targetDir, { recursive: true });
100
+ function writeDumpReport(fileName, dumpData) {
101
+ const { dir } = getPkgInfo();
102
+ const reportTplPath = (0, import_node_path.join)(dir, "./report/index.html");
103
+ (0, import_node_fs.existsSync)(reportTplPath) || (0, import_node_assert.default)(false, `report template not found: ${reportTplPath}`);
104
+ const reportPath = (0, import_node_path.join)(getLogDirByType("report"), `${fileName}.html`);
105
+ const tpl = (0, import_node_fs.readFileSync)(reportTplPath, "utf-8");
106
+ let reportContent;
107
+ if (typeof dumpData === "string") {
108
+ reportContent = tpl.replace(
109
+ "{{dump}}",
110
+ `<script type="midscene_web_dump" type="application/json">${dumpData}</script>`
111
+ );
112
+ } else {
113
+ const dumps = dumpData.map(({ dumpString, attributes }) => {
114
+ const attributesArr = Object.keys(attributes || {}).map((key) => {
115
+ return `${key}="${encodeURIComponent(attributes[key])}"`;
116
+ });
117
+ return `<script type="midscene_web_dump" type="application/json" ${attributesArr.join(" ")}>${dumpString}</script>`;
118
+ });
119
+ reportContent = tpl.replace("{{dump}}", dumps.join("\n"));
93
120
  }
121
+ (0, import_node_fs.writeFileSync)(reportPath, reportContent);
122
+ return reportPath;
123
+ }
124
+ function writeLogFile(opts) {
125
+ const { fileName, fileExt, fileContent, type = "dump" } = opts;
126
+ const targetDir = getLogDirByType(type);
94
127
  if (!logEnvReady) {
95
128
  (0, import_node_assert.default)(targetDir, "logDir should be set before writing dump file");
96
129
  const gitIgnorePath = (0, import_node_path.join)(targetDir, "../../.gitignore");
@@ -114,8 +147,8 @@ ${logDirName}/dump
114
147
  }
115
148
  const filePath = (0, import_node_path.join)(targetDir, `${fileName}.${fileExt}`);
116
149
  (0, import_node_fs.writeFileSync)(filePath, fileContent);
117
- if (type === "dump") {
118
- (0, import_node_fs.copyFileSync)(filePath, (0, import_node_path.join)(targetDir, `latest.${fileExt}`));
150
+ if (opts == null ? void 0 : opts.generateReport) {
151
+ return writeDumpReport(fileName, fileContent);
119
152
  }
120
153
  return filePath;
121
154
  }
@@ -151,8 +184,8 @@ function stringifyDumpData(data, indents) {
151
184
  // Annotate the CommonJS export names for ESM import in node:
152
185
  0 && (module.exports = {
153
186
  commonScreenshotParam,
154
- getDumpDir,
155
- getDumpDirPath,
187
+ getLogDir,
188
+ getLogDirByType,
156
189
  getPkgInfo,
157
190
  getTmpDir,
158
191
  getTmpFile,
@@ -160,8 +193,9 @@ function stringifyDumpData(data, indents) {
160
193
  insightDumpFileExt,
161
194
  overlapped,
162
195
  replacerForPageObject,
163
- setDumpDir,
196
+ setLogDir,
164
197
  sleep,
165
198
  stringifyDumpData,
166
- writeDumpFile
199
+ writeDumpReport,
200
+ writeLogFile
167
201
  });
@@ -1,8 +1,8 @@
1
1
  import { ChatCompletionMessageParam } from 'openai/resources';
2
2
  export { ChatCompletionMessageParam } from 'openai/resources';
3
- import { c as callAiFn } from './index-b994b411.js';
4
- export { d as describeUserPage, p as plan } from './index-b994b411.js';
5
- import { B as BaseElement, U as UIContext, e as AIElementParseResponse, f as AISectionParseResponse, g as AIAssertionResponse } from './types-60690f66.js';
3
+ import { c as callAiFn } from './index-f43935c0.js';
4
+ export { d as describeUserPage, p as plan } from './index-f43935c0.js';
5
+ import { B as BaseElement, U as UIContext, e as AIElementParseResponse, f as AISectionParseResponse, g as AIAssertionResponse } from './types-81f7991c.js';
6
6
 
7
7
  declare function AiInspectElement<ElementType extends BaseElement = BaseElement>(options: {
8
8
  context: UIContext<ElementType>;
@@ -1,5 +1,5 @@
1
1
  import { Buffer } from 'node:buffer';
2
- import { S as Size, R as Rect, h as UISection, U as UIContext, x as Color } from './types-60690f66.js';
2
+ import { S as Size, R as Rect, h as UISection, U as UIContext, y as Color } from './types-81f7991c.js';
3
3
  import 'openai/resources';
4
4
 
5
5
  /**
@@ -1,4 +1,4 @@
1
- import { B as BaseElement, U as UIContext, p as PlanningAction } from './types-60690f66.js';
1
+ import { B as BaseElement, U as UIContext, q as PlanningAction } from './types-81f7991c.js';
2
2
  import { ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam } from 'openai/resources';
3
3
 
4
4
  type AIArgs = [