Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions apps/desktop/src/ipc/methods/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,42 @@ describe("preview IPC methods", () => {
).toThrow();
});
});

describe("automationPressResult", () => {
effectIt.effect("returns an interrupted outcome for human control takeover", () =>
Effect.gen(function* () {
const error = new PreviewManager.PreviewAutomationControlInterruptedError({
operation: "press",
tabId: "tab-1",
webContentsId: 42,
});

expect(yield* PreviewIpc.automationPressResult(Effect.fail(error))).toEqual({
status: "interrupted",
});
}),
);

effectIt.effect("returns a completed outcome after a successful press", () =>
Effect.gen(function* () {
expect(yield* PreviewIpc.automationPressResult(Effect.succeed("restored"))).toEqual({
status: "completed",
focusDisposition: "restored",
});
}),
);

effectIt.effect("keeps unexpected failures as rejected effects", () =>
Effect.gen(function* () {
const error = new PreviewManager.PreviewWebContentsNotFoundError({
tabId: "tab-1",
webContentsId: 42,
});
const exit = yield* Effect.exit(PreviewIpc.automationPressResult(Effect.fail(error)));

expect(Exit.isFailure(exit)).toBe(true);
if (Exit.isSuccess(exit)) return;
expect(Cause.squash(exit.cause)).toBe(error);
}),
);
});
22 changes: 20 additions & 2 deletions apps/desktop/src/ipc/methods/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DesktopPreviewAutomationClickInputSchema,
DesktopPreviewAutomationEvaluateInputSchema,
DesktopPreviewAutomationPressInputSchema,
DesktopPreviewAutomationPressResultSchema,
DesktopPreviewAutomationScrollInputSchema,
DesktopPreviewAutomationStatusSchema,
DesktopPreviewAutomationTypeInputSchema,
Expand All @@ -23,6 +24,8 @@ import {
DesktopPreviewCreateTabInputSchema,
DesktopPreviewTabInputSchema,
DesktopPreviewWebviewConfigSchema,
type DesktopPreviewAutomationPressFocusDisposition,
type DesktopPreviewAutomationPressResult,
PreviewAnnotationSubmissionResultSchema,
PreviewAutomationSnapshot,
DEFAULT_BROWSER_PROFILE_ID,
Expand Down Expand Up @@ -420,13 +423,28 @@ export const automationType = DesktopIpc.makeIpcMethod({
export const automationPress = DesktopIpc.makeIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_PRESS_CHANNEL,
payload: DesktopPreviewAutomationPressInputSchema,
result: Schema.Void,
result: DesktopPreviewAutomationPressResultSchema,
handler: Effect.fn("desktop.ipc.preview.automationPress")(function* ({ tabId, input }) {
const manager = yield* PreviewManager.PreviewManager;
yield* manager.automationPress(tabId, input);
return yield* automationPressResult(manager.automationPress(tabId, input));
}),
});

export const automationPressResult = (
effect: Effect.Effect<
DesktopPreviewAutomationPressFocusDisposition,
PreviewManager.PreviewManagerError
>,
): Effect.Effect<DesktopPreviewAutomationPressResult, PreviewManager.PreviewManagerError> =>
effect.pipe(
Effect.map((focusDisposition) => ({ status: "completed" as const, focusDisposition })),
Effect.catch((error: PreviewManager.PreviewManagerError) =>
PreviewManager.isPreviewAutomationControlInterruptedError(error)
? Effect.succeed({ status: "interrupted" as const })
: Effect.fail(error),
),
);

export const automationScroll = DesktopIpc.makeIpcMethod({
channel: IpcChannels.PREVIEW_AUTOMATION_SCROLL_CHANNEL,
payload: DesktopPreviewAutomationScrollInputSchema,
Expand Down
116 changes: 107 additions & 9 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const {
createFromPath,
fromId,
getFocusedWebContents,
getFocusedWindow,
mkdir,
showItemInFolder,
webviewSend,
Expand All @@ -126,6 +127,9 @@ const {
createFromPath: vi.fn((): { readonly isEmpty: () => boolean } => ({ isEmpty: () => false })),
fromId: vi.fn<(_id?: number) => Electron.WebContents | null>((_id?: number) => null),
getFocusedWebContents: vi.fn(() => null),
getFocusedWindow: vi.fn<() => Electron.BrowserWindow | null>(
() => ({}) as Electron.BrowserWindow,
),
mkdir: vi.fn((_path: string) => undefined),
showItemInFolder: vi.fn(),
webviewSend: vi.fn(),
Expand All @@ -134,7 +138,7 @@ const {
}));

vi.mock("electron", () => ({
BrowserWindow: browserWindowConstructor,
BrowserWindow: Object.assign(browserWindowConstructor, { getFocusedWindow }),
clipboard: {
writeImage,
},
Expand Down Expand Up @@ -466,6 +470,8 @@ describe("PreviewManager", () => {
fromId.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValue(null);
getFocusedWindow.mockReset();
getFocusedWindow.mockReturnValue({} as Electron.BrowserWindow);
mkdir.mockClear();
writeFile.mockClear();
showItemInFolder.mockClear();
Expand Down Expand Up @@ -3503,6 +3509,13 @@ describe("PreviewManager", () => {
withManager((manager) =>
Effect.gen(function* () {
let failKeyDown = false;
let interruptPress = false;
let blockCleanup = false;
const cleanupStarted = Deferred.makeUnsafe<void>();
let releaseCleanup!: () => void;
const cleanupRelease = new Promise<void>((resolve) => {
releaseCleanup = resolve;
});
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
if (
Expand All @@ -3518,22 +3531,30 @@ describe("PreviewManager", () => {
) {
humanInput?.(
{},
{
kind: "key",
key: params["key"],
code: params["code"] ?? "Digit1",
},
interruptPress
? { kind: "pointer", x: 400, y: 300, button: 0 }
: {
kind: "key",
key: params["key"],
code: params["code"] ?? "Digit1",
},
);
}
if (blockCleanup && method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp") {
Deferred.doneUnsafe(cleanupStarted, Effect.void);
await cleanupRelease;
}
return method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined;
});
const restoreFocus = vi.fn();
const focus = vi.fn();
getFocusedWebContents.mockReturnValue({
const previousFocused = {
id: 7,
isDestroyed: () => false,
focus: restoreFocus,
} as never);
} as never;
const focusedGuest = { id: 42, isDestroyed: () => false } as never;
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
fromId.mockReturnValue({
id: 42,
isDestroyed: () => false,
Expand Down Expand Up @@ -3571,7 +3592,7 @@ describe("PreviewManager", () => {
yield* manager.registerWebview("tab_input", 42);
yield* manager.automationType("tab_input", { text: "hello", clear: true });
yield* manager.automationType("tab_input", { text: "", clear: true });
yield* manager.automationPress("tab_input", { key: "x" });
expect(yield* manager.automationPress("tab_input", { key: "x" })).toBe("restored");

const calls = sendCommand.mock.calls;
const methods = calls.map(([method]) => method);
Expand Down Expand Up @@ -3630,6 +3651,18 @@ describe("PreviewManager", () => {
expect(sendCommand).toHaveBeenCalledWith("Input.setIgnoreInputEvents", { ignore: false });

sendCommand.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(previousFocused);
const restoreCallsBeforeAlreadyFocusedPress = restoreFocus.mock.calls.length;
// The host may have moved DOM focus while the app renderer stayed
// focused. Mark the native disposition as restored without calling
// focus again so the host can restore its DOM focus.
expect(yield* manager.automationPress("tab_input", { key: "=" })).toBe("restored");
expect(restoreFocus).toHaveBeenCalledTimes(restoreCallsBeforeAlreadyFocusedPress);

sendCommand.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
failKeyDown = true;
const failedPress = yield* Effect.exit(manager.automationPress("tab_input", { key: "y" }));

Expand All @@ -3655,6 +3688,8 @@ describe("PreviewManager", () => {
).toHaveLength(1);

sendCommand.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
failKeyDown = false;
yield* manager.automationPress("tab_input", { key: "!" });
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
Expand All @@ -3669,6 +3704,69 @@ describe("PreviewManager", () => {
unmodifiedText: "!",
});
expect(restoreFocus).toHaveBeenCalledTimes(3);

sendCommand.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents
.mockReturnValueOnce({ id: 7, isDestroyed: () => false, focus: restoreFocus } as never)
.mockReturnValueOnce(null);
getFocusedWindow.mockReturnValue(null);
expect(yield* manager.automationPress("tab_input", { key: "?" })).toBe("preserved");
expect(restoreFocus).toHaveBeenCalledTimes(3);

getFocusedWebContents.mockReset();
getFocusedWebContents
.mockReturnValueOnce({ id: 7, isDestroyed: () => false, focus: restoreFocus } as never)
.mockReturnValueOnce({ id: 43, isDestroyed: () => false } as never);
getFocusedWindow.mockReturnValue({} as Electron.BrowserWindow);
expect(yield* manager.automationPress("tab_input", { key: "/" })).toBe("preserved");
expect(restoreFocus).toHaveBeenCalledTimes(3);

sendCommand.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
interruptPress = true;
const interruptedPress = yield* Effect.exit(
manager.automationPress("tab_input", { key: "=" }),
);
expect(Exit.isFailure(interruptedPress)).toBe(true);
if (Exit.isSuccess(interruptedPress)) return;
expect(Option.getOrThrow(Cause.findErrorOption(interruptedPress.cause))).toMatchObject({
_tag: "PreviewAutomationControlInterruptedError",
operation: "press",
tabId: "tab_input",
webContentsId: 42,
});
expect(restoreFocus).toHaveBeenCalledTimes(3);

sendCommand.mockClear();
getFocusedWebContents.mockReset();
getFocusedWebContents.mockReturnValueOnce(previousFocused).mockReturnValue(focusedGuest);
interruptPress = false;
blockCleanup = true;
const cleanupPress = yield* manager
.automationPress("tab_input", { key: "~" })
.pipe(Effect.forkChild);
yield* Deferred.await(cleanupStarted);
const interruptCleanup = yield* Effect.forkChild(Fiber.interrupt(cleanupPress));
yield* Effect.yieldNow;
releaseCleanup();
yield* Fiber.join(interruptCleanup);

const cleanupExit = yield* Fiber.await(cleanupPress);
expect(Exit.isFailure(cleanupExit)).toBe(true);
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
type: "keyUp",
key: "~",
code: "Backquote",
modifiers: 0,
windowsVirtualKeyCode: 192,
location: 0,
isKeypad: false,
});
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
enabled: false,
});
}),
),
);
Expand Down
Loading
Loading