Skip to content

Commit 3dabba5

Browse files
committed
fix(desktop): preserve download event state on rejected commands
1 parent 6c5a55a commit 3dabba5

2 files changed

Lines changed: 43 additions & 2 deletions

File tree

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,44 @@ describe("CDP relay downloads", () => {
256256
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
257257
expect(written).toEqual([]);
258258
});
259+
260+
it.each([false, true])(
261+
"keeps the previous download event setting (%s) when configuration fails",
262+
async (enabled) => {
263+
const send = vi
264+
.fn(async () => ({}))
265+
.mockResolvedValueOnce({})
266+
.mockRejectedValueOnce(new Error("Page command rejected"));
267+
const written: Array<Record<string, unknown>> = [];
268+
const relay = createCdpRelayConnection(makeTarget(send), (raw) =>
269+
written.push(JSON.parse(raw)),
270+
);
271+
relay.receive(JSON.stringify({ id: 1, method: "Target.setAutoAttach", params: {} }));
272+
for (const [id, eventsEnabled] of [enabled, !enabled].entries()) {
273+
relay.receive(
274+
JSON.stringify({
275+
id: id + 2,
276+
method: "Browser.setDownloadBehavior",
277+
params: { behavior: "allowAndName", downloadPath: "/srv/artifacts", eventsEnabled },
278+
}),
279+
);
280+
await settle();
281+
}
282+
expect(written).toContainEqual({
283+
id: 3,
284+
error: { code: -32000, message: "Page command rejected" },
285+
});
286+
written.length = 0;
287+
relay.event("Page.downloadWillBegin", { guid: "g1" }, "");
288+
relay.event("Page.downloadProgress", { guid: "g1", state: "completed" }, "");
289+
expect(written).toEqual(
290+
enabled
291+
? [
292+
{ method: "Browser.downloadWillBegin", params: { guid: "g1" } },
293+
{ method: "Browser.downloadProgress", params: { guid: "g1", state: "completed" } },
294+
]
295+
: [],
296+
);
297+
},
298+
);
259299
});

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,14 @@ export function createCdpRelayConnection(
7979
const directory = command.params?.["downloadPath"];
8080
const allowed = command.params?.["behavior"] !== "deny" && typeof directory === "string";
8181
target.setDownloadDirectory(allowed ? directory : null);
82-
downloadEventsEnabled = command.params?.["eventsEnabled"] === true;
8382
// Browser-scoped behavior targets Electron's app session; preview paths belong to will-download.
84-
return target.send(
83+
const result = await target.send(
8584
"Page.setDownloadBehavior",
8685
{ behavior: command.params?.["behavior"] === "deny" ? "deny" : "default" },
8786
undefined,
8887
);
88+
downloadEventsEnabled = command.params?.["eventsEnabled"] === true;
89+
return result;
8990
}
9091
case "Browser.getVersion":
9192
return {

0 commit comments

Comments
 (0)