Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Cause from "effect/Cause";
3import * as Effect from "effect/Effect";
4import { vi } from "vite-plus/test";
5
6import * as DesktopIpc from "./DesktopIpc.ts";
7
8const invokeMethod: DesktopIpc.DesktopIpcMethod<never, never> = {
9 channel: "desktop.test.invoke",
10 handler: () => Effect.void,
11};
12
13const syncMethod: DesktopIpc.DesktopSyncIpcMethod<never, never> = {
14 channel: "desktop.test.sync",
15 handler: () => Effect.void,
16};
17
18function makeIpcMain(
19 overrides: Partial<DesktopIpc.DesktopIpcMain> = {},
20): DesktopIpc.DesktopIpcMain {
21 return {
22 removeHandler: vi.fn(),
23 handle: vi.fn(),
24 removeAllListeners: vi.fn(),
25 on: vi.fn(),
26 ...overrides,
27 };
28}
29
30describe("DesktopIpc", () => {
31 it.effect("preserves invoke registration context and cause", () =>
32 Effect.gen(function* () {
33 const cause = new Error("invoke registration failed");
34 const ipcMain = makeIpcMain({
35 handle: () => {
36 throw cause;
37 },
38 });
39 const ipc = DesktopIpc.make(ipcMain);
40
41 const error = yield* Effect.flip(Effect.scoped(ipc.handle(invokeMethod)));
42
43 assert.instanceOf(error, DesktopIpc.DesktopIpcRegistrationError);
44 assert.isTrue(DesktopIpc.isDesktopIpcError(error));
45 assert.strictEqual(error.handlerKind, "invoke");
46 assert.strictEqual(error.channel, invokeMethod.channel);
47 assert.strictEqual(error.cause, cause);
48 assert.include(error.message, "invoke");
49 assert.include(error.message, invokeMethod.channel);
50 assert.notInclude(error.message, cause.message);
51 }),
52 );
53
54 it.effect("preserves sync unregistration context and cause in the finalizer defect", () =>
55 Effect.gen(function* () {
56 const cause = new Error("sync unregistration failed");
57 let removeCount = 0;
58 const ipcMain = makeIpcMain({
59 removeAllListeners: () => {
60 removeCount += 1;
61 if (removeCount === 2) throw cause;
62 },
63 });
64 const ipc = DesktopIpc.make(ipcMain);
65
66 const exit = yield* Effect.exit(Effect.scoped(ipc.handleSync(syncMethod)));
67
68 assert.isTrue(exit._tag === "Failure");
69 if (exit._tag === "Success") return;
70 const error = Cause.squash(exit.cause);
71 assert.instanceOf(error, DesktopIpc.DesktopIpcUnregistrationError);
72 assert.isTrue(DesktopIpc.isDesktopIpcError(error));
73 assert.strictEqual(error.handlerKind, "sync");
74 assert.strictEqual(error.channel, syncMethod.channel);
75 assert.strictEqual(error.cause, cause);
76 assert.notInclude(error.message, cause.message);
77 }),
78 );
79});