diff --git a/apps/server/src/preview/ServerBrowser.test.ts b/apps/server/src/preview/ServerBrowser.test.ts index 3bc4eba2f240c..9d1460c9befd5 100644 --- a/apps/server/src/preview/ServerBrowser.test.ts +++ b/apps/server/src/preview/ServerBrowser.test.ts @@ -3069,6 +3069,55 @@ it.effect("a desktop tab reports the size the desktop draws after a resize", () height: 760, }); expect(yield* status).toEqual({ width: 560, height: 760 }); + const unpainted = Promise.withResolvers(); + heldStill = () => unpainted.promise; + // Fill asks for no size, so a page that keeps its size without a resize is not undrawn. + const kept = yield* broker + .invoke({ + scope, + tabId, + operation: "resize", + input: { mode: "fill" }, + }) + .pipe(Effect.forkScoped); + let keptEvent = yield* PubSub.take(events); + while (keptEvent.type !== "resized" || keptEvent.tabId !== tabId) { + keptEvent = yield* PubSub.take(events); + } + yield* TestClock.adjust("1 second"); + expect((yield* Fiber.join(kept)).viewport).toEqual({ width: 560, height: 760 }); + // A desktop that never draws the new size, such as a minimized window, fails the resize. + const undrawn = yield* broker + .invoke({ + scope, + tabId, + operation: "resize", + input: { mode: "freeform", width: 1440, height: 900 }, + }) + .pipe(Effect.exit, Effect.forkScoped); + let undrawnEvent = yield* PubSub.take(events); + while (undrawnEvent.type !== "resized" || undrawnEvent.tabId !== tabId) { + undrawnEvent = yield* PubSub.take(events); + } + yield* TestClock.adjust("1 second"); + expect(Exit.isFailure(yield* Fiber.join(undrawn))).toBe(true); + // The same size again still waits for a draw rather than reporting the old size. + const retried = yield* broker + .invoke({ + scope, + tabId, + operation: "resize", + input: { mode: "freeform", width: 1440, height: 900 }, + }) + .pipe(Effect.exit, Effect.forkScoped); + let retriedEvent = yield* PubSub.take(events); + while (retriedEvent.type !== "resized" || retriedEvent.tabId !== tabId) { + retriedEvent = yield* PubSub.take(events); + } + yield* TestClock.adjust("1 second"); + expect(Exit.isFailure(yield* Fiber.join(retried))).toBe(true); + heldStill = null; + unpainted.resolve(); expect(yield* resize({ mode: "fill" }, 1234, 700)).toEqual({ width: 1234, height: 700 }); // No page resize or load since that read, so status answers without reading again. let reads = 0; diff --git a/apps/server/src/preview/ServerBrowser.ts b/apps/server/src/preview/ServerBrowser.ts index 0a0dad06fb50d..26203fc1a63c8 100644 --- a/apps/server/src/preview/ServerBrowser.ts +++ b/apps/server/src/preview/ServerBrowser.ts @@ -385,6 +385,8 @@ interface ServerTab { desktopViewportChanges: number; /** Settles at the desktop page's next main-frame resize. */ desktopResize: PromiseWithResolvers; + /** The last resize failed undrawn, so the tab's setting is a size the page never took. */ + desktopResizeUndrawn: boolean; loading: boolean; loadError: PreviewAutomationLoadError | undefined; /** The renderer crash this page died of, or the one the page it replaced died of. */ @@ -1189,6 +1191,7 @@ const make = Effect.gen(function* () { desktopViewportStale: true, desktopViewportChanges: 0, desktopResize: Promise.withResolvers(), + desktopResizeUndrawn: false, loading: false, loadError: undefined, // The page a crash left behind is blank; the session still names what crashed. @@ -2821,7 +2824,8 @@ const make = Effect.gen(function* () { viewport: tab.page.viewportSize() ?? UNATTACHED_FILL_VIEWPORT, }; } - const changed = from?.width !== to?.width || from?.height !== to?.height; + const changed = + tab.desktopResizeUndrawn || from?.width !== to?.width || from?.height !== to?.height; const remainingMs = () => Math.max(0, deadlineMs - performance.now()); const within = (promise: Promise, ms: number) => runPromise( @@ -2833,6 +2837,7 @@ const make = Effect.gen(function* () { // The desktop draws the new size after the session update; its page resize says it has. // Half the time left keeps room for the read, which runs even if nothing was drawn, // because a page can keep another size than the one asked for. + let undrawn = false; if (changed) { // Electron resizes no hidden guest it does not paint; a pending capture makes the desktop paint it. let waiting = true; @@ -2842,13 +2847,30 @@ const make = Effect.gen(function* () { } })().catch(constVoid); try { - await within(drawn, Math.min(DESKTOP_RESIZE_WAIT_MS, remainingMs() / 2)); + undrawn = + (await within( + drawn.then(() => true), + Math.min(DESKTOP_RESIZE_WAIT_MS, remainingMs() / 2), + )) === null; } finally { waiting = false; } } const changes = tab.desktopViewportChanges; const measured = await within(measureDesktopViewport(tab), remainingMs()); + // A window that does not paint, such as a minimized one, never draws the new size. + // Fill asks for no size, so only a fixed one can be missed. + tab.desktopResizeUndrawn = + undrawn && + to !== null && + (measured?.width !== to.width || measured?.height !== to.height); + if (tab.desktopResizeUndrawn) { + throw new ServerBrowserPage.ServerBrowserOperationError({ + tag: "PreviewAutomationExecutionError", + message: + "The desktop did not draw the page at the new size. Show or restore the T3 Code window and try again.", + }); + } // Only a read after the draw, with no page resize or load during it, measures this resize. return { tabId: tab.tabId,