patchright-bun 1.58.0 → 1.58.2

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 (180) hide show
  1. package/README.md +225 -225
  2. package/ThirdPartyNotices.txt +5041 -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 +348 -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 +282 -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 +132 -132
  29. package/lib/common/suiteUtils.js +140 -140
  30. package/lib/common/test.js +321 -321
  31. package/lib/common/testLoader.js +101 -101
  32. package/lib/common/testType.js +298 -298
  33. package/lib/common/validators.js +68 -68
  34. package/lib/fsWatcher.js +67 -67
  35. package/lib/index.js +726 -726
  36. package/lib/internalsForTest.js +42 -42
  37. package/lib/isomorphic/events.js +77 -77
  38. package/lib/isomorphic/folders.js +30 -30
  39. package/lib/isomorphic/stringInternPool.js +69 -69
  40. package/lib/isomorphic/teleReceiver.js +521 -521
  41. package/lib/isomorphic/teleSuiteUpdater.js +157 -157
  42. package/lib/isomorphic/testServerConnection.js +225 -225
  43. package/lib/isomorphic/testServerInterface.js +16 -16
  44. package/lib/isomorphic/testTree.js +329 -329
  45. package/lib/isomorphic/types.d.js +16 -16
  46. package/lib/loader/loaderMain.js +59 -59
  47. package/lib/matchers/expect.js +311 -311
  48. package/lib/matchers/matcherHint.js +44 -44
  49. package/lib/matchers/matchers.js +383 -383
  50. package/lib/matchers/toBeTruthy.js +75 -75
  51. package/lib/matchers/toEqual.js +100 -100
  52. package/lib/matchers/toHaveURL.js +101 -101
  53. package/lib/matchers/toMatchAriaSnapshot.js +159 -159
  54. package/lib/matchers/toMatchSnapshot.js +342 -342
  55. package/lib/matchers/toMatchText.js +99 -99
  56. package/lib/mcp/browser/browserContextFactory.js +329 -329
  57. package/lib/mcp/browser/browserServerBackend.js +84 -84
  58. package/lib/mcp/browser/config.js +421 -421
  59. package/lib/mcp/browser/context.js +244 -244
  60. package/lib/mcp/browser/response.js +278 -278
  61. package/lib/mcp/browser/sessionLog.js +75 -75
  62. package/lib/mcp/browser/tab.js +343 -343
  63. package/lib/mcp/browser/tools/common.js +65 -65
  64. package/lib/mcp/browser/tools/console.js +46 -46
  65. package/lib/mcp/browser/tools/dialogs.js +60 -60
  66. package/lib/mcp/browser/tools/evaluate.js +61 -61
  67. package/lib/mcp/browser/tools/files.js +58 -58
  68. package/lib/mcp/browser/tools/form.js +63 -63
  69. package/lib/mcp/browser/tools/install.js +72 -72
  70. package/lib/mcp/browser/tools/keyboard.js +107 -107
  71. package/lib/mcp/browser/tools/mouse.js +107 -107
  72. package/lib/mcp/browser/tools/navigate.js +71 -71
  73. package/lib/mcp/browser/tools/network.js +63 -63
  74. package/lib/mcp/browser/tools/open.js +57 -57
  75. package/lib/mcp/browser/tools/pdf.js +49 -49
  76. package/lib/mcp/browser/tools/runCode.js +78 -78
  77. package/lib/mcp/browser/tools/screenshot.js +93 -93
  78. package/lib/mcp/browser/tools/snapshot.js +173 -173
  79. package/lib/mcp/browser/tools/tabs.js +67 -67
  80. package/lib/mcp/browser/tools/tool.js +47 -47
  81. package/lib/mcp/browser/tools/tracing.js +74 -74
  82. package/lib/mcp/browser/tools/utils.js +94 -94
  83. package/lib/mcp/browser/tools/verify.js +143 -143
  84. package/lib/mcp/browser/tools/wait.js +63 -63
  85. package/lib/mcp/browser/tools.js +84 -84
  86. package/lib/mcp/browser/watchdog.js +44 -44
  87. package/lib/mcp/config.d.js +16 -16
  88. package/lib/mcp/extension/cdpRelay.js +351 -351
  89. package/lib/mcp/extension/extensionContextFactory.js +76 -76
  90. package/lib/mcp/extension/protocol.js +28 -28
  91. package/lib/mcp/index.js +61 -61
  92. package/lib/mcp/log.js +35 -35
  93. package/lib/mcp/program.js +111 -111
  94. package/lib/mcp/sdk/exports.js +28 -28
  95. package/lib/mcp/sdk/http.js +152 -152
  96. package/lib/mcp/sdk/inProcessTransport.js +71 -71
  97. package/lib/mcp/sdk/server.js +223 -223
  98. package/lib/mcp/sdk/tool.js +47 -47
  99. package/lib/mcp/terminal/cli.js +296 -296
  100. package/lib/mcp/terminal/command.js +56 -56
  101. package/lib/mcp/terminal/commands.js +333 -333
  102. package/lib/mcp/terminal/daemon.js +129 -129
  103. package/lib/mcp/terminal/help.json +31 -31
  104. package/lib/mcp/terminal/helpGenerator.js +88 -88
  105. package/lib/mcp/terminal/socketConnection.js +80 -80
  106. package/lib/mcp/test/browserBackend.js +98 -98
  107. package/lib/mcp/test/generatorTools.js +122 -122
  108. package/lib/mcp/test/plannerTools.js +145 -145
  109. package/lib/mcp/test/seed.js +82 -82
  110. package/lib/mcp/test/streams.js +44 -44
  111. package/lib/mcp/test/testBackend.js +99 -99
  112. package/lib/mcp/test/testContext.js +285 -285
  113. package/lib/mcp/test/testTool.js +30 -30
  114. package/lib/mcp/test/testTools.js +108 -108
  115. package/lib/plugins/gitCommitInfoPlugin.js +198 -198
  116. package/lib/plugins/index.js +28 -28
  117. package/lib/plugins/webServerPlugin.js +237 -237
  118. package/lib/program.js +417 -417
  119. package/lib/reporters/base.js +634 -634
  120. package/lib/reporters/blob.js +138 -138
  121. package/lib/reporters/dot.js +99 -99
  122. package/lib/reporters/empty.js +32 -32
  123. package/lib/reporters/github.js +128 -128
  124. package/lib/reporters/html.js +633 -633
  125. package/lib/reporters/internalReporter.js +138 -138
  126. package/lib/reporters/json.js +254 -254
  127. package/lib/reporters/junit.js +232 -232
  128. package/lib/reporters/line.js +131 -131
  129. package/lib/reporters/list.js +253 -253
  130. package/lib/reporters/listModeReporter.js +69 -69
  131. package/lib/reporters/markdown.js +144 -144
  132. package/lib/reporters/merge.js +558 -558
  133. package/lib/reporters/multiplexer.js +112 -112
  134. package/lib/reporters/reporterV2.js +102 -102
  135. package/lib/reporters/teleEmitter.js +317 -317
  136. package/lib/reporters/versions/blobV1.js +16 -16
  137. package/lib/runner/dispatcher.js +530 -530
  138. package/lib/runner/failureTracker.js +72 -72
  139. package/lib/runner/lastRun.js +77 -77
  140. package/lib/runner/loadUtils.js +334 -334
  141. package/lib/runner/loaderHost.js +89 -89
  142. package/lib/runner/processHost.js +180 -180
  143. package/lib/runner/projectUtils.js +241 -241
  144. package/lib/runner/rebase.js +189 -189
  145. package/lib/runner/reporters.js +138 -138
  146. package/lib/runner/sigIntWatcher.js +96 -96
  147. package/lib/runner/storage.js +91 -91
  148. package/lib/runner/taskRunner.js +127 -127
  149. package/lib/runner/tasks.js +410 -410
  150. package/lib/runner/testGroups.js +125 -125
  151. package/lib/runner/testRunner.js +398 -398
  152. package/lib/runner/testServer.js +269 -269
  153. package/lib/runner/uiModeReporter.js +30 -30
  154. package/lib/runner/vcs.js +72 -72
  155. package/lib/runner/watchMode.js +396 -396
  156. package/lib/runner/workerHost.js +104 -104
  157. package/lib/third_party/pirates.js +62 -62
  158. package/lib/third_party/tsconfig-loader.js +103 -103
  159. package/lib/transform/babelBundle.js +46 -46
  160. package/lib/transform/babelBundleImpl.js +461 -461
  161. package/lib/transform/compilationCache.js +274 -274
  162. package/lib/transform/esmLoader.js +103 -103
  163. package/lib/transform/md.js +221 -221
  164. package/lib/transform/portTransport.js +67 -67
  165. package/lib/transform/transform.js +303 -303
  166. package/lib/util.js +400 -400
  167. package/lib/utilsBundle.js +50 -50
  168. package/lib/utilsBundleImpl.js +103 -103
  169. package/lib/worker/fixtureRunner.js +262 -262
  170. package/lib/worker/testInfo.js +536 -536
  171. package/lib/worker/testTracing.js +345 -345
  172. package/lib/worker/timeoutManager.js +174 -174
  173. package/lib/worker/util.js +31 -31
  174. package/lib/worker/workerMain.js +530 -530
  175. package/package.json +2 -2
  176. package/test.d.ts +18 -18
  177. package/test.js +24 -24
  178. package/test.mjs +34 -34
  179. package/types/test.d.ts +10251 -10251
  180. package/types/testReporter.d.ts +822 -822
@@ -1,530 +1,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-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
+ 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
+ });