TypeScript
fix(server): a timed-out browser drag no longer exits the server (#17360)
Scott Norteman8f760371978f
diff --git a/apps/server/src/preview/ServerBrowserPage.test.ts b/apps/server/src/preview/ServerBrowserPage.test.tsindex 291f89e125e..9a4775bb471 100644--- a/apps/server/src/preview/ServerBrowserPage.test.ts+++ b/apps/server/src/preview/ServerBrowserPage.test.ts@@ -277,3 +277,45 @@ describe("server browser element refs", () => {expect(await ServerBrowserPage.evaluate(cdp, { expression: "1 + 1" }, 2_000)).toBe(2);});});++describe("server browser drag", () => {+ it("fails the drag, not the server, when it times out while the cursor is still moving", async () => {+ const timedOut = new Error("locator.dragTo: Timeout 30000ms exceeded.");+ const box = { x: 0, y: 0, width: 20, height: 20 };+ const targetBox = Promise.withResolvers<typeof box>();+ // Only the calls `drag` makes. The drag fails at once; the target's position arrives only+ // when the test hands it over, after the rejection has had a turn to go unobserved.+ const page = {+ locator: (selector: string) =>+ selector === "#card"+ ? {+ scrollIntoViewIfNeeded: async () => {},+ boundingBox: async () => box,+ dragTo: () => Promise.reject(timedOut),+ }+ : { boundingBox: () => targetBox.promise },+ } as unknown as Page;+ const unobserved: Array<unknown> = [];+ const onUnhandled = (reason: unknown) => unobserved.push(reason);+ process.on("unhandledRejection", onUnhandled);+ try {+ let finished = false;+ const settled = ServerBrowserPage.drag(page, { source: "#card", target: "#lane" }).then(+ () => null,+ (error: unknown) => error,+ );+ void settled.then(() => (finished = true));+ // Two macrotask turns: Node reports a rejection nobody observed after the first.+ await new Promise((resolve) => setImmediate(resolve));+ await new Promise((resolve) => setImmediate(resolve));+ // The failed drag waits for the cursor to reach the target before it reports.+ expect(finished).toBe(false);+ targetBox.resolve(box);++ expect(await settled).toBe(timedOut);+ expect(unobserved).toEqual([]);+ } finally {+ process.off("unhandledRejection", onUnhandled);+ }+ });+});diff --git a/apps/server/src/preview/ServerBrowserPage.ts b/apps/server/src/preview/ServerBrowserPage.tsindex 4c2a36478e0..3df4901f469 100644--- a/apps/server/src/preview/ServerBrowserPage.ts+++ b/apps/server/src/preview/ServerBrowserPage.ts@@ -380,11 +380,16 @@ export const drag = async (const source = targetLocator(page, { locator: input.source })!;const target = targetLocator(page, { locator: input.target })!;await pointer(await targetPoint(page, source, {}, timeout), "move");- // The cursor travels with the drag; the page sees one continuous gesture.- const dropped = source.dragTo(target, { timeout });+ // The cursor travels with the drag; the page sees one continuous gesture. Observe the drag's+ // outcome as it starts: a rejection nobody handles while the cursor moves exits the server.+ const dragFailure = source.dragTo(target, { timeout }).then(+ () => null,+ (error: unknown) => ({ error }),+ );const end = await target.boundingBox({ timeout }).catch(() => null);if (end) await pointer({ x: end.x + end.width / 2, y: end.y + end.height / 2 }, "move");- await dropped;+ const failure = await dragFailure;+ if (failure) throw failure.error;};/** Sets files on one file input; false when no locator or selector names one. */