testdriverai 7.2.79 → 7.2.81

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.
@@ -1,5 +1,6 @@
1
1
  import { execSync } from "child_process";
2
2
  import crypto from "crypto";
3
+ import fs from "fs";
3
4
  import { createRequire } from "module";
4
5
  import path from "path";
5
6
  import { postOrUpdateTestResults } from "../lib/github-comment.mjs";
@@ -1201,6 +1202,49 @@ function getGitInfo() {
1201
1202
  // GitHub Comment Helper
1202
1203
  // ============================================================================
1203
1204
 
1205
+ /**
1206
+ * Extract PR number from GitHub Actions environment
1207
+ * Checks multiple sources: env vars, event file, and GITHUB_REF
1208
+ * @returns {string|null} PR number or null if not found
1209
+ */
1210
+ function extractPRNumber() {
1211
+ // Try direct environment variables first
1212
+ let prNumber =
1213
+ process.env.GITHUB_PR_NUMBER ||
1214
+ process.env.TD_GITHUB_PR ||
1215
+ process.env.PR_NUMBER;
1216
+
1217
+ if (prNumber) {
1218
+ return prNumber;
1219
+ }
1220
+
1221
+ // Try to extract from GitHub Actions event path
1222
+ if (process.env.GITHUB_EVENT_PATH) {
1223
+ try {
1224
+ const eventData = JSON.parse(
1225
+ fs.readFileSync(process.env.GITHUB_EVENT_PATH, "utf8"),
1226
+ );
1227
+ if (eventData.pull_request?.number) {
1228
+ return String(eventData.pull_request.number);
1229
+ }
1230
+ } catch (err) {
1231
+ logger.debug("Could not read GitHub event file:", err.message);
1232
+ }
1233
+ }
1234
+
1235
+ // Try to extract from GITHUB_REF (refs/pull/123/merge or refs/pull/123/head)
1236
+ if (process.env.GITHUB_REF) {
1237
+ const match = process.env.GITHUB_REF.match(
1238
+ /refs\/pull\/(\d+)\/(merge|head)/,
1239
+ );
1240
+ if (match) {
1241
+ return match[1];
1242
+ }
1243
+ }
1244
+
1245
+ return null;
1246
+ }
1247
+
1204
1248
  /**
1205
1249
  * Post GitHub comment with test results if enabled
1206
1250
  * Checks for GitHub token and PR number in environment variables
@@ -1220,7 +1264,7 @@ async function postGitHubCommentIfEnabled(testRunUrl, stats, completeData) {
1220
1264
 
1221
1265
  // Check if GitHub comment posting is enabled
1222
1266
  const githubToken = process.env.GITHUB_TOKEN || process.env.GH_TOKEN;
1223
- const prNumber = process.env.GITHUB_PR_NUMBER;
1267
+ const prNumber = extractPRNumber();
1224
1268
  const commitSha = process.env.GITHUB_SHA || pluginState.gitInfo.commit;
1225
1269
 
1226
1270
  // Only post if we have a token and either a PR number or commit SHA
@@ -28,6 +28,68 @@ function httpsGet(url) {
28
28
  });
29
29
  }
30
30
 
31
+ /**
32
+ * Safely parse JSON response that may contain extra characters or multiple objects.
33
+ * The 2captcha API sometimes returns concatenated JSON or has trailing characters.
34
+ * @param {string} text - The response text to parse
35
+ * @returns {object} The parsed JSON object
36
+ */
37
+ function safeParseJson(text) {
38
+ // Trim whitespace first
39
+ const trimmed = text.trim();
40
+
41
+ // Try standard parsing first
42
+ try {
43
+ return JSON.parse(trimmed);
44
+ } catch {
45
+ // If standard parsing fails, try to extract the first valid JSON object
46
+ const jsonStart = trimmed.indexOf("{");
47
+ if (jsonStart === -1) {
48
+ throw new Error("No JSON object found in response: " + trimmed);
49
+ }
50
+
51
+ // Find the matching closing brace by counting braces
52
+ let braceCount = 0;
53
+ let inString = false;
54
+ let escape = false;
55
+
56
+ for (let i = jsonStart; i < trimmed.length; i++) {
57
+ const char = trimmed[i];
58
+
59
+ if (escape) {
60
+ escape = false;
61
+ continue;
62
+ }
63
+
64
+ if (char === "\\") {
65
+ escape = true;
66
+ continue;
67
+ }
68
+
69
+ if (char === '"') {
70
+ inString = !inString;
71
+ continue;
72
+ }
73
+
74
+ if (!inString) {
75
+ if (char === "{") {
76
+ braceCount++;
77
+ } else if (char === "}") {
78
+ braceCount--;
79
+ if (braceCount === 0) {
80
+ // Found the end of the first complete JSON object
81
+ const jsonStr = trimmed.substring(jsonStart, i + 1);
82
+ return JSON.parse(jsonStr);
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ // If we get here, we couldn't find a complete JSON object
89
+ throw new Error("Invalid JSON in response: " + trimmed);
90
+ }
91
+ }
92
+
31
93
  // Auto-detection script that runs in the browser context
32
94
  const detectCaptchaScript = `
33
95
  (function() {
@@ -232,7 +294,7 @@ const checkSuccessScript = `(function() {
232
294
 
233
295
  // Submit to 2captcha
234
296
  console.log("SUBMITTING...");
235
- const submitResp = JSON.parse(await httpsGet(submitUrl));
297
+ const submitResp = safeParseJson(await httpsGet(submitUrl));
236
298
  if (submitResp.status !== 1) {
237
299
  throw new Error("Submit failed: " + JSON.stringify(submitResp));
238
300
  }
@@ -244,7 +306,7 @@ const checkSuccessScript = `(function() {
244
306
  const maxAttempts = Math.ceil(config.timeout / config.pollInterval);
245
307
  for (let i = 0; i < maxAttempts; i++) {
246
308
  await sleep(config.pollInterval);
247
- const resultResp = JSON.parse(
309
+ const resultResp = safeParseJson(
248
310
  await httpsGet(
249
311
  "https://2captcha.com/res.php?key=" +
250
312
  config.apiKey +
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "testdriverai",
3
- "version": "7.2.79",
3
+ "version": "7.2.81",
4
4
  "description": "Next generation autonomous AI agent for end-to-end testing of web & desktop",
5
5
  "main": "sdk.js",
6
6
  "types": "sdk.d.ts",