Open History to see the last commit
1import { it as effectIt } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import * as Exit from "effect/Exit";
5import * as Option from "effect/Option";
6import * as Schema from "effect/Schema";
7import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
8
9import * as PreviewManager from "../../preview/Manager.ts";
10import * as PreviewIpc from "./preview.ts";
11
12const { fromPartition } = vi.hoisted(() => ({
13 fromPartition: vi.fn(() => {
14 throw new Error("Session can only be received when app is ready");
15 }),
16}));
17
18vi.mock("electron", () => ({
19 BrowserWindow: {
20 getAllWindows: vi.fn(() => []),
21 },
22 session: {
23 fromPartition,
24 },
25 webContents: {
26 fromId: vi.fn(() => null),
27 },
28}));
29
30describe("preview IPC methods", () => {
31 beforeEach(() => {
32 fromPartition.mockClear();
33 });
34
35 it("does not access the Electron session while the module loads", async () => {
36 await expect(import("./preview.ts")).resolves.toBeDefined();
37 expect(fromPartition).not.toHaveBeenCalled();
38 });
39
40 effectIt.effect("rejects invalid webContents ids before resolving the preview service", () =>
41 Effect.map(
42 PreviewIpc.registerWebview
43 .handler({ tabId: "tab-1", webContentsId: 0 })
44 .pipe(Effect.provideService(PreviewManager.PreviewManager, null as never), Effect.exit),
45 (exit) => {
46 expect(Exit.isFailure(exit)).toBe(true);
47 if (Exit.isSuccess(exit)) return;
48 const error = Cause.findErrorOption(exit.cause);
49 expect(Option.isSome(error) && Schema.isSchemaError(error.value)).toBe(true);
50 expect(fromPartition).not.toHaveBeenCalled();
51 },
52 ),
53 );
54});