Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

preview.test.ts

139 lines4.7 KB

1import { it as effectIt } from "@effect/vitest";
2import {
3 DEFAULT_BROWSER_PROFILE_ID,
4 INCOGNITO_BROWSER_PROFILE_ID,
5 PreviewAutomationStatus,
6} from "@t3tools/contracts";
7import * as Cause from "effect/Cause";
8import * as Effect from "effect/Effect";
9import * as Exit from "effect/Exit";
10import * as Option from "effect/Option";
11import * as Schema from "effect/Schema";
12import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
13​
14import * as PreviewManager from "../../preview/Manager.ts";
15import * as PreviewIpc from "./preview.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​
35describe("preview IPC methods", () => {
36 beforeEach(() => {
37 fromPartition.mockClear();
38 });
39​
40 it("does not access the Electron session while the module loads", async () => {
41 await expect(import("./preview.ts")).resolves.toBeDefined();
42 expect(fromPartition).not.toHaveBeenCalled();
43 });
44​
45 it("derives distinct partition scopes when identifiers contain the delimiter", () => {
46 const first = PreviewIpc.resolvePartitionScope("a", "b::c");
47 const second = PreviewIpc.resolvePartitionScope("a::b", "c");
48​
49 expect(first).toEqual({ scope: '["a","b::c"]', persistent: true, namespace: "profile" });
50 expect(second).toEqual({ scope: '["a::b","c"]', persistent: true, namespace: "profile" });
51 expect(first.scope).not.toBe(second.scope);
52 });
53​
54 it("preserves lone surrogates without collapsing them to replacement characters", () => {
55 const highSurrogate = PreviewIpc.resolvePartitionScope("environment", "profile-\ud800");
56 const lowSurrogate = PreviewIpc.resolvePartitionScope("environment", "profile-\udc00");
57 const replacement = PreviewIpc.resolvePartitionScope("environment", "profile-�");
58​
59 expect(highSurrogate.scope).toBe('["environment","profile-\\ud800"]');
60 expect(lowSurrogate.scope).toBe('["environment","profile-\\udc00"]');
61 expect(highSurrogate.scope).not.toBe(lowSurrogate.scope);
62 expect(highSurrogate.scope).not.toBe(replacement.scope);
63 expect(lowSurrogate.scope).not.toBe(replacement.scope);
64 });
65​
66 it("keeps the legacy default partition scope and incognito persistence", () => {
67 expect(PreviewIpc.resolvePartitionScope("environment::legacy", undefined)).toEqual({
68 scope: "environment::legacy",
69 persistent: true,
70 });
71 expect(
72 PreviewIpc.resolvePartitionScope("environment::legacy", DEFAULT_BROWSER_PROFILE_ID),
73 ).toEqual({ scope: "environment::legacy", persistent: true });
74 expect(
75 PreviewIpc.resolvePartitionScope("environment::legacy", INCOGNITO_BROWSER_PROFILE_ID),
76 ).toEqual({
77 scope: '["environment::legacy","incognito"]',
78 persistent: false,
79 namespace: "profile",
80 });
81 });
82​
83 effectIt.effect("rejects invalid webContents ids before resolving the preview service", () =>
84 Effect.map(
85 PreviewIpc.registerWebview
86 .handler({ tabId: "tab-1", webContentsId: 0 })
87 .pipe(Effect.provideService(PreviewManager.PreviewManager, null as never), Effect.exit),
88 (exit) => {
89 expect(Exit.isFailure(exit)).toBe(true);
90 if (Exit.isSuccess(exit)) return;
91 const error = Cause.findErrorOption(exit.cause);
92 expect(Option.isSome(error) && Schema.isSchemaError(error.value)).toBe(true);
93 expect(fromPartition).not.toHaveBeenCalled();
94 },
95 ),
96 );
97​
98 effectIt.effect("returns automation status for long runtime tab ids", () =>
99 Effect.gen(function* () {
100 const tabId =
101 `["environment-1","thread:delegated-task:${"a".repeat(120)}",` +
102 `"server-epoch-1","preview-1"]`;
103 const status = {
104 available: false,
105 visible: true,
106 tabId,
107 url: null,
108 title: null,
109 loading: false,
110 };
111 const manager = PreviewManager.PreviewManager.of({
112 automationStatus: () => Effect.succeed(status),
113 } as unknown as PreviewManager.PreviewManager["Service"]);
114​
115 expect(tabId.length).toBeGreaterThan(128);
116 expect(
117 yield* PreviewIpc.automationStatus
118 .handler({ tabId })
119 .pipe(Effect.provideService(PreviewManager.PreviewManager, manager)),
120 ).toEqual(status);
121 }),
122 );
123​
124 it("keeps the public automation status tab id limit", () => {
125 const encode = Schema.encodeUnknownSync(PreviewAutomationStatus);
126 const tabId = "t".repeat(129);
127​
128 expect(() =>
129 encode({
130 available: false,
131 visible: true,
132 tabId,
133 url: null,
134 title: null,
135 loading: false,
136 }),
137 ).toThrow();
138 });
139});