patchright-bun 1.58.1 → 1.59.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.
Files changed (183) hide show
  1. package/README.md +225 -225
  2. package/ThirdPartyNotices.txt +3918 -5041
  3. package/cli.js +19 -19
  4. package/index.d.ts +17 -17
  5. package/index.js +17 -17
  6. package/index.mjs +18 -18
  7. package/jsx-runtime.js +42 -42
  8. package/jsx-runtime.mjs +21 -21
  9. package/lib/agents/agentParser.js +89 -89
  10. package/lib/agents/copilot-setup-steps.yml +34 -34
  11. package/lib/agents/generateAgents.js +346 -348
  12. package/lib/agents/playwright-test-coverage.prompt.md +31 -31
  13. package/lib/agents/playwright-test-generate.prompt.md +8 -8
  14. package/lib/agents/playwright-test-generator.agent.md +88 -88
  15. package/lib/agents/playwright-test-heal.prompt.md +6 -6
  16. package/lib/agents/playwright-test-healer.agent.md +55 -55
  17. package/lib/agents/playwright-test-plan.prompt.md +9 -9
  18. package/lib/agents/playwright-test-planner.agent.md +73 -73
  19. package/lib/common/config.js +281 -282
  20. package/lib/common/configLoader.js +344 -344
  21. package/lib/common/esmLoaderHost.js +104 -104
  22. package/lib/common/expectBundle.js +28 -28
  23. package/lib/common/expectBundleImpl.js +407 -407
  24. package/lib/common/fixtures.js +302 -302
  25. package/lib/common/globals.js +58 -58
  26. package/lib/common/ipc.js +60 -60
  27. package/lib/common/poolBuilder.js +85 -85
  28. package/lib/common/process.js +133 -132
  29. package/lib/common/suiteUtils.js +140 -140
  30. package/lib/common/test.js +330 -321
  31. package/lib/common/testLoader.js +101 -101
  32. package/lib/common/testType.js +301 -298
  33. package/lib/common/validators.js +68 -68
  34. package/lib/errorContext.js +121 -0
  35. package/lib/fsWatcher.js +67 -67
  36. package/lib/index.js +764 -726
  37. package/lib/internalsForTest.js +42 -42
  38. package/lib/isomorphic/events.js +77 -77
  39. package/lib/isomorphic/folders.js +30 -30
  40. package/lib/isomorphic/stringInternPool.js +69 -69
  41. package/lib/isomorphic/teleReceiver.js +538 -521
  42. package/lib/isomorphic/teleSuiteUpdater.js +157 -157
  43. package/lib/isomorphic/testServerConnection.js +223 -225
  44. package/lib/isomorphic/testServerInterface.js +16 -16
  45. package/lib/isomorphic/testTree.js +329 -329
  46. package/lib/isomorphic/types.d.js +16 -16
  47. package/lib/loader/loaderMain.js +59 -59
  48. package/lib/matchers/expect.js +311 -311
  49. package/lib/matchers/matcherHint.js +44 -44
  50. package/lib/matchers/matchers.js +385 -383
  51. package/lib/matchers/toBeTruthy.js +75 -75
  52. package/lib/matchers/toEqual.js +100 -100
  53. package/lib/matchers/toHaveURL.js +101 -101
  54. package/lib/matchers/toMatchAriaSnapshot.js +163 -159
  55. package/lib/matchers/toMatchSnapshot.js +349 -342
  56. package/lib/matchers/toMatchText.js +99 -99
  57. package/lib/mcp/test/browserBackend.js +121 -98
  58. package/lib/mcp/test/generatorTools.js +122 -122
  59. package/lib/mcp/test/plannerTools.js +145 -145
  60. package/lib/mcp/test/seed.js +82 -82
  61. package/lib/mcp/test/streams.js +44 -44
  62. package/lib/mcp/test/testBackend.js +99 -99
  63. package/lib/mcp/test/testContext.js +283 -285
  64. package/lib/mcp/test/testTool.js +30 -30
  65. package/lib/mcp/test/testTools.js +108 -108
  66. package/lib/plugins/gitCommitInfoPlugin.js +198 -198
  67. package/lib/plugins/index.js +28 -28
  68. package/lib/plugins/webServerPlugin.js +238 -237
  69. package/lib/program.js +239 -417
  70. package/lib/reportActions.js +80 -0
  71. package/lib/reporters/base.js +633 -634
  72. package/lib/reporters/blob.js +138 -138
  73. package/lib/reporters/dot.js +99 -99
  74. package/lib/reporters/empty.js +32 -32
  75. package/lib/reporters/github.js +127 -128
  76. package/lib/reporters/html.js +666 -633
  77. package/lib/reporters/internalReporter.js +138 -138
  78. package/lib/reporters/json.js +254 -254
  79. package/lib/reporters/junit.js +321 -232
  80. package/lib/reporters/line.js +131 -131
  81. package/lib/reporters/list.js +252 -253
  82. package/lib/reporters/listModeReporter.js +69 -69
  83. package/lib/reporters/markdown.js +144 -144
  84. package/lib/reporters/merge.js +579 -558
  85. package/lib/reporters/multiplexer.js +116 -112
  86. package/lib/reporters/reporterV2.js +102 -102
  87. package/lib/reporters/teleEmitter.js +319 -317
  88. package/lib/reporters/versions/blobV1.js +16 -16
  89. package/lib/runner/dispatcher.js +522 -530
  90. package/lib/runner/failureTracker.js +72 -72
  91. package/lib/runner/lastRun.js +77 -77
  92. package/lib/runner/loadUtils.js +340 -334
  93. package/lib/runner/loaderHost.js +89 -89
  94. package/lib/runner/processHost.js +180 -180
  95. package/lib/runner/projectUtils.js +241 -241
  96. package/lib/runner/rebase.js +189 -189
  97. package/lib/runner/reporters.js +143 -138
  98. package/lib/runner/sigIntWatcher.js +96 -96
  99. package/lib/runner/taskRunner.js +127 -127
  100. package/lib/runner/tasks.js +413 -410
  101. package/lib/runner/testGroups.js +125 -125
  102. package/lib/runner/testRunner.js +398 -398
  103. package/lib/runner/testServer.js +269 -269
  104. package/lib/runner/uiModeReporter.js +30 -30
  105. package/lib/runner/vcs.js +72 -72
  106. package/lib/runner/watchMode.js +396 -396
  107. package/lib/runner/workerHost.js +101 -104
  108. package/lib/testActions.js +220 -0
  109. package/lib/third_party/pirates.js +62 -62
  110. package/lib/third_party/tsconfig-loader.js +103 -103
  111. package/lib/transform/babelBundle.js +43 -46
  112. package/lib/transform/babelBundleImpl.js +461 -461
  113. package/lib/transform/compilationCache.js +272 -274
  114. package/lib/transform/esmLoader.js +105 -103
  115. package/lib/transform/portTransport.js +67 -67
  116. package/lib/transform/transform.js +296 -303
  117. package/lib/util.js +400 -400
  118. package/lib/utilsBundle.js +43 -50
  119. package/lib/utilsBundleImpl.js +100 -103
  120. package/lib/worker/fixtureRunner.js +262 -262
  121. package/lib/worker/testInfo.js +532 -536
  122. package/lib/worker/testTracing.js +351 -345
  123. package/lib/worker/timeoutManager.js +185 -174
  124. package/lib/worker/util.js +31 -31
  125. package/lib/worker/workerMain.js +533 -530
  126. package/package.json +2 -6
  127. package/test.d.ts +18 -18
  128. package/test.js +24 -24
  129. package/test.mjs +34 -34
  130. package/types/test.d.ts +10322 -10251
  131. package/types/testReporter.d.ts +822 -822
  132. package/lib/mcp/browser/browserContextFactory.js +0 -329
  133. package/lib/mcp/browser/browserServerBackend.js +0 -84
  134. package/lib/mcp/browser/config.js +0 -421
  135. package/lib/mcp/browser/context.js +0 -244
  136. package/lib/mcp/browser/response.js +0 -278
  137. package/lib/mcp/browser/sessionLog.js +0 -75
  138. package/lib/mcp/browser/tab.js +0 -343
  139. package/lib/mcp/browser/tools/common.js +0 -65
  140. package/lib/mcp/browser/tools/console.js +0 -46
  141. package/lib/mcp/browser/tools/dialogs.js +0 -60
  142. package/lib/mcp/browser/tools/evaluate.js +0 -61
  143. package/lib/mcp/browser/tools/files.js +0 -58
  144. package/lib/mcp/browser/tools/form.js +0 -63
  145. package/lib/mcp/browser/tools/install.js +0 -72
  146. package/lib/mcp/browser/tools/keyboard.js +0 -107
  147. package/lib/mcp/browser/tools/mouse.js +0 -107
  148. package/lib/mcp/browser/tools/navigate.js +0 -71
  149. package/lib/mcp/browser/tools/network.js +0 -63
  150. package/lib/mcp/browser/tools/open.js +0 -57
  151. package/lib/mcp/browser/tools/pdf.js +0 -49
  152. package/lib/mcp/browser/tools/runCode.js +0 -78
  153. package/lib/mcp/browser/tools/screenshot.js +0 -93
  154. package/lib/mcp/browser/tools/snapshot.js +0 -173
  155. package/lib/mcp/browser/tools/tabs.js +0 -67
  156. package/lib/mcp/browser/tools/tool.js +0 -47
  157. package/lib/mcp/browser/tools/tracing.js +0 -74
  158. package/lib/mcp/browser/tools/utils.js +0 -94
  159. package/lib/mcp/browser/tools/verify.js +0 -143
  160. package/lib/mcp/browser/tools/wait.js +0 -63
  161. package/lib/mcp/browser/tools.js +0 -84
  162. package/lib/mcp/browser/watchdog.js +0 -44
  163. package/lib/mcp/config.d.js +0 -16
  164. package/lib/mcp/extension/cdpRelay.js +0 -351
  165. package/lib/mcp/extension/extensionContextFactory.js +0 -76
  166. package/lib/mcp/extension/protocol.js +0 -28
  167. package/lib/mcp/index.js +0 -61
  168. package/lib/mcp/log.js +0 -35
  169. package/lib/mcp/program.js +0 -111
  170. package/lib/mcp/sdk/exports.js +0 -28
  171. package/lib/mcp/sdk/http.js +0 -152
  172. package/lib/mcp/sdk/inProcessTransport.js +0 -71
  173. package/lib/mcp/sdk/server.js +0 -223
  174. package/lib/mcp/sdk/tool.js +0 -47
  175. package/lib/mcp/terminal/cli.js +0 -296
  176. package/lib/mcp/terminal/command.js +0 -56
  177. package/lib/mcp/terminal/commands.js +0 -333
  178. package/lib/mcp/terminal/daemon.js +0 -129
  179. package/lib/mcp/terminal/help.json +0 -32
  180. package/lib/mcp/terminal/helpGenerator.js +0 -88
  181. package/lib/mcp/terminal/socketConnection.js +0 -80
  182. package/lib/runner/storage.js +0 -91
  183. package/lib/transform/md.js +0 -221
@@ -1,530 +1,522 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
- var dispatcher_exports = {};
20
- __export(dispatcher_exports, {
21
- Dispatcher: () => Dispatcher
22
- });
23
- module.exports = __toCommonJS(dispatcher_exports);
24
- var import_utils = require("patchright-core/lib/utils");
25
- var import_utils2 = require("patchright-core/lib/utils");
26
- var import_rebase = require("./rebase");
27
- var import_workerHost = require("./workerHost");
28
- var import_ipc = require("../common/ipc");
29
- var import_internalReporter = require("../reporters/internalReporter");
30
- var import_util = require("../util");
31
- var import_storage = require("./storage");
32
- class Dispatcher {
33
- constructor(config, reporter, failureTracker) {
34
- this._workerSlots = [];
35
- this._queue = [];
36
- this._workerLimitPerProjectId = /* @__PURE__ */ new Map();
37
- this._queuedOrRunningHashCount = /* @__PURE__ */ new Map();
38
- this._finished = new import_utils.ManualPromise();
39
- this._isStopped = true;
40
- this._extraEnvByProjectId = /* @__PURE__ */ new Map();
41
- this._producedEnvByProjectId = /* @__PURE__ */ new Map();
42
- this._config = config;
43
- this._reporter = reporter;
44
- this._failureTracker = failureTracker;
45
- for (const project of config.projects) {
46
- if (project.workers)
47
- this._workerLimitPerProjectId.set(project.id, project.workers);
48
- }
49
- }
50
- _findFirstJobToRun() {
51
- for (let index = 0; index < this._queue.length; index++) {
52
- const job = this._queue[index];
53
- const projectIdWorkerLimit = this._workerLimitPerProjectId.get(job.projectId);
54
- if (!projectIdWorkerLimit)
55
- return index;
56
- const runningWorkersWithSameProjectId = this._workerSlots.filter((w) => w.busy && w.worker && w.worker.projectId() === job.projectId).length;
57
- if (runningWorkersWithSameProjectId < projectIdWorkerLimit)
58
- return index;
59
- }
60
- return -1;
61
- }
62
- _scheduleJob() {
63
- if (this._isStopped)
64
- return;
65
- const jobIndex = this._findFirstJobToRun();
66
- if (jobIndex === -1)
67
- return;
68
- const job = this._queue[jobIndex];
69
- let workerIndex = this._workerSlots.findIndex((w) => !w.busy && w.worker && w.worker.hash() === job.workerHash && !w.worker.didSendStop());
70
- if (workerIndex === -1)
71
- workerIndex = this._workerSlots.findIndex((w) => !w.busy);
72
- if (workerIndex === -1) {
73
- return;
74
- }
75
- this._queue.splice(jobIndex, 1);
76
- const jobDispatcher = new JobDispatcher(job, this._config, this._reporter, this._failureTracker, () => this.stop().catch(() => {
77
- }));
78
- this._workerSlots[workerIndex].busy = true;
79
- this._workerSlots[workerIndex].jobDispatcher = jobDispatcher;
80
- void this._runJobInWorker(workerIndex, jobDispatcher).then(() => {
81
- this._workerSlots[workerIndex].jobDispatcher = void 0;
82
- this._workerSlots[workerIndex].busy = false;
83
- this._checkFinished();
84
- this._scheduleJob();
85
- });
86
- }
87
- async _runJobInWorker(index, jobDispatcher) {
88
- const job = jobDispatcher.job;
89
- if (jobDispatcher.skipWholeJob())
90
- return;
91
- let worker = this._workerSlots[index].worker;
92
- if (worker && (worker.hash() !== job.workerHash || worker.didSendStop())) {
93
- await worker.stop();
94
- worker = void 0;
95
- if (this._isStopped)
96
- return;
97
- }
98
- let startError;
99
- if (!worker) {
100
- worker = this._createWorker(job, index, (0, import_ipc.serializeConfig)(this._config, true));
101
- this._workerSlots[index].worker = worker;
102
- worker.on("exit", () => this._workerSlots[index].worker = void 0);
103
- startError = await worker.start();
104
- if (this._isStopped)
105
- return;
106
- }
107
- if (startError)
108
- jobDispatcher.onExit(startError);
109
- else
110
- jobDispatcher.runInWorker(worker);
111
- const result = await jobDispatcher.jobResult;
112
- this._updateCounterForWorkerHash(job.workerHash, -1);
113
- if (result.didFail)
114
- void worker.stop(
115
- true
116
- /* didFail */
117
- );
118
- else if (this._isWorkerRedundant(worker))
119
- void worker.stop();
120
- if (!this._isStopped && result.newJob) {
121
- this._queue.unshift(result.newJob);
122
- this._updateCounterForWorkerHash(result.newJob.workerHash, 1);
123
- }
124
- }
125
- _checkFinished() {
126
- if (this._finished.isDone())
127
- return;
128
- if (this._queue.length && !this._isStopped)
129
- return;
130
- if (this._workerSlots.some((w) => w.busy))
131
- return;
132
- this._finished.resolve();
133
- }
134
- _isWorkerRedundant(worker) {
135
- let workersWithSameHash = 0;
136
- for (const slot of this._workerSlots) {
137
- if (slot.worker && !slot.worker.didSendStop() && slot.worker.hash() === worker.hash())
138
- workersWithSameHash++;
139
- }
140
- return workersWithSameHash > this._queuedOrRunningHashCount.get(worker.hash());
141
- }
142
- _updateCounterForWorkerHash(hash, delta) {
143
- this._queuedOrRunningHashCount.set(hash, delta + (this._queuedOrRunningHashCount.get(hash) || 0));
144
- }
145
- async run(testGroups, extraEnvByProjectId) {
146
- this._extraEnvByProjectId = extraEnvByProjectId;
147
- this._queue = testGroups;
148
- for (const group of testGroups)
149
- this._updateCounterForWorkerHash(group.workerHash, 1);
150
- this._isStopped = false;
151
- this._workerSlots = [];
152
- if (this._failureTracker.hasReachedMaxFailures())
153
- void this.stop();
154
- for (let i = 0; i < this._config.config.workers; i++)
155
- this._workerSlots.push({ busy: false });
156
- for (let i = 0; i < this._workerSlots.length; i++)
157
- this._scheduleJob();
158
- this._checkFinished();
159
- await this._finished;
160
- }
161
- _createWorker(testGroup, parallelIndex, loaderData) {
162
- const projectConfig = this._config.projects.find((p) => p.id === testGroup.projectId);
163
- const outputDir = projectConfig.project.outputDir;
164
- const worker = new import_workerHost.WorkerHost(testGroup, {
165
- parallelIndex,
166
- config: loaderData,
167
- extraEnv: this._extraEnvByProjectId.get(testGroup.projectId) || {},
168
- outputDir,
169
- pauseOnError: this._failureTracker.pauseOnError(),
170
- pauseAtEnd: this._failureTracker.pauseAtEnd(projectConfig)
171
- });
172
- const handleOutput = (params) => {
173
- const chunk = chunkFromParams(params);
174
- if (worker.didFail()) {
175
- return { chunk };
176
- }
177
- const currentlyRunning = this._workerSlots[parallelIndex].jobDispatcher?.currentlyRunning();
178
- if (!currentlyRunning)
179
- return { chunk };
180
- return { chunk, test: currentlyRunning.test, result: currentlyRunning.result };
181
- };
182
- worker.on("stdOut", (params) => {
183
- const { chunk, test, result } = handleOutput(params);
184
- result?.stdout.push(chunk);
185
- this._reporter.onStdOut?.(chunk, test, result);
186
- });
187
- worker.on("stdErr", (params) => {
188
- const { chunk, test, result } = handleOutput(params);
189
- result?.stderr.push(chunk);
190
- this._reporter.onStdErr?.(chunk, test, result);
191
- });
192
- worker.on("teardownErrors", (params) => {
193
- this._failureTracker.onWorkerError();
194
- for (const error of params.fatalErrors)
195
- this._reporter.onError?.(error);
196
- });
197
- worker.on("exit", () => {
198
- const producedEnv = this._producedEnvByProjectId.get(testGroup.projectId) || {};
199
- this._producedEnvByProjectId.set(testGroup.projectId, { ...producedEnv, ...worker.producedEnv() });
200
- });
201
- worker.onRequest("cloneStorage", async (params) => {
202
- return await import_storage.Storage.clone(params.storageFile, outputDir);
203
- });
204
- worker.onRequest("upstreamStorage", async (params) => {
205
- await import_storage.Storage.upstream(params.storageFile, params.storageOutFile);
206
- });
207
- return worker;
208
- }
209
- producedEnvByProjectId() {
210
- return this._producedEnvByProjectId;
211
- }
212
- async stop() {
213
- if (this._isStopped)
214
- return;
215
- this._isStopped = true;
216
- await Promise.all(this._workerSlots.map(({ worker }) => worker?.stop()));
217
- this._checkFinished();
218
- }
219
- }
220
- class JobDispatcher {
221
- constructor(job, config, reporter, failureTracker, stopCallback) {
222
- this.jobResult = new import_utils.ManualPromise();
223
- this._listeners = [];
224
- this._failedTests = /* @__PURE__ */ new Set();
225
- this._failedWithNonRetriableError = /* @__PURE__ */ new Set();
226
- this._remainingByTestId = /* @__PURE__ */ new Map();
227
- this._dataByTestId = /* @__PURE__ */ new Map();
228
- this._parallelIndex = 0;
229
- this._workerIndex = 0;
230
- this.job = job;
231
- this._config = config;
232
- this._reporter = reporter;
233
- this._failureTracker = failureTracker;
234
- this._stopCallback = stopCallback;
235
- this._remainingByTestId = new Map(this.job.tests.map((e) => [e.id, e]));
236
- }
237
- _onTestBegin(params) {
238
- const test = this._remainingByTestId.get(params.testId);
239
- if (!test) {
240
- return;
241
- }
242
- const result = test._appendTestResult();
243
- this._dataByTestId.set(test.id, { test, result, steps: /* @__PURE__ */ new Map() });
244
- result.parallelIndex = this._parallelIndex;
245
- result.workerIndex = this._workerIndex;
246
- result.startTime = new Date(params.startWallTime);
247
- this._reporter.onTestBegin?.(test, result);
248
- this._currentlyRunning = { test, result };
249
- }
250
- _onTestEnd(params) {
251
- if (this._failureTracker.hasReachedMaxFailures()) {
252
- params.status = "interrupted";
253
- params.errors = [];
254
- }
255
- const data = this._dataByTestId.get(params.testId);
256
- if (!data) {
257
- return;
258
- }
259
- this._dataByTestId.delete(params.testId);
260
- this._remainingByTestId.delete(params.testId);
261
- const { result, test } = data;
262
- result.duration = params.duration;
263
- result.errors = params.errors;
264
- result.error = result.errors[0];
265
- result.status = params.status;
266
- result.annotations = params.annotations;
267
- test.annotations = [...params.annotations];
268
- test.expectedStatus = params.expectedStatus;
269
- test.timeout = params.timeout;
270
- const isFailure = result.status !== "skipped" && result.status !== test.expectedStatus;
271
- if (isFailure)
272
- this._failedTests.add(test);
273
- if (params.hasNonRetriableError)
274
- this._addNonretriableTestAndSerialModeParents(test);
275
- this._reportTestEnd(test, result);
276
- this._currentlyRunning = void 0;
277
- }
278
- _addNonretriableTestAndSerialModeParents(test) {
279
- this._failedWithNonRetriableError.add(test);
280
- for (let parent = test.parent; parent; parent = parent.parent) {
281
- if (parent._parallelMode === "serial")
282
- this._failedWithNonRetriableError.add(parent);
283
- }
284
- }
285
- _onStepBegin(params) {
286
- const data = this._dataByTestId.get(params.testId);
287
- if (!data) {
288
- return;
289
- }
290
- const { result, steps, test } = data;
291
- const parentStep = params.parentStepId ? steps.get(params.parentStepId) : void 0;
292
- const step = {
293
- title: params.title,
294
- titlePath: () => {
295
- const parentPath = parentStep?.titlePath() || [];
296
- return [...parentPath, params.title];
297
- },
298
- parent: parentStep,
299
- category: params.category,
300
- startTime: new Date(params.wallTime),
301
- duration: -1,
302
- steps: [],
303
- attachments: [],
304
- annotations: [],
305
- location: params.location
306
- };
307
- steps.set(params.stepId, step);
308
- (parentStep || result).steps.push(step);
309
- this._reporter.onStepBegin?.(test, result, step);
310
- }
311
- _onStepEnd(params) {
312
- const data = this._dataByTestId.get(params.testId);
313
- if (!data) {
314
- return;
315
- }
316
- const { result, steps, test } = data;
317
- const step = steps.get(params.stepId);
318
- if (!step) {
319
- this._reporter.onStdErr?.("Internal error: step end without step begin: " + params.stepId, test, result);
320
- return;
321
- }
322
- step.duration = params.wallTime - step.startTime.getTime();
323
- if (params.error)
324
- step.error = params.error;
325
- if (params.suggestedRebaseline)
326
- (0, import_rebase.addSuggestedRebaseline)(step.location, params.suggestedRebaseline);
327
- step.annotations = params.annotations;
328
- steps.delete(params.stepId);
329
- this._reporter.onStepEnd?.(test, result, step);
330
- }
331
- _onAttach(params) {
332
- const data = this._dataByTestId.get(params.testId);
333
- if (!data) {
334
- return;
335
- }
336
- const attachment = {
337
- name: params.name,
338
- path: params.path,
339
- contentType: params.contentType,
340
- body: params.body !== void 0 ? Buffer.from(params.body, "base64") : void 0
341
- };
342
- data.result.attachments.push(attachment);
343
- if (params.stepId) {
344
- const step = data.steps.get(params.stepId);
345
- if (step)
346
- step.attachments.push(attachment);
347
- else
348
- this._reporter.onStdErr?.("Internal error: step id not found: " + params.stepId);
349
- }
350
- }
351
- _failTestWithErrors(test, errors) {
352
- const runData = this._dataByTestId.get(test.id);
353
- let result;
354
- if (runData) {
355
- result = runData.result;
356
- } else {
357
- result = test._appendTestResult();
358
- this._reporter.onTestBegin?.(test, result);
359
- }
360
- result.errors = [...errors];
361
- result.error = result.errors[0];
362
- result.status = errors.length ? "failed" : "skipped";
363
- this._reportTestEnd(test, result);
364
- this._failedTests.add(test);
365
- }
366
- _massSkipTestsFromRemaining(testIds, errors) {
367
- for (const test of this._remainingByTestId.values()) {
368
- if (!testIds.has(test.id))
369
- continue;
370
- if (!this._failureTracker.hasReachedMaxFailures()) {
371
- this._failTestWithErrors(test, errors);
372
- errors = [];
373
- }
374
- this._remainingByTestId.delete(test.id);
375
- }
376
- if (errors.length) {
377
- this._failureTracker.onWorkerError();
378
- for (const error of errors)
379
- this._reporter.onError?.(error);
380
- }
381
- }
382
- _onDone(params) {
383
- if (!this._remainingByTestId.size && !this._failedTests.size && !params.fatalErrors.length && !params.skipTestsDueToSetupFailure.length && !params.fatalUnknownTestIds && !params.unexpectedExitError && !params.stoppedDueToUnhandledErrorInTestFail) {
384
- this._finished({ didFail: false });
385
- return;
386
- }
387
- for (const testId of params.fatalUnknownTestIds || []) {
388
- const test = this._remainingByTestId.get(testId);
389
- if (test) {
390
- this._remainingByTestId.delete(testId);
391
- this._failTestWithErrors(test, [{ message: `Test not found in the worker process. Make sure test title does not change.` }]);
392
- }
393
- }
394
- if (params.fatalErrors.length) {
395
- this._massSkipTestsFromRemaining(new Set(this._remainingByTestId.keys()), params.fatalErrors);
396
- }
397
- this._massSkipTestsFromRemaining(new Set(params.skipTestsDueToSetupFailure), []);
398
- if (params.unexpectedExitError) {
399
- if (this._currentlyRunning)
400
- this._massSkipTestsFromRemaining(/* @__PURE__ */ new Set([this._currentlyRunning.test.id]), [params.unexpectedExitError]);
401
- else
402
- this._massSkipTestsFromRemaining(new Set(this._remainingByTestId.keys()), [params.unexpectedExitError]);
403
- }
404
- const retryCandidates = /* @__PURE__ */ new Set();
405
- const serialSuitesWithFailures = /* @__PURE__ */ new Set();
406
- for (const failedTest of this._failedTests) {
407
- if (this._failedWithNonRetriableError.has(failedTest))
408
- continue;
409
- retryCandidates.add(failedTest);
410
- let outermostSerialSuite;
411
- for (let parent = failedTest.parent; parent; parent = parent.parent) {
412
- if (parent._parallelMode === "serial")
413
- outermostSerialSuite = parent;
414
- }
415
- if (outermostSerialSuite && !this._failedWithNonRetriableError.has(outermostSerialSuite))
416
- serialSuitesWithFailures.add(outermostSerialSuite);
417
- }
418
- const testsBelongingToSomeSerialSuiteWithFailures = [...this._remainingByTestId.values()].filter((test) => {
419
- let parent = test.parent;
420
- while (parent && !serialSuitesWithFailures.has(parent))
421
- parent = parent.parent;
422
- return !!parent;
423
- });
424
- this._massSkipTestsFromRemaining(new Set(testsBelongingToSomeSerialSuiteWithFailures.map((test) => test.id)), []);
425
- for (const serialSuite of serialSuitesWithFailures) {
426
- serialSuite.allTests().forEach((test) => retryCandidates.add(test));
427
- }
428
- const remaining = [...this._remainingByTestId.values()];
429
- for (const test of retryCandidates) {
430
- if (test.results.length < test.retries + 1)
431
- remaining.push(test);
432
- }
433
- const newJob = remaining.length ? { ...this.job, tests: remaining } : void 0;
434
- this._finished({ didFail: true, newJob });
435
- }
436
- onExit(data) {
437
- const unexpectedExitError = data.unexpectedly ? {
438
- message: `Error: worker process exited unexpectedly (code=${data.code}, signal=${data.signal})`
439
- } : void 0;
440
- this._onDone({ skipTestsDueToSetupFailure: [], fatalErrors: [], unexpectedExitError });
441
- }
442
- _finished(result) {
443
- import_utils.eventsHelper.removeEventListeners(this._listeners);
444
- this.jobResult.resolve(result);
445
- }
446
- runInWorker(worker) {
447
- this._parallelIndex = worker.parallelIndex;
448
- this._workerIndex = worker.workerIndex;
449
- const runPayload = {
450
- file: this.job.requireFile,
451
- entries: this.job.tests.map((test) => {
452
- return { testId: test.id, retry: test.results.length };
453
- })
454
- };
455
- worker.runTestGroup(runPayload);
456
- this._listeners = [
457
- import_utils.eventsHelper.addEventListener(worker, "testBegin", this._onTestBegin.bind(this)),
458
- import_utils.eventsHelper.addEventListener(worker, "testEnd", this._onTestEnd.bind(this)),
459
- import_utils.eventsHelper.addEventListener(worker, "stepBegin", this._onStepBegin.bind(this)),
460
- import_utils.eventsHelper.addEventListener(worker, "stepEnd", this._onStepEnd.bind(this)),
461
- import_utils.eventsHelper.addEventListener(worker, "attach", this._onAttach.bind(this)),
462
- import_utils.eventsHelper.addEventListener(worker, "testPaused", this._onTestPaused.bind(this, worker)),
463
- import_utils.eventsHelper.addEventListener(worker, "done", this._onDone.bind(this)),
464
- import_utils.eventsHelper.addEventListener(worker, "exit", this.onExit.bind(this))
465
- ];
466
- }
467
- _onTestPaused(worker, params) {
468
- const data = this._dataByTestId.get(params.testId);
469
- if (!data)
470
- return;
471
- const { result, test } = data;
472
- const sendMessage = async (message) => {
473
- try {
474
- if (this.jobResult.isDone())
475
- throw new Error("Test has already stopped");
476
- const response = await worker.sendCustomMessage({ testId: test.id, request: message.request });
477
- if (response.error)
478
- (0, import_internalReporter.addLocationAndSnippetToError)(this._config.config, response.error);
479
- return response;
480
- } catch (e) {
481
- const error = (0, import_util.serializeError)(e);
482
- (0, import_internalReporter.addLocationAndSnippetToError)(this._config.config, error);
483
- return { response: void 0, error };
484
- }
485
- };
486
- result.status = params.status;
487
- result.errors = params.errors;
488
- result.error = result.errors[0];
489
- void this._reporter.onTestPaused?.(test, result).then(() => {
490
- worker.sendResume({});
491
- });
492
- this._failureTracker.onTestPaused?.({ ...params, sendMessage });
493
- }
494
- skipWholeJob() {
495
- const allTestsSkipped = this.job.tests.every((test) => test.expectedStatus === "skipped");
496
- if (allTestsSkipped && !this._failureTracker.hasReachedMaxFailures()) {
497
- for (const test of this.job.tests) {
498
- const result = test._appendTestResult();
499
- this._reporter.onTestBegin?.(test, result);
500
- result.status = "skipped";
501
- result.annotations = [...test.annotations];
502
- this._reportTestEnd(test, result);
503
- }
504
- return true;
505
- }
506
- return false;
507
- }
508
- currentlyRunning() {
509
- return this._currentlyRunning;
510
- }
511
- _reportTestEnd(test, result) {
512
- this._reporter.onTestEnd?.(test, result);
513
- const hadMaxFailures = this._failureTracker.hasReachedMaxFailures();
514
- this._failureTracker.onTestEnd(test, result);
515
- if (this._failureTracker.hasReachedMaxFailures()) {
516
- this._stopCallback();
517
- if (!hadMaxFailures)
518
- this._reporter.onError?.({ message: import_utils2.colors.red(`Testing stopped early after ${this._failureTracker.maxFailures()} maximum allowed failures.`) });
519
- }
520
- }
521
- }
522
- function chunkFromParams(params) {
523
- if (typeof params.text === "string")
524
- return params.text;
525
- return Buffer.from(params.buffer, "base64");
526
- }
527
- // Annotate the CommonJS export names for ESM import in node:
528
- 0 && (module.exports = {
529
- Dispatcher
530
- });
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var dispatcher_exports = {};
20
+ __export(dispatcher_exports, {
21
+ Dispatcher: () => Dispatcher
22
+ });
23
+ module.exports = __toCommonJS(dispatcher_exports);
24
+ var import_utils = require("patchright-bun-core/lib/utils");
25
+ var import_utils2 = require("patchright-bun-core/lib/utils");
26
+ var import_rebase = require("./rebase");
27
+ var import_workerHost = require("./workerHost");
28
+ var import_ipc = require("../common/ipc");
29
+ var import_internalReporter = require("../reporters/internalReporter");
30
+ var import_util = require("../util");
31
+ class Dispatcher {
32
+ constructor(config, reporter, failureTracker) {
33
+ // Worker slot is claimed when it has jobDispatcher assigned.
34
+ this._workerSlots = [];
35
+ this._queue = [];
36
+ this._workerLimitPerProjectId = /* @__PURE__ */ new Map();
37
+ this._queuedOrRunningHashCount = /* @__PURE__ */ new Map();
38
+ this._finished = new import_utils.ManualPromise();
39
+ this._isStopped = true;
40
+ this._extraEnvByProjectId = /* @__PURE__ */ new Map();
41
+ this._producedEnvByProjectId = /* @__PURE__ */ new Map();
42
+ this._config = config;
43
+ this._reporter = reporter;
44
+ this._failureTracker = failureTracker;
45
+ for (const project of config.projects) {
46
+ if (project.workers)
47
+ this._workerLimitPerProjectId.set(project.id, project.workers);
48
+ }
49
+ }
50
+ _findFirstJobToRun() {
51
+ for (let index = 0; index < this._queue.length; index++) {
52
+ const job = this._queue[index];
53
+ const projectIdWorkerLimit = this._workerLimitPerProjectId.get(job.projectId);
54
+ if (!projectIdWorkerLimit)
55
+ return index;
56
+ const runningWorkersWithSameProjectId = this._workerSlots.filter((w) => w.jobDispatcher?.job.projectId === job.projectId).length;
57
+ if (runningWorkersWithSameProjectId < projectIdWorkerLimit)
58
+ return index;
59
+ }
60
+ return -1;
61
+ }
62
+ _scheduleJob() {
63
+ if (this._isStopped)
64
+ return;
65
+ const jobIndex = this._findFirstJobToRun();
66
+ if (jobIndex === -1)
67
+ return;
68
+ const job = this._queue[jobIndex];
69
+ let workerIndex = this._workerSlots.findIndex((w) => !w.jobDispatcher && w.worker && w.worker.hash() === job.workerHash && !w.worker.didSendStop());
70
+ if (workerIndex === -1)
71
+ workerIndex = this._workerSlots.findIndex((w) => !w.jobDispatcher);
72
+ if (workerIndex === -1) {
73
+ return;
74
+ }
75
+ this._queue.splice(jobIndex, 1);
76
+ const jobDispatcher = new JobDispatcher(job, this._config, this._reporter, this._failureTracker, () => this.stop().catch(() => {
77
+ }));
78
+ this._workerSlots[workerIndex].jobDispatcher = jobDispatcher;
79
+ void this._runJobInWorker(workerIndex, jobDispatcher).then(() => {
80
+ this._workerSlots[workerIndex].jobDispatcher = void 0;
81
+ this._checkFinished();
82
+ this._scheduleJob();
83
+ });
84
+ }
85
+ async _runJobInWorker(index, jobDispatcher) {
86
+ const job = jobDispatcher.job;
87
+ if (jobDispatcher.skipWholeJob())
88
+ return;
89
+ let worker = this._workerSlots[index].worker;
90
+ if (worker && (worker.hash() !== job.workerHash || worker.didSendStop())) {
91
+ await worker.stop();
92
+ worker = void 0;
93
+ if (this._isStopped)
94
+ return;
95
+ }
96
+ let startError;
97
+ if (!worker) {
98
+ worker = this._createWorker(job, index, (0, import_ipc.serializeConfig)(this._config, true));
99
+ this._workerSlots[index].worker = worker;
100
+ worker.on("exit", () => this._workerSlots[index].worker = void 0);
101
+ startError = await worker.start();
102
+ if (this._isStopped)
103
+ return;
104
+ }
105
+ if (startError)
106
+ jobDispatcher.onExit(startError);
107
+ else
108
+ jobDispatcher.runInWorker(worker);
109
+ const result = await jobDispatcher.jobResult;
110
+ this._updateCounterForWorkerHash(job.workerHash, -1);
111
+ if (result.didFail)
112
+ void worker.stop(
113
+ true
114
+ /* didFail */
115
+ );
116
+ else if (this._isWorkerRedundant(worker))
117
+ void worker.stop();
118
+ if (!this._isStopped && result.newJob) {
119
+ this._queue.unshift(result.newJob);
120
+ this._updateCounterForWorkerHash(result.newJob.workerHash, 1);
121
+ }
122
+ }
123
+ _checkFinished() {
124
+ if (this._finished.isDone())
125
+ return;
126
+ if (this._queue.length && !this._isStopped)
127
+ return;
128
+ if (this._workerSlots.some((w) => !!w.jobDispatcher))
129
+ return;
130
+ this._finished.resolve();
131
+ }
132
+ _isWorkerRedundant(worker) {
133
+ let workersWithSameHash = 0;
134
+ for (const slot of this._workerSlots) {
135
+ if (slot.worker && !slot.worker.didSendStop() && slot.worker.hash() === worker.hash())
136
+ workersWithSameHash++;
137
+ }
138
+ return workersWithSameHash > this._queuedOrRunningHashCount.get(worker.hash());
139
+ }
140
+ _updateCounterForWorkerHash(hash, delta) {
141
+ this._queuedOrRunningHashCount.set(hash, delta + (this._queuedOrRunningHashCount.get(hash) || 0));
142
+ }
143
+ async run(testGroups, extraEnvByProjectId) {
144
+ this._extraEnvByProjectId = extraEnvByProjectId;
145
+ this._queue = testGroups;
146
+ for (const group of testGroups)
147
+ this._updateCounterForWorkerHash(group.workerHash, 1);
148
+ this._isStopped = false;
149
+ this._workerSlots = [];
150
+ if (this._failureTracker.hasReachedMaxFailures())
151
+ void this.stop();
152
+ for (let i = 0; i < this._config.config.workers; i++)
153
+ this._workerSlots.push({});
154
+ for (let i = 0; i < this._workerSlots.length; i++)
155
+ this._scheduleJob();
156
+ this._checkFinished();
157
+ await this._finished;
158
+ }
159
+ _createWorker(testGroup, parallelIndex, loaderData) {
160
+ const projectConfig = this._config.projects.find((p) => p.id === testGroup.projectId);
161
+ const outputDir = projectConfig.project.outputDir;
162
+ const worker = new import_workerHost.WorkerHost(testGroup, {
163
+ parallelIndex,
164
+ config: loaderData,
165
+ extraEnv: this._extraEnvByProjectId.get(testGroup.projectId) || {},
166
+ outputDir,
167
+ pauseOnError: this._failureTracker.pauseOnError(),
168
+ pauseAtEnd: this._failureTracker.pauseAtEnd(projectConfig)
169
+ });
170
+ const handleOutput = (params) => {
171
+ const chunk = chunkFromParams(params);
172
+ if (worker.didFail()) {
173
+ return { chunk };
174
+ }
175
+ const currentlyRunning = this._workerSlots[parallelIndex].jobDispatcher?.currentlyRunning();
176
+ if (!currentlyRunning)
177
+ return { chunk };
178
+ return { chunk, test: currentlyRunning.test, result: currentlyRunning.result };
179
+ };
180
+ worker.on("stdOut", (params) => {
181
+ const { chunk, test, result } = handleOutput(params);
182
+ result?.stdout.push(chunk);
183
+ this._reporter.onStdOut?.(chunk, test, result);
184
+ });
185
+ worker.on("stdErr", (params) => {
186
+ const { chunk, test, result } = handleOutput(params);
187
+ result?.stderr.push(chunk);
188
+ this._reporter.onStdErr?.(chunk, test, result);
189
+ });
190
+ worker.on("teardownErrors", (params) => {
191
+ this._failureTracker.onWorkerError();
192
+ for (const error of params.fatalErrors)
193
+ this._reporter.onError?.(error);
194
+ });
195
+ worker.on("exit", () => {
196
+ const producedEnv = this._producedEnvByProjectId.get(testGroup.projectId) || {};
197
+ this._producedEnvByProjectId.set(testGroup.projectId, { ...producedEnv, ...worker.producedEnv() });
198
+ });
199
+ return worker;
200
+ }
201
+ producedEnvByProjectId() {
202
+ return this._producedEnvByProjectId;
203
+ }
204
+ async stop() {
205
+ if (this._isStopped)
206
+ return;
207
+ this._isStopped = true;
208
+ await Promise.all(this._workerSlots.map(({ worker }) => worker?.stop()));
209
+ this._checkFinished();
210
+ }
211
+ }
212
+ class JobDispatcher {
213
+ constructor(job, config, reporter, failureTracker, stopCallback) {
214
+ this.jobResult = new import_utils.ManualPromise();
215
+ this._listeners = [];
216
+ this._failedTests = /* @__PURE__ */ new Set();
217
+ this._failedWithNonRetriableError = /* @__PURE__ */ new Set();
218
+ this._remainingByTestId = /* @__PURE__ */ new Map();
219
+ this._dataByTestId = /* @__PURE__ */ new Map();
220
+ this._parallelIndex = 0;
221
+ this._workerIndex = 0;
222
+ this.job = job;
223
+ this._config = config;
224
+ this._reporter = reporter;
225
+ this._failureTracker = failureTracker;
226
+ this._stopCallback = stopCallback;
227
+ this._remainingByTestId = new Map(this.job.tests.map((e) => [e.id, e]));
228
+ }
229
+ _onTestBegin(params) {
230
+ const test = this._remainingByTestId.get(params.testId);
231
+ if (!test) {
232
+ return;
233
+ }
234
+ const result = test._appendTestResult();
235
+ this._dataByTestId.set(test.id, { test, result, steps: /* @__PURE__ */ new Map() });
236
+ result.parallelIndex = this._parallelIndex;
237
+ result.workerIndex = this._workerIndex;
238
+ result.startTime = new Date(params.startWallTime);
239
+ this._reporter.onTestBegin?.(test, result);
240
+ this._currentlyRunning = { test, result };
241
+ }
242
+ _onTestEnd(params) {
243
+ if (this._failureTracker.hasReachedMaxFailures()) {
244
+ params.status = "interrupted";
245
+ params.errors = [];
246
+ }
247
+ const data = this._dataByTestId.get(params.testId);
248
+ if (!data) {
249
+ return;
250
+ }
251
+ this._dataByTestId.delete(params.testId);
252
+ this._remainingByTestId.delete(params.testId);
253
+ const { result, test } = data;
254
+ result.duration = params.duration;
255
+ result.errors = params.errors;
256
+ result.error = result.errors[0];
257
+ result.status = params.status;
258
+ result.annotations = params.annotations;
259
+ test.annotations = [...params.annotations];
260
+ test.expectedStatus = params.expectedStatus;
261
+ test.timeout = params.timeout;
262
+ const isFailure = result.status !== "skipped" && result.status !== test.expectedStatus;
263
+ if (isFailure)
264
+ this._failedTests.add(test);
265
+ if (params.hasNonRetriableError)
266
+ this._addNonretriableTestAndSerialModeParents(test);
267
+ this._reportTestEnd(test, result);
268
+ this._currentlyRunning = void 0;
269
+ }
270
+ _addNonretriableTestAndSerialModeParents(test) {
271
+ this._failedWithNonRetriableError.add(test);
272
+ for (let parent = test.parent; parent; parent = parent.parent) {
273
+ if (parent._parallelMode === "serial")
274
+ this._failedWithNonRetriableError.add(parent);
275
+ }
276
+ }
277
+ _onStepBegin(params) {
278
+ const data = this._dataByTestId.get(params.testId);
279
+ if (!data) {
280
+ return;
281
+ }
282
+ const { result, steps, test } = data;
283
+ const parentStep = params.parentStepId ? steps.get(params.parentStepId) : void 0;
284
+ const step = {
285
+ title: params.title,
286
+ titlePath: () => {
287
+ const parentPath = parentStep?.titlePath() || [];
288
+ return [...parentPath, params.title];
289
+ },
290
+ parent: parentStep,
291
+ category: params.category,
292
+ startTime: new Date(params.wallTime),
293
+ duration: -1,
294
+ steps: [],
295
+ attachments: [],
296
+ annotations: [],
297
+ location: params.location
298
+ };
299
+ steps.set(params.stepId, step);
300
+ (parentStep || result).steps.push(step);
301
+ this._reporter.onStepBegin?.(test, result, step);
302
+ }
303
+ _onStepEnd(params) {
304
+ const data = this._dataByTestId.get(params.testId);
305
+ if (!data) {
306
+ return;
307
+ }
308
+ const { result, steps, test } = data;
309
+ const step = steps.get(params.stepId);
310
+ if (!step) {
311
+ this._reporter.onStdErr?.("Internal error: step end without step begin: " + params.stepId, test, result);
312
+ return;
313
+ }
314
+ step.duration = params.wallTime - step.startTime.getTime();
315
+ if (params.error)
316
+ step.error = params.error;
317
+ if (params.suggestedRebaseline)
318
+ (0, import_rebase.addSuggestedRebaseline)(step.location, params.suggestedRebaseline);
319
+ step.annotations = params.annotations;
320
+ steps.delete(params.stepId);
321
+ this._reporter.onStepEnd?.(test, result, step);
322
+ }
323
+ _onAttach(params) {
324
+ const data = this._dataByTestId.get(params.testId);
325
+ if (!data) {
326
+ return;
327
+ }
328
+ const attachment = {
329
+ name: params.name,
330
+ path: params.path,
331
+ contentType: params.contentType,
332
+ body: params.body !== void 0 ? Buffer.from(params.body, "base64") : void 0
333
+ };
334
+ data.result.attachments.push(attachment);
335
+ if (params.stepId) {
336
+ const step = data.steps.get(params.stepId);
337
+ if (step)
338
+ step.attachments.push(attachment);
339
+ else
340
+ this._reporter.onStdErr?.("Internal error: step id not found: " + params.stepId);
341
+ }
342
+ }
343
+ _failTestWithErrors(test, errors) {
344
+ const runData = this._dataByTestId.get(test.id);
345
+ let result;
346
+ if (runData) {
347
+ result = runData.result;
348
+ } else {
349
+ result = test._appendTestResult();
350
+ this._reporter.onTestBegin?.(test, result);
351
+ }
352
+ result.errors = [...errors];
353
+ result.error = result.errors[0];
354
+ result.status = errors.length ? "failed" : "skipped";
355
+ this._reportTestEnd(test, result);
356
+ this._failedTests.add(test);
357
+ }
358
+ _massSkipTestsFromRemaining(testIds, errors) {
359
+ for (const test of this._remainingByTestId.values()) {
360
+ if (!testIds.has(test.id))
361
+ continue;
362
+ if (!this._failureTracker.hasReachedMaxFailures()) {
363
+ this._failTestWithErrors(test, errors);
364
+ errors = [];
365
+ }
366
+ this._remainingByTestId.delete(test.id);
367
+ }
368
+ if (errors.length) {
369
+ this._failureTracker.onWorkerError();
370
+ for (const error of errors)
371
+ this._reporter.onError?.(error);
372
+ }
373
+ }
374
+ _onDone(params) {
375
+ if (!this._remainingByTestId.size && !this._failedTests.size && !params.fatalErrors.length && !params.skipTestsDueToSetupFailure.length && !params.fatalUnknownTestIds && !params.unexpectedExitError && !params.stoppedDueToUnhandledErrorInTestFail) {
376
+ this._finished({ didFail: false });
377
+ return;
378
+ }
379
+ for (const testId of params.fatalUnknownTestIds || []) {
380
+ const test = this._remainingByTestId.get(testId);
381
+ if (test) {
382
+ this._remainingByTestId.delete(testId);
383
+ this._failTestWithErrors(test, [{ message: `Test not found in the worker process. Make sure test title does not change.` }]);
384
+ }
385
+ }
386
+ if (params.fatalErrors.length) {
387
+ this._massSkipTestsFromRemaining(new Set(this._remainingByTestId.keys()), params.fatalErrors);
388
+ }
389
+ this._massSkipTestsFromRemaining(new Set(params.skipTestsDueToSetupFailure), []);
390
+ if (params.unexpectedExitError) {
391
+ if (this._currentlyRunning)
392
+ this._massSkipTestsFromRemaining(/* @__PURE__ */ new Set([this._currentlyRunning.test.id]), [params.unexpectedExitError]);
393
+ else
394
+ this._massSkipTestsFromRemaining(new Set(this._remainingByTestId.keys()), [params.unexpectedExitError]);
395
+ }
396
+ const retryCandidates = /* @__PURE__ */ new Set();
397
+ const serialSuitesWithFailures = /* @__PURE__ */ new Set();
398
+ for (const failedTest of this._failedTests) {
399
+ if (this._failedWithNonRetriableError.has(failedTest))
400
+ continue;
401
+ retryCandidates.add(failedTest);
402
+ let outermostSerialSuite;
403
+ for (let parent = failedTest.parent; parent; parent = parent.parent) {
404
+ if (parent._parallelMode === "serial")
405
+ outermostSerialSuite = parent;
406
+ }
407
+ if (outermostSerialSuite && !this._failedWithNonRetriableError.has(outermostSerialSuite))
408
+ serialSuitesWithFailures.add(outermostSerialSuite);
409
+ }
410
+ const testsBelongingToSomeSerialSuiteWithFailures = [...this._remainingByTestId.values()].filter((test) => {
411
+ let parent = test.parent;
412
+ while (parent && !serialSuitesWithFailures.has(parent))
413
+ parent = parent.parent;
414
+ return !!parent;
415
+ });
416
+ this._massSkipTestsFromRemaining(new Set(testsBelongingToSomeSerialSuiteWithFailures.map((test) => test.id)), []);
417
+ for (const serialSuite of serialSuitesWithFailures) {
418
+ serialSuite.allTests().forEach((test) => retryCandidates.add(test));
419
+ }
420
+ const remaining = [...this._remainingByTestId.values()];
421
+ for (const test of retryCandidates) {
422
+ if (test.results.length < test.retries + 1)
423
+ remaining.push(test);
424
+ }
425
+ const newJob = remaining.length ? { ...this.job, tests: remaining } : void 0;
426
+ this._finished({ didFail: true, newJob });
427
+ }
428
+ onExit(data) {
429
+ const unexpectedExitError = data.unexpectedly ? {
430
+ message: `Error: worker process exited unexpectedly (code=${data.code}, signal=${data.signal})`
431
+ } : void 0;
432
+ this._onDone({ skipTestsDueToSetupFailure: [], fatalErrors: [], unexpectedExitError });
433
+ }
434
+ _finished(result) {
435
+ import_utils.eventsHelper.removeEventListeners(this._listeners);
436
+ this.jobResult.resolve(result);
437
+ }
438
+ runInWorker(worker) {
439
+ this._parallelIndex = worker.parallelIndex;
440
+ this._workerIndex = worker.workerIndex;
441
+ const runPayload = {
442
+ file: this.job.requireFile,
443
+ entries: this.job.tests.map((test) => {
444
+ return { testId: test.id, retry: test.results.length };
445
+ })
446
+ };
447
+ worker.runTestGroup(runPayload);
448
+ this._listeners = [
449
+ import_utils.eventsHelper.addEventListener(worker, "testBegin", this._onTestBegin.bind(this)),
450
+ import_utils.eventsHelper.addEventListener(worker, "testEnd", this._onTestEnd.bind(this)),
451
+ import_utils.eventsHelper.addEventListener(worker, "stepBegin", this._onStepBegin.bind(this)),
452
+ import_utils.eventsHelper.addEventListener(worker, "stepEnd", this._onStepEnd.bind(this)),
453
+ import_utils.eventsHelper.addEventListener(worker, "attach", this._onAttach.bind(this)),
454
+ import_utils.eventsHelper.addEventListener(worker, "testPaused", this._onTestPaused.bind(this, worker)),
455
+ import_utils.eventsHelper.addEventListener(worker, "done", this._onDone.bind(this)),
456
+ import_utils.eventsHelper.addEventListener(worker, "exit", this.onExit.bind(this))
457
+ ];
458
+ }
459
+ _onTestPaused(worker, params) {
460
+ const data = this._dataByTestId.get(params.testId);
461
+ if (!data)
462
+ return;
463
+ const { result, test } = data;
464
+ const sendMessage = async (message) => {
465
+ try {
466
+ if (this.jobResult.isDone())
467
+ throw new Error("Test has already stopped");
468
+ const response = await worker.sendCustomMessage({ testId: test.id, request: message.request });
469
+ if (response.error)
470
+ (0, import_internalReporter.addLocationAndSnippetToError)(this._config.config, response.error);
471
+ return response;
472
+ } catch (e) {
473
+ const error = (0, import_util.serializeError)(e);
474
+ (0, import_internalReporter.addLocationAndSnippetToError)(this._config.config, error);
475
+ return { response: void 0, error };
476
+ }
477
+ };
478
+ result.status = params.status;
479
+ result.errors = params.errors;
480
+ result.error = result.errors[0];
481
+ void this._reporter.onTestPaused?.(test, result).then(() => {
482
+ worker.sendResume({});
483
+ });
484
+ this._failureTracker.onTestPaused?.({ ...params, sendMessage });
485
+ }
486
+ skipWholeJob() {
487
+ const allTestsSkipped = this.job.tests.every((test) => test.expectedStatus === "skipped");
488
+ if (allTestsSkipped && !this._failureTracker.hasReachedMaxFailures()) {
489
+ for (const test of this.job.tests) {
490
+ const result = test._appendTestResult();
491
+ this._reporter.onTestBegin?.(test, result);
492
+ result.status = "skipped";
493
+ result.annotations = [...test.annotations];
494
+ this._reportTestEnd(test, result);
495
+ }
496
+ return true;
497
+ }
498
+ return false;
499
+ }
500
+ currentlyRunning() {
501
+ return this._currentlyRunning;
502
+ }
503
+ _reportTestEnd(test, result) {
504
+ this._reporter.onTestEnd?.(test, result);
505
+ const hadMaxFailures = this._failureTracker.hasReachedMaxFailures();
506
+ this._failureTracker.onTestEnd(test, result);
507
+ if (this._failureTracker.hasReachedMaxFailures()) {
508
+ this._stopCallback();
509
+ if (!hadMaxFailures)
510
+ this._reporter.onError?.({ message: import_utils2.colors.red(`Testing stopped early after ${this._failureTracker.maxFailures()} maximum allowed failures.`) });
511
+ }
512
+ }
513
+ }
514
+ function chunkFromParams(params) {
515
+ if (typeof params.text === "string")
516
+ return params.text;
517
+ return Buffer.from(params.buffer, "base64");
518
+ }
519
+ // Annotate the CommonJS export names for ESM import in node:
520
+ 0 && (module.exports = {
521
+ Dispatcher
522
+ });