Skip to content

Commit 3910370

Browse files
committed
Merge remote-tracking branch 'github/main' into perf/release-left-thread-state-v3
# Conflicts: # apps/web/src/components/ChatView.browser.test.tsx
2 parents 431d40a + 8ee7183 commit 3910370

159 files changed

Lines changed: 7424 additions & 1050 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const command = (
3434
const makeDebuggee = (
3535
options: {
3636
readonly deferFocus?: boolean;
37+
/** The guest is still loading its first page. */
38+
readonly loading?: boolean;
3739
readonly refuse?: (method: string, sessionId: string | undefined) => boolean;
3840
} = {},
3941
) => {
@@ -69,10 +71,13 @@ const makeDebuggee = (
6971
sendWaiters.shift()?.();
7072
}),
7173
});
74+
let destroyed = false;
7275
const webContents = Object.assign(new NodeEvents.EventEmitter(), {
7376
getURL: () => "http://localhost/",
7477
getTitle: () => "Page",
7578
getUserAgent: () => "Electron",
79+
isLoadingMainFrame: () => options.loading === true,
80+
isDestroyed: () => destroyed,
7681
});
7782
return {
7883
tab: {
@@ -81,6 +86,12 @@ const makeDebuggee = (
8186
},
8287
emit: (method: string, params: unknown) => emitter.emit("message", {}, method, params, ""),
8388
navigate: () => webContents.emit("did-navigate"),
89+
domReady: () => webContents.emit("dom-ready"),
90+
destroy: () => {
91+
destroyed = true;
92+
},
93+
failLoad: (code: number) =>
94+
webContents.emit("did-fail-load", {}, code, "Failed", "http://localhost/", true),
8495
/** Ids of the commands the debugger received, in order. */
8596
sent,
8697
focusEmulation,
@@ -144,6 +155,71 @@ describe("DesktopBrowserHost", () => {
144155
}),
145156
);
146157

158+
it.effect("announces a guest still loading its first page only once that page is up", () =>
159+
Effect.gen(function* () {
160+
const host = yield* DesktopBrowserHost.make;
161+
const loading = makeDebuggee({ loading: true });
162+
const other = { ...key, tabId: "tab-2" };
163+
const reader = yield* takeEvents(host, 3).pipe(Effect.forkScoped);
164+
165+
yield* Effect.yieldNow;
166+
host.attach(key, loading.tab);
167+
host.setShown(key, true);
168+
host.attach(other, makeDebuggee().tab);
169+
loading.domReady();
170+
171+
expect(yield* Fiber.join(reader)).toEqual([
172+
{ type: "attached", ...other },
173+
{ type: "attached", ...key },
174+
{ type: "shown", ...key, shown: true },
175+
]);
176+
}),
177+
);
178+
179+
it.effect(
180+
"keeps a loading guest unannounced across a replaced navigation and a new backend",
181+
() =>
182+
Effect.gen(function* () {
183+
const host = yield* DesktopBrowserHost.make;
184+
const loading = makeDebuggee({ loading: true });
185+
const other = { ...key, tabId: "tab-2" };
186+
host.attach(key, loading.tab);
187+
loading.failLoad(-3);
188+
const reader = yield* takeEvents(host, 2).pipe(Effect.forkScoped);
189+
190+
yield* Effect.yieldNow;
191+
host.attach(other, makeDebuggee().tab);
192+
loading.domReady();
193+
194+
expect(yield* Fiber.join(reader)).toEqual([
195+
{ type: "attached", ...other },
196+
{ type: "attached", ...key },
197+
]);
198+
}),
199+
);
200+
201+
it.effect(
202+
"announces a first page that never comes up, but never a tab closed or destroyed first",
203+
() =>
204+
Effect.gen(function* () {
205+
const host = yield* DesktopBrowserHost.make;
206+
const closed = { ...key, tabId: "tab-2" };
207+
const destroyed = { ...key, tabId: "tab-3" };
208+
const destroyedGuest = makeDebuggee({ loading: true });
209+
const reader = yield* takeEvents(host, 1).pipe(Effect.forkScoped);
210+
211+
yield* Effect.yieldNow;
212+
host.attach(closed, makeDebuggee({ loading: true }).tab);
213+
host.detach(closed);
214+
host.attach(destroyed, destroyedGuest.tab);
215+
destroyedGuest.destroy();
216+
host.attach(key, makeDebuggee({ loading: true }).tab);
217+
yield* TestClock.adjust("5 seconds");
218+
219+
expect(yield* Fiber.join(reader)).toEqual([{ type: "attached", ...key }]);
220+
}),
221+
);
222+
147223
it.effect("re-sends shown to a backend that starts later and when a tab attaches again", () =>
148224
Effect.gen(function* () {
149225
const host = yield* DesktopBrowserHost.make;

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

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import * as Context from "effect/Context";
99
import * as NodePath from "node:path";
1010
import * as Effect from "effect/Effect";
11+
import * as Fiber from "effect/Fiber";
1112
import * as Layer from "effect/Layer";
1213
import * as Option from "effect/Option";
1314
import * as PubSub from "effect/PubSub";
@@ -26,6 +27,9 @@ const AGENT_INPUT_COMMAND =
2627
const AGENT_INPUT_METHOD = /^(?:Input\.|Page\.navigate$|Runtime\.(?:evaluate|callFunctionOn)$)/;
2728
const AGENT_DOWNLOAD_WINDOW_MS = 5_000;
2829
const FOCUS_IDLE = "5 seconds";
30+
/** Well inside the server's 10 s DESKTOP_ATTACH_TIMEOUT, for a first page that never reports it is up. */
31+
const FIRST_PAGE_TIMEOUT = "5 seconds";
32+
const ERR_ABORTED = -3;
2933

3034
export interface DesktopBrowserTabKey {
3135
readonly threadId: string;
@@ -78,6 +82,8 @@ interface AttachedTab {
7882
sessionId: string,
7983
) => void;
8084
readonly onNewDocument: () => void;
85+
/** Stops waiting for the first page; set until the tab is announced. */
86+
stopWaiting: (() => void) | null;
8187
}
8288

8389
export class DesktopBrowserHost extends Context.Service<
@@ -413,11 +419,13 @@ export const make = Effect.gen(function* () {
413419
const tab = tabs.get(id);
414420
if (!tab) return;
415421
tabs.delete(id);
422+
const announced = tab.stopWaiting === null;
423+
tab.stopWaiting?.();
416424
tab.relay = null;
417425
tab.debuggee.debugger.off("message", tab.onMessage);
418426
tab.debuggee.webContents.off("did-navigate", tab.onNewDocument);
419427
syncPainting(tab);
420-
emit({ type: "detached", ...key });
428+
if (announced) emit({ type: "detached", ...key });
421429
};
422430

423431
const attach = (key: DesktopBrowserTabKey, debuggee: DesktopBrowserTabDebugger) => {
@@ -455,13 +463,53 @@ export const make = Effect.gen(function* () {
455463
dropFocusHolds(tab);
456464
if (tab.screenshotTurn !== null) nextScreenshot(tab);
457465
},
466+
stopWaiting: null,
458467
};
459468
tabs.set(id, tab);
460469
debuggee.debugger.on("message", tab.onMessage);
461470
debuggee.webContents.on("did-navigate", tab.onNewDocument);
462-
emit({ type: "attached", ...key });
463-
const shown = shownTabs.get(id);
464-
if (shown !== undefined) emit({ type: "shown", ...key, shown });
471+
const announce = () => {
472+
emit({ type: "attached", ...key });
473+
const shown = shownTabs.get(id);
474+
if (shown !== undefined) emit({ type: "shown", ...key, shown });
475+
};
476+
if (!debuggee.webContents.isLoadingMainFrame()) {
477+
announce();
478+
return;
479+
}
480+
// Electron does not pause a guest for the debugger, so Playwright connecting during its first
481+
// commit builds its utility world in the discarded document and every wait on it hangs.
482+
const { webContents } = debuggee;
483+
// ERR_ABORTED is a newer navigation replacing this one, whose commit is still to come.
484+
const onFailLoad = (
485+
_event: Electron.Event,
486+
code: number,
487+
_description: string,
488+
_url: string,
489+
isMainFrame: boolean,
490+
) => {
491+
if (isMainFrame && code !== ERR_ABORTED) ready();
492+
};
493+
const ready = () => {
494+
if (tab.stopWaiting === null) return;
495+
// The manager's cleanup for a destroyed guest can still be queued behind another tab's attach.
496+
if (webContents.isDestroyed()) return detach(key);
497+
tab.stopWaiting();
498+
announce();
499+
};
500+
const timeout = runFork(
501+
Effect.sleep(FIRST_PAGE_TIMEOUT).pipe(Effect.andThen(Effect.sync(ready))),
502+
);
503+
tab.stopWaiting = () => {
504+
tab.stopWaiting = null;
505+
webContents.off("dom-ready", ready);
506+
webContents.off("did-stop-loading", ready);
507+
webContents.off("did-fail-load", onFailLoad);
508+
runFork(Fiber.interrupt(timeout));
509+
};
510+
webContents.on("dom-ready", ready);
511+
webContents.on("did-stop-loading", ready);
512+
webContents.on("did-fail-load", onFailLoad);
465513
};
466514

467515
const setShown = (key: DesktopBrowserTabKey, shown: boolean | undefined) => {
@@ -472,7 +520,7 @@ export const make = Effect.gen(function* () {
472520
}
473521
if (shownTabs.get(id) === shown) return;
474522
shownTabs.set(id, shown);
475-
if (tabs.has(id)) emit({ type: "shown", ...key, shown });
523+
if (tabs.get(id)?.stopWaiting === null) emit({ type: "shown", ...key, shown });
476524
};
477525

478526
const handleCommandLine = (line: string) =>
@@ -511,6 +559,8 @@ export const make = Effect.gen(function* () {
511559
[...tabs.values()],
512560
(tab) => {
513561
dropRelay(tab);
562+
// Announced once its first page is up.
563+
if (tab.stopWaiting !== null) return Effect.void;
514564
const shown = shownTabs.get(keyOf(tab.key));
515565
return PubSub.publish(outbox, { type: "attached", ...tab.key }).pipe(
516566
Effect.andThen(

‎apps/desktop/src/window/DesktopWindow.test.ts‎

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ function layerTest(input: {
233233
readonly destroyAll?: Effect.Effect<void>;
234234
readonly onWindowCreate?: Effect.Effect<void>;
235235
readonly openedExternalUrls?: unknown[];
236-
readonly copiedTexts?: string[];
236+
readonly onCopyText?: (text: string) => Effect.Effect<void>;
237237
readonly onPopupTemplate?: (input: ElectronMenu.ElectronMenuTemplateInput) => Effect.Effect<void>;
238238
readonly previewZoomReapplies?: number[];
239239
readonly onReveal?: (window: Electron.BrowserWindow) => void;
@@ -325,10 +325,7 @@ function layerTest(input: {
325325
return true;
326326
}),
327327
openSystemSettings: () => Effect.succeed(true),
328-
copyText: (text) =>
329-
Effect.sync(() => {
330-
input.copiedTexts?.push(text);
331-
}),
328+
copyText: input.onCopyText ?? (() => Effect.void),
332329
} satisfies ElectronShell.ElectronShell["Service"]),
333330
layerElectronTheme,
334331
layerElectronWindow,
@@ -1186,6 +1183,56 @@ describe("DesktopWindow", () => {
11861183
}),
11871184
);
11881185

1186+
it.effect("offers to copy the bare address when right-clicking a mailto link", () =>
1187+
Effect.gen(function* () {
1188+
const fakeWindow = makeFakeBrowserWindow();
1189+
const createCount = yield* Ref.make(0);
1190+
const mainWindow = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
1191+
const popup = yield* Deferred.make<ElectronMenu.ElectronMenuTemplateInput>();
1192+
const copied = yield* Deferred.make<string>();
1193+
const layer = layerTest({
1194+
window: fakeWindow.window,
1195+
createCount,
1196+
mainWindow,
1197+
onPopupTemplate: (input) => Deferred.succeed(popup, input),
1198+
onCopyText: (text) => Deferred.succeed(copied, text),
1199+
});
1200+
1201+
yield* Effect.gen(function* () {
1202+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
1203+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
1204+
1205+
const contextMenu = fakeWindow.webContentsListeners.get("context-menu");
1206+
if (!contextMenu) {
1207+
return yield* Effect.die("context-menu listener was not registered");
1208+
}
1209+
contextMenu(
1210+
{ preventDefault: vi.fn() },
1211+
{
1212+
linkURL: "mailto:restaurant%40example.com?subject=Hi",
1213+
misspelledWord: "",
1214+
dictionarySuggestions: [],
1215+
mediaType: "none",
1216+
editFlags: { canCut: false, canCopy: false, canPaste: false, canSelectAll: true },
1217+
},
1218+
);
1219+
1220+
const { template } = yield* Deferred.await(popup);
1221+
const copyEmail = template.find((item) => item.label === "Copy Email Address");
1222+
assert.isDefined(
1223+
copyEmail,
1224+
`no "Copy Email Address" item in ${JSON.stringify(template.map((item) => item.label ?? item.role ?? item.type))}`,
1225+
);
1226+
copyEmail?.click?.(
1227+
{} as Electron.MenuItem,
1228+
fakeWindow.window,
1229+
{} as Electron.KeyboardEvent,
1230+
);
1231+
assert.equal(yield* Deferred.await(copied), "[email protected]");
1232+
}).pipe(Effect.provide(layer));
1233+
}),
1234+
);
1235+
11891236
it.effect(
11901237
"retries opening the real main on activate when a failed post-readiness open left only the splash",
11911238
() =>

‎apps/desktop/src/window/DesktopWindow.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ class ContextMenuImageCopyError extends Schema.TaggedError<ContextMenuImageCopyE
5353
const MACOS_WORKSPACE_TOPBAR_HEIGHT = 52;
5454
const MACOS_WINDOW_BUTTON_RADIUS = 7;
5555

56+
function parseMailtoAddress(rawUrl: string): Option.Option<string> {
57+
let url: URL;
58+
try {
59+
url = new URL(rawUrl);
60+
} catch {
61+
return Option.none();
62+
}
63+
if (url.protocol !== "mailto:" || url.pathname === "") return Option.none();
64+
try {
65+
return Option.some(decodeURIComponent(url.pathname));
66+
} catch {
67+
return Option.some(url.pathname);
68+
}
69+
}
70+
5671
function syncMacosWindowButtons(window: Electron.BrowserWindow): void {
5772
if (window.isDestroyed() || window.isFullScreen()) return;
5873
window.setWindowButtonPosition({
@@ -590,6 +605,19 @@ export const make = Effect.gen(function* () {
590605
);
591606
}
592607

608+
const mailtoAddress = parseMailtoAddress(params.linkURL);
609+
if (Option.isSome(mailtoAddress)) {
610+
menuTemplate.push(
611+
{
612+
label: "Copy Email Address",
613+
click: () => {
614+
void runPromise(electronShell.copyText(mailtoAddress.value));
615+
},
616+
},
617+
{ type: "separator" },
618+
);
619+
}
620+
593621
if (params.mediaType === "image") {
594622
menuTemplate.push({
595623
label: "Copy Image",

‎apps/mobile/src/components/ui/FilePreviewSession.test.tsx‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { act, useState } from "react";
22
import { createRoot } from "react-dom/client";
33
import { expect, it, vi } from "vite-plus/test";
4-
import { EnvironmentId, SOURCE_CONTROL_MEDIA_ASSET } from "@t3tools/contracts";
4+
import {
5+
AssetWorkspaceAssetNotFoundError,
6+
EnvironmentId,
7+
SOURCE_CONTROL_MEDIA_ASSET,
8+
ThreadId,
9+
} from "@t3tools/contracts";
510

611
const harness = vi.hoisted(() => ({
7-
refreshAssetUrl: vi.fn<(environmentId: string, resource: { url: string }) => Promise<string>>(),
12+
refreshAssetUrl:
13+
vi.fn<(environmentId: string, resource: { url: string }) => Promise<string | null>>(),
814
alert: vi.fn(),
915
previews: [] as unknown[],
1016
mounts: 0,
@@ -144,3 +150,24 @@ it("closes the preview when the selected image cannot be resolved", async () =>
144150
expect(preview.onRequestClose).toHaveBeenCalledOnce();
145151
expect(harness.alert).toHaveBeenCalledOnce();
146152
});
153+
154+
it("names the checked path when the server cannot find the file", async () => {
155+
const resource = {
156+
_tag: "media-file" as const,
157+
threadId: ThreadId.make("thread"),
158+
path: "v2/extended/duo-color-3-extended-FINAL.png",
159+
};
160+
const checkedPath = "/repo/v2/extended/duo-color-3-extended-FINAL.png";
161+
harness.refreshAssetUrl.mockRejectedValue(
162+
new AssetWorkspaceAssetNotFoundError({ resource, checkedPath }),
163+
);
164+
const selected: FilePreviewItem = { kind: "image", environmentId, resource };
165+
await using preview = await openGallery(selected, [selected], false);
166+
167+
expect(preview.onRequestClose).toHaveBeenCalledOnce();
168+
expect(harness.alert).toHaveBeenCalledWith(
169+
"Could not open preview",
170+
expect.stringContaining(checkedPath),
171+
);
172+
expect(harness.alert.mock.calls[0]?.[1]).not.toMatch(/reconnect/i);
173+
});

0 commit comments

Comments
 (0)