Open History to see the last commit
1import { it as effectIt } from "@effect/vitest";
2import {
3 DEFAULT_BROWSER_PROFILE_ID,
4 INCOGNITO_BROWSER_PROFILE_ID,
5 PreviewCaptureFailure,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import * as Effect from "effect/Effect";
9import * as Schema from "effect/Schema";
10import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
11
12import * as PreviewManager from "../../preview/Manager.ts";
13import * as PreviewIpc from "./preview.ts";
14import * as DesktopIpc from "../DesktopIpc.ts";
15import * as DesktopClientSettings from "../../settings/DesktopClientSettings.ts";
16
17const { fromPartition } = vi.hoisted(() => ({
18 fromPartition: vi.fn(() => {
19 throw new Error("Session can only be received when app is ready");
20 }),
21}));
22
23vi.mock("electron", () => ({
24 BrowserWindow: {
25 getAllWindows: vi.fn(() => []),
26 },
27 session: {
28 fromPartition,
29 },
30 webContents: {
31 fromId: vi.fn(() => null),
32 },
33}));
34
35const decodeCaptureFailure = Schema.decodeUnknownEffect(PreviewCaptureFailure);
36
37describe("preview IPC methods", () => {
38 const invokeCapture = (
39 method: typeof PreviewIpc.startRecording | typeof PreviewIpc.captureScreenshot,
40 manager: Partial<PreviewManager.PreviewManager["Service"]>,
41 ) =>
42 Effect.scoped(
43 Effect.gen(function* () {
44 let listener: DesktopIpc.DesktopIpcHandleListener | undefined;
45 const ipc = DesktopIpc.make({
46 removeHandler: vi.fn(),
47 handle: (_channel, registered) => {
48 listener = registered;
49 },
50 removeAllListeners: vi.fn(),
51 on: vi.fn(),
52 });
53 yield* ipc.handle(method);
54 return yield* Effect.promise(async () =>
55 structuredClone(await listener!({ sender: { id: 7 } }, { tabId: "capture-tab" })),
56 );
57 }).pipe(
58 Effect.provideService(
59 PreviewManager.PreviewManager,
60 manager as PreviewManager.PreviewManager["Service"],
61 ),
62 Effect.provideService(DesktopClientSettings.DesktopClientSettings, {
63 get: Effect.succeedNone,
64 set: () => Effect.void,
65 }),
66 ),
67 );
68
69 effectIt.effect.each([
70 {
71 cause: new Error("Current display surface not available for capture"),
72 reason: "capture-unavailable",
73 guidance: ["target tab", "unlocked", "display is on", "window is restored"],
74 },
75 {
76 cause: new Cause.TimeoutError(),
77 reason: "capture-timeout",
78 guidance: ["timed out during capture-page"],
79 },
80 {
81 cause: new Error("Current display surface not available for capture: private page data"),
82 reason: "capture-failed",
83 guidance: ["Preview capture failed during capture-page"],
84 },
85 ])(
86 "transports a safe $reason screenshot diagnostic through registered IPC",
87 ({ cause, reason, guidance }) =>
88 Effect.gen(function* () {
89 const result = yield* invokeCapture(PreviewIpc.captureScreenshot, {
90 captureScreenshot: (tabId) =>
91 Effect.fail(
92 new PreviewManager.PreviewOperationError({
93 operation: "captureScreenshot.capturePage",
94 tabId,
95 artifactPath: "/private/capture.png",
96 cause,
97 }),
98 ),
99 });
100 expect(result).toEqual({
101 _tag: "PreviewCaptureFailure",
102 reason,
103 stage: "capture-page",
104 });
105 const failure = yield* decodeCaptureFailure(result);
106 for (const phrase of guidance) expect(failure.message).toContain(phrase);
107 }),
108 );
109
110 beforeEach(() => {
111 fromPartition.mockClear();
112 });
113
114 it("derives distinct partition scopes when identifiers contain the delimiter", () => {
115 const first = PreviewIpc.resolvePartitionScope("a", "b::c");
116 const second = PreviewIpc.resolvePartitionScope("a::b", "c");
117
118 expect(first).toEqual({ scope: '["a","b::c"]', persistent: true, namespace: "profile" });
119 expect(second).toEqual({ scope: '["a::b","c"]', persistent: true, namespace: "profile" });
120 expect(first.scope).not.toBe(second.scope);
121 });
122
123 it("keeps the legacy default partition scope and incognito persistence", () => {
124 expect(PreviewIpc.resolvePartitionScope("environment::legacy", undefined)).toEqual({
125 scope: "environment::legacy",
126 persistent: true,
127 });
128 expect(
129 PreviewIpc.resolvePartitionScope("environment::legacy", DEFAULT_BROWSER_PROFILE_ID),
130 ).toEqual({ scope: "environment::legacy", persistent: true });
131 expect(
132 PreviewIpc.resolvePartitionScope("environment::legacy", INCOGNITO_BROWSER_PROFILE_ID),
133 ).toEqual({
134 scope: '["environment::legacy","incognito"]',
135 persistent: false,
136 namespace: "profile",
137 });
138 });
139});