Skip to content

Commit e7c375d

Browse files
fix(desktop): keep preview download settings out of app exports (#25428)
Co-authored-by: maria-rcks <[email protected]>
1 parent a48a076 commit e7c375d

4 files changed

Lines changed: 121 additions & 13 deletions

File tree

‎apps/desktop/src/preview/CdpRelay.test.ts‎

Lines changed: 100 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ describe("CDP relay extra sessions", () => {
171171
});
172172

173173
describe("CDP relay downloads", () => {
174-
it("applies the server's download directory and reports downloads on the root session", async () => {
174+
it("captures preview downloads without changing the default browser context", async () => {
175175
const send = vi.fn(async () => ({}));
176176
const directories: Array<string | null> = [];
177177
const written: Array<Record<string, unknown>> = [];
@@ -194,17 +194,110 @@ describe("CDP relay downloads", () => {
194194
);
195195
await settle();
196196
expect(directories).toEqual(["/srv/artifacts"]);
197-
expect(send).toHaveBeenCalledWith(
198-
"Browser.setDownloadBehavior",
199-
{ behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: true },
200-
undefined,
201-
);
197+
expect(send.mock.calls).toEqual([
198+
["Page.setDownloadBehavior", { behavior: "default" }, undefined],
199+
]);
202200
written.length = 0;
203-
relay.event("Browser.downloadWillBegin", { guid: "g1", suggestedFilename: "r.csv" }, "");
201+
relay.event("Page.downloadWillBegin", { guid: "g1", suggestedFilename: "r.csv" }, "");
202+
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
204203
expect(written).toEqual([
205204
{ method: "Browser.downloadWillBegin", params: { guid: "g1", suggestedFilename: "r.csv" } },
205+
{ method: "Browser.downloadProgress", params: { guid: "g1", state: "completed" } },
206206
]);
207207
});
208+
209+
it("keeps denial scoped to the preview and restores Electron's save handling when allowed", async () => {
210+
const send = vi.fn(async () => ({}));
211+
const directories: Array<string | null> = [];
212+
const relay = makeRelay(
213+
makeTarget(send, (directory) => directories.push(directory)),
214+
() => {},
215+
);
216+
for (const [id, behavior] of ["deny", "allowAndName"].entries()) {
217+
relay.receive(
218+
JSON.stringify({
219+
id,
220+
method: "Browser.setDownloadBehavior",
221+
params: { behavior, downloadPath: "/srv/artifacts" },
222+
}),
223+
);
224+
await settle();
225+
}
226+
expect(directories).toEqual([null, "/srv/artifacts"]);
227+
expect(send.mock.calls).toEqual([
228+
["Page.setDownloadBehavior", { behavior: "deny" }, undefined],
229+
["Page.setDownloadBehavior", { behavior: "default" }, undefined],
230+
]);
231+
});
232+
233+
it("does not publish download events when the connection has disabled them", async () => {
234+
const written: Array<Record<string, unknown>> = [];
235+
const relay = makeRelay(makeTarget(vi.fn(async () => ({}))), (raw) =>
236+
written.push(JSON.parse(raw)),
237+
);
238+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
239+
relay.receive(
240+
JSON.stringify({
241+
id: 2,
242+
method: "Browser.setDownloadBehavior",
243+
params: { behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: false },
244+
}),
245+
);
246+
await settle();
247+
written.length = 0;
248+
relay.event("Page.downloadWillBegin", { guid: "g1" }, "");
249+
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
250+
expect(written).toEqual([]);
251+
});
252+
253+
it.each([false, true])(
254+
"keeps the previous download configuration (events enabled: %s) when configuration fails",
255+
async (enabled) => {
256+
const send = vi
257+
.fn(async () => ({}))
258+
.mockResolvedValueOnce({})
259+
.mockRejectedValueOnce(new Error("Page command rejected"));
260+
const written: Array<Record<string, unknown>> = [];
261+
let directory: string | null = null;
262+
const relay = makeRelay(
263+
makeTarget(send, (value) => {
264+
directory = value;
265+
}),
266+
(raw) => written.push(JSON.parse(raw)),
267+
);
268+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
269+
for (const [id, eventsEnabled] of [enabled, !enabled].entries()) {
270+
relay.receive(
271+
JSON.stringify({
272+
id: id + 2,
273+
method: "Browser.setDownloadBehavior",
274+
params: {
275+
behavior: id === 1 && enabled ? "deny" : "allowAndName",
276+
downloadPath: id === 0 ? "/srv/artifacts" : "/srv/new-artifacts",
277+
eventsEnabled,
278+
},
279+
}),
280+
);
281+
await settle();
282+
}
283+
expect(written).toContainEqual({
284+
id: 3,
285+
error: { code: -32000, message: "Page command rejected" },
286+
});
287+
expect(directory).toBe("/srv/artifacts");
288+
written.length = 0;
289+
relay.event("Page.downloadWillBegin", { guid: "g1" }, "");
290+
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
291+
expect(written).toEqual(
292+
enabled
293+
? [
294+
{ method: "Browser.downloadWillBegin", params: { guid: "g1" } },
295+
{ method: "Browser.downloadProgress", params: { guid: "g1", state: "completed" } },
296+
]
297+
: [],
298+
);
299+
},
300+
);
208301
});
209302

210303
describe("CDP relay payload budget", () => {

‎apps/desktop/src/preview/CdpRelay.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ export function createCdpRelayConnection(
9898
close();
9999
};
100100
let attached = false;
101+
let downloadEventsEnabled = false;
101102
/** Relay-created page sessions share one real debugger; child frame sessions retain their own ids. */
102103
const sessions = new Map<string, "browser" | "page">([[PAGE_SESSION_ID, "page"]]);
103104
let sessionSequence = 0;
@@ -122,9 +123,15 @@ export function createCdpRelayConnection(
122123
case "Browser.setDownloadBehavior": {
123124
const directory = command.params?.["downloadPath"];
124125
const allowed = command.params?.["behavior"] !== "deny" && typeof directory === "string";
126+
// Browser-scoped behavior targets Electron's app session; preview paths belong to will-download.
127+
const result = await target.send(
128+
"Page.setDownloadBehavior",
129+
{ behavior: command.params?.["behavior"] === "deny" ? "deny" : "default" },
130+
undefined,
131+
);
125132
target.setDownloadDirectory(allowed ? directory : null);
126-
const { browserContextId: _ignored, ...params } = command.params ?? {};
127-
return target.send("Browser.setDownloadBehavior", params, undefined);
133+
downloadEventsEnabled = command.params?.["eventsEnabled"] === true;
134+
return result;
128135
}
129136
case "Browser.getVersion":
130137
return {
@@ -232,9 +239,10 @@ export function createCdpRelayConnection(
232239
},
233240
event: (method, params, sessionId) => {
234241
if (!attached) return;
242+
if (method.startsWith("Page.download")) method = method.replace("Page.", "Browser.");
235243
// Download events are the browser's; Playwright listens on its root session.
236244
if (method.startsWith("Browser.download")) {
237-
send({ method, params });
245+
if (downloadEventsEnabled) send({ method, params });
238246
return;
239247
}
240248
// Electron reports the page's own events with an empty session id.

‎apps/desktop/src/preview/DesktopBrowserHost.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,12 +917,14 @@ describe("DesktopBrowserHost", () => {
917917
const host = yield* DesktopBrowserHost.make;
918918
const debuggee = makeDebuggee();
919919
host.attach(key, debuggee.tab);
920+
const replies = yield* watchReplies(host);
920921
const paths: Array<string> = [];
921922
const item = {
922923
setSavePath: (path: string) => void paths.push(path),
923924
} as unknown as Electron.DownloadItem;
924925

925926
expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
927+
const sent = debuggee.nextSend();
926928
yield* host.handleCommandLine(
927929
encodeJson({
928930
type: "cdp",
@@ -934,10 +936,15 @@ describe("DesktopBrowserHost", () => {
934936
}),
935937
}),
936938
);
937-
debuggee.emit("Browser.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
939+
yield* Effect.promise(() => sent);
940+
debuggee.release();
941+
expect(yield* Queue.take(replies)).toBe(1);
942+
debuggee.emit("Page.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
943+
expect(host.placeDownload(makeDebuggee().tab.webContents, item)).toBe(false);
938944
expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(true);
939945
expect(paths).toEqual(["/srv/downloads/guid-1"]);
940-
}),
946+
expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
947+
}).pipe(Effect.scoped),
941948
);
942949

943950
it.effect("tells a download the person clicked from one the agent's input started", () =>

‎apps/desktop/src/preview/DesktopBrowserHost.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ export const make = Effect.gen(function* () {
438438
focusReleasing: false,
439439
screencasts: new Set(),
440440
onMessage: (_event, method, params, sessionId) => {
441-
if (method === "Browser.downloadWillBegin") {
441+
if (method === "Page.downloadWillBegin") {
442442
const guid = (params as { guid?: unknown } | undefined)?.guid;
443443
tab.pendingDownloadGuid = typeof guid === "string" ? guid : null;
444444
}

0 commit comments

Comments
 (0)