Skip to content

Commit 4ba8c96

Browse files
juliusmarmingecodex
andcommitted
Fix preview automation edge cases
Co-authored-by: codex <[email protected]>
1 parent ffae541 commit 4ba8c96

7 files changed

Lines changed: 128 additions & 8 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { it as effectIt } from "@effect/vitest";
2+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
23
import * as Cause from "effect/Cause";
34
import * as Effect from "effect/Effect";
45
import * as Exit from "effect/Exit";
@@ -91,6 +92,7 @@ const layer = PreviewManager.layer.pipe(
9192
Layer.provideMerge(environmentLayer),
9293
Layer.provideMerge(fileSystemLayer),
9394
Layer.provideMerge(Path.layer),
95+
Layer.provideMerge(Layer.succeed(HostProcessPlatform, "linux")),
9496
);
9597
const encodePreviewManagerError = Schema.encodeSync(PreviewManager.PreviewManagerError);
9698

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {
2525
PreviewAutomationTypeInput,
2626
PreviewAutomationWaitForInput,
2727
} from "@t3tools/contracts";
28+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
2829
import { normalizePreviewUrl } from "@t3tools/shared/preview";
2930
import {
3031
type BrowserWindow,
@@ -379,6 +380,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
379380
artifactDirectory: string,
380381
) {
381382
const fileSystem = yield* FileSystem.FileSystem;
383+
const hostPlatform = yield* HostProcessPlatform;
382384
const path = yield* Path.Path;
383385
const parentScope = yield* Scope.Scope;
384386
const context = yield* Effect.context<never>();
@@ -2226,7 +2228,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
22262228
sendCleanup: SendCommand,
22272229
) {
22282230
yield* prepareAutomationInput(send, false);
2229-
const keySequence = makePreviewAutomationKeySequence(input);
2231+
const keySequence = makePreviewAutomationKeySequence(input, {
2232+
isMac: hostPlatform === "darwin",
2233+
});
22302234
const previouslyFocused = yield* attempt(
22312235
{ operation: "automationPress.getFocusedWebContents", tabId, webContentsId: wc.id },
22322236
() => webContents.getFocusedWebContents(),

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,30 @@ describe("preview keyboard packets", () => {
4242
});
4343

4444
it("suppresses text and uses raw key-down for shortcuts", () => {
45-
expect(makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown).toEqual({
45+
expect(
46+
makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }, { isMac: true }).keyDown,
47+
).toEqual({
4648
type: "rawKeyDown",
4749
key: "a",
4850
code: "KeyA",
4951
modifiers: 4,
5052
windowsVirtualKeyCode: 65,
5153
location: 0,
5254
isKeypad: false,
55+
commands: ["selectAll"],
5356
});
5457
});
5558

59+
it("maps common macOS editing shortcuts without changing other platforms", () => {
60+
expect(
61+
makePreviewAutomationKeySequence({ key: "z", modifiers: ["Shift", "Meta"] }, { isMac: true })
62+
.keyDown.commands,
63+
).toEqual(["redo"]);
64+
expect(
65+
makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown,
66+
).not.toHaveProperty("commands");
67+
});
68+
5669
it("resolves shifted printable keys to their browser values", () => {
5770
const sequence = makePreviewAutomationKeySequence({ key: "1", modifiers: ["Shift"] });
5871
expect(sequence.keyDown).toMatchObject({

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface PreviewAutomationKeyEvent {
2020
readonly isKeypad: boolean;
2121
readonly text?: string;
2222
readonly unmodifiedText?: string;
23+
readonly commands?: ReadonlyArray<string>;
2324
}
2425

2526
export interface PreviewAutomationKeySequence {
@@ -79,6 +80,42 @@ const PRINTABLE_KEYS: ReadonlyArray<KeyDefinition> = [
7980
{ code: "Slash", key: "/", shiftedKey: "?", keyCode: 191 },
8081
];
8182

83+
/**
84+
* Chromium does not infer macOS editing commands from synthetic Meta chords.
85+
* Keep the common browser editing/navigation shortcuts explicit so dispatched
86+
* key events behave like their physical-key equivalents.
87+
*/
88+
const MAC_EDITING_COMMANDS: Readonly<Record<string, string>> = {
89+
"Meta+Backspace": "deleteToBeginningOfLine",
90+
"Meta+ArrowUp": "moveToBeginningOfDocument",
91+
"Meta+ArrowDown": "moveToEndOfDocument",
92+
"Meta+ArrowLeft": "moveToLeftEndOfLine",
93+
"Meta+ArrowRight": "moveToRightEndOfLine",
94+
"Shift+Meta+ArrowUp": "moveToBeginningOfDocumentAndModifySelection",
95+
"Shift+Meta+ArrowDown": "moveToEndOfDocumentAndModifySelection",
96+
"Shift+Meta+ArrowLeft": "moveToLeftEndOfLineAndModifySelection",
97+
"Shift+Meta+ArrowRight": "moveToRightEndOfLineAndModifySelection",
98+
"Meta+KeyA": "selectAll",
99+
"Meta+KeyC": "copy",
100+
"Meta+KeyX": "cut",
101+
"Meta+KeyV": "paste",
102+
"Meta+KeyZ": "undo",
103+
"Shift+Meta+KeyZ": "redo",
104+
};
105+
const SHORTCUT_MODIFIER_ORDER = ["Shift", "Control", "Alt", "Meta"] as const;
106+
107+
const macEditingCommands = (
108+
code: string,
109+
modifiers: PreviewAutomationPressInput["modifiers"],
110+
): ReadonlyArray<string> => {
111+
const shortcut = [
112+
...SHORTCUT_MODIFIER_ORDER.filter((modifier) => modifiers?.includes(modifier)),
113+
code,
114+
].join("+");
115+
const command = MAC_EDITING_COMMANDS[shortcut];
116+
return command ? [command] : [];
117+
};
118+
82119
const modifierMask = (modifiers: PreviewAutomationPressInput["modifiers"]): number =>
83120
(modifiers ?? []).reduce((value, modifier) => {
84121
switch (modifier) {
@@ -136,12 +173,14 @@ function resolveKeyDefinition(input: PreviewAutomationPressInput): KeyDefinition
136173
*/
137174
export function makePreviewAutomationKeySequence(
138175
input: PreviewAutomationPressInput,
176+
options?: { readonly isMac?: boolean },
139177
): PreviewAutomationKeySequence {
140178
const definition = resolveKeyDefinition(input);
141179
const modifiers = modifierMask(input.modifiers);
142180
const suppressText = input.modifiers?.some((modifier) => modifier !== "Shift") ?? false;
143181
const text = suppressText ? "" : (definition.text ?? "");
144182
const location = definition.location ?? 0;
183+
const commands = options?.isMac ? macEditingCommands(definition.code, input.modifiers) : [];
145184
const shared = {
146185
key: definition.key,
147186
code: definition.code,
@@ -156,6 +195,7 @@ export function makePreviewAutomationKeySequence(
156195
type: text ? "keyDown" : "rawKeyDown",
157196
...shared,
158197
...(text ? { text, unmodifiedText: text } : {}),
198+
...(commands.length > 0 ? { commands } : {}),
159199
},
160200
keyUp: { type: "keyUp", ...shared },
161201
signal: { kind: "key", key: definition.key, code: definition.code },

‎apps/web/src/components/preview/PreviewAutomationHosts.tsx‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
PreviewAutomationTargetUnavailableError,
5252
PreviewAutomationViewportTimeoutError,
5353
} from "./previewAutomationErrors";
54+
import { previewAutomationOpenNeedsOverlay } from "./previewAutomationOpenReadiness";
5455
import { createPreviewAutomationRequestConsumerAtom } from "./previewAutomationRequestConsumer";
5556
import { createPreviewAutomationClientId } from "./previewAutomationClientId";
5657
import {
@@ -333,6 +334,9 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
333334
const input = request.input as PreviewAutomationOpenInput;
334335
let activeTabId =
335336
(input.reuseExistingTab ?? true) ? (state.snapshot?.tabId ?? null) : null;
337+
let activeSnapshot = activeTabId
338+
? (state.sessions[activeTabId] ?? state.snapshot ?? undefined)
339+
: undefined;
336340
const reusedExistingTab = activeTabId !== null;
337341
tabId = activeTabId;
338342
if (!activeTabId) {
@@ -349,17 +353,20 @@ function PreviewAutomationHost(props: { readonly environmentId: EnvironmentId })
349353
const snapshot = result.value;
350354
applyPreviewServerSnapshot(threadRef, snapshot);
351355
activeTabId = snapshot.tabId;
356+
activeSnapshot = snapshot;
352357
tabId = activeTabId;
353358
}
354359
if (input.show ?? true) {
355360
useRightPanelStore.getState().openBrowser(threadRef, activeTabId);
356361
}
357-
await waitForDesktopOverlay(
358-
threadRef,
359-
request.requestId,
360-
activeTabId,
361-
request.timeoutMs,
362-
);
362+
if (activeSnapshot && previewAutomationOpenNeedsOverlay(input, activeSnapshot)) {
363+
await waitForDesktopOverlay(
364+
threadRef,
365+
request.requestId,
366+
activeTabId,
367+
request.timeoutMs,
368+
);
369+
}
363370
if (reusedExistingTab && input.url && previewBridge) {
364371
const resolution = resolveBrowserNavigationTarget(environmentId, {
365372
kind: "url",
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type { PreviewAutomationOpenInput, PreviewSessionSnapshot } from "@t3tools/contracts";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
import { previewAutomationOpenNeedsOverlay } from "./previewAutomationOpenReadiness";
5+
6+
const snapshot = (navStatus: PreviewSessionSnapshot["navStatus"]): PreviewSessionSnapshot => ({
7+
threadId: "thread-1",
8+
tabId: "tab-1",
9+
navStatus,
10+
canGoBack: false,
11+
canGoForward: false,
12+
updatedAt: "2026-06-26T00:00:00.000Z",
13+
});
14+
15+
describe("preview automation open readiness", () => {
16+
it("does not wait for a desktop overlay when opening an empty tab", () => {
17+
expect(
18+
previewAutomationOpenNeedsOverlay(
19+
{} as PreviewAutomationOpenInput,
20+
snapshot({ _tag: "Idle" }),
21+
),
22+
).toBe(false);
23+
});
24+
25+
it("waits when an empty tab is immediately given a URL", () => {
26+
expect(
27+
previewAutomationOpenNeedsOverlay(
28+
{ url: "https://example.com" } as PreviewAutomationOpenInput,
29+
snapshot({ _tag: "Idle" }),
30+
),
31+
).toBe(true);
32+
});
33+
34+
it("waits for existing tabs that already have rendered content", () => {
35+
expect(
36+
previewAutomationOpenNeedsOverlay(
37+
{} as PreviewAutomationOpenInput,
38+
snapshot({
39+
_tag: "Success",
40+
url: "https://example.com/",
41+
title: "Example",
42+
}),
43+
),
44+
).toBe(true);
45+
});
46+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { PreviewAutomationOpenInput, PreviewSessionSnapshot } from "@t3tools/contracts";
2+
3+
export function previewAutomationOpenNeedsOverlay(
4+
input: PreviewAutomationOpenInput,
5+
snapshot: PreviewSessionSnapshot,
6+
): boolean {
7+
return input.url !== undefined || snapshot.navStatus._tag !== "Idle";
8+
}

0 commit comments

Comments
 (0)