Skip to content

Commit 8ab4d34

Browse files
test: restore the event-driven adapter harnesses dropped by the #22557 rebase
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 3e4bc50 commit 8ab4d34

3 files changed

Lines changed: 479 additions & 565 deletions

File tree

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

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ const makeSourcePng = (width = 1, height = 1): Buffer => {
376376
};
377377

378378
const makeFaviconWebContents = (options?: {
379+
readonly changes?: ChangeSignal;
379380
readonly fetch?: (url: string, init?: RequestInit) => Promise<Response>;
380381
readonly id?: number;
381382
readonly rasterize?: (code: string) => Promise<unknown>;
@@ -386,16 +387,25 @@ const makeFaviconWebContents = (options?: {
386387
let currentUrl = options?.url ?? "http://localhost:3200/";
387388
let destroyed = false;
388389
let loading = false;
389-
const fetch = vi.fn(
390+
const fetchImpl =
390391
options?.fetch ??
391-
(async () =>
392-
new Response(new Uint8Array(sourcePng), {
393-
headers: { "content-type": "image/png" },
394-
})),
395-
);
392+
(async () =>
393+
new Response(new Uint8Array(sourcePng), {
394+
headers: { "content-type": "image/png" },
395+
}));
396+
const fetch = vi.fn((url: string, init?: RequestInit) => {
397+
const response = fetchImpl(url, init);
398+
options?.changes?.notify();
399+
return response;
400+
});
396401
const executeJavaScriptInIsolatedWorld = vi.fn(
397-
async (_worldId: number, scripts: ReadonlyArray<{ readonly code: string }>) =>
398-
options?.rasterize ? options.rasterize(scripts[0]?.code ?? "") : TEST_FAVICON,
402+
(_worldId: number, scripts: ReadonlyArray<{ readonly code: string }>) => {
403+
const result = options?.rasterize
404+
? options.rasterize(scripts[0]?.code ?? "")
405+
: Promise.resolve(TEST_FAVICON);
406+
options?.changes?.notify();
407+
return result;
408+
},
399409
);
400410
const reload = vi.fn();
401411
const loadURL = vi.fn(async (url: string) => {
@@ -460,10 +470,33 @@ const makeFaviconWebContents = (options?: {
460470
};
461471
};
462472

463-
const settle = function* (until: () => boolean) {
464-
for (let attempt = 0; attempt < 30 && !until(); attempt++) {
465-
yield* Effect.promise(() => Promise.resolve());
466-
}
473+
// Electron mocks and state subscribers call `notify`, so a wait re-checks its
474+
// predicate exactly when the state it reads changes.
475+
const makeChangeSignal = () => {
476+
const waiters = new Set<() => void>();
477+
return {
478+
notify: () => {
479+
for (const wake of waiters) wake();
480+
},
481+
until: (predicate: () => boolean) =>
482+
Effect.callback<void>((resume) => {
483+
if (predicate()) return resume(Effect.void);
484+
const wake = () => {
485+
if (!predicate()) return;
486+
waiters.delete(wake);
487+
resume(Effect.void);
488+
};
489+
waiters.add(wake);
490+
return Effect.sync(() => void waiters.delete(wake));
491+
}),
492+
};
493+
};
494+
type ChangeSignal = ReturnType<typeof makeChangeSignal>;
495+
496+
// Favicon captures run detached from the Electron listener, so asserting that
497+
// one published nothing can only give its promise chain a fixed number of turns.
498+
const flushMicrotasks = function* () {
499+
for (let turn = 0; turn < 30; turn++) yield* Effect.promise(() => Promise.resolve());
467500
};
468501

469502
const makeTestPictureInPictureWindow = (loadURL: () => Promise<void> = async () => undefined) => {
@@ -843,11 +876,13 @@ describe("PreviewManager", () => {
843876
effectIt.effect("shares an identical in-flight event and lets a changed event win", () =>
844877
withManager((manager) =>
845878
Effect.gen(function* () {
879+
const changes = makeChangeSignal();
846880
let resolveFirst!: (response: Response) => void;
847881
const firstResponse = new Promise<Response>((resolve) => {
848882
resolveFirst = resolve;
849883
});
850884
const preview = makeFaviconWebContents({
885+
changes,
851886
fetch: (url) =>
852887
url.endsWith("first.png")
853888
? firstResponse
@@ -862,6 +897,7 @@ describe("PreviewManager", () => {
862897
yield* manager.subscribeStateChanges((_tabId, state) =>
863898
Effect.sync(() => {
864899
states.push(state);
900+
changes.notify();
865901
}),
866902
);
867903
yield* manager.createTab("tab_favicon_latest");
@@ -870,15 +906,15 @@ describe("PreviewManager", () => {
870906
const faviconUpdated = preview.listeners.get("page-favicon-updated")!;
871907
faviconUpdated({} as never, ["http://localhost:3200/first.png"] as never);
872908
faviconUpdated({} as never, ["http://localhost:3200/first.png"] as never);
873-
yield* settle(() => preview.fetch.mock.calls.length === 1);
909+
yield* changes.until(() => preview.fetch.mock.calls.length === 1);
874910
faviconUpdated({} as never, ["http://localhost:3200/second.png"] as never);
875-
yield* settle(() => states.at(-1)?.favicon !== undefined);
911+
yield* changes.until(() => states.at(-1)?.favicon !== undefined);
876912
resolveFirst(
877913
new Response(new Uint8Array(makeSourcePng()), {
878914
headers: { "content-type": "image/png" },
879915
}),
880916
);
881-
yield* settle(() => false);
917+
yield* flushMicrotasks();
882918

883919
expect(preview.fetch).toHaveBeenCalledTimes(2);
884920
expect(states.filter((state) => state.favicon !== undefined)).toHaveLength(1);
@@ -889,26 +925,29 @@ describe("PreviewManager", () => {
889925
effectIt.effect("allows an identical retry after an undecodable capture", () =>
890926
withManager((manager) =>
891927
Effect.gen(function* () {
928+
const changes = makeChangeSignal();
892929
let rasterizations = 0;
893930
const preview = makeFaviconWebContents({
931+
changes,
894932
rasterize: async () => (++rasterizations === 1 ? null : TEST_FAVICON),
895933
});
896934
fromId.mockReturnValue(preview.webContents);
897935
const states: PreviewManager.PreviewTabState[] = [];
898936
yield* manager.subscribeStateChanges((_tabId, state) =>
899937
Effect.sync(() => {
900938
states.push(state);
939+
changes.notify();
901940
}),
902941
);
903942
yield* manager.createTab("tab_favicon_retry");
904943
yield* manager.registerWebview("tab_favicon_retry", 42);
905944
const faviconUpdated = preview.listeners.get("page-favicon-updated")!;
906945

907946
faviconUpdated({} as never, ["http://localhost:3200/favicon.png"] as never);
908-
yield* settle(() => rasterizations === 1);
909-
yield* settle(() => false);
947+
yield* changes.until(() => rasterizations === 1);
948+
yield* flushMicrotasks();
910949
faviconUpdated({} as never, ["http://localhost:3200/favicon.png"] as never);
911-
yield* settle(() => states.at(-1)?.favicon !== undefined);
950+
yield* changes.until(() => states.at(-1)?.favicon !== undefined);
912951

913952
expect(rasterizations).toBe(2);
914953
expect(states.at(-1)?.favicon?.dataUrl).toBe(TEST_FAVICON);
@@ -919,8 +958,10 @@ describe("PreviewManager", () => {
919958
effectIt.effect("does not publish a capture invalidated by navigation", () =>
920959
withManager((manager) =>
921960
Effect.gen(function* () {
961+
const changes = makeChangeSignal();
922962
let resolveFetch!: (response: Response) => void;
923963
const preview = makeFaviconWebContents({
964+
changes,
924965
fetch: () =>
925966
new Promise<Response>((resolve) => {
926967
resolveFetch = resolve;
@@ -931,6 +972,7 @@ describe("PreviewManager", () => {
931972
yield* manager.subscribeStateChanges((_tabId, state) =>
932973
Effect.sync(() => {
933974
states.push(state);
975+
changes.notify();
934976
}),
935977
);
936978
yield* manager.createTab("tab_favicon_navigation");
@@ -939,7 +981,7 @@ describe("PreviewManager", () => {
939981
{} as never,
940982
["http://localhost:3200/favicon.png"] as never,
941983
);
942-
yield* settle(() => preview.fetch.mock.calls.length === 1);
984+
yield* changes.until(() => preview.fetch.mock.calls.length === 1);
943985
preview.listeners.get("did-start-navigation")?.({
944986
isMainFrame: true,
945987
isSameDocument: false,
@@ -950,7 +992,7 @@ describe("PreviewManager", () => {
950992
headers: { "content-type": "image/png" },
951993
}),
952994
);
953-
yield* settle(() => false);
995+
yield* flushMicrotasks();
954996

955997
expect(states.some((state) => state.favicon !== undefined)).toBe(false);
956998
}),
@@ -960,22 +1002,25 @@ describe("PreviewManager", () => {
9601002
effectIt.effect("ignores an old capture that completes after webview replacement", () =>
9611003
withManager((manager) =>
9621004
Effect.gen(function* () {
1005+
const changes = makeChangeSignal();
9631006
let resolveFetch!: (response: Response) => void;
9641007
const initial = makeFaviconWebContents({
1008+
changes,
9651009
id: 42,
9661010
fetch: () =>
9671011
new Promise<Response>((resolve) => {
9681012
resolveFetch = resolve;
9691013
}),
9701014
});
971-
const replacement = makeFaviconWebContents({ id: 43 });
1015+
const replacement = makeFaviconWebContents({ changes, id: 43 });
9721016
fromId.mockImplementation((id?: number) =>
9731017
id === 42 ? initial.webContents : id === 43 ? replacement.webContents : null,
9741018
);
9751019
const states: PreviewManager.PreviewTabState[] = [];
9761020
yield* manager.subscribeStateChanges((_tabId, state) =>
9771021
Effect.sync(() => {
9781022
states.push(state);
1023+
changes.notify();
9791024
}),
9801025
);
9811026
yield* manager.createTab("tab_favicon_late_replace");
@@ -984,15 +1029,15 @@ describe("PreviewManager", () => {
9841029
{} as never,
9851030
["http://localhost:3200/favicon.png"] as never,
9861031
);
987-
yield* settle(() => initial.fetch.mock.calls.length === 1);
1032+
yield* changes.until(() => initial.fetch.mock.calls.length === 1);
9881033

9891034
yield* manager.registerWebview("tab_favicon_late_replace", 43);
9901035
resolveFetch(
9911036
new Response(new Uint8Array(makeSourcePng()), {
9921037
headers: { "content-type": "image/png" },
9931038
}),
9941039
);
995-
yield* settle(() => false);
1040+
yield* flushMicrotasks();
9961041

9971042
expect(states.at(-1)?.webContentsId).toBe(43);
9981043
expect(
@@ -1005,14 +1050,16 @@ describe("PreviewManager", () => {
10051050
effectIt.effect("treats a reused WebContents id as a new attachment", () =>
10061051
withManager((manager) =>
10071052
Effect.gen(function* () {
1008-
const initial = makeFaviconWebContents({ id: 42 });
1009-
const replacement = makeFaviconWebContents({ id: 42 });
1053+
const changes = makeChangeSignal();
1054+
const initial = makeFaviconWebContents({ changes, id: 42 });
1055+
const replacement = makeFaviconWebContents({ changes, id: 42 });
10101056
let active = initial.webContents;
10111057
fromId.mockImplementation(() => active);
10121058
const states: PreviewManager.PreviewTabState[] = [];
10131059
yield* manager.subscribeStateChanges((_tabId, state) =>
10141060
Effect.sync(() => {
10151061
states.push(state);
1062+
changes.notify();
10161063
}),
10171064
);
10181065
yield* manager.createTab("tab_favicon_reused_id");
@@ -1021,7 +1068,7 @@ describe("PreviewManager", () => {
10211068
{} as never,
10221069
["http://localhost:3200/favicon.png"] as never,
10231070
);
1024-
yield* settle(() => states.at(-1)?.favicon !== undefined);
1071+
yield* changes.until(() => states.at(-1)?.favicon !== undefined);
10251072

10261073
active = replacement.webContents;
10271074
yield* manager.registerWebview("tab_favicon_reused_id", 42);

0 commit comments

Comments
 (0)