Skip to content

Commit e816064

Browse files
authored
fix(desktop): keep preview keystrokes out of the composer (#11354)
Co-authored-by: maria-rcks <[email protected]>
1 parent 8eec78c commit e816064

4 files changed

Lines changed: 766 additions & 137 deletions

File tree

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

Lines changed: 174 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodeVM from "node:vm";
12
import { it as effectIt } from "@effect/vitest";
23
import { DESKTOP_PREVIEW_RECORDING_CAPTURE_TRIGGER } from "@t3tools/contracts";
34
import type { DesktopPreviewRecordingFrame } from "@t3tools/contracts";
@@ -3925,29 +3926,92 @@ describe("PreviewManager", () => {
39253926
withManager((manager) =>
39263927
Effect.gen(function* () {
39273928
let failKeyDown = false;
3929+
let routeToIframe = false;
3930+
let interruptFrameKeyDown = false;
3931+
let holdKeyUp = false;
3932+
let releaseKeyUp: (() => void) | undefined;
3933+
let notifyKeyUpQueued: (() => void) | undefined;
3934+
const keyUpQueued = new Promise<void>((resolve) => {
3935+
notifyKeyUpQueued = resolve;
3936+
});
3937+
const listeners = new Map<string, (event: unknown) => void>();
3938+
const eventCounts = new Map<string, number>();
3939+
const animationFrames = new Map<number, () => void>();
3940+
let animationFrameId = 0;
3941+
const renderFrame = () => {
3942+
const callbacks = [...animationFrames.values()];
3943+
animationFrames.clear();
3944+
callbacks.forEach((callback) => callback());
3945+
};
3946+
const frameContext = NodeVM.createContext({
3947+
performance: { eventCounts },
3948+
requestAnimationFrame: (callback: () => void) => {
3949+
const id = ++animationFrameId;
3950+
animationFrames.set(id, callback);
3951+
return id;
3952+
},
3953+
cancelAnimationFrame: (id: number) => animationFrames.delete(id),
3954+
window: {
3955+
addEventListener: (type: string, listener: (event: unknown) => void) =>
3956+
listeners.set(type, listener),
3957+
removeEventListener: (type: string) => listeners.delete(type),
3958+
},
3959+
});
3960+
const frame = {
3961+
executeJavaScript: vi.fn(async (expression: string) =>
3962+
NodeVM.runInContext(expression, frameContext),
3963+
),
3964+
};
39283965
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
3929-
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
3930-
if (
3931-
failKeyDown &&
3932-
method === "Input.dispatchKeyEvent" &&
3933-
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
3934-
) {
3935-
throw new Error("key dispatch failed");
3936-
}
3937-
if (
3938-
method === "Input.dispatchKeyEvent" &&
3939-
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
3940-
) {
3941-
humanInput?.(
3942-
{},
3943-
{
3944-
kind: "key",
3945-
key: params["key"],
3946-
code: params["code"] ?? "Digit1",
3947-
},
3948-
);
3966+
const sendCommand = vi.fn(
3967+
async (method: string, params?: Record<string, unknown>, sessionId?: string) => {
3968+
if (method === "Runtime.evaluate") {
3969+
if (params?.["returnByValue"] === true) return { result: { value: { ok: true } } };
3970+
return {
3971+
result:
3972+
routeToIframe && !sessionId
3973+
? { objectId: "focused-iframe-object" }
3974+
: { subtype: "null" },
3975+
};
3976+
}
3977+
if (method === "DOM.describeNode") return { node: { frameId: "focused-frame" } };
3978+
if (method === "Target.getTargets")
3979+
return {
3980+
targetInfos: [
3981+
{ targetId: "unrelated-frame", type: "iframe" },
3982+
{ targetId: "focused-frame", type: "iframe" },
3983+
],
3984+
};
3985+
if (method === "Target.attachToTarget") return { sessionId: "child-session" };
3986+
if (method === "Input.dispatchKeyEvent" && params?.["type"] !== "keyUp") {
3987+
if (failKeyDown) throw new Error("key dispatch failed");
3988+
if (interruptFrameKeyDown)
3989+
humanInput?.({}, { kind: "pointer", x: 80, y: 40, button: 0 });
3990+
}
3991+
return undefined;
3992+
},
3993+
);
3994+
const sendInputEvent = vi.fn((input: Electron.KeyboardInputEvent) => {
3995+
const signal = {
3996+
kind: "key",
3997+
key: input.keyCode,
3998+
code: input.keyCode === "!" ? "Digit1" : `Key${input.keyCode.toUpperCase()}`,
3999+
};
4000+
if (input.type === "keyUp") {
4001+
const deliver = () => {
4002+
eventCounts.set("keyup", (eventCounts.get("keyup") ?? 0) + 1);
4003+
renderFrame();
4004+
};
4005+
if (holdKeyUp) {
4006+
releaseKeyUp = deliver;
4007+
notifyKeyUpQueued?.();
4008+
} else {
4009+
queueMicrotask(deliver);
4010+
}
39494011
}
3950-
return method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined;
4012+
if (input.type !== "keyDown") return;
4013+
if (failKeyDown) throw new Error("key dispatch failed");
4014+
humanInput?.({}, signal);
39514015
});
39524016
const restoreFocus = vi.fn();
39534017
const focus = vi.fn();
@@ -3958,13 +4022,15 @@ describe("PreviewManager", () => {
39584022
} as never);
39594023
fromId.mockReturnValue({
39604024
id: 42,
4025+
mainFrame: { framesInSubtree: [frame] },
39614026
isDestroyed: () => false,
39624027
getType: () => "webview",
39634028
getURL: () => "https://example.com",
39644029
getTitle: () => "Example",
39654030
isLoading: () => false,
39664031
isDevToolsOpened: () => false,
39674032
focus,
4033+
sendInputEvent,
39684034
getZoomFactor: () => 1,
39694035
setZoomFactor: vi.fn(),
39704036
setAudioMuted: vi.fn(),
@@ -4003,13 +4069,6 @@ describe("PreviewManager", () => {
40034069
([method, params]) =>
40044070
method === "Emulation.setFocusEmulationEnabled" && params?.["enabled"] === true,
40054071
);
4006-
const keyDownIndex = calls.findIndex(
4007-
([method, params]) =>
4008-
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyDown",
4009-
);
4010-
const keyUpIndex = calls.findIndex(
4011-
([method, params]) => method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
4012-
);
40134072
const focusOffIndex = calls.findIndex(
40144073
([method, params]) =>
40154074
method === "Emulation.setFocusEmulationEnabled" && params?.["enabled"] === false,
@@ -4037,61 +4096,102 @@ describe("PreviewManager", () => {
40374096
expect(clearOnlyEvaluation).toBeDefined();
40384097
expect(methods).not.toContain("Input.insertText");
40394098
expect(enableIndex).toBeGreaterThanOrEqual(0);
4040-
expect(focus).toHaveBeenCalledOnce();
4041-
expect(restoreFocus).toHaveBeenCalledOnce();
4042-
expect(methods).toContain("Page.bringToFront");
4099+
expect(methods).not.toContain("Page.bringToFront");
4100+
expect(methods).not.toContain("Input.dispatchKeyEvent");
40434101
expect(enableIndex).toBeLessThan(focusOnIndex);
4044-
expect(focusOnIndex).toBeLessThan(keyDownIndex);
4045-
expect(keyDownIndex).toBeLessThan(keyUpIndex);
4046-
expect(keyUpIndex).toBeLessThan(focusOffIndex);
4047-
expect(
4048-
calls.filter(
4049-
([method, params]) =>
4050-
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
4051-
),
4052-
).toHaveLength(1);
4102+
expect(sendCommand.mock.invocationCallOrder[focusOnIndex]).toBeLessThan(
4103+
sendInputEvent.mock.invocationCallOrder[0]!,
4104+
);
4105+
expect(sendInputEvent.mock.invocationCallOrder[2]).toBeLessThan(
4106+
sendCommand.mock.invocationCallOrder[focusOffIndex]!,
4107+
);
4108+
expect(sendInputEvent.mock.calls.map(([input]) => input.type)).toEqual([
4109+
"keyDown",
4110+
"char",
4111+
"keyUp",
4112+
]);
40534113
expect(sendCommand).toHaveBeenCalledWith("Input.setIgnoreInputEvents", { ignore: false });
4114+
expect(listeners.size).toBe(0);
4115+
expect(animationFrames.size).toBe(0);
40544116

40554117
sendCommand.mockClear();
4056-
failKeyDown = true;
4057-
const failedPress = yield* Effect.exit(manager.automationPress("tab_input", { key: "y" }));
4058-
4059-
expect(Exit.isFailure(failedPress)).toBe(true);
4060-
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
4061-
type: "keyUp",
4062-
key: "y",
4063-
code: "KeyY",
4064-
modifiers: 0,
4065-
windowsVirtualKeyCode: 89,
4066-
location: 0,
4067-
isKeypad: false,
4118+
sendInputEvent.mockClear();
4119+
getFocusedWebContents.mockReturnValue(null);
4120+
holdKeyUp = true;
4121+
const backgroundPress = yield* manager
4122+
.automationPress("tab_input", { key: "x" })
4123+
.pipe(Effect.forkChild({ startImmediately: true }));
4124+
yield* Effect.promise(() => keyUpQueued);
4125+
expect(sendCommand).not.toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
4126+
enabled: false,
40684127
});
4128+
renderFrame();
4129+
expect(animationFrames.size).toBe(1);
4130+
releaseKeyUp?.();
4131+
yield* Fiber.join(backgroundPress);
4132+
expect(listeners.size).toBe(0);
4133+
expect(animationFrames.size).toBe(0);
40694134
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
40704135
enabled: false,
40714136
});
4072-
expect(restoreFocus).toHaveBeenCalledTimes(2);
4073-
expect(
4074-
sendCommand.mock.calls.filter(
4075-
([method, params]) =>
4076-
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
4077-
),
4078-
).toHaveLength(1);
4137+
holdKeyUp = false;
4138+
4139+
// Both native failures and expected-input matching must leave focus emulation off.
4140+
for (const key of ["y", "!"]) {
4141+
sendCommand.mockClear();
4142+
sendInputEvent.mockClear();
4143+
failKeyDown = key === "y";
4144+
const exit = yield* Effect.exit(manager.automationPress("tab_input", { key }));
4145+
expect(Exit.isFailure(exit)).toBe(failKeyDown);
4146+
expect(sendInputEvent.mock.calls.map(([input]) => input.type)).toEqual(
4147+
failKeyDown ? ["keyDown", "keyUp"] : ["keyDown", "char", "keyUp"],
4148+
);
4149+
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
4150+
enabled: false,
4151+
});
4152+
}
40794153

4080-
sendCommand.mockClear();
4081-
failKeyDown = false;
4082-
yield* manager.automationPress("tab_input", { key: "!" });
4083-
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
4084-
type: "keyDown",
4085-
key: "!",
4086-
code: "Digit1",
4087-
modifiers: 0,
4088-
windowsVirtualKeyCode: 49,
4089-
location: 0,
4090-
isKeypad: false,
4091-
text: "!",
4092-
unmodifiedText: "!",
4093-
});
4094-
expect(restoreFocus).toHaveBeenCalledTimes(3);
4154+
routeToIframe = true;
4155+
sendInputEvent.mockClear();
4156+
for (const outcome of ["success", "failure", "interrupted"]) {
4157+
sendCommand.mockClear();
4158+
failKeyDown = outcome === "failure";
4159+
interruptFrameKeyDown = outcome === "interrupted";
4160+
const exit = yield* Effect.exit(
4161+
manager.automationPress("tab_input", {
4162+
key: outcome === "success" ? "Enter" : "x",
4163+
}),
4164+
);
4165+
expect(Exit.isSuccess(exit)).toBe(outcome === "success");
4166+
if (outcome === "interrupted" && Exit.isFailure(exit)) {
4167+
expect(Option.getOrThrow(Cause.findErrorOption(exit.cause))).toMatchObject({
4168+
_tag: "PreviewAutomationControlInterruptedError",
4169+
});
4170+
}
4171+
expect(sendInputEvent).not.toHaveBeenCalled();
4172+
expect(sendCommand).toHaveBeenCalledWith("Target.attachToTarget", {
4173+
targetId: "focused-frame",
4174+
flatten: true,
4175+
});
4176+
expect(
4177+
sendCommand.mock.calls
4178+
.filter(([method]) => method === "Input.dispatchKeyEvent")
4179+
.map(([, params, sessionId]) => ({ type: params?.["type"], sessionId })),
4180+
).toEqual([
4181+
{ type: "keyDown", sessionId: "child-session" },
4182+
{ type: "keyUp", sessionId: "child-session" },
4183+
]);
4184+
expect(sendCommand).toHaveBeenCalledWith(
4185+
"Emulation.setFocusEmulationEnabled",
4186+
{ enabled: false },
4187+
"child-session",
4188+
);
4189+
expect(sendCommand).toHaveBeenCalledWith("Target.detachFromTarget", {
4190+
sessionId: "child-session",
4191+
});
4192+
}
4193+
expect(focus).not.toHaveBeenCalled();
4194+
expect(restoreFocus).not.toHaveBeenCalled();
40954195
}),
40964196
),
40974197
);

0 commit comments

Comments
 (0)