Skip to content

Commit 6c5a55a

Browse files
committed
fix(desktop): keep preview download settings out of app exports
1 parent 1316d48 commit 6c5a55a

4 files changed

Lines changed: 65 additions & 12 deletions

File tree

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

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ describe("CDP relay extra sessions", () => {
178178
});
179179

180180
describe("CDP relay downloads", () => {
181-
it("applies the server's download directory and reports downloads on the root session", async () => {
181+
it("captures preview downloads without changing the default browser context", async () => {
182182
const send = vi.fn(async () => ({}));
183183
const directories: Array<string | null> = [];
184184
const written: Array<Record<string, unknown>> = [];
@@ -201,15 +201,59 @@ describe("CDP relay downloads", () => {
201201
);
202202
await settle();
203203
expect(directories).toEqual(["/srv/artifacts"]);
204-
expect(send).toHaveBeenCalledWith(
205-
"Browser.setDownloadBehavior",
206-
{ behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: true },
207-
undefined,
208-
);
204+
expect(send.mock.calls).toEqual([
205+
["Page.setDownloadBehavior", { behavior: "default" }, undefined],
206+
]);
209207
written.length = 0;
210-
relay.event("Browser.downloadWillBegin", { guid: "g1", suggestedFilename: "r.csv" }, "");
208+
relay.event("Page.downloadWillBegin", { guid: "g1", suggestedFilename: "r.csv" }, "");
209+
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
211210
expect(written).toEqual([
212211
{ method: "Browser.downloadWillBegin", params: { guid: "g1", suggestedFilename: "r.csv" } },
212+
{ method: "Browser.downloadProgress", params: { guid: "g1", state: "completed" } },
213+
]);
214+
});
215+
216+
it("keeps denial scoped to the preview and restores Electron's save handling when allowed", async () => {
217+
const send = vi.fn(async () => ({}));
218+
const directories: Array<string | null> = [];
219+
const relay = createCdpRelayConnection(
220+
makeTarget(send, (directory) => directories.push(directory)),
221+
() => {},
222+
);
223+
for (const [id, behavior] of ["deny", "allowAndName"].entries()) {
224+
relay.receive(
225+
JSON.stringify({
226+
id,
227+
method: "Browser.setDownloadBehavior",
228+
params: { behavior, downloadPath: "/srv/artifacts" },
229+
}),
230+
);
231+
await settle();
232+
}
233+
expect(directories).toEqual([null, "/srv/artifacts"]);
234+
expect(send.mock.calls).toEqual([
235+
["Page.setDownloadBehavior", { behavior: "deny" }, undefined],
236+
["Page.setDownloadBehavior", { behavior: "default" }, undefined],
213237
]);
214238
});
239+
240+
it("does not publish download events when the connection has disabled them", async () => {
241+
const written: Array<Record<string, unknown>> = [];
242+
const relay = createCdpRelayConnection(makeTarget(vi.fn(async () => ({}))), (raw) =>
243+
written.push(JSON.parse(raw)),
244+
);
245+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
246+
relay.receive(
247+
JSON.stringify({
248+
id: 2,
249+
method: "Browser.setDownloadBehavior",
250+
params: { behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled: false },
251+
}),
252+
);
253+
await settle();
254+
written.length = 0;
255+
relay.event("Page.downloadWillBegin", { guid: "g1" }, "");
256+
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
257+
expect(written).toEqual([]);
258+
});
215259
});

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export function createCdpRelayConnection(
5353
): CdpRelayConnection {
5454
const send = (message: Record<string, unknown>) => write(JSON.stringify(message));
5555
let attached = false;
56+
let downloadEventsEnabled = false;
5657
/** Relay-created page sessions share one real debugger; child frame sessions retain their own ids. */
5758
const sessions = new Map<string, "browser" | "page">([[PAGE_SESSION_ID, "page"]]);
5859
let sessionSequence = 0;
@@ -78,8 +79,13 @@ export function createCdpRelayConnection(
7879
const directory = command.params?.["downloadPath"];
7980
const allowed = command.params?.["behavior"] !== "deny" && typeof directory === "string";
8081
target.setDownloadDirectory(allowed ? directory : null);
81-
const { browserContextId: _ignored, ...params } = command.params ?? {};
82-
return target.send("Browser.setDownloadBehavior", params, undefined);
82+
downloadEventsEnabled = command.params?.["eventsEnabled"] === true;
83+
// Browser-scoped behavior targets Electron's app session; preview paths belong to will-download.
84+
return target.send(
85+
"Page.setDownloadBehavior",
86+
{ behavior: command.params?.["behavior"] === "deny" ? "deny" : "default" },
87+
undefined,
88+
);
8389
}
8490
case "Browser.getVersion":
8591
return {
@@ -186,9 +192,10 @@ export function createCdpRelayConnection(
186192
},
187193
event: (method, params, sessionId) => {
188194
if (!attached) return;
195+
if (method.startsWith("Page.download")) method = method.replace("Page.", "Browser.");
189196
// Download events are the browser's; Playwright listens on its root session.
190197
if (method.startsWith("Browser.download")) {
191-
send({ method, params });
198+
if (downloadEventsEnabled) send({ method, params });
192199
return;
193200
}
194201
// Electron reports the page's own events with an empty session id.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,9 +934,11 @@ describe("DesktopBrowserHost", () => {
934934
}),
935935
}),
936936
);
937-
debuggee.emit("Browser.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
937+
debuggee.emit("Page.downloadWillBegin", { guid: "guid-1", suggestedFilename: "r.csv" });
938+
expect(host.placeDownload(makeDebuggee().tab.webContents, item)).toBe(false);
938939
expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(true);
939940
expect(paths).toEqual(["/srv/downloads/guid-1"]);
941+
expect(host.placeDownload(debuggee.tab.webContents, item)).toBe(false);
940942
}),
941943
);
942944

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ export const make = Effect.gen(function* () {
422422
focusReleasing: false,
423423
screencasts: new Set(),
424424
onMessage: (_event, method, params, sessionId) => {
425-
if (method === "Browser.downloadWillBegin") {
425+
if (method === "Page.downloadWillBegin") {
426426
const guid = (params as { guid?: unknown } | undefined)?.guid;
427427
tab.pendingDownloadGuid = typeof guid === "string" ? guid : null;
428428
}

0 commit comments

Comments
 (0)