Skip to content

Commit 59ab685

Browse files
fix(desktop): preserve agent captures during native navigation
1 parent 57095e6 commit 59ab685

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3105,19 +3105,23 @@ describe("PreviewManager", () => {
31053105
);
31063106

31073107
effectIt.effect(
3108-
"waits for the renderer before copying an inactive guest and bounds preparation",
3108+
"bounds renderer preparation and keeps agent captures alive across native navigation",
31093109
() =>
31103110
Effect.gen(function* () {
31113111
const {
3112+
hostWebContents,
31123113
manager,
31133114
wc,
31143115
copies,
31153116
captures,
3117+
capturePage,
31163118
replies,
31173119
send,
31183120
rendererStates,
31193121
setBackgroundThrottling,
31203122
} = yield* setupAgentCapture(false);
3123+
const loadURL = vi.fn(async () => undefined);
3124+
Object.assign(wc, { loadURL });
31213125
const requests = () => rendererStates.at(-1)?.captureRequestIds ?? [];
31223126
yield* send(1);
31233127
yield* TestClock.adjust(0);
@@ -3137,13 +3141,18 @@ describe("PreviewManager", () => {
31373141
yield* TestClock.adjust(0);
31383142
const second = requests().find((id) => id !== first)!;
31393143
expect(requests()).toHaveLength(2);
3144+
yield* manager.navigate("tab_agent_capture", "https://example.com/next");
3145+
expect(loadURL).toHaveBeenLastCalledWith("https://example.com/next");
3146+
expect(requests()).toEqual([first, second]);
31403147
yield* manager.captureReady("tab_agent_capture", wc.id, expired);
31413148
yield* manager.captureReady("tab_agent_capture", wc.id + 1, first);
31423149
expect(captures).toHaveLength(0);
3143-
yield* manager.captureReady("tab_agent_capture", wc.id, first);
3150+
yield* manager.captureReady("tab_agent_capture", wc.id, requests()[0]!);
31443151
yield* TestClock.adjust(0);
31453152
expect(captures).toHaveLength(1);
31463153
expect(copies).toHaveLength(1);
3154+
yield* manager.navigate("tab_agent_capture", "https://example.com/while-capturing");
3155+
expect(requests()).toEqual([first, second]);
31473156
captures[0]!.resolve({ data: "first" });
31483157
expect(yield* Queue.take(replies)).toMatchObject({ id: 2, result: { data: "first" } });
31493158
expect(requests()).toEqual([second]);
@@ -3155,6 +3164,27 @@ describe("PreviewManager", () => {
31553164
expect(requests()).toEqual([]);
31563165
expect(setBackgroundThrottling).toHaveBeenLastCalledWith(true);
31573166
expect(wc.setBackgroundThrottling).not.toHaveBeenCalled();
3167+
3168+
const replacement = Object.assign(
3169+
makeTestPreviewWebContents(capturePage, 43, hostWebContents),
3170+
{ isDevToolsOpened: () => false, getUserAgent: () => "Electron" },
3171+
);
3172+
Object.assign(replacement.debugger, {
3173+
sendCommand: async () => ({ data: "after navigation" }),
3174+
});
3175+
fromId.mockImplementation((id) => (id === replacement.id ? replacement : wc));
3176+
yield* manager.registerWebview("tab_agent_capture", replacement.id);
3177+
yield* TestClock.adjust(0);
3178+
yield* send(4);
3179+
yield* TestClock.adjust(0);
3180+
expect(requests()).toHaveLength(1);
3181+
yield* manager.captureReady("tab_agent_capture", replacement.id, requests()[0]!);
3182+
yield* TestClock.adjust(0);
3183+
expect(yield* Queue.poll(replies)).toEqual(
3184+
Option.some({ id: 4, result: { data: "after navigation" } }),
3185+
);
3186+
expect(requests()).toEqual([]);
3187+
expect(setBackgroundThrottling).toHaveBeenLastCalledWith(true);
31583188
}).pipe(Effect.provide(managerLayer("linux")), Effect.scoped),
31593189
);
31603190

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
20202020
audible: current?.audible ?? false,
20212021
controller: current?.controller ?? "none",
20222022
...(current?.favicon ? { favicon: current.favicon } : {}),
2023+
...(current?.captureRequestIds ? { captureRequestIds: current.captureRequestIds } : {}),
2024+
...(current?.serverTab ? { serverTab: current.serverTab } : {}),
20232025
updatedAt,
20242026
};
20252027
return [

0 commit comments

Comments
 (0)