Open History to see the last commit
1import { it as effectIt } from "@effect/vitest";
2import { PreviewAutomationStatus } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Exit from "effect/Exit";
6import * as Option from "effect/Option";
7import * as Schema from "effect/Schema";
8import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
9
10import * as PreviewManager from "../../preview/Manager.ts";
11import * as PreviewIpc from "./preview.ts";
12
13const { fromPartition } = vi.hoisted(() => ({
14 fromPartition: vi.fn(() => {
15 throw new Error("Session can only be received when app is ready");
16 }),
17}));
18
19vi.mock("electron", () => ({
20 BrowserWindow: {
21 getAllWindows: vi.fn(() => []),
22 },
23 session: {
24 fromPartition,
25 },
26 webContents: {
27 fromId: vi.fn(() => null),
28 },
29}));
30
31describe("preview IPC methods", () => {
32 beforeEach(() => {
33 fromPartition.mockClear();
34 });
35
36 it("does not access the Electron session while the module loads", async () => {
37 await expect(import("./preview.ts")).resolves.toBeDefined();
38 expect(fromPartition).not.toHaveBeenCalled();
39 });
40
41 effectIt.effect("rejects invalid webContents ids before resolving the preview service", () =>
42 Effect.map(
43 PreviewIpc.registerWebview
44 .handler({ tabId: "tab-1", webContentsId: 0 })
45 .pipe(Effect.provideService(PreviewManager.PreviewManager, null as never), Effect.exit),
46 (exit) => {
47 expect(Exit.isFailure(exit)).toBe(true);
48 if (Exit.isSuccess(exit)) return;
49 const error = Cause.findErrorOption(exit.cause);
50 expect(Option.isSome(error) && Schema.isSchemaError(error.value)).toBe(true);
51 expect(fromPartition).not.toHaveBeenCalled();
52 },
53 ),
54 );
55
56 effectIt.effect("returns automation status for long runtime tab ids", () =>
57 Effect.gen(function* () {
58 const tabId =
59 `["environment-1","thread:delegated-task:${"a".repeat(120)}",` +
60 `"server-epoch-1","preview-1"]`;
61 const status = {
62 available: false,
63 visible: true,
64 tabId,
65 url: null,
66 title: null,
67 loading: false,
68 };
69 const manager = PreviewManager.PreviewManager.of({
70 automationStatus: () => Effect.succeed(status),
71 } as unknown as PreviewManager.PreviewManager["Service"]);
72
73 expect(tabId.length).toBeGreaterThan(128);
74 expect(
75 yield* PreviewIpc.automationStatus
76 .handler({ tabId })
77 .pipe(Effect.provideService(PreviewManager.PreviewManager, manager)),
78 ).toEqual(status);
79 }),
80 );
81
82 it("keeps the public automation status tab id limit", () => {
83 const encode = Schema.encodeUnknownSync(PreviewAutomationStatus);
84 const tabId = "t".repeat(129);
85
86 expect(() =>
87 encode({
88 available: false,
89 visible: true,
90 tabId,
91 url: null,
92 title: null,
93 loading: false,
94 }),
95 ).toThrow();
96 });
97});