Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions apps/server/src/preview/ServerBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>();
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<PreviewAutomationResizeResult>({
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<PreviewAutomationResizeResult>({
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<PreviewAutomationResizeResult>({
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;
Expand Down
26 changes: 24 additions & 2 deletions apps/server/src/preview/ServerBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ interface ServerTab {
desktopViewportChanges: number;
/** Settles at the desktop page's next main-frame resize. */
desktopResize: PromiseWithResolvers<void>;
/** 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. */
Expand Down Expand Up @@ -1189,6 +1191,7 @@ const make = Effect.gen(function* () {
desktopViewportStale: true,
desktopViewportChanges: 0,
desktopResize: Promise.withResolvers<void>(),
desktopResizeUndrawn: false,
loading: false,
loadError: undefined,
// The page a crash left behind is blank; the session still names what crashed.
Expand Down Expand Up @@ -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 = <A>(promise: Promise<A>, ms: number) =>
runPromise(
Expand All @@ -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;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (changed) {
// Electron resizes no hidden guest it does not paint; a pending capture makes the desktop paint it.
let waiting = true;
Expand All @@ -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,
Expand Down
Loading