Skip to content

Commit 9df34f4

Browse files
PR Batch Testerclaude
andcommitted
fix(server): remeasure after a desktop dialog closes and keep notifications
Closing a JavaScript dialog resizes nothing, so a viewer that attached while an alert blocked the first size read never got a size. The desktop tab now remeasures on Page.javascriptDialogClosed through the same single read. Compacting a stalled viewer's backlog drops frames and superseded control and viewport messages, and keeps pickers and one-time notifications in order. When they no longer fit, the oldest notifications go before the newest control or size. Keeping the picker message makes rebuilding it unnecessary. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 08c7095 commit 9df34f4

2 files changed

Lines changed: 79 additions & 51 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function makeSession(
131131
return { result: { value } };
132132
}
133133
if (method === "Runtime.evaluate" && (input as { contextId?: number }).contextId === 9)
134-
return { result: { value: { ...viewport } } };
134+
return desktopSizeAnswer?.() ?? { result: { value: { ...viewport } } };
135135
return { result: { value: "evaluated" } };
136136
}),
137137
};
@@ -285,6 +285,8 @@ let focusedFrame: ReturnType<typeof makeFrame> | null = null;
285285
let contextFailure: Error | null = null;
286286
let initialNavigation: (() => Promise<void>) | null = null;
287287
let desktopRendersNext = false;
288+
/** Replaces the answer to the desktop page's size reads. */
289+
let desktopSizeAnswer: (() => Promise<Record<string, unknown>>) | null = null;
288290
const desktopDetaches = new NodeEvents.EventEmitter();
289291
let desktopDetachReady = Promise.withResolvers<void>();
290292
let desktopReleased = Promise.withResolvers<void>();
@@ -415,9 +417,6 @@ const openDesktopViewer = Effect.gen(function* () {
415417
Object.assign(context.cssViewport, { width, height });
416418
return Effect.promise(() => cdp.emitAsync("Page.frameResized"));
417419
};
418-
// Ready once the size measured on attach and on a first resize both reached the viewer.
419-
yield* resize(1920, 1080);
420-
expect(viewportSizes(yield* Queue.takeAll(viewer.output))).toEqual(["1920x1080", "1920x1080"]);
421420
return { tabId, viewer, context, cdp, resize };
422421
});
423422

@@ -433,6 +432,7 @@ beforeEach(() => {
433432
focusedFrame = null;
434433
desktopTabs.clear();
435434
desktopRendersNext = false;
435+
desktopSizeAnswer = null;
436436
desktopDetachReady = Promise.withResolvers<void>();
437437
desktopReleased = Promise.withResolvers<void>();
438438
releasedDesktopTabs.length = 0;
@@ -1204,21 +1204,41 @@ it.effect(
12041204
() =>
12051205
Effect.scoped(
12061206
Effect.gen(function* () {
1207-
const { viewer, cdp } = yield* openDesktopViewer;
12081207
// Each page size read waits here for its answer.
12091208
const reads = yield* Queue.unbounded<PromiseWithResolvers<Record<string, unknown>>>();
1210-
const send = cdp.send.getMockImplementation()!;
1211-
cdp.send.mockImplementation((method, input) => {
1212-
if (method !== "Runtime.evaluate" || (input as { contextId?: number }).contextId !== 9)
1213-
return send(method, input);
1214-
const read = Promise.withResolvers<Record<string, unknown>>();
1215-
Queue.offerUnsafe(reads, read);
1216-
return read.promise;
1217-
});
1209+
const deferReads = () => {
1210+
desktopSizeAnswer = () => {
1211+
const read = Promise.withResolvers<Record<string, unknown>>();
1212+
Queue.offerUnsafe(reads, read);
1213+
return read.promise;
1214+
};
1215+
};
1216+
deferReads();
1217+
const { viewer, cdp } = yield* openDesktopViewer;
12181218
const answer = (width: number, height: number) => ({
12191219
result: { value: { width, height } },
12201220
});
12211221
const resized = () => cdp.emitAsync("Page.frameResized");
1222+
// An alert open on attach blocks the first read, which gives up with no size to send.
1223+
const blocked = yield* Queue.take(reads);
1224+
yield* TestClock.adjust("1 second");
1225+
// Closing it resizes nothing, so the close itself remeasures.
1226+
desktopSizeAnswer = null;
1227+
yield* Effect.promise(() => cdp.emitAsync("Page.javascriptDialogClosed"));
1228+
expect(viewportSizes(yield* Queue.takeAll(viewer.output))).toEqual(["1920x1080"]);
1229+
// The late answer, a read a navigation breaks and a hidden webview keep the last size.
1230+
blocked.resolve(answer(100, 100));
1231+
deferReads();
1232+
const navigated = resized();
1233+
(yield* Queue.take(reads)).reject(new Error("Cannot find context with specified id"));
1234+
yield* Effect.promise(() => navigated);
1235+
const hidden = resized();
1236+
(yield* Queue.take(reads)).resolve(answer(0, 0));
1237+
yield* Effect.promise(() => hidden);
1238+
expect(viewportSizes(yield* Queue.takeAll(viewer.output))).toEqual([
1239+
"1920x1080",
1240+
"1920x1080",
1241+
]);
12221242
// Resizes during a read share one more read, so the newest size arrives last.
12231243
const first = resized();
12241244
const firstRead = yield* Queue.take(reads);
@@ -1232,24 +1252,6 @@ it.effect(
12321252
"1000x600",
12331253
"1000x600",
12341254
]);
1235-
// A dialog blocks page script, so a read that never answers gives up.
1236-
const blocked = resized();
1237-
const stuck = yield* Queue.take(reads);
1238-
yield* TestClock.adjust("1 second");
1239-
yield* Effect.promise(() => blocked);
1240-
// Its late answer, a read a navigation breaks and a hidden webview keep the last size.
1241-
stuck.resolve(answer(100, 100));
1242-
const navigated = resized();
1243-
(yield* Queue.take(reads)).reject(new Error("Cannot find context with specified id"));
1244-
yield* Effect.promise(() => navigated);
1245-
const hidden = resized();
1246-
(yield* Queue.take(reads)).resolve(answer(0, 0));
1247-
yield* Effect.promise(() => hidden);
1248-
expect(viewportSizes(yield* Queue.takeAll(viewer.output))).toEqual([
1249-
"1000x600",
1250-
"1000x600",
1251-
"1000x600",
1252-
]);
12531255
}),
12541256
).pipe(Effect.provide(layer)),
12551257
);
@@ -1258,6 +1260,14 @@ it.effect("a stalled desktop viewer still gets its size, control and reconnect",
12581260
Effect.scoped(
12591261
Effect.gen(function* () {
12601262
const { tabId, viewer, context, resize } = yield* openDesktopViewer;
1263+
const output =
1264+
viewer.output as unknown as Queue.Queue<ServerBrowser.ServerBrowserViewerOutput>;
1265+
// Ready once the sizes measured on attach and on a first resize both arrived.
1266+
yield* resize(1920, 1080);
1267+
expect(viewportSizes(yield* Queue.takeAll(viewer.output))).toEqual([
1268+
"1920x1080",
1269+
"1920x1080",
1270+
]);
12611271
yield* Effect.promise(() => desktopDetachReady.promise);
12621272
yield* viewer.input({ type: "takeControl" });
12631273
context.page.emit("filechooser", {
@@ -1268,18 +1278,26 @@ it.effect("a stalled desktop viewer still gets its size, control and reconnect",
12681278
let chooser = yield* Queue.take(viewer.output);
12691279
while (chooser._tag !== "fileChooser") chooser = yield* Queue.take(viewer.output);
12701280
yield* Queue.clear(viewer.output);
1271-
const control = expect.objectContaining({ _tag: "control", controller: "you" });
1272-
// A resize replaces a full backlog but keeps control and the controller's open picker.
1281+
// A resize compacts a full backlog but keeps control, the open picker and
1282+
// one-time notifications in order.
12731283
yield* viewer.input({ type: "takeControl" });
1284+
const notifications: Array<ServerBrowser.ServerBrowserViewerOutput> = [
1285+
{ _tag: "fileChooserClosed", id: "chooser-0" },
1286+
{ _tag: "popup", tabId: "popup-1" },
1287+
{ _tag: "download", id: "download-1", fileName: "report.csv", sizeBytes: 3 },
1288+
{ _tag: "clipboard", text: "copied" },
1289+
];
1290+
for (const item of notifications) Queue.offerUnsafe(output, item);
12741291
let acked = stallViewer(viewer);
12751292
yield* resize(1000, 600);
12761293
expect(yield* Queue.takeAll(viewer.output)).toEqual([
1277-
{ _tag: "viewport", width: 1000, height: 600 },
1278-
control,
12791294
chooser,
1295+
...notifications,
1296+
expect.objectContaining({ _tag: "control", controller: "you" }),
1297+
{ _tag: "viewport", width: 1000, height: 600 },
12801298
]);
12811299
yield* acked;
1282-
// A control change keeps the newest size.
1300+
// A control change keeps only the newest size.
12831301
yield* resize(900, 500);
12841302
yield* resize(1200, 700);
12851303
acked = stallViewer(viewer);
@@ -1289,6 +1307,13 @@ it.effect("a stalled desktop viewer still gets its size, control and reconnect",
12891307
{ _tag: "viewport", width: 1200, height: 700 },
12901308
]);
12911309
yield* acked;
1310+
// Notifications that no longer fit go oldest first, never the size.
1311+
let copies = 0;
1312+
while (Queue.offerUnsafe(output, { _tag: "clipboard", text: `copy ${copies}` })) copies++;
1313+
yield* resize(1000, 600);
1314+
const compacted = yield* Queue.takeAll(viewer.output);
1315+
expect(compacted.at(0)).toEqual({ _tag: "clipboard", text: "copy 1" });
1316+
expect(compacted.at(-1)).toEqual({ _tag: "viewport", width: 1000, height: 600 });
12921317
// The desktop takes the page back; a resize after it cannot bury the reconnect.
12931318
acked = stallViewer(viewer);
12941319
desktopDetaches.emit("detach", { threadId: scope.thread.threadId, tabId });

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,12 @@ const make = Effect.gen(function* () {
853853
await applyRendering(tab, snapshot);
854854
if (desktop) {
855855
// The desktop resizes and zooms its webview itself; viewers map input with the new size.
856-
cdp.on("Page.frameResized", () => {
856+
const remeasure = () => {
857857
if (tab.viewers.size > 0) return broadcastViewport(tab).catch(constVoid);
858-
});
858+
};
859+
cdp.on("Page.frameResized", remeasure);
860+
// A dialog blocks size reads, and closing one resizes nothing.
861+
cdp.on("Page.javascriptDialogClosed", remeasure);
859862
await cdp.send("Page.enable");
860863
}
861864
const isMainNavigation = (request: { isNavigationRequest(): boolean; frame(): unknown }) =>
@@ -2368,22 +2371,22 @@ const make = Effect.gen(function* () {
23682371
// Dropped frames must still release Chromium.
23692372
if (Queue.offerUnsafe(output, next)) return;
23702373
if (next._tag === "frame") runFork(next.ack);
2371-
// State a stalled viewer cannot miss replaces its backlog. It runs
2372-
// synchronously so an older replacement can never land after a newer one.
2374+
// State a stalled viewer cannot miss compacts its backlog. It runs
2375+
// synchronously so an older compaction can never land after a newer one.
23732376
else if (["gone", "reconnect", "control", "viewport", "fileChooser"].includes(next._tag)) {
23742377
const dropped = Effect.runSyncExit(Queue.clear(output));
23752378
if (dropped._tag === "Failure") return;
2376-
Queue.offerUnsafe(output, next);
2377-
// Without its latest size the viewer cannot map input at all.
2378-
for (const tag of ["control", "viewport"] as const) {
2379-
if (next._tag === tag) continue;
2380-
const retained = dropped.value.findLast((item) => item._tag === tag);
2381-
if (retained) Queue.offerUnsafe(output, retained);
2382-
}
2383-
// The controller's open picker may have been in the dropped backlog.
2384-
const chooser =
2385-
next._tag === "control" || next._tag === "viewport" ? fileChooserMessage(tab) : null;
2386-
if (chooser && tab.control.controller === viewer.id) Queue.offerUnsafe(output, chooser);
2379+
const items = [...dropped.value, next];
2380+
// Without its latest size and control the viewer cannot map input at all.
2381+
const state = (["control", "viewport"] as const).flatMap(
2382+
(tag) => items.findLast((item) => item._tag === tag) ?? [],
2383+
);
2384+
// Everything else is one-time; if it no longer fits, the oldest goes first.
2385+
const rest = items.filter(
2386+
(item) => item._tag !== "frame" && item._tag !== "control" && item._tag !== "viewport",
2387+
);
2388+
for (const item of [...rest.slice(state.length - VIEWER_OUTPUT_LIMIT), ...state])
2389+
Queue.offerUnsafe(output, item);
23872390
for (const item of dropped.value) if (item._tag === "frame") runFork(item.ack);
23882391
}
23892392
},

0 commit comments

Comments
 (0)